Created
April 16, 2013 19:11
-
-
Save joebew42/5398693 to your computer and use it in GitHub Desktop.
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 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