Created
August 1, 2013 17:43
-
-
Save techpeace/6133572 to your computer and use it in GitHub Desktop.
A refactored, decomposed version of the code from Testing, part 2.
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 = [] | |
@total_score = 0 | |
@current_roll = 0 | |
end | |
# Record a @roll in the game. | |
# | |
# pins - The Integer number of pins knocked down in this @roll. | |
# | |
# Returns nothing. | |
def roll(pins) | |
@rolls.push(pins) | |
end | |
# Returns the Integer score for this game. | |
def score | |
while @current_roll < @rolls.size - 1 | |
init_roll | |
if strike? | |
score_strike | |
elsif spare? | |
score_spare | |
else | |
score_partial | |
end | |
end | |
return @total_score | |
end | |
private | |
def init_roll | |
@roll = @rolls[@current_roll] | |
@next_roll = @rolls[@current_roll + 1] | |
end | |
def strike? | |
@roll == 10 | |
end | |
def score_strike | |
@total_score += 10 + @rolls[@current_roll + 1] + @rolls[@current_roll + 2] | |
@current_roll += 1 | |
end | |
def spare? | |
@roll + @next_roll == 10 | |
end | |
def score_spare | |
@total_score += 10 + @rolls[@current_roll + 2] | |
@current_roll += 2 | |
end | |
def score_partial | |
@total_score += @roll + @next_roll | |
@current_roll += 2 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment