Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.22 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env ruby
  2. # Greed is a dice game where you roll up to five dice to accumulate
  3. # points.  The following "score" function will be used calculate the
  4. # score of a single roll of the dice.
  5. #
  6. # A greed roll is scored as follows:
  7. #
  8. # * A set of three ones is 1000 points
  9. #
  10. # * A set of three numbers (other than ones) is worth 100 times the
  11. #   number. (e.g. three fives is 500 points).
  12. #
  13. # * A one (that is not part of a set of three) is worth 100 points.
  14. #
  15. # * A five (that is not part of a set of three) is worth 50 points.
  16. #
  17. # * Everything else is worth 0 points.
  18. #
  19. #
  20. # Examples:
  21. #
  22. # score([1,1,1,5,1]) => 1150 points
  23. # score([2,3,4,6,2]) => 0 points
  24. # score([3,4,5,3,3]) => 350 points
  25. # score([1,5,1,2,4]) => 250 points
  26. #
  27. # More scoring examples are given in the tests below:
  28. #
  29. # Your goal is to write the score method.
  30.  
  31. def score(dice)
  32.   # You need to write this method
  33.   keeper = Array.new(6, 0)
  34.   score = 0
  35.   dice[0, 5].each do
  36.     raise ArgumentError, "Invalid die face #{val}" if val < 1 || val > 6
  37.     |val| keeper[val - 1] += 1
  38.   end
  39.   score += 1000 if keeper[0] >= 3
  40.   score += 100 * (keeper[0] % 3)
  41.   score += 50 * (keeper[4] % 3)
  42.   keeper.each_with_index do |tally, val|
  43.     score += (val + 2) * 100 if tally >= 3
  44.   end
  45.   score
  46. end