Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. def score(dice) #dice is an array of numbers, i.e. [3,4,5,3,3]
  2. return 0 if(dice == [] || dice == nil)
  3.  
  4. dice.sort!
  5.  
  6. return 1000 + score(dice[3..-1]) if(dice[0..2] == [1,1,1])
  7. return 600 + score(dice[3..-1]) if(dice[0..2] == [6,6,6])
  8. return 500 + score(dice[3..-1]) if(dice[0..2] == [5,5,5])
  9. return 400 + score(dice[3..-1]) if(dice[0..2] == [4,4,4])
  10. return 300 + score(dice[3..-1]) if(dice[0..2] == [3,3,3])
  11. return 200 + score(dice[3..-1]) if(dice[0..2] == [2,2,2])
  12. return 100 + score(dice[1..-1]) if(dice[0] == 1)
  13. return 50 + score(dice[1..-1]) if(dice[0] == 5)
  14. return 0 + score(dice[1..-1]);
  15. end
  16.  
  17. # Greed is a dice game where you roll up to five dice to accumulate
  18. # points. A greed roll is scored as follows:
  19. #
  20. # * A set of three ones is 1000 points
  21. #
  22. # * A set of three numbers (other than ones) is worth 100 times the
  23. # number. (e.g. three fours is 400 points).
  24. #
  25. # * A one (that is not part of a set of three) is worth 100 points.
  26. #
  27. # * A five (that is not part of a set of three) is worth 50 points.
  28. #
  29. # * Everything else is worth 0 points.
  30. #
  31. #
  32. # Examples:
  33. #
  34. # score([1,1,1,5,1]) => 1150 points
  35. # score([2,3,4,6,2]) => 0 points
  36. # score([3,4,5,3,3]) => 350 points
  37. # score([1,5,1,2,4]) => 250 points
  38. #
  39. # More scoring examples are given in the tests below:
  40.  
  41.  
  42. class AboutScoringProject < EdgeCase::Koan
  43. def test_score_of_an_empty_list_is_zero
  44. assert_equal 0, score([])
  45. end
  46.  
  47. def test_score_of_a_single_roll_of_5_is_50
  48. assert_equal 50, score([5])
  49. end
  50.  
  51. def test_score_of_a_single_roll_of_1_is_100
  52. assert_equal 100, score([1])
  53. end
  54.  
  55. def test_score_of_a_single_roll_of_1_is_100
  56. assert_equal 200, score([1,1])
  57. end
  58.  
  59. def test_score_of_multiple_1s_and_5s_is_the_sum_of_individual_scores
  60. assert_equal 300, score([1,5,5,1])
  61. end
  62.  
  63. def test_score_of_single_2s_3s_4s_and_6s_are_zero
  64. assert_equal 0, score([2,3,4,6])
  65. end
  66.  
  67. def test_score_of_a_triple_1_is_1000
  68. assert_equal 1000, score([1,1,1])
  69. end
  70.  
  71. def test_score_of_other_triples_is_100x
  72. assert_equal 200, score([2,2,2])
  73. assert_equal 300, score([3,3,3])
  74. assert_equal 400, score([4,4,4])
  75. assert_equal 500, score([5,5,5])
  76. assert_equal 600, score([6,6,6])
  77. end
  78.  
  79. def test_score_of_mixed_is_sum
  80. assert_equal 250, score([2,5,2,2,3])
  81. assert_equal 550, score([5,5,5,5])
  82. end
  83.  
  84. def test_score_of_a_triple_1_is_1000A
  85. assert_equal 1150, score([1,1,1,5,1])
  86. end
  87.  
  88. def test_score_of_a_triple_1_is_1000B
  89. assert_equal 350, score([3,4,5,3,3])
  90. end
  91.  
  92. def test_score_of_a_triple_1_is_1000C
  93. assert_equal 250, score([1,5,1,2,4])
  94. end
  95. end
  96.  
  97. def score(dice)
  98. score = 0
  99.  
  100. # Below is equivalent to:
  101. # counts = dice.inject(Hash.new(0)) { |h, x| h[x] += 1; h }
  102. counts = Hash.new(0)
  103. dice.each do |x|
  104. counts[x] += 1
  105. end
  106.  
  107. (1..6).each do |i|
  108. if counts[i] >= 3
  109. if i == 1
  110. score += 1000
  111. else
  112. score += 100 * i
  113. end
  114.  
  115. counts[i] = [counts[i] - 3, 0].max
  116. end
  117.  
  118. if i == 1
  119. score += 100 * counts[i]
  120. elsif i == 5
  121. score += 50 * counts[i]
  122. end
  123. end
  124.  
  125. score
  126. end
  127.  
  128. def score dice
  129. dice.group_by(&:to_i).inject(0) do |score, combo|
  130. score + combos_score(*combo) + ones_score(*combo) + fives_score(*combo)
  131. end
  132. end
  133.  
  134. def combos_score dice_value, dice_with_value
  135. number_of_bonues = [dice_with_value.size - 2, 0].max
  136.  
  137. bonus_for(dice_value) * number_of_bonues
  138. end
  139.  
  140. def bonus_for dice_value
  141. dice_value == 1 ? 1000 : dice_value * 100
  142. end
  143.  
  144. def ones_score dice_value, dice_with_value
  145. return 0 if dice_value != 1 || dice_with_value.size > 2
  146.  
  147. dice_with_value.size * 100
  148. end
  149.  
  150. def fives_score dice_value, dice_with_value
  151. return 0 if dice_value != 5 || dice_with_value.size > 2
  152.  
  153. dice_with_value.size * 50
  154. end
  155.  
  156. def score(dice)
  157.  
  158. totals = Hash.new(0)
  159.  
  160. dice.each { |x| totals[x] += 1 }
  161.  
  162. score = 0
  163.  
  164. totals.each do |x, t|
  165.  
  166. if t >= 3
  167. score += x == 1 ? 1000 : (x * 100)
  168. t -= 3;
  169. end
  170.  
  171. if x == 1
  172. score += t * 100
  173. elsif x == 5
  174. score += t * 50
  175. end
  176.  
  177. end
  178.  
  179. score
  180.  
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement