Skip to content

Instantly share code, notes, and snippets.

@mikeebert
Created August 12, 2013 01:17
Show Gist options
  • Save mikeebert/6207653 to your computer and use it in GitHub Desktop.
Save mikeebert/6207653 to your computer and use it in GitHub Desktop.
Just messing around with the bowling kata
class BowlingScorer
def initialize
@score = 0
@rolls = []
@frame_index = 0
end
def roll(n)
@rolls << n
end
def score
10.times do
if strike?
score_strike_bonus
elsif spare?
score_spare_bonus
else
score_standard_frame
end
advance_frame
end
@score
end
private
def strike?
@rolls[@frame_index] == 10
end
def score_strike_bonus
@score += 10 + @rolls[@frame_index + 1] + @rolls[@frame_index + 2]
end
def spare?
@rolls[@frame_index] + @rolls[@frame_index + 1] == 10
end
def score_spare_bonus
@score += 10 + @rolls[@frame_index + 2]
end
def score_standard_frame
@score += @rolls[@frame_index] + @rolls[@frame_index + 1]
end
def advance_frame
strike? ? @frame_index += 1 : @frame_index += 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment