ruby bowling.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 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








以上是关于ruby bowling.rb的主要内容,如果未能解决你的问题,请参考以下文章

ruby [Ruby Cheat] Cheatsheet #ruby

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?