Skip to content

Instantly share code, notes, and snippets.

@joshkennedy
Created April 3, 2023 18:19
Show Gist options
  • Save joshkennedy/111735c90bf1b669fddfb58097034cd4 to your computer and use it in GitHub Desktop.
Save joshkennedy/111735c90bf1b669fddfb58097034cd4 to your computer and use it in GitHub Desktop.
Bowling
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