Day 2 Homework #2

Closed
wants to merge 2 commits into
from
View
@@ -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
@@ -1,35 +1,54 @@
require_relative "fighter"
require_relative "turn"
+
+require 'pp'
+
class Match
attr_reader :turns
def initialize(opponent_a, opponent_b)
@opponent_a = opponent_a
@opponent_b = opponent_b
@turns = build_turns
+ @results = []
end
def opponents
[@opponent_a, @opponent_b]
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
@@ -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
@@ -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
@@ -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

jwo May 3, 2012

Owner

Good stuff here!

+ 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
@@ -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))