Created
June 4, 2013 03:58
-
-
Save shicholas/5703467 to your computer and use it in GitHub Desktop.
Pair Programming Session w/ @marksim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bowling | |
def initialize | |
@rolls = [] | |
end | |
def roll(pins) | |
@rolls << pins | |
end | |
def score | |
index = 0 | |
(0..9).map do |frame| | |
if @rolls[index] == 10 | |
frame_score = 10 + @rolls[index+1] + @rolls[index+2] | |
index += 1 | |
else | |
frame_score = @rolls[index] + @rolls[index+1] | |
frame_score += @rolls[index+2] if frame_score == 10 | |
index += 2 | |
end | |
frame_score | |
end.inject {|a, m| a + m} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Bowling do | |
let(:game) { Bowling.new } | |
it "adds each roll to the score when less than 10" do | |
20.times do | |
game.roll(4) | |
end | |
game.score.should == 80 | |
end | |
it "adds a spare when two rolls add up to 10" do | |
2.times do | |
game.roll(4) | |
end | |
game.roll(2) | |
game.roll(8) | |
16.times do | |
game.roll(4) | |
end | |
game.score.should == 86 | |
end | |
it "adds the next two when a strike is thrown" do | |
game.roll(10) | |
18.times do | |
game.roll(4) | |
end | |
game.score.should == 90 | |
end | |
it "should add another strike in the last frame" do | |
18.times do | |
game.roll(4) | |
end | |
game.roll(10) | |
game.roll(10) | |
game.roll(2) | |
game.score.should == 94 | |
end | |
it "should give 300 for a perfect game" do | |
12.times do | |
game.roll(10) | |
end | |
game.score.should == 300 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment