Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions lib/match.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
11 changes: 8 additions & 3 deletions lib/turn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

12 changes: 9 additions & 3 deletions spec/match_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/move_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions spec/turn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions superfight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"