require 'osx/cocoa' class Bowling < OSX::NSObject def initialize @rolls = [] end def roll(score) @rolls << score end def score score = 0 first_roll_in_frame = 0 10.times do |index| if is_strike?(first_roll_in_frame) score += 10 + next_two_rolls_for_strike(first_roll_in_frame) first_roll_in_frame += 1 elsif is_spare?(first_roll_in_frame) score += 10 + bonus_roll_for_spare(first_roll_in_frame) first_roll_in_frame += 2 else score += next_two_rolls(first_roll_in_frame) first_roll_in_frame += 2 end end return score end def frame first_roll_in_frame = 0 10.times do |index| if is_strike?(first_roll_in_frame) first_roll_in_frame += 1 else return index + 1 if @rolls[first_roll_in_frame].nil? or @rolls[first_roll_in_frame + 1].nil? first_roll_in_frame += 2 end end end def is_spare?(index) return (@rolls[index].to_i + @rolls[index + 1].to_i == 10) end def is_strike?(index) return @rolls[index] == 10 end private ################################################# def next_two_rolls_for_strike(index) return @rolls[index + 1].to_i + @rolls[index + 2].to_i end def bonus_roll_for_spare(index) return @rolls[index + 2].to_i end def next_two_rolls(index) return @rolls[index].to_i + @rolls[index + 1].to_i end end