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