Skip to content

Instantly share code, notes, and snippets.

@joebew42
Created April 16, 2013 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebew42/5398693 to your computer and use it in GitHub Desktop.
Save joebew42/5398693 to your computer and use it in GitHub Desktop.
class Game
def initialize
@rolls = Array.new(21) {0}
@current_roll = 0
end
def roll(pins)
@rolls[@current_roll] = pins
@current_roll += 1
end
def score
total_score = 0
roll_index = 0
10.times do
if is_strike? roll_index
total_score += 10 + get_strike_bonus(roll_index)
roll_index += 1
elsif is_spare? roll_index
total_score += 10 + get_spare_bonus(roll_index)
roll_index += 2
else
total_score += get_frame_score(roll_index)
roll_index += 2
end
end
total_score
end
private
@rolls
@current_roll
def is_spare?(roll_index)
get_frame_score(roll_index) == 10
end
def is_strike?(roll_index)
@rolls[roll_index] == 10
end
def get_strike_bonus roll_index
get_frame_score(roll_index+1)
end
def get_spare_bonus frame
@rolls[frame*2+2]
end
def get_frame_score roll_index
@rolls[roll_index] + @rolls[roll_index+1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment