Advertisement
Guest User

Untitled

a guest
May 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.43 KB | None | 0 0
  1. puts 'Press "r" and then "enter" to roll dice or "q" and then "enter" to quit or "s" and enter to see score'
  2. @ones = []
  3. @twos = []
  4. @threes = []
  5. @fours = []
  6. @fives = []
  7. @sixes = []
  8. @three_of_a_kind = []
  9. @four_of_a_kind = []
  10. @full_house = []
  11. @chance = []
  12. @small_straight = []
  13. @large_straight = []
  14. @yahtzee = []
  15.  
  16. while true
  17. if @ones.first.nil? == true
  18. @ones_score = 0
  19. else
  20. @ones_score = @ones.first
  21. end
  22. if @twos.first.nil? == true
  23. @twos_score = 0
  24. else
  25. @twos_score = @twos.first
  26. end
  27. if @threes.first.nil? == true
  28. @threes_score = 0
  29. else
  30. @threes_score = @threes.first
  31. end
  32. if @fours.first.nil? == true
  33. @fours_score = 0
  34. else
  35. @fours_score = @fours.first
  36. end
  37. if @fives.first.nil? == true
  38. @fives_score = 0
  39. else
  40. @fives_score = @fives.first
  41. end
  42. if @sixes.first.nil? == true
  43. @sixes_score = 0
  44. else
  45. @sixes_score = @sixes.first
  46. end
  47. if @three_of_a_kind.first.nil? == true
  48. @three_of_a_kind_score = 0
  49. else
  50. @three_of_a_kind_score = @three_of_a_kind.first
  51. end
  52. if @four_of_a_kind.first.nil? == true
  53. @four_of_a_kind_score = 0
  54. else
  55. @four_of_a_kind_score = @four_of_a_kind.first
  56. end
  57. if @full_house.first.nil? == true
  58. @full_house_score = "You don't have a full house"
  59. @full_house_points = 0
  60. else
  61. @reduce_full_house = @full_house.reduce :+
  62. if @reduce_full_house == 5
  63. @full_house_points = 25
  64. @full_house_score = "You have a full house"
  65. else
  66. @full_house_score = "You saved something but it's not a full house"
  67. @full_house_points = 0
  68. end
  69. end
  70. if @small_straight.first.nil? == true
  71. @small_straight_score = "You don't have a small straight"
  72. @small_straight_points = 0
  73. else
  74. @reduce_small_straight = @small_straight.reduce :+
  75. if @reduce_small_straight == 3
  76. @small_straight_score = "You have a small straight"
  77. @small_straight_points = 30
  78. else
  79. @small_straight_score = "You saved something but it's not a small straight"
  80. @small_straight_points = 0
  81. end
  82. end
  83. if @large_straight.first.nil? == true
  84. @large_straight_score = "You don't have a large straight"
  85. @large_straight_points = 0
  86. else
  87. @reduce_large_straight = @large_straight.reduce :+
  88. if @reduce_large_straight == 4
  89. @large_straight_score = "You have a large straight"
  90. @large_straight_points = 40
  91. else
  92. @large_straight_score = "You saved something but it's not a large straight"
  93. @large_straight_points = 0
  94. end
  95. end
  96. if @chance.first.nil? == true
  97. @chance_score = "You haven't saved chance yet"
  98. @chance_points = 0
  99. else
  100. @chance_score = "Your chance is #{@chance.first}"
  101. @chance_points = @chance.first
  102. end
  103. if @yahtzee.first.nil? == true
  104. @yahtzee_score = "You don't have Yahtzee :("
  105. @yahtzee_points = 0
  106. else
  107. @yahtzee_points = 50
  108. @yahtzee_score = "You have Yahtzee:)"
  109. end
  110. input = gets.chomp
  111. break if input == 'q'
  112. if input == 's'
  113. @points = (@ones_score.to_i * 1) + (@twos_score.to_i * 2) + (@threes_score.to_i * 3) + (@fours_score.to_i * 4) + (@fives_score.to_i * 5) + (@sixes_score.to_i * 6) + (@three_of_a_kind_score.to_i * 3) + (@four_of_a_kind_score.to_i * 4) + @full_house_points + @chance_points + @small_straight_points + @large_straight_points + @yahtzee_points
  114. puts "You have #{@ones_score} ones"
  115. puts "You have #{@twos_score} twos"
  116. puts "You have #{@threes_score} threes"
  117. puts "You have #{@fours_score} fours"
  118. puts "You have #{@fives_score} fives"
  119. puts "You have #{@sixes_score} sixes"
  120. puts "You have #{@three_of_a_kind_score}'s as your three of a kind"
  121. puts "You have #{@four_of_a_kind_score}'s as your four of a kind"
  122. puts "#{@full_house_score}"
  123. puts "#{@large_straight_score}"
  124. puts "#{@small_straight_score}"
  125. puts "#{@chance_score}"
  126. puts "#{@yahtzee_score}"
  127. puts "Your current total points = #{@points}"
  128. print "High Score: "
  129.  
  130. File.open("high_score.txt", "r") do |f|
  131. f.each_line do |line|
  132. if @points > line.to_i
  133. File.open("high_score.txt", "w") do |file|
  134. file.puts @points
  135. end
  136. end
  137. end
  138. end
  139.  
  140. File.open("high_score.txt", "r") do |f|
  141. f.each_line do |line|
  142. puts line
  143. end
  144. end
  145.  
  146. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  147. puts 'Type "r" and hit enter to roll again'
  148. elsif input == 'r'
  149. @random = 5.times.map{ 1 + Random.rand(6) }
  150. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  151. puts 'New Roll:'
  152. puts @random
  153. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  154. puts 'If you want to reroll some dice type the order number of the dice you wish to keep separated by a comma for example "1,3" would reroll all the dice except for the first and third. If you want to keep the entire roll type "skip". If you want to reroll all the dice type "reroll" and push enter.'
  155. 2.times do |num|
  156. dice_keep = gets.chomp
  157. if dice_keep.to_i != 0
  158. @save_dice = dice_keep.split(',')
  159.  
  160. @save_dice.map! do |dice|
  161. dice.to_i - 1
  162. end
  163.  
  164. @save_dice.map! do |reroll|
  165. @random[reroll]
  166. end
  167. @reroll_amnt = 5 - @save_dice.length
  168. @random = @reroll_amnt.times.map{ 1 + Random.rand(6) }
  169. @save_dice.each do |move|
  170. @random.unshift(move)
  171. end
  172. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  173. puts 'Updated Roll:'
  174. puts @random
  175. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  176. elsif dice_keep == "skip"
  177. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  178. puts @random
  179. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  180. elsif dice_keep == "reroll"
  181. @random = 5.times.map{ 1 + Random.rand(6) }
  182. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  183. puts "Reroll:"
  184. puts @random
  185. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  186. end
  187. if num != 1
  188. puts 'If you want to reroll some dice type the order number of the dice you wish to keep separated by a comma for example "1,3" would reroll all the dice except for the first and third. If you want to keep the entire roll type "skip". If you want to reroll all the dice type "reroll" and push enter.'
  189. end
  190. end
  191. puts 'Save 1s by typing "ones" and pressing enter'
  192. puts 'Save 2s by typing "twos" and pressing enter'
  193. puts 'Save 3s by typing "threes" and pressing enter'
  194. puts 'Save 4s by typing "fours" and pressing enter'
  195. puts 'Save 5s by typing "fives" and pressing enter'
  196. puts 'Save 6s by typing "sixes" and pressing enter'
  197. puts 'Save three of a kind by typing "three of a kind" and pressing enter'
  198. puts 'Save four of a kind by typing "four of a kind" and pressing enter'
  199. puts 'Save a full house by typing "full house" and pressing enter'
  200. puts 'Save a small straight by typing "small straight" and pressing enter'
  201. puts 'Save a large straight by typing "large straight" and pressing enter'
  202. puts 'Save chance by typing "chance" and pressing enter'
  203. puts 'Save Yahtzee by typing "yahtzee" and pressing enter'
  204. save_num = gets.chomp
  205. if save_num == 'ones'
  206. @ones.push(@random.count(1))
  207. puts "You saved #{@random.count(1)} ones"
  208. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  209. @random = nil
  210. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  211. elsif save_num == 'twos'
  212. @twos.push(@random.count(2))
  213. puts "You saved #{@random.count(2)} twos"
  214. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  215. @random = nil
  216. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  217. elsif save_num == 'threes'
  218. @threes.push(@random.count(3))
  219. puts "You saved #{@random.count(3)} threes"
  220. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  221. @random = nil
  222. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  223. elsif save_num == 'fours'
  224. @fours.push(@random.count(4))
  225. puts "You saved #{@random.count(4)} fours"
  226. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  227. @random = nil
  228. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  229. elsif save_num == 'fives'
  230. @fives.push(@random.count(5))
  231. puts "You saved #{@random.count(5)} fives"
  232. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  233. @random = nil
  234. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  235. elsif save_num == 'sixes'
  236. @sixes.push(@random.count(6))
  237. puts "You saved #{@random.count(6)} sixes"
  238. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  239. @random = nil
  240. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  241. elsif save_num == 'three of a kind'
  242. # Note didn't work with times do, look into why
  243. @numbers = (1..6).to_a
  244. @numbers.each do |kind|
  245. if @random.count(kind) == 3
  246. @three_of_a_kind.push(kind)
  247. end
  248. end
  249. puts "You saved #{@three_of_a_kind}'s as your three of a kind"
  250. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  251. @random = nil
  252. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  253. elsif save_num == 'four of a kind'
  254. @numbers = (1..6).to_a
  255. @numbers.each do |kind|
  256. if @random.count(kind) == 4
  257. @four_of_a_kind.push(kind)
  258. end
  259. end
  260. puts "You saved #{@four_of_a_kind}'s as your four of a kind"
  261. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  262. @random = nil
  263. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  264. elsif save_num == 'large straight'
  265. @random_sorted = @random.sort
  266. @random_sorted.each_cons(2) do |cons|
  267. if cons[1] == cons[0] + 1
  268. @large_straight.push(1)
  269. end
  270. end
  271. @reduce_large_straight = @large_straight.reduce :+
  272. if @reduce_large_straight == 4
  273. puts "You saved a large straight."
  274. else
  275. puts "You saved something in the large straight category, but it's not a large straight."
  276. end
  277. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  278. @random = nil
  279. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  280. elsif save_num == 'small straight'
  281. @random_sorted = @random.sort
  282. @random_sorted.each_cons(2) do |cons|
  283. if cons[1] == cons[0] + 1
  284. @small_straight.push(1)
  285. end
  286. end
  287. @reduce_small_straight = @small_straight.reduce :+
  288. if @reduce_small_straight == 3
  289. puts "You saved a small straight."
  290. else
  291. puts "You saved something in the small straight category, but it's not a small straight."
  292. end
  293. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  294. elsif save_num == 'chance'
  295. @reduce_chance = @random.reduce :+
  296. @chance.push(@reduce_chance)
  297. puts "Your chance score is #{@reduce_chance}"
  298. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  299. @random = nil
  300. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  301. elsif save_num == 'yahtzee'
  302. @numbers = (1..6).to_a
  303. @numbers.each do |kind|
  304. if @random.count(kind) == 5
  305. @yahtzee.push(kind)
  306. end
  307. end
  308. if @yahtzee.first.nil? == true
  309. puts "You saved something in your yahtzee category, but it wasn't a yahtzee"
  310. else
  311. puts "You saved your Yahtzee!!!"
  312. end
  313. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  314. @random = nil
  315. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  316. elsif save_num == 'full house'
  317. @numbers = (1..6).to_a
  318. @numbers.each do |kind|
  319. if @random.count(kind) == 3
  320. @full_house.push(3)
  321. binding
  322. end
  323. if @random.count(kind) == 2
  324. @full_house.push(2)
  325. end
  326. end
  327. @reduce_full_house = @full_house.reduce :+
  328. if @reduce_full_house == 5
  329. puts "You got a Full House."
  330. else
  331. puts "You saved something as a full house but sadly it's not a full house."
  332. end
  333. puts '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
  334. @random = nil
  335. puts 'Type "r" and push enter to roll again and "s" and then enter to see score'
  336. end
  337. end
  338. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement