diff --git a/README.md b/README.md index 3af9f84..ee2b93c 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ end 3. Create a `Match.replay` method that loops through the turns and outputs to the screen a listing of the turns and who won -Eagel level +Eagle level ------------ 1. Strike vs Strike and Strike vs Block should let the higher rank win diff --git a/lib/match.rb b/lib/match.rb index 3d171bc..41a3751 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -1,14 +1,14 @@ require_relative "fighter" require_relative "turn" class Match - + attr_reader :turns def initialize(opponent_a, opponent_b) @opponent_a = opponent_a @opponent_b = opponent_b - @turns = build_turns + build_turns end - + def opponents [@opponent_a, @opponent_b] end @@ -22,14 +22,28 @@ def winner end def winner_count_for_opponent(opponent) - @turns.select{ |turn| opponent.strike == turn.winner}.count + @turns.select{ |winner| opponent == winner}.count + end + + def announce_turn_winners + @turns.each_with_index do |winner, i| + puts "#{winner.name} won round #{i+1}!" + end + end + + def winner_for_turn(turn) + turns[turn].name end private def build_turns - 13.times.map do - Turn.new(@opponent_a.strike, @opponent_b.strike) + @turns = 13.times.map do + turn = Turn.new(@opponent_a.strike, @opponent_b.strike) + if @opponent_a.strike == turn.winner + @opponent_a + else + @opponent_b + end end end - end diff --git a/lib/turn.rb b/lib/turn.rb index 10578d3..4093518 100644 --- a/lib/turn.rb +++ b/lib/turn.rb @@ -4,11 +4,16 @@ class Turn def initialize(move_a, move_b) @move_a = move_a @move_b = move_b - @winner = determine_winner + @winner = determine_winner end private def determine_winner - [@move_a, @move_b].sample - end + if @move_a.type == :block && @move_b.type == :block + [@move_a, @move_b].sample + else + [@move_a, @move_b].max { |a, b| a.ranking <=> b.ranking } + end + end end + diff --git a/spec/match_spec.rb b/spec/match_spec.rb index f60e7d3..722d421 100644 --- a/spec/match_spec.rb +++ b/spec/match_spec.rb @@ -18,13 +18,19 @@ end it "should declare bob the winner if bob wins more" do - subject.stub(:winner_count_for_opponent).with(bob) {8} - subject.stub(:winner_count_for_opponent).with(fred) {5} + subject.stub(:winner_count_for_opponent).with(bob) {8} + subject.stub(:winner_count_for_opponent).with(fred) {5} subject.winner.should eq(bob) end it "should declare fred the winner if fred wins more" do subject.stub(:winner_count_for_opponent).with(bob) {3} - subject.stub(:winner_count_for_opponent).with(fred) {10} + subject.stub(:winner_count_for_opponent).with(fred) {10} subject.winner.should eq(fred) end + + it "should be able to provide the winner of a given turn" do + [bob.name,fred.name].should include subject.winner_for_turn(1) + end + + $stdout = StringIO.new end diff --git a/spec/move_spec.rb b/spec/move_spec.rb index 87d2e72..29f2b2e 100644 --- a/spec/move_spec.rb +++ b/spec/move_spec.rb @@ -10,6 +10,6 @@ Move.new(:block).type.should eq(:block) end it "has a ranking" do - (1..100).should include Move.new(stub).ranking + (1..100).should include Move.new(double).ranking end end diff --git a/spec/turn_spec.rb b/spec/turn_spec.rb index 8382133..cad966c 100644 --- a/spec/turn_spec.rb +++ b/spec/turn_spec.rb @@ -8,4 +8,20 @@ it "should have a winner" do [strike_a, strike_b].should include Turn.new(strike_a, strike_b).winner end + + it "should select move with the higher rank as the turn winner" do + strike_c = double("strike_c", ranking: 90, type: :strike) + strike_d = double("strike_d", ranking: 50, type: :block) + + Turn.new(strike_c, strike_d).winner.should eq(strike_c) + end + + it "should pick a random winner if it's block vs block" do + strike_e = double("strike_e", type: :block) + strike_f = double("strike_f", type: :block) + + [strike_e, strike_f].should include Turn.new(strike_e, strike_f).winner + end + + end diff --git a/superfight.rb b/superfight.rb index 081a711..c35af67 100644 --- a/superfight.rb +++ b/superfight.rb @@ -2,10 +2,15 @@ puts "What is your first fighter's name?" -fighter_a = $stdin.gets +fighter_a = $stdin.gets.chomp puts "What is your second fighter's name?" -fighter_b = $stdin.gets - +fighter_b = $stdin.gets.chomp +puts "..." +puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +puts "READY, SET, FIGHT!!!!!!" +puts "..." match = Match.new(Fighter.new(fighter_a), Fighter.new(fighter_b)) - -puts "The winner of match is ....... #{match.winner.name}" +match.announce_turn_winners +puts "..." +puts "The winner of the match is ....... #{match.winner.name}!!!" +puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"