Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panda, tiger, eagle levels #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 9 additions & 3 deletions lib/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 14 additions & 9 deletions lib/turn.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

You probably want to use && in most cases of and. Same with || and or (reasons: http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/)

@fighter_a_move.ranking > @fighter_b_move.ranking ? @fighter_a : @fighter_b
end
end
7 changes: 7 additions & 0 deletions spec/match_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'rspec'
require_relative "../lib/match"
require_relative "../lib/turn"


describe Match do
let(:bob) { Fighter.new("bob") }
Expand Down Expand Up @@ -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!"])
Copy link
Member

Choose a reason for hiding this comment

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

lol. way to go bob!

end
end
8 changes: 4 additions & 4 deletions spec/move_spec.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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

44 changes: 39 additions & 5 deletions spec/turn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

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

Hopefully this is what you meant be specific example:

In turn.rb I create a turn with 2 Fighters. These fighters already have moves assigned on creation.

  1. I need to assign a specific move and ranking for testing
    Next, Turn randomly chooses a move for each fighter.
  2. I need to assign a specific move to each fighter so I can test that the right move won

Are you saying I should have created a method #random on Turn that took 2 Moves so that when testing I could send in the Moves I want?

Copy link
Member

Choose a reason for hiding this comment

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

Right, so maybe instead of

[move_a, move_b].sample

You could have a method

choose_random [move_a, move_b]

And then you have full control over choose_random

muhahahaha 🎉

# 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
1 change: 1 addition & 0 deletions superfight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"