|
1 | 1 | import random |
2 | 2 |
|
3 | | -deck = [ |
4 | | - "A♠️", "2♠️", "3♠️", "4♠️", "5♠️", "6♠️", "7♠️", "8♠️", "9♠️", "10♠️", "J♠️", "Q♠️", "K♠️", |
5 | | - |
6 | | - "A♥️", "2♥️", "3♥️", "4♥️", "5♥️", "6♥️", "7♥️", "8♥️", "9♥️", "10♥️", "J♥️", "Q♥️", "K♥️", |
7 | | - |
8 | | - "A♦️", "2♦️", "3♦️", "4♦️", "5♦️", "6♦️", "7♦️", "8♦️", "9♦️", "10♦️", "J♦️", "Q♦️", "K♦️", |
9 | | - |
10 | | - "A♣️", "2♣️", "3♣️", "4♣️", "5♣️", "6♣️", "7♣️", "8♣️", "9♣️", "10♣️", "J♣️", "Q♣️", "K♣️" |
11 | | -] |
12 | | - |
13 | | -random.shuffle(deck) |
14 | | - |
15 | | -player_hand = [] |
16 | | -dealer_hand = [] |
17 | | - |
18 | | -player_cards = [] |
19 | | -dealer_cards = [] |
20 | | - |
21 | | - |
22 | | -def calculate(hand): |
23 | | - count = 0 |
24 | | - aces = 0 |
25 | | - for value in hand: |
26 | | - count += value |
27 | | - if value == 1: |
28 | | - aces += 1 |
29 | | - |
30 | | - while aces > 0 and count + 10 <= 21: |
31 | | - count += 10 |
| 3 | +while True: |
| 4 | + print("\n" + "="*40) |
| 5 | + print("🃏 WELCOME TO BLACKJACK 21 🃏") |
| 6 | + print("="*40 + "\n") |
| 7 | + |
| 8 | + deck = [ |
| 9 | + "A♠️", "2♠️", "3♠️", "4♠️", "5♠️", "6♠️", "7♠️", "8♠️", "9♠️", "10♠️", "J♠️", "Q♠️", "K♠️", |
| 10 | + "A♥️", "2♥️", "3♥️", "4♥️", "5♥️", "6♥️", "7♥️", "8♥️", "9♥️", "10♥️", "J♥️", "Q♥️", "K♥️", |
| 11 | + "A♦️", "2♦️", "3♦️", "4♦️", "5♦️", "6♦️", "7♦️", "8♦️", "9♦️", "10♦️", "J♦️", "Q♦️", "K♦️", |
| 12 | + "A♣️", "2♣️", "3♣️", "4♣️", "5♣️", "6♣️", "7♣️", "8♣️", "9♣️", "10♣️", "J♣️", "Q♣️", "K♣️" |
| 13 | + ] |
| 14 | + random.shuffle(deck) |
| 15 | + |
| 16 | + player_cards = [] |
| 17 | + player_hand = [] |
| 18 | + dealer_cards = [] |
| 19 | + dealer_hand = [] |
| 20 | + |
| 21 | + # Initial Draw |
| 22 | + for _ in range(2): |
| 23 | + # Player draw |
| 24 | + card = deck.pop() |
| 25 | + player_cards.append(card) |
| 26 | + rank = card[:-2] |
| 27 | + if rank in ['Q','K','J']: |
| 28 | + player_hand.append(10) |
| 29 | + elif rank == 'A': |
| 30 | + player_hand.append(1) |
| 31 | + else: |
| 32 | + player_hand.append(int(rank)) |
| 33 | + |
| 34 | + # Dealer draw |
| 35 | + card = deck.pop() |
| 36 | + dealer_cards.append(card) |
| 37 | + rank = card[:-2] |
| 38 | + if rank in ['Q','K','J']: |
| 39 | + dealer_hand.append(10) |
| 40 | + elif rank == 'A': |
| 41 | + dealer_hand.append(1) |
| 42 | + else: |
| 43 | + dealer_hand.append(int(rank)) |
| 44 | + |
| 45 | + # Calculate player score initially |
| 46 | + player_count = sum(player_hand) |
| 47 | + aces = player_hand.count(1) |
| 48 | + while aces > 0 and player_count + 10 <= 21: |
| 49 | + player_count += 10 |
32 | 50 | aces -= 1 |
33 | 51 |
|
34 | | - return count |
35 | | - |
36 | | -def check(rank): |
37 | | - if rank in ['Q','K','J']: |
38 | | - return 10 |
39 | | - elif rank == 'A': |
40 | | - return 1 |
41 | | - else: |
42 | | - return int(rank) |
43 | | - |
44 | | -def player_draws(): |
45 | | - card = deck.pop() # take a card from deck |
46 | | - |
47 | | - player_cards.append(card) |
48 | | - |
49 | | - rank = card[:-2] # extract Rank |
50 | | - |
51 | | - rank = check(rank) # validate the rank into numbers |
52 | | - |
53 | | - player_hand.append(rank) |
54 | | - |
55 | | - |
56 | | - |
57 | | -def dealer_draws(): |
58 | | - card = deck.pop() |
59 | | - |
60 | | - dealer_cards.append(card) |
61 | | - |
62 | | - rank = card[:-2] |
63 | | - |
64 | | - rank = check(rank) |
65 | | - |
66 | | - dealer_hand.append(rank) |
67 | | - |
68 | | - |
69 | | -player_draws() |
70 | | -dealer_draws() |
71 | | - |
72 | | -player_draws() |
73 | | -dealer_draws() |
74 | | - |
75 | | - |
76 | | -player_count = calculate(player_hand) |
77 | | -dealer_count = calculate(dealer_hand) |
78 | | - |
79 | | - |
80 | | -player_turn = True |
81 | | - |
82 | | -while player_turn: |
83 | | - |
84 | | - choice = input("hit or Stand: ").lower() |
85 | | - |
86 | | - match choice: |
87 | | - case "hit": |
88 | | - player_draws() |
89 | | - player_count = calculate(player_hand) |
| 52 | + # Main Player Loop |
| 53 | + player_bust = False |
| 54 | + while True: |
| 55 | + print(f"🃏 Dealer's visible card: {dealer_cards[0]}") |
| 56 | + print(f"🃏 Your cards: {', '.join(player_cards)} (Score: {player_count})") |
| 57 | + |
| 58 | + choice = input("👉 Hit or Stand? [h/s]: ").strip().lower() |
| 59 | + |
| 60 | + if choice in ['h', 'hit']: |
| 61 | + card = deck.pop() |
| 62 | + player_cards.append(card) |
| 63 | + rank = card[:-2] |
| 64 | + if rank in ['Q','K','J']: |
| 65 | + player_hand.append(10) |
| 66 | + elif rank == 'A': |
| 67 | + player_hand.append(1) |
| 68 | + else: |
| 69 | + player_hand.append(int(rank)) |
90 | 70 |
|
91 | | - print("player cards", player_cards) |
92 | | - print("player_count", player_count) |
93 | | - |
| 71 | + player_count = sum(player_hand) |
| 72 | + aces = player_hand.count(1) |
| 73 | + while aces > 0 and player_count + 10 <= 21: |
| 74 | + player_count += 10 |
| 75 | + aces -= 1 |
| 76 | + |
94 | 77 | if player_count > 21: |
95 | | - print("Bust! player lose!") |
96 | | - player_turn = False |
97 | | - exit() |
98 | | - |
99 | | - case "stand": |
100 | | - player_count = calculate(player_hand) |
101 | | - |
102 | | - print("player cards", player_cards) |
103 | | - print("player_count", player_count) |
104 | | - print("player stands...") |
| 78 | + print(f"🃏 Your cards: {', '.join(player_cards)} (Score: {player_count})") |
| 79 | + print("💥 BUST! You went over 21. Dealer wins!") |
| 80 | + player_bust = True |
| 81 | + break |
| 82 | + elif choice in ['s', 'stand']: |
| 83 | + print("🛑 You chose to stand.") |
105 | 84 | break |
| 85 | + else: |
| 86 | + print("⚠️ Invalid choice. Please enter 'hit' or 'stand'.") |
| 87 | + |
| 88 | + # Dealer Turn |
| 89 | + if not player_bust: |
| 90 | + dealer_count = sum(dealer_hand) |
| 91 | + aces = dealer_hand.count(1) |
| 92 | + while aces > 0 and dealer_count + 10 <= 21: |
| 93 | + dealer_count += 10 |
| 94 | + aces -= 1 |
106 | 95 |
|
107 | | - |
108 | | -while dealer_count < 17: |
109 | | - dealer_draws() |
110 | | - |
111 | | - dealer_count = calculate(dealer_hand) |
112 | | - |
113 | | - |
114 | | -# final result |
115 | | - |
116 | | -if dealer_count > 21: |
117 | | - print("dealer Bust! player wins!") |
118 | | -elif player_count == dealer_count: |
119 | | - print("draw!") |
120 | | -elif player_count > dealer_count: |
121 | | - print("player wins!") |
122 | | -else: |
123 | | - print("dealer wins!") |
124 | | - |
125 | | -print("dealer cards", dealer_cards) |
126 | | -print("player cards", player_cards) |
127 | | - |
128 | | - |
129 | | -print("dealer_count", dealer_count, " \n player_count", player_count) |
130 | | - |
131 | | - |
| 96 | + print(f"\n🃏 Dealer's full cards: {', '.join(dealer_cards)} (Score: {dealer_count})") |
| 97 | + |
| 98 | + while dealer_count < 17: |
| 99 | + print("🃏 Dealer hits...") |
| 100 | + card = deck.pop() |
| 101 | + dealer_cards.append(card) |
| 102 | + rank = card[:-2] |
| 103 | + if rank in ['Q','K','J']: |
| 104 | + dealer_hand.append(10) |
| 105 | + elif rank == 'A': |
| 106 | + dealer_hand.append(1) |
| 107 | + else: |
| 108 | + dealer_hand.append(int(rank)) |
| 109 | + |
| 110 | + dealer_count = sum(dealer_hand) |
| 111 | + aces = dealer_hand.count(1) |
| 112 | + while aces > 0 and dealer_count + 10 <= 21: |
| 113 | + dealer_count += 10 |
| 114 | + aces -= 1 |
| 115 | + print(f"🃏 Dealer's cards: {', '.join(dealer_cards)} (Score: {dealer_count})") |
| 116 | + |
| 117 | + # Determine Winner |
| 118 | + print("\n" + "-"*30) |
| 119 | + print("🎯 FINAL RESULTS 🎯") |
| 120 | + print(f"🧑 Your Score: {player_count}") |
| 121 | + print(f"🤖 Dealer Score: {dealer_count}") |
| 122 | + print("-"*30) |
| 123 | + |
| 124 | + if dealer_count > 21: |
| 125 | + print("🎉 DEALER BUSTED! YOU WIN! 🎉") |
| 126 | + elif player_count == dealer_count: |
| 127 | + print("🤝 IT'S A DRAW! 🤝") |
| 128 | + elif player_count > dealer_count: |
| 129 | + print("🏆 YOU WIN! 🏆") |
| 130 | + else: |
| 131 | + print("💸 DEALER WINS! 💸") |
| 132 | + |
| 133 | + # Replay loop |
| 134 | + while True: |
| 135 | + replay = input("\n🔄 Play again? [y/n]: ").strip().lower() |
| 136 | + if replay in ['y', 'yes', 'n', 'no']: |
| 137 | + break |
| 138 | + print("⚠️ Invalid input. Please enter 'y' or 'n'.") |
| 139 | + |
| 140 | + if replay in ['n', 'no']: |
| 141 | + print("👋 Thanks for playing! Goodbye!") |
| 142 | + break |
0 commit comments