Created
May 16, 2012 07:52
-
-
Save haru01/2708466 to your computer and use it in GitHub Desktop.
bowlinggame
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 BowlingGame | |
attr_reader :rolls | |
def initialize(rolls) | |
@rolls = rolls | |
end | |
def score | |
(1..10).reduce([]) { |frame_calc_a, frame_number| | |
frame_calc_a << rolls[range(frame_number)] | |
}.flatten.reduce(:+) | |
end | |
def range(frame_number) | |
first_i = (frame_number - 1) * 2 | |
second_i = first_i + 1 | |
return (first_i..second_i + 3) if frame_number <= 8 and double_strike?(first_i) | |
return (first_i..second_i + 2) if strike?(first_i) | |
return (first_i..second_i + 1) if spare?(first_i) | |
(first_i..second_i) | |
end | |
def spare?(first_i) | |
(rolls[first_i] + rolls[first_i + 1]) == 10 and !strike?(first_i) | |
end | |
def strike?(first_i) | |
rolls[first_i] == 10 | |
end | |
def double_strike?(first_i) | |
strike?(first_i) and strike?(first_i + 2) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment