Guest User

Untitled

a guest
Apr 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. import random
  2.  
  3. play = False
  4.  
  5. suits = ('H','S','C','D')
  6.  
  7. card_num = ('A','2','3','4','5','6','7','8','9','10','J','Q','K')
  8.  
  9. card_val = {'A':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10}
  10.  
  11. bet = 1
  12.  
  13. chips = 100
  14.  
  15. restart = "Press d to deal again or q to quit."
  16.  
  17.  
  18. class Card:
  19.  
  20. def __init__(self,suit,c_num):
  21. self.suit = suit
  22. self.c_num = c_num
  23.  
  24. def __str__(self):
  25. return self.c_num + self.suit
  26.  
  27. def get_suit(self):
  28. return self.suit
  29.  
  30. def get_num(self):
  31. return self.c_num
  32.  
  33. def draw(self):
  34. # Show the card drawn
  35. print(self.c_num + self.suit)
  36.  
  37. class Hand:
  38.  
  39. def __init__(self):
  40. self.cards = []
  41. # Hand value
  42. self.h_val = 0
  43.  
  44. # establish presence of ace since value can be 1 or 11
  45. self.ace = False
  46.  
  47. def __str__(self):
  48. # Return current hand
  49. cur_hand = ""
  50.  
  51. for card in self.cards:
  52. card_name = card.__str__()
  53. cur_hand += " " + card_name
  54.  
  55. return "Your hand is %s" % cur_hand
  56.  
  57. def add_card(self,card):
  58. self.cards.append(card)
  59. # Checks to see if the card is an Ace
  60. if card.c_num == "A":
  61. self.ace = True
  62. self.h_val += card_val[card.c_num]
  63.  
  64. def calc_hand(self):
  65. # Determines the better hand with ace as 1 or 11
  66. if (self.ace == True and self.h_val < 12):
  67. return self.h_val + 10
  68. else:
  69. return self.h_val
  70.  
  71. def draw_card(self,hidden):
  72. # Hide one card for the dealer
  73. if hidden == True and play == True:
  74. start_cards = 1
  75. else:
  76. start_cards = 0
  77. for x in range(start_cards,len(self.cards)):
  78. self.cards[x].draw()
  79.  
  80.  
  81.  
  82. class Deck:
  83.  
  84. def __init__(self):
  85. # Create a deck
  86. self.deck = []
  87. for suit in suits:
  88. for num in card_num:
  89. self.deck.append(Card(suit,num))
  90.  
  91. def shuffle(self):
  92. # Shuffle the deck
  93. random.shuffle(self.deck)
  94.  
  95. def deal(self):
  96. # Deal the cards from the shuffled deck
  97. one_card = self.deck.pop()
  98. return one_card
  99.  
  100. def __str__(self):
  101. deck_cards = ""
  102. for card in self.cards:
  103. deck_cards += " " + deck_cards.__str__
  104. return("The deck has " + deck_cards)
  105.  
  106. def place_bet():
  107.  
  108. global bet
  109. bet = 0
  110.  
  111. print("How many chips would you like to bet?")
  112.  
  113. while bet == 0:
  114. player_bet = int(input())
  115. if player_bet > 0 and player_bet <= chips:
  116. bet = player_bet
  117. else:
  118. print("This is a bet you can't afford! You only have " + str(chips) + " chips!")
  119.  
  120. def deal():
  121. # Deal out the cards for the hand
  122.  
  123. global result,play,deck,players_hand,dealers_hand,chips,bet
  124.  
  125. # Create a deck and shuffle
  126. deck = Deck()
  127. deck.shuffle()
  128.  
  129. place_bet()
  130.  
  131. # Create and deal out hands
  132. players_hand = Hand()
  133. dealers_hand = Hand()
  134.  
  135. players_hand.add_card(deck.deal())
  136. players_hand.add_card(deck.deal())
  137.  
  138. dealers_hand.add_card(deck.deal())
  139. dealers_hand.add_card(deck.deal())
  140.  
  141. result = "Would you like to 'Hit' or 'Stand'? Press s or h."
  142.  
  143. if play == True:
  144. print('You fold.')
  145. chips -= bet
  146.  
  147. play = True
  148. game_step()
  149.  
  150. def hit():
  151.  
  152. # Decide whether the player hits or stays
  153. global play,chips,deck,players_hand,dealers_hand,result,bet
  154.  
  155. if play:
  156. if players_hand.calc_hand() <= 21:
  157. players_hand.add_card(deck.deal())
  158.  
  159. print("The player's hand is %s" % players_hand)
  160.  
  161. if players_hand.calc_hand() > 21:
  162. result = 'You busted! ' + restart
  163.  
  164. chips -= bet
  165. play = False
  166.  
  167. else:
  168. result = "Sorry you can't hit." + restart
  169.  
  170. game_step()
  171.  
  172.  
  173. def stand():
  174.  
  175. # Once the player stands, the dealer plays
  176. global play,chips,deck,players_hand,dealers_hand,result,bet
  177.  
  178. if play == False:
  179. if players_hand.calc_hand() > 0:
  180. result = "Sorry, you can't stand!"
  181. else:
  182.  
  183. # Dealer rule of 17
  184. while dealers_hand.calc_hand() < 17:
  185. dealers_hand.add_card(deck.deal())
  186.  
  187. # When dealer busts
  188. if dealers_hand.calc_hand() > 21:
  189. result = 'Dealer busts! You win!' + restart
  190. chips += bet
  191. play = False
  192.  
  193. # When player beats dealer
  194. elif dealers_hand.calc_hand() < players_hand.calc_hand():
  195. result = 'You beat the dealer, you win!' + restart
  196. chips += bet
  197. play = False
  198.  
  199. # Tie with player and dealer
  200. elif dealers_hand.calc_hand() == players_hand.calc_hand():
  201. result = 'Tied up, push!' + restart
  202. play = False
  203.  
  204. # When dealer wins
  205. else:
  206. result = 'Dealer Wins!' + restart
  207. chips -= bet
  208. play = False
  209. game_step()
  210.  
  211.  
  212. def game_step():
  213.  
  214. # Show player's hand
  215. print("")
  216. print("The player's hand is: "),
  217. players_hand.draw_card(hidden = False)
  218.  
  219. print("The player's hand value is: " + str(players_hand.calc_hand()))
  220.  
  221. # Show Dealer's hand
  222. print("The dealer's hand is: "),
  223. dealers_hand.draw_card(hidden=True)
  224.  
  225. # When game is over
  226. if play == False:
  227. print(" --- for a total of " + str(dealers_hand.calc_hand()))
  228. print("Chip Total: " + str(chips))
  229. else:
  230. print(" with another card hidden.")
  231.  
  232. # Print result of hit or stand.
  233. print(result)
  234.  
  235. player_input()
  236.  
  237. def game_exit():
  238. print('Thanks for playing!')
  239. exit()
  240.  
  241. def player_input():
  242. # Convert input to upper and check for match
  243. answ = input().upper()
  244.  
  245. if answ == 'H':
  246. hit()
  247. elif answ == 'S':
  248. stand()
  249. elif answ == 'D':
  250. if chips > 0:
  251. deal()
  252. else:
  253. print("Sorry, you're out of chips!")
  254. game_exit()
  255. elif answ == 'Q':
  256. game_exit()
  257. else:
  258. print("Please input h, s, d, or q: ")
  259. player_input()
  260.  
  261. def intro():
  262. statement = """Let's play some Black Jack! The goal of the game is to get 21!
  263. Aces are worth 1 or 11. Beat the dealer to win some chips!"""
  264. print(statement)
  265.  
  266.  
  267.  
  268. # Create deck and shuffle
  269. deck = Deck()
  270. deck.shuffle()
  271. # Create player and dealer hands
  272. players_hand = Hand()
  273. dealers_hand = Hand()
  274. # Intro to the game
  275. intro()
  276. # Deal out the cards and start
  277. deal()
Add Comment
Please, Sign In to add comment