Guest User

Untitled

a guest
May 4th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. # Greed is a dice game where you roll up to five dice to accumulate
  2. # points. The following "score" function will be used to calculate the
  3. # score of a single roll of the dice.
  4. #
  5. # A greed roll is scored as follows:
  6. #
  7. # * A set of three ones is 1000 points
  8. #
  9. # * A set of three numbers (other than ones) is worth 100 times the
  10. # number. (e.g. three fives is 500 points).
  11. #
  12. # * A one (that is not part of a set of three) is worth 100 points.
  13. #
  14. # * A five (that is not part of a set of three) is worth 50 points.
  15. #
  16. # * Everything else is worth 0 points.
  17. #
  18. #
  19. # Examples:
  20. #
  21. # score([1,1,1,5,1]) => 1150 points
  22. # score([2,3,4,6,2]) => 0 points
  23. # score([3,4,5,3,3]) => 350 points
  24. # score([1,5,1,2,4]) => 250 points
  25. #
  26. # More scoring examples are given in the tests below:
  27. #
  28. # Your goal is to write the score method.
  29.  
  30. def score(dice)
  31. # Hash holds values seen in the roll and the number of duplicates for each
  32. h = Hash.new(0)
  33. dice.each { | d | h.store(d, h[d]+1) }
  34.  
  35. score = 0
  36.  
  37. score += h[1] * 100
  38. score += 700 if h[1] >= 3 # 1000 bonus points, minus the 3 * 100 points for the 1's in the set
  39.  
  40. score += 200 if h[2] >= 3
  41. score += 300 if h[3] >= 3
  42. score += 400 if h[4] >= 3
  43.  
  44. score += h[5] * 50
  45. score += 350 if h[5] >= 3 # 500 bonus points, minus the 3 * 50 points for the 5's in the set
  46.  
  47. score += 600 if h[6] >= 3
  48.  
  49. score
  50. end
Advertisement
Add Comment
Please, Sign In to add comment