Guest User

Untitled

a guest
Dec 24th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.05 KB | None | 0 0
  1. class Bowling
  2.     attr_reader :score
  3.     attr_reader :table
  4.    
  5.     def initialize
  6.         @score=0
  7.         @table=[]
  8.     end
  9.    
  10.    
  11.     def previousStrike?
  12.         ( (@table.length>0) and (@table.length<9) and  ( @table[-1] == 10 ) )
  13.     end
  14.  
  15.     def antePreviousStrike?
  16.         ( (@table.length>1) and  (  @table[-2]==10) )
  17.     end
  18.    
  19.     def spare?
  20.         ( (not(previousStrike?)) and (not(antePreviousStrike?)) and (@table.length<9) and (@table.length>1) and (( @table[-1] + @table[-2] )==10) and (@table.length.even?))
  21.     end
  22.    
  23.    
  24.     def roll(nb_quilles)
  25.         @score+=nb_quilles
  26.        
  27.         if (previousStrike?) or (spare?)
  28.             @score+=nb_quilles
  29.         end
  30.  
  31.         if (antePreviousStrike?)
  32.             @score+=nb_quilles
  33.         end
  34.        
  35.         @table << nb_quilles
  36.     end
  37.    
  38.     def rolls(lancers)
  39.         lancers.each {|quilles| roll(quilles)}
  40.     end
  41. end
  42.  
  43.  
  44. require "test/unit"
  45. require "bowling"
  46.  
  47. class Test_bowling < Test::Unit::TestCase
  48.     def setup
  49.         @bowling=Bowling.new
  50.     end
  51.    
  52.     def test_rateTout
  53.         20.times do
  54.             @bowling.roll(0)
  55.         end
  56.         assert_equal(0,@bowling.score)
  57.     end
  58.  
  59.     def test_troisPuisCinq
  60.             @bowling.rolls([3,5])
  61.         assert_equal(8,@bowling.score)
  62.     end
  63.    
  64.     def test_renverseTroisQuillesAChaqueFois
  65.         20.times do
  66.             @bowling.roll(3)
  67.         end
  68.         assert_equal(60,@bowling.score)
  69.     end
  70.  
  71.     def test_spareSimple
  72.         @bowling.rolls([7,3,4])
  73.         assert_equal(18,@bowling.score)
  74.     end
  75.  
  76.     def test_spareAndNotSpare
  77.         @bowling.rolls([7,3,7,2])
  78.         assert_equal(26,@bowling.score)
  79.     end
  80.  
  81.     def test_spareAndSecondSpare
  82.         @bowling.rolls([7,3,8,2,1,1])
  83.         assert_equal(31,@bowling.score)
  84.     end
  85.  
  86.     def test_strike
  87.         @bowling.rolls([10,1,3])
  88.         assert_equal(18,@bowling.score)
  89.     end
  90.  
  91.     def test_strike
  92.         @bowling.rolls([10,10,10,1,1])
  93.         assert_equal(65,@bowling.score)
  94.     end
  95.  
  96.     def test_strike
  97.         12.times { @bowling.roll(10) }
  98.         assert_equal(300,@bowling.score)
  99.     end
  100.    
  101.     def test_finalSpare
  102.         8.times { @bowling.roll(1) }
  103.         @bowling.rolls([4,6,1,1])
  104.         assert_equal(20,@bowling.score)
  105.     end
  106.  
  107.     def test_finalSpareFalseHorse
  108.         8.times { @bowling.roll(1) }
  109.         @bowling.rolls([4,6,4,1])
  110.         assert_equal(23,@bowling.score)
  111.     end
  112.    
  113. end
Add Comment
Please, Sign In to add comment