Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bowling
- attr_reader :score
- attr_reader :table
- def initialize
- @score=0
- @table=[]
- end
- def previousStrike?
- ( (@table.length>0) and (@table.length<9) and ( @table[-1] == 10 ) )
- end
- def antePreviousStrike?
- ( (@table.length>1) and ( @table[-2]==10) )
- end
- def spare?
- ( (not(previousStrike?)) and (not(antePreviousStrike?)) and (@table.length<9) and (@table.length>1) and (( @table[-1] + @table[-2] )==10) and (@table.length.even?))
- end
- def roll(nb_quilles)
- @score+=nb_quilles
- if (previousStrike?) or (spare?)
- @score+=nb_quilles
- end
- if (antePreviousStrike?)
- @score+=nb_quilles
- end
- @table << nb_quilles
- end
- def rolls(lancers)
- lancers.each {|quilles| roll(quilles)}
- end
- end
- require "test/unit"
- require "bowling"
- class Test_bowling < Test::Unit::TestCase
- def setup
- @bowling=Bowling.new
- end
- def test_rateTout
- 20.times do
- @bowling.roll(0)
- end
- assert_equal(0,@bowling.score)
- end
- def test_troisPuisCinq
- @bowling.rolls([3,5])
- assert_equal(8,@bowling.score)
- end
- def test_renverseTroisQuillesAChaqueFois
- 20.times do
- @bowling.roll(3)
- end
- assert_equal(60,@bowling.score)
- end
- def test_spareSimple
- @bowling.rolls([7,3,4])
- assert_equal(18,@bowling.score)
- end
- def test_spareAndNotSpare
- @bowling.rolls([7,3,7,2])
- assert_equal(26,@bowling.score)
- end
- def test_spareAndSecondSpare
- @bowling.rolls([7,3,8,2,1,1])
- assert_equal(31,@bowling.score)
- end
- def test_strike
- @bowling.rolls([10,1,3])
- assert_equal(18,@bowling.score)
- end
- def test_strike
- @bowling.rolls([10,10,10,1,1])
- assert_equal(65,@bowling.score)
- end
- def test_strike
- 12.times { @bowling.roll(10) }
- assert_equal(300,@bowling.score)
- end
- def test_finalSpare
- 8.times { @bowling.roll(1) }
- @bowling.rolls([4,6,1,1])
- assert_equal(20,@bowling.score)
- end
- def test_finalSpareFalseHorse
- 8.times { @bowling.roll(1) }
- @bowling.rolls([4,6,4,1])
- assert_equal(23,@bowling.score)
- end
- end
Add Comment
Please, Sign In to add comment