Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. # My approach for the greed game was to take the values in a roll and sort them into seperate arrays. For example all the ones would be in an array, twos in an array, etc.. To keep these neat and tidy I will organize them into a hash.
  2. # I could create a class called dice with two methods, roll and score:
  3. #The roll method will randomly generate the numbers for the roll (I don't think this project included that but I'll add it anyways) and them store them into their arrays of similar numbers.
  4. # The score method would be responsible for taking the sorted arrays and calculating the score then return the score.
  5.  
  6. class Dice
  7.  
  8. def initialize
  9. @roll = {1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => []}
  10. @points = 0
  11. end
  12.  
  13. def roll(count)
  14. dice = []
  15.  
  16. # Generate Values
  17. dice = (1..count).map { |_| Random.rand(1..6) }
  18.  
  19. dice.each do |die|
  20. @roll[die] << die
  21. end
  22.  
  23. # Output roll to user
  24. puts dice.join(", ")
  25. end
  26.  
  27. def score
  28. # Split the hash
  29. remain = @roll.reject{ |k| k == 1 || k == 5 }
  30. special = @roll.reject{ |k| k == 2 || k == 3 || k == 4 || k == 6 }
  31.  
  32. # Handle the specials
  33. special.each do |key, array|
  34. if key == 1
  35. calculate_specials(key, array, 1000, 100)
  36. elsif key == 5
  37. calculate_specials(key, array, 500, 50)
  38. end
  39.  
  40. end
  41.  
  42. # Handle the remaining
  43. remain.each do |key, array|
  44. @points = @points + (100 * key) if array.length >= 3
  45. end
  46.  
  47. puts "#{@points}"
  48. end
  49.  
  50. def calculate_specials(value, nums = [], high_score, low_score)
  51. if nums.length >= 4
  52. @points = @points + high_score
  53. leftovers = nums.length - 3
  54. @points = @points + (low_score * leftovers)
  55. elsif nums.length == 3
  56. @points = @points + high_score
  57. else
  58. @points = @points + (low_score * nums.length)
  59. end
  60.  
  61. @points
  62. end
  63. end
  64.  
  65.  
  66. # Just testing to make sure everything works...
  67.  
  68. test = Dice.new
  69. testb = Dice.new
  70. test.roll(5)
  71. testb.roll(5)
  72. test.score
  73. testb.score
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement