Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Day 2 Homework #2

Closed
wants to merge 2 commits into from

2 participants

@nullthreat

So i know i didn't do this like the others. I had planned on doing the extra credit tonight so I started refactoring it in ways it would be easer to add the planned features later. I added a private fight! method that will run all 13 rounds and add the winner to an array. To decide who won the match i search the array for the number of times that fighters name is in the winner array.

I look forward to seeing how some of the other, more proficient developers solve this problem.

lib/match.rb
((31 lines not shown))
end
end
-
+
+ def fight!
+ round_num = 1
+ @turns.select do |turn|
@jwo Owner
jwo added a note

You used a #select here ... I think an #each would work better... A select is used to reduce the array down by returning true/false to use again later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo jwo commented on the diff
spec/turn_spec.rb
@@ -8,4 +8,15 @@
it "should have a winner" do
[strike_a, strike_b].should include Turn.new(strike_a, strike_b).winner
end
+
+ it "should declare the higher ranking strike winner" do
+ strike_a.stub(:ranking) {8}
@jwo Owner
jwo added a note

Good stuff here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jwo
Owner
jwo commented

I think you have a good start here.... The only thing I'd recommend is rather than "puts" out the result, I'd like to be able to replay them again.... So be able to do (after you've fought)

match.turns.each do |turn|
  puts turn.result
end

Or something similar

@jwo jwo closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  README.md
@@ -6,7 +6,7 @@ A Game engine with Fighters, turns, and Matches
Your Assignment
---------------
-1. Instead of randomly selecting the winner of a turn, use the Move's ranking to determine a winner
+1. Instead of randomly selecting the winner of a turn, use the Move's ranking to determine a winner (Done)
2. Output to the screen a listing of the turns and who won
Extra Credit (challenging)
View
43 lib/match.rb
@@ -1,5 +1,8 @@
require_relative "fighter"
require_relative "turn"
+
+require 'pp'
+
class Match
attr_reader :turns
@@ -7,6 +10,7 @@ def initialize(opponent_a, opponent_b)
@opponent_a = opponent_a
@opponent_b = opponent_b
@turns = build_turns
+ @results = []
end
def opponents
@@ -14,22 +18,37 @@ def opponents
end
def winner
- if winner_count_for_opponent(@opponent_a) > winner_count_for_opponent(@opponent_b)
- @opponent_a
- else
- @opponent_b
- end
- end
-
- def winner_count_for_opponent(opponent)
- @turns.select{ |turn| opponent.strike == turn.winner}.count
+ fight!
+ if winner_count_for_opponent(@opponent_a.name) > winner_count_for_opponent(@opponent_b.name)
+ @opponent_a
+ else
+ @opponent_b
+ end
end
+ def winner_count_for_opponent(opponent)
+ @results.select{|winner| opponent == winner}.count
+ end
+
private
def build_turns
- 13.times.map do
- Turn.new(@opponent_a.strike, @opponent_b.strike)
+ 13.times.map do
+ Turn.new(@opponent_a.strike, @opponent_b.strike)
end
end
-
+
+ def fight!
+ round_num = 1
+ @turns.each do |turn|
+ puts "Round #{round_num}. FIGHT!"
+ if turn.move_a == turn.winner
+ puts "Round won by #{@opponent_a.name}"
+ @results << @opponent_a.name
+ else
+ puts "Round won by #{@opponent_b.name}"
+ @results << @opponent_b.name
+ end
+ round_num += 1
+ end
+ end
end
View
8 lib/turn.rb
@@ -1,6 +1,6 @@
require_relative "move"
class Turn
- attr_reader :winner
+ attr_reader :winner, :move_a, :move_b
def initialize(move_a, move_b)
@move_a = move_a
@move_b = move_b
@@ -9,6 +9,10 @@ def initialize(move_a, move_b)
private
def determine_winner
- [@move_a, @move_b].sample
+ if @move_a.ranking > @move_b.ranking
+ @move_a
+ else
+ @move_b
+ end
end
end
View
8 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.name) {8}
+ subject.stub(:winner_count_for_opponent).with(fred.name) {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(bob.name) {3}
+ subject.stub(:winner_count_for_opponent).with(fred.name) {10}
subject.winner.should eq(fred)
end
end
View
11 spec/turn_spec.rb
@@ -8,4 +8,15 @@
it "should have a winner" do
[strike_a, strike_b].should include Turn.new(strike_a, strike_b).winner
end
+
+ it "should declare the higher ranking strike winner" do
+ strike_a.stub(:ranking) {8}
@jwo Owner
jwo added a note

Good stuff here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ strike_b.stub(:ranking) {75}
+ Turn.new(strike_a, strike_b).winner.should eq(strike_b)
+ end
+
+ it "should have a turn number" do
+
+ end
+
end
View
7 superfight.rb
@@ -2,9 +2,12 @@
puts "What is your first fighter's name?"
-fighter_a = $stdin.gets
+#fighter_a = $stdin.gets
puts "What is your second fighter's name?"
-fighter_b = $stdin.gets
+#fighter_b = $stdin.gets
+
+fighter_a = "Adam Carolla"
+fighter_b = "The Hammer"
match = Match.new(Fighter.new(fighter_a), Fighter.new(fighter_b))
Something went wrong with that request. Please try again.