Slight update to the specs to make them more reusable for others #67

Merged
merged 1 commit into from Apr 16, 2012
View
10 Gemfile.lock
@@ -9,9 +9,19 @@ GEM
debugger-linecache (1.1.1)
debugger-ruby_core_source (>= 1.1.1)
debugger-ruby_core_source (1.1.1)
+ diff-lcs (1.1.3)
+ rspec (2.9.0)
+ rspec-core (~> 2.9.0)
+ rspec-expectations (~> 2.9.0)
+ rspec-mocks (~> 2.9.0)
+ rspec-core (2.9.0)
+ rspec-expectations (2.9.1)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.9.0)
PLATFORMS
ruby
DEPENDENCIES
debugger
+ rspec
View
81 players/dank.rb
@@ -27,96 +27,97 @@ def trade(command,target=nil)
end
end
- protected
-
- def rest_or_attack
- @target = available_monsters.sample if @target.nil?
- dangerous_health? ? [:rest] : [:attack,@target]
- end
-
- def command_and_control
- @target = @next_target
- set_attack_targets
- rest_or_attack
- end
-
- def set_attack_targets
- @current_friends = friends
+ protected
+
+ def rest_or_attack
+ @target = available_monsters.sample if @target.nil?
+ dangerous_health? ? [:rest] : [:attack,@target]
+ end
+
+ def command_and_control
+ ensure_only_one_boss
+ @target = @next_target
+ set_attack_targets
+ rest_or_attack
+ end
+
+ def set_attack_targets
+ @current_friends = friends
attackers = ATTACKER_POOL
-
+
friends.each_with_index do |friend,index|
new_target = index <= attackers ? strongest_enemies[0] : strongest_enemies[1]
friend.trade( :set_target, new_target )
end
-
- @next_target = strongest_enemies[0]
+
+ @next_target = strongest_enemies[0] || friends.first # Everyone else is dead... start giving more experience to the boss
end
-
+
def aggregrate_strength
@current_friends.inject(0) { |sum,friend| sum + friend.stats[:strength] }
end
-
+
def dangerous_health?
boss? ?
(stats[:health] <= BOSS_DANGER_ZONE) :
(stats[:health] <= MINION_DANGER_ZONE)
end
-
+
def target_set?
@target
end
-
+
def promote_minion
@minion = true
most_experienced_minion.trade(:set_boss)
end
-
+
def boss_lacks_experience?
stats[:experience] < most_experienced_minion.stats[:experience]
end
-
+
def prepare_friendlies
@minion = false
friends.each do |friend|
friend.trade(:begin_game)
end
end
-
+
def friends
Game.world[:players].select { |p| p.to_s =~ /(Minion|Dan Knox)/ && p != self }
end
-
+
def enemies
Game.world[:players].select { |p| p.alive }.reject { |p| friends.include?(p) or p == self }
end
-
+
def strongest_enemies
enemies.sort { |a,b| b.stats[:experience] <=> a.stats[:experience] }
end
-
+
def most_experienced_minion
friends.sort_by { |m| m.stats[:experience] }.last
end
-
+
def available_monsters
Game.world[:players].select { |player| monsters.include?(player.to_s) && player.alive }
end
-
+
def monsters
["rat"]
end
-
- def minion?
+
+ def minion?
@minion
end
-
- def boss?
- !@minion
- end
-
- def game_is_beginning?
- !@game_underway
- end
+
+ def boss?
+ !@minion
+ end
+
+ def game_is_beginning?
+ !@game_underway
+ end
end
)
end
View
29 spec/players/dank_spec.rb
@@ -59,24 +59,12 @@ def load_players_for_dan
it "should tell the remaining minions to attack the second most experienced enemy" do
target = @enemies.sort_by { |e| e.stats[:experience] }[-2]
- @minions.first.should_receive(:trade) do |command,new_target|
+ @minions.last.should_receive(:trade) do |command,new_target|
new_target.stats[:experience].should == target.stats[:experience]
end
@boss.send(:set_attack_targets)
end
end
-
- context "during the end game" do
- before(:each) do
- 100.times { commence_round }
- end
-
- it "Dan Knox should always be the winner!" do
- proxies = @game.proxies
- winner = proxies.inject(proxies[0]) {|max, item| item.stats[:experience] > max.stats[:experience] ? item : max }
- winner.to_s.should =~ /Dan Knox/
- end
- end
end
describe "Dank(1..9)", "The Minions" do
@@ -143,4 +131,19 @@ def load_players_for_dan
@boss.send(:boss_lacks_experience?).should be_false
end
end
+end
+
+describe "New Game" do
+ before(:each) { start_game and load_all_players_and_monsters and silence_output }
+ context "during the end game after 100 rounds" do
+ before(:each) do
+ 100.times { commence_round }
+ end
+
+ it "Dan Knox should always be the winner!" do
+ proxies = @game.proxies
+ winner = proxies.inject(proxies[0]) {|max, item| item.stats[:experience] > max.stats[:experience] ? item : max }
+ winner.to_s.should =~ /(Dan Knox|Minion)/ # its possible for minion to have equal experience until i put in place a system to destroy minions when all enemies are dead
+ end
+ end
end
View
25 spec/spec_helper.rb
@@ -1,9 +1,3 @@
-[:engine,:monsters].each do |dir|
- Dir[File.expand_path("../../#{dir}/*.rb",__FILE__)].each {|f| require f}
-end
-
-require File.expand_path("../../players/dank",__FILE__)
-
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -12,6 +6,8 @@
end
Dir[File.expand_path("../support/*.rb",__FILE__)].each {|f| require f}
+GameManager::Setup.perform_setup
+
def start_game
@game = Game.new
@@ -27,11 +23,26 @@ def create_player_for_module(mod)
p
end
+def load_all_players_and_monsters
+ @players = []
+ GameManager::Setup.player_modules.each do |mod|
+ @players << create_player_for_module(mod)
+ end
+ 5.times do
+ monster = Monster.new
+ @game.players << monster
+ r = PlayerProxy.new(monster)
+ r.extend Rat
+ monster.proxy = r
+ @game.proxies << r
+ end
+end
+
def commence_round
@game.round(1)
end
def silence_output
@original_stdout = $stdout
$stdout = File.new('/dev/null', 'w')
-end
+end
View
27 spec/support/setup.rb
@@ -0,0 +1,27 @@
+module GameManager
+ class Setup
+ def self.require_directory(dirs)
+ dirs.each do |dir|
+ Dir[File.expand_path("../../../#{dir}/*.rb",__FILE__)].each {|f| require f}
+ end
+ end
+
+ def self.modules
+ @modules ||= []
+ end
+
+ def self.player_modules
+ @player_modules ||= []
+ end
+
+ def self.perform_setup
+ require_directory [:engine]
+ ObjectSpace.each_object(Module) {|m| modules << m.name }
+ require_directory [:players]
+ ObjectSpace.each_object(Module) do |m|
+ player_modules << m unless modules.include?(m.name)
+ end
+ require_directory [:monsters]
+ end
+ end
+end