Guest User

Untitled

a guest
Nov 22nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. # Blackjack Project
  2.  
  3. import random
  4.  
  5. def symbol_repl(card_str):
  6. return card_str.replace('S','\u2660').replace('H','\u2665').replace('C','\u2663').replace('D','\u2666')
  7.  
  8. def create_deck():
  9. deck =[]
  10. suits = ['C','D','H','S']
  11. card_val = ['A','J','K','Q','T','9','8','7','6','5','4','3','2']
  12. for suit in suits:
  13. for card in card_val:
  14. deck += [symbol_repl(card + suit)]
  15. return deck
  16.  
  17. def eval(hand):
  18. score = 0
  19. ace = False
  20. card_val = {'A':11,'K':10,'Q':10,'J':10,'T':10,'9':9,'8':8,'7':7,'6':6,'5':5,'4':4,'3':3,'2':2}
  21.  
  22. for card in hand:
  23. score += card_val[card[0]]
  24.  
  25. for card in hand:
  26. if('A' in card):
  27. ace = True
  28.  
  29. if(score > 21 and ace == True):
  30. score -= 10
  31.  
  32. return score
  33.  
  34. # def count(players):
  35. # players = int(input("How many players are there? (1-3): "))
  36. # if(players == 1):
  37. # player1_hand = []
  38. # elif(players == 2):
  39. # player2_hand = []
  40. # elif(players == 3):
  41. # player3_hand = []
  42.  
  43. deck = create_deck()
  44. random.shuffle(deck)
  45.  
  46. dealer_hand = []
  47. player1_hand = []
  48. cardnum = 0
  49.  
  50. replay = True
  51.  
  52. while(replay):
  53. # initial card deal
  54. player1_hand += [deck[cardnum]]
  55. cardnum += 1
  56. dealer_hand += [deck[cardnum]]
  57. cardnum += 1
  58. player1_hand += [deck[cardnum]]
  59. cardnum += 1
  60. dealer_hand += [deck[cardnum]]
  61. cardnum += 1
  62.  
  63. print("Player 1's hand:", player1_hand[0], player1_hand[1], "\nDealer's hand: ??", dealer_hand[1])
  64. print("\n***************", "\nYour score:", eval(player1_hand))
  65.  
  66. # split
  67.  
  68. # if(player1_hand[0][0] == [1][0]):
  69. # split = input("Do you want to split your hand? (y/n): ")
  70. # if(split == 'y'):
  71. #TODO: work on splitting hands
  72.  
  73. # blackjack detection
  74. blackjack = False
  75. if(eval(player1_hand) == 21):
  76. print("Natural blackjack!")
  77. blackjack = True
  78. elif(eval(dealer_hand) == 21):
  79. print("Dealer blackjack!")
  80. blackjack = True
  81.  
  82. # player hit or stand
  83. while(blackjack == False and eval(player1_hand) <= 21):
  84. hit_stand = input("Do you want to hit or stand?: ").lower()
  85. print("\n***************")
  86. if(hit_stand[0] == 'h'):
  87. player1_hand += [deck[cardnum]]
  88. cardnum += 1
  89.  
  90. if(eval(player1_hand) > 21):
  91. print("Player 1's hand:", player1_hand, "\n***************")
  92. break
  93.  
  94. print("Player 1's hand:", player1_hand, "\nDealer's hand: ??", dealer_hand[1] )
  95. print("\n***************", "\nYour score:", eval(player1_hand))
  96.  
  97. elif(hit_stand[0] == 's'):
  98. break
  99.  
  100. # dealer hit or stand
  101. while(blackjack == False and eval(dealer_hand) < 17 ):
  102. print("Dealer hits.")
  103. dealer_hand += [deck[cardnum]]
  104. cardnum += 1
  105. print("Dealer's hand:", dealer_hand, "\n***************")
  106.  
  107. # hand evaluations
  108. if(eval(player1_hand) > eval(dealer_hand) and eval(player1_hand) <= 21):
  109. print("You win!", "\nYour hand:", player1_hand,"\nDealer's hand:", dealer_hand)
  110. elif(eval(player1_hand) < eval(dealer_hand) and eval(dealer_hand) <= 21):
  111. print("Dealer wins!","\nDealer's hand:", dealer_hand)
  112. elif(eval(player1_hand) > 21 and eval(dealer_hand) <= 21):
  113. print("You bust! Dealer won!", "\nYour hand:", player1_hand,"\nDealer's hand:", dealer_hand)
  114. elif(eval(player1_hand) <= 21 and eval(dealer_hand) > 21):
  115. print("Player hand:", player1_hand, "\nYou win! Dealer bust!")
  116. elif(eval(player1_hand) > 21 and eval(dealer_hand) > 21):
  117. print("Both busted!")
  118. elif(eval(player1_hand) == eval(dealer_hand)):
  119. print("Player's hand:", player1_hand,"\nDealer's hand:", dealer_hand, "\nPush!")
  120.  
  121. replay = input("Do you want to play again? (y/n): ").lower()
  122. print("\n***************")
  123. deck = create_deck()
  124. random.shuffle(deck)
  125.  
  126. dealer_hand = []
  127. player1_hand = []
  128. cardnum = 0
  129.  
  130. if(replay != 'y'):
  131. replay = False
  132.  
  133. #TODO: figure out splitting cards
  134. #TODO: figure out multiple players
Add Comment
Please, Sign In to add comment