From 668456fb89af336bcad9b5d9b185ee1212ff8ea2 Mon Sep 17 00:00:00 2001 From: jason perez Date: Mon, 6 Jan 2014 13:06:33 -0800 Subject: [PATCH 1/8] remove stub from move spec, replace with double --- README.md | 2 +- lib/match.rb | 4 ++-- lor | 4 ++++ spec/match_spec.rb | 6 +++--- spec/move_spec.rb | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 lor 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..b2714fd 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 end - + def opponents [@opponent_a, @opponent_b] end diff --git a/lor b/lor new file mode 100644 index 0000000..b992dff --- /dev/null +++ b/lor @@ -0,0 +1,4 @@ +............ + +Finished in 0.00273 seconds +12 examples, 0 failures diff --git a/spec/match_spec.rb b/spec/match_spec.rb index f60e7d3..7f8f4d9 100644 --- a/spec/match_spec.rb +++ b/spec/match_spec.rb @@ -18,13 +18,13 @@ 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 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 From 85d5b89f19c70fa3b62b24002db3f93ce98398eb Mon Sep 17 00:00:00 2001 From: jason perez Date: Tue, 7 Jan 2014 13:47:49 -0800 Subject: [PATCH 2/8] add feature to output the winner of each turn --- lib/match.rb | 15 +++++++++++---- lib/turn.rb | 4 ++-- spec/match_spec.rb | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/match.rb b/lib/match.rb index b2714fd..5410e18 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -6,7 +6,7 @@ class Match def initialize(opponent_a, opponent_b) @opponent_a = opponent_a @opponent_b = opponent_b - @turns = build_turns + build_turns end def opponents @@ -22,14 +22,21 @@ def winner end def winner_count_for_opponent(opponent) - @turns.select{ |turn| opponent.strike == turn.winner}.count + @turns.select{ |winner| opponent == winner}.count end private def build_turns + @turns = [] 13.times.map do - Turn.new(@opponent_a.strike, @opponent_b.strike) + turn = Turn.new(@opponent_a.strike, @opponent_b.strike) + if @opponent_a.strike == turn.winner + @turns << @opponent_a + puts "#{@opponent_a.name} won this round!" + else + @turns << @opponent_b + puts "#{@opponent_b.name} won this round!" + end end end - end diff --git a/lib/turn.rb b/lib/turn.rb index 10578d3..f3b73ff 100644 --- a/lib/turn.rb +++ b/lib/turn.rb @@ -4,11 +4,11 @@ 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 + end end diff --git a/spec/match_spec.rb b/spec/match_spec.rb index 7f8f4d9..0027157 100644 --- a/spec/match_spec.rb +++ b/spec/match_spec.rb @@ -27,4 +27,6 @@ subject.stub(:winner_count_for_opponent).with(fred) {10} subject.winner.should eq(fred) end + + $stdout = StringIO.new end From 4b5e199160df9faac8d0681b6c0e431295f81547 Mon Sep 17 00:00:00 2001 From: jason perez Date: Tue, 7 Jan 2014 13:53:44 -0800 Subject: [PATCH 3/8] update superfight output to have a little more flair --- superfight.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/superfight.rb b/superfight.rb index 081a711..f5b17b9 100644 --- a/superfight.rb +++ b/superfight.rb @@ -2,10 +2,14 @@ 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}" +puts "..." +puts "The winner of match is ....... #{match.winner.name}!!!" +puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" From c354e028f6860fd04d12de6229b27ee2275eb4ce Mon Sep 17 00:00:00 2001 From: jason perez Date: Tue, 7 Jan 2014 13:59:47 -0800 Subject: [PATCH 4/8] update to show rounds in each turn --- lib/match.rb | 6 +++--- superfight.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/match.rb b/lib/match.rb index 5410e18..087618b 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -28,14 +28,14 @@ def winner_count_for_opponent(opponent) private def build_turns @turns = [] - 13.times.map do + 13.times do |x| turn = Turn.new(@opponent_a.strike, @opponent_b.strike) if @opponent_a.strike == turn.winner @turns << @opponent_a - puts "#{@opponent_a.name} won this round!" + puts "#{@opponent_a.name} won round #{x+1}!" else @turns << @opponent_b - puts "#{@opponent_b.name} won this round!" + puts "#{@opponent_b.name} won round #{x+1}!" end end end diff --git a/superfight.rb b/superfight.rb index f5b17b9..51fd94c 100644 --- a/superfight.rb +++ b/superfight.rb @@ -11,5 +11,5 @@ puts "..." match = Match.new(Fighter.new(fighter_a), Fighter.new(fighter_b)) puts "..." -puts "The winner of match is ....... #{match.winner.name}!!!" +puts "The winner of the match is ....... #{match.winner.name}!!!" puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" From 6b300e92f9f3e569bffac62ba596e5792cc5455d Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 8 Jan 2014 13:15:08 -0800 Subject: [PATCH 5/8] refactor panda level feature --- lib/match.rb | 15 +++++++++------ lor | 4 ---- superfight.rb | 1 + 3 files changed, 10 insertions(+), 10 deletions(-) delete mode 100644 lor diff --git a/lib/match.rb b/lib/match.rb index 087618b..1e0066b 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -25,17 +25,20 @@ def winner_count_for_opponent(opponent) @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 + private def build_turns - @turns = [] - 13.times do |x| + @turns = 13.times.map do |x| turn = Turn.new(@opponent_a.strike, @opponent_b.strike) if @opponent_a.strike == turn.winner - @turns << @opponent_a - puts "#{@opponent_a.name} won round #{x+1}!" + @opponent_a else - @turns << @opponent_b - puts "#{@opponent_b.name} won round #{x+1}!" + @opponent_b end end end diff --git a/lor b/lor deleted file mode 100644 index b992dff..0000000 --- a/lor +++ /dev/null @@ -1,4 +0,0 @@ -............ - -Finished in 0.00273 seconds -12 examples, 0 failures diff --git a/superfight.rb b/superfight.rb index 51fd94c..c35af67 100644 --- a/superfight.rb +++ b/superfight.rb @@ -10,6 +10,7 @@ puts "READY, SET, FIGHT!!!!!!" puts "..." match = Match.new(Fighter.new(fighter_a), Fighter.new(fighter_b)) +match.announce_turn_winners puts "..." puts "The winner of the match is ....... #{match.winner.name}!!!" puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" From 856911e41fdec3a7d9b3b98de321235cad3d6904 Mon Sep 17 00:00:00 2001 From: jason perez Date: Wed, 8 Jan 2014 13:47:40 -0800 Subject: [PATCH 6/8] update rank to select move with the higher rank as the turn winner --- lib/turn.rb | 3 ++- spec/turn_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/turn.rb b/lib/turn.rb index f3b73ff..89ebe35 100644 --- a/lib/turn.rb +++ b/lib/turn.rb @@ -9,6 +9,7 @@ def initialize(move_a, move_b) private def determine_winner - [@move_a, @move_b].sample + [@move_a, @move_b].max { |a, b| a.ranking <=> b.ranking } end end + diff --git a/spec/turn_spec.rb b/spec/turn_spec.rb index 8382133..09c0dc4 100644 --- a/spec/turn_spec.rb +++ b/spec/turn_spec.rb @@ -8,4 +8,11 @@ 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) + strike_d = double("Strike_d", ranking: 50) + + Turn.new(strike_c, strike_d).winner.should eq(strike_c) + end end From 69844da0c599f1af7ac2f0a64a026d59d66a6590 Mon Sep 17 00:00:00 2001 From: jason perez Date: Fri, 10 Jan 2014 12:44:45 -0800 Subject: [PATCH 7/8] add ability to provide the winner of a given turn --- lib/match.rb | 6 +++++- spec/match_spec.rb | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/match.rb b/lib/match.rb index 1e0066b..41a3751 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -31,9 +31,13 @@ def announce_turn_winners end end + def winner_for_turn(turn) + turns[turn].name + end + private def build_turns - @turns = 13.times.map do |x| + @turns = 13.times.map do turn = Turn.new(@opponent_a.strike, @opponent_b.strike) if @opponent_a.strike == turn.winner @opponent_a diff --git a/spec/match_spec.rb b/spec/match_spec.rb index 0027157..722d421 100644 --- a/spec/match_spec.rb +++ b/spec/match_spec.rb @@ -28,5 +28,9 @@ 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 From f69983cd265707b5a98e04b75875286645b08221 Mon Sep 17 00:00:00 2001 From: jason perez Date: Fri, 10 Jan 2014 14:03:28 -0800 Subject: [PATCH 8/8] add Block vs Block should pick a random winner --- lib/turn.rb | 6 +++++- spec/turn_spec.rb | 13 +++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/turn.rb b/lib/turn.rb index 89ebe35..4093518 100644 --- a/lib/turn.rb +++ b/lib/turn.rb @@ -9,7 +9,11 @@ def initialize(move_a, move_b) private def determine_winner - [@move_a, @move_b].max { |a, b| a.ranking <=> b.ranking } + 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/turn_spec.rb b/spec/turn_spec.rb index 09c0dc4..cad966c 100644 --- a/spec/turn_spec.rb +++ b/spec/turn_spec.rb @@ -10,9 +10,18 @@ end it "should select move with the higher rank as the turn winner" do - strike_c = double("strike_c", ranking: 90) - strike_d = double("Strike_d", ranking: 50) + 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