Created
March 26, 2009 10:29
-
-
Save ronhopper/85999 to your computer and use it in GitHub Desktop.
A simple bowling scorer that came to me in a dream.
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 BowlingScorer | |
def initialize(rolls) | |
@rolls = rolls | |
end | |
def score | |
total = 0 | |
rolls_left = @rolls.dup | |
10.times { total += score_frame(rolls_left) } | |
total | |
end | |
private | |
def score_frame(rolls_left) | |
roll1 = rolls_left.shift | |
if roll1 == 10 | |
return 10 + rolls_left[0] + rolls_left[1] | |
end | |
roll2 = rolls_left.shift | |
if roll1 + roll2 == 10 | |
return 10 + rolls_left[0] | |
end | |
roll1 + roll2 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment