Created
April 3, 2023 18:19
-
-
Save joshkennedy/111735c90bf1b669fddfb58097034cd4 to your computer and use it in GitHub Desktop.
Bowling
This file contains hidden or 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 Game | |
def initialize(rolls: []) | |
@rolls = rolls | |
@score = 0 | |
end | |
def roll(pins) | |
@rolls << pins | |
end | |
def score | |
frame = 0 | |
i = 0 | |
while frame < 10 | |
if strike?(i) | |
@score += @rolls[i] + @rolls[i + 1] + @rolls[i + 2] | |
i += 1 | |
elsif spare?(i) | |
@score += @rolls[i] + @rolls[i + 1] + @rolls[i + 2] | |
i += 2 | |
else | |
@score += @rolls[i] + @rolls[i + 1] | |
i += 2 | |
end | |
frame += 1 | |
end | |
@score | |
end | |
def spare?(index) | |
@rolls[index] + @rolls[index + 1] == 10 | |
end | |
def strike?(index) | |
@rolls[index] == 10 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment