Advertisement
ifigazsi

Black Jack (simple)

Oct 27th, 2023 (edited)
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | Gaming | 0 0
  1. from random import shuffle
  2.  
  3. #Creating deck and hands
  4. numbers = {1: "A", 11: "J", 12: "Q", 13: "K", 14: "A"}
  5. suits = ["♠", "♦", "♥", "♣"]
  6. deck = []
  7. for suit in suits:
  8.     deck += [[x, suit] for x in range(2, 15)]
  9. shuffle(deck)
  10. player_hand, computer_hand = [], []
  11. players = {"player": player_hand, "computer": computer_hand}
  12.  
  13. def draw_card(player):
  14.     cards = players[player]
  15.     cards.append(deck.pop())
  16.     change_ace_if_needed(player)
  17.  
  18. def value_of_cards(player):
  19.     cards = players[player]
  20.     count = sum([x[0] for x in cards if x[0] < 10])         #NORMAL CARDS
  21.     count += sum([10 for x in cards if 10 <= x[0] < 14])    #PICTURED CARDS
  22.     count += sum([11 for x in cards if x[0] == 14])         #ACES
  23.     return count
  24.  
  25. def show_hand(player):
  26.     cards = players[player]
  27.     print(f"{player.capitalize()}: ", end="")
  28.     for card in cards:
  29.         number, suit = card
  30.         print(f"{numbers.get(number, number)}{suit}", end=" ")
  31.     print(f" => {value_of_cards(player)}")
  32.  
  33. def find_index_of_first_ace(cards):
  34.     numbers = [x[0] for x in cards]
  35.     if 14 in numbers:
  36.         return numbers.index(14)
  37.     return False
  38.  
  39. def change_ace_if_needed(player):
  40.     cards = players[player]
  41.     if value_of_cards(player) > 21:
  42.         index = find_index_of_first_ace(cards)
  43.         if index:
  44.             number, suit = cards[index]
  45.             cards[index] = [1, suit]
  46.  
  47. draw_card("player")
  48. draw_card("computer")
  49.  
  50. #PLAYERS TURN
  51. player_action = "1"
  52. while value_of_cards("player") <= 21 and player_action == "1":
  53.     draw_card("player")
  54.     show_hand("player")
  55.     show_hand("computer")
  56.     player_action = input("1., Hit, 2., Stand\n")
  57.  
  58. #COMPUTERS TURN
  59. while value_of_cards("computer") < 17:
  60.     draw_card("computer")
  61.  
  62. #EVALUATION
  63. if value_of_cards("computer") < value_of_cards("player") <= 21:
  64.     print("You win")
  65. elif value_of_cards("player") <= 21 and value_of_cards("computer") > 21:
  66.     print("You win")
  67. elif value_of_cards("player") == value_of_cards("computer") and value_of_cards("player") <= 21:
  68.     print("Tie")
  69. else:
  70.     print("You lost")
  71. show_hand("player")
  72. show_hand("computer")
  73.  
Tags: Black Jack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement