From d02a018e6bf0ab9c88dc2f84d0c183d18b50e203 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Sun, 2 Feb 2014 23:23:34 -0500 Subject: [PATCH] panda, tiger, eagle levels --- lib/match.rb | 12 +++++++++--- lib/turn.rb | 23 ++++++++++++++--------- spec/match_spec.rb | 7 +++++++ spec/move_spec.rb | 8 ++++---- spec/spec_helper.rb | 13 +++++++++++++ spec/turn_spec.rb | 44 +++++++++++++++++++++++++++++++++++++++----- superfight.rb | 1 + 7 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 spec/spec_helper.rb diff --git a/lib/match.rb b/lib/match.rb index 3d171bc..08f41c0 100644 --- a/lib/match.rb +++ b/lib/match.rb @@ -22,13 +22,19 @@ def winner end def winner_count_for_opponent(opponent) - @turns.select{ |turn| opponent.strike == turn.winner}.count + @turns.select{ |turn| opponent == turn.winner}.count + end + + def replay + @turns.each_with_index.map do |turn, turn_num| + "Round #{turn_num+1} -- #{turn.winner.name.chomp} won!" + end end private def build_turns - 13.times.map do - Turn.new(@opponent_a.strike, @opponent_b.strike) + 13.times.map do |i| + Turn.new(@opponent_a, @opponent_b) end end diff --git a/lib/turn.rb b/lib/turn.rb index 10578d3..1bc70bc 100644 --- a/lib/turn.rb +++ b/lib/turn.rb @@ -1,14 +1,19 @@ require_relative "move" class Turn - attr_reader :winner - def initialize(move_a, move_b) - @move_a = move_a - @move_b = move_b - @winner = determine_winner + attr_reader :fighter_a_move, :fighter_b_move + def initialize(fighter_a, fighter_b) + @fighter_a = fighter_a + @fighter_b = fighter_b + @fighter_a_move = [@fighter_a.strike, @fighter_a.block].sample + @fighter_b_move = [@fighter_b.strike, @fighter_b.block].sample end - private - def determine_winner - [@move_a, @move_b].sample - end + def tie + [@fighter_a, @fighter_b].sample + end + + def winner + return tie if @fighter_a_move.type == :block and @fighter_b_move.type == :block + @fighter_a_move.ranking > @fighter_b_move.ranking ? @fighter_a : @fighter_b + end end diff --git a/spec/match_spec.rb b/spec/match_spec.rb index f60e7d3..77f4722 100644 --- a/spec/match_spec.rb +++ b/spec/match_spec.rb @@ -1,5 +1,7 @@ require 'rspec' require_relative "../lib/match" +require_relative "../lib/turn" + describe Match do let(:bob) { Fighter.new("bob") } @@ -27,4 +29,9 @@ subject.stub(:winner_count_for_opponent).with(fred) {10} subject.winner.should eq(fred) end + + it 'should output the winner of each turn' do + fixed_match = Match.new(bob, bob) + expect(fixed_match.replay[0..1]).to eq(["Round 1 -- bob won!", "Round 2 -- bob won!"]) + end end diff --git a/spec/move_spec.rb b/spec/move_spec.rb index 87d2e72..ee0e064 100644 --- a/spec/move_spec.rb +++ b/spec/move_spec.rb @@ -1,15 +1,15 @@ -require 'rspec' +require 'spec_helper' require_relative '../lib/move' describe Move do - it "can be a strike" do + it 'can be a strike' do Move.new(:strike).type.should eq(:strike) end - it "can be a block" do + it 'can be a block' do Move.new(:block).type.should eq(:block) end - it "has a ranking" do + it 'has a ranking' do (1..100).should include Move.new(stub).ranking end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..cbae34c --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,13 @@ +require 'rspec' + +RSpec.configure do |config| + # Use color in STDOUT + config.color_enabled = true + + # Use color not only in STDOUT but also in pagers and files + config.tty = true + + # Use the specified formatter + config.formatter = :documentation # :progress, :html, :textmate +end + diff --git a/spec/turn_spec.rb b/spec/turn_spec.rb index 8382133..054faf7 100644 --- a/spec/turn_spec.rb +++ b/spec/turn_spec.rb @@ -2,10 +2,44 @@ require_relative "../lib/turn" describe Turn do - let(:strike_a) { Move.new(:strike) } - let(:strike_b) { Move.new(:strike) } - it "should have a winner" do - [strike_a, strike_b].should include Turn.new(strike_a, strike_b).winner - end + let(:fighter_a) { Fighter.new('bob') } + let(:fighter_b) { Fighter.new('fred') } + let(:strike_a) {Move.new(:strike)} + let(:strike_b) {Move.new(:strike)} + let(:block_a) {Move.new(:block)} + let(:block_b) {Move.new(:block)} + + subject { Turn.new(fighter_a, fighter_b) } + + it "should have a winner" do + [fighter_a, fighter_b].should include subject.winner + end + + it "should determine winner by rank on strike vs strike" do + # turn = Turn.new(fighter_a, fighter_b) + strike_a.stub(:ranking) {99} + strike_b.stub(:ranking) {1} + subject.instance_variable_set(:@fighter_a_move, strike_a) + subject.instance_variable_set(:@fighter_b_move, strike_b) + expect(subject.winner).to eq(fighter_a) + end + + it "should determine winner by rank on strike vs block" do + # subject = subject.new(fighter_a, fighter_b) + strike_a.stub(:ranking) {1} + block_b.stub(:ranking) {99} + subject.instance_variable_set(:@fighter_a_move, strike_a) + subject.instance_variable_set(:@fighter_b_move, block_b) + expect(subject.winner).to eq(fighter_b) + end + + it "should determine winner on block vs block" do + # subject = subject.new(fighter_a, fighter_b) + block_a.stub(:ranking) {1} + block_b.stub(:ranking) {99} + subject.instance_variable_set(:@fighter_a_move, block_a) + subject.instance_variable_set(:@fighter_b_move, block_b) + expect([fighter_a, fighter_b]).to include(subject.winner) + end end diff --git a/superfight.rb b/superfight.rb index 081a711..dfa8e6d 100644 --- a/superfight.rb +++ b/superfight.rb @@ -7,5 +7,6 @@ fighter_b = $stdin.gets match = Match.new(Fighter.new(fighter_a), Fighter.new(fighter_b)) +puts match.replay puts "The winner of match is ....... #{match.winner.name}"