KillerDesigner
10/6/2013 - 5:49 AM

bowling.rb

class Bowling

  attr_reader :score

	def initialize
		@score = 0
		@spare = 0
		@turn = 0
		@strike = 0
		@pins_this_frame = 0
	end

	def score
		@score
	end

	def roll(pins)
		if @turn >= 2 #we started a new frame reset some variables
			@turn = 0
			@pins_this_frame = 0
		end
		@turn += 1 #we are taking another turn

		@score += pins # update score
		@pins_this_frame += pins

		if @spare == 1 # we had a spare last turn
			@score += pins # add the pins from this turn to score again
			@spare -= 1
		end

		if @strike > 0 # we had a strike last turn or the turn before that
			@score += pins # add the pins from this turn to score again
			@strike -= 1
		end

		#raise CheaterException if @pins_this_frame > 10

		if pins == 10 && @turn == 1 # we got a strike
			@strike = 2 # the pins from our next 2 rolls will be added again to score
			@turn == 3 # getting a strike puts you on the next frame
		end

		if @pins_this_frame == 10 && @turn == 2 # we got a spare
			@spare = 1 # the pins from our next turn will be added to score
		end
	end

	def perfect_score
		if @score == 300
			true
		else
			false
		end
	end
end

class CheaterException < Exception
end