Advertisement
Guest User

Untitled

a guest
Sep 12th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. SCORE = { "A" => 11, "J" => 10, "Q" => 10, "K" => 10, "2" => 2, "3" => 3,
  2. "4" => 4, "5" => 5, "6" => 6, "7" => 7, "8" => 8, "9" => 9,
  3. "10" => 10 }
  4.  
  5. SUITS = ['H', 'D', 'S', 'C']
  6. VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
  7.  
  8. BUST_POINT = 21
  9. DEALER_HEDGE = 17
  10. WAIT_TIME = 1.5
  11.  
  12. def initialize_deck
  13. SUITS.product(VALUES).shuffle
  14. end
  15.  
  16. def update_total(plyr_hand)
  17. values = plyr_hand.map { |card| card[1] }
  18. sum = 0
  19.  
  20. values.each do |value|
  21. sum += SCORE[value]
  22. end
  23.  
  24. values.select { |value| value == "A" }.count.times do
  25. sum -= 10 if sum > BUST_POINT
  26. end
  27.  
  28. sum
  29. end
  30.  
  31. def update_deck(array, draw)
  32. array.select! { |e| !draw.include?(e) }
  33. end
  34.  
  35. def busted?(plyr_tot)
  36. plyr_tot > BUST_POINT
  37. end
  38.  
  39. def check_winner(player_tot, dealer_tot)
  40. if player_tot > BUST_POINT
  41. 'player busted'
  42. elsif dealer_tot > BUST_POINT
  43. 'dealer busted'
  44. elsif dealer_tot < player_tot
  45. 'Player'
  46. elsif player_tot < dealer_tot
  47. 'Dealer'
  48. else
  49. 'tie'
  50. end
  51. end
  52.  
  53. def display_winner(player_tot, dealer_tot)
  54. result = check_winner(player_tot, dealer_tot)
  55.  
  56. case result
  57. when 'player busted'
  58. puts 'You busted! Dealer wins!'
  59. when 'dealer busted'
  60. puts 'Dealer busted! You win!'
  61. when 'Player'
  62. puts 'You win!'
  63. when 'Dealer'
  64. puts 'Dealer wins!'
  65. when 'tie'
  66. puts "It's a tie"
  67. end
  68. end
  69.  
  70. # def display_player_hand(player_hand)
  71. # end
  72. #
  73. # def display_dealer_hand(dealer_hand)
  74. # end
  75.  
  76. def play_again?
  77. choice = ''
  78. loop do
  79. puts 'Do you want to play again? y/n '
  80. choice = gets.chomp.downcase
  81. break if %w(y n).include?(choice)
  82. puts "invalid response. enter 'y' or 'n'"
  83. end
  84. choice == 'y'
  85. end
  86.  
  87. def update_score(hsh, string)
  88. hsh[string] += 1
  89. end
  90.  
  91. def grand_winner?(player_score, dealer_score)
  92. player_score == 5 || dealer_score == 5
  93. end
  94.  
  95. def display_grand_winner(player_score, dealer_score)
  96. if player_score == 5
  97. puts "Game over! You have won 5 games!"
  98. elsif dealer_score == 5
  99. puts "Game over! Dealer has won 5 games!"
  100. end
  101. end
  102.  
  103. def request_player_choice
  104. player_choice = ''
  105. loop do
  106. print "Enter 'h' to hit or 's' to stay: "
  107. player_choice = gets.chomp.downcase
  108. break if %w(h s).include?(player_choice)
  109. puts "invalid choice, please enter 'h' or 's'"
  110. end
  111. player_choice
  112. end
  113. # Game Play
  114.  
  115. puts "Welcome to the game of 21!"
  116. sleep WAIT_TIME
  117. # intialize counters
  118. player_score = 0
  119. dealer_score = 0
  120.  
  121. loop do
  122. deck = initialize_deck
  123. system 'cls'
  124.  
  125. loop do
  126. puts "...issuing two cards to player"
  127. sleep WAIT_TIME
  128. player_hand = deck.sample(2)
  129. update_deck(deck, player_hand)
  130.  
  131. puts "...issuing two cards to dealer"
  132. sleep WAIT_TIME
  133. dealer_hand = deck.sample(2)
  134. update_deck(deck, dealer_hand)
  135.  
  136. player_total = update_total(player_hand)
  137. dealer_total = update_total(dealer_hand)
  138.  
  139. puts "Dealer has: #{dealer_hand.sample[1]} and unknown card"
  140. puts "You have: #{player_hand[0][1]} and #{player_hand[1][1]}"
  141.  
  142. loop do
  143. player_choice = request_player_choice
  144.  
  145. if player_choice == 'h'
  146. player_hand += [deck.sample]
  147. puts 'You chose to hit'
  148. puts "Your cards are now #{player_hand}"
  149. update_deck(deck, player_hand)
  150. player_total = update_total(player_hand)
  151. end
  152.  
  153. break if busted?(player_total) || player_choice == 's'
  154. end
  155.  
  156. if busted?(player_total)
  157. dealer_score += 1
  158. display_winner(player_total, dealer_total)
  159. break
  160. else
  161. puts 'You chose to stay!'
  162. puts "Your current total is #{player_total}"
  163. end
  164.  
  165. puts 'Dealer turn...'
  166. sleep WAIT_TIME
  167.  
  168. while dealer_total < DEALER_HEDGE
  169. puts 'Dealer hits!'
  170. dealer_hand += [deck.sample]
  171. puts "Dealer's cards are now #{dealer_hand}"
  172. update_deck(deck, dealer_hand)
  173. dealer_total = update_total(dealer_hand)
  174.  
  175. break if busted?(dealer_total)
  176. end
  177.  
  178. if busted?(dealer_total)
  179. player_score += 1
  180. display_winner(player_total, dealer_total)
  181. break
  182. else
  183. puts "Dealer stays at #{dealer_total}"
  184. end
  185.  
  186. # result time
  187. puts '======================='
  188. puts "Dealer has #{dealer_hand}, for a total of: #{dealer_total}"
  189. puts "Player has #{player_hand}, for a total of: #{player_total}"
  190. puts '======================='
  191.  
  192. result = check_winner(player_total, dealer_total)
  193. player_score += 1 if result == "Player"
  194. dealer_score += 1 if result == "Dealer"
  195.  
  196. display_winner(player_total, dealer_total)
  197.  
  198. if grand_winner?(player_score, dealer_score)
  199. display_grand_winner(player_score, dealer_score)
  200. end
  201.  
  202. break
  203. end
  204. break if grand_winner?(player_score, dealer_score)
  205. break unless play_again?
  206. end
  207.  
  208. puts "Thank you for playing #{BUST_POINT}!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement