Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
21 changes: 14 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,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
13.times.map do
Turn.new(@opponent_a.strike, @opponent_b.strike)
@turns = []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're looping over something to collect items into an array, I'd rather you use map.

@turns = 13.times.map do |x|
  turn = Turn.new(@opponent_a.strike, @opponent_b.strike)
  if @opponent_a.strike == turn.winner
    @opponent_a
  else
    @opponent_b
  end
end

@turns.each_with_index do |winner, i|
  puts "#{winner.name} won round #{i+1}!"
end

I also separated the running of the turns with the reporting of the turns.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. So in this case I'm interested in getting an array of winners for a given round. map returns an array so it more directly conveys what I intend to receive. Whereas in my implementation, I went in a round about way to get there by separately instantiating an array then using an iterator to populate that array. Is this a good way to look at it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep exactly. 😃

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 round #{x+1}!"
else
@turns << @opponent_b
puts "#{@opponent_b.name} won round #{x+1}!"
end
end
end

end
4 changes: 2 additions & 2 deletions lib/turn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lor
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
............

Finished in 0.00273 seconds
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file probably shouldn't have been added to the repo.

12 examples, 0 failures
8 changes: 5 additions & 3 deletions spec/match_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
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

$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
14 changes: 9 additions & 5 deletions superfight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 the match is ....... #{match.winner.name}!!!"
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"