Created
July 8, 2012 08:50
-
-
Save shostakovich/3070003 to your computer and use it in GitHub Desktop.
Bowling game 4th iteration
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 BowlingGame | |
def initialize | |
@rolls = [] | |
end | |
def roll(pins) | |
@rolls << pins | |
end | |
def score | |
calculator = Score.new(@rolls) | |
calculator.calculate | |
end | |
class Score | |
def initialize(rolls) | |
@rolls = rolls | |
@first_ball_of_frame = 0 | |
end | |
def calculate() | |
score = 0 | |
0.upto(9) do | |
if strike? | |
score += 10 + next_two_balls_for_strike | |
@first_ball_of_frame += 1 | |
elsif spare? | |
score += 10 + extra_ball_for_square | |
@first_ball_of_frame += 2 | |
else | |
score += two_balls_in_frame | |
@first_ball_of_frame += 2 | |
end | |
end | |
score | |
end | |
def strike? | |
@rolls[@first_ball_of_frame] == 10 | |
end | |
def spare? | |
@rolls[@first_ball_of_frame] + @rolls[@first_ball_of_frame + 1] == 10 | |
end | |
def two_balls_in_frame | |
@rolls[@first_ball_of_frame] + @rolls[@first_ball_of_frame + 1] | |
end | |
def extra_ball_for_square | |
@rolls[@first_ball_of_frame + 2] | |
end | |
def next_two_balls_for_strike | |
@rolls[@first_ball_of_frame + 1] + @rolls[@first_ball_of_frame + 2] | |
end | |
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
describe BowlingGame do | |
let(:game){ BowlingGame.new } | |
it "calculates the score of a gutter game" do | |
20.times { game.roll(0) } | |
game.score.should be == 0 | |
end | |
it "calculates the score of 3 pins followed by gutter balls" do | |
game.roll(3) | |
19.times { game.roll(0) } | |
game.score.should be == 3 | |
end | |
it "calculates the score of a spare followed by some pins + gutter balls" do | |
roll_spare() | |
game.roll(3) | |
17.times { game.roll(0) } | |
game.score.should be == 16 | |
end | |
it "calculates the perfect game" do | |
12.times { roll_strike() } | |
game.score.should be == 300 | |
end | |
it "calculates a strike at the end" do | |
18.times { game.roll(0) } | |
roll_strike | |
2.times { game.roll(2) } | |
game.score.should be == 14 | |
end | |
def roll_strike | |
game.roll(10) | |
end | |
def roll_spare() | |
game.roll(5) | |
game.roll(5) | |
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
BowlingGame | |
calculates the score of a gutter game | |
calculates the score of 3 pins followed by gutter balls | |
calculates the score of a spare followed by some pins + gutter balls | |
calculates the perfect game | |
calculates a strike at the end | |
Finished in 0.00527 seconds | |
5 examples, 0 failures |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks quite verbose. Not happy with it - but at least it finally seems to work..