-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
127 lines (100 loc) · 3.35 KB
/
blackjack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import random
import os
# Global constants
SUITS = ['♠', '♣', '♦', '♥']
RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
CARD_VALUES = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11}
# Card class
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return self.rank + self.suit
# Deck class
class Deck:
def __init__(self):
self.cards = []
for suit in SUITS:
for rank in RANKS:
card = Card(rank, suit)
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
def deal_card(self):
return self.cards.pop()
# Hand class
class Hand:
def __init__(self):
self.cards = []
self.total = 0
self.aces = 0
def add_card(self, card):
self.cards.append(card)
self.total += CARD_VALUES[card.rank]
if card.rank == 'A':
self.aces += 1
def adjust_for_ace(self):
while self.total > 21 and self.aces > 0:
self.total -= 10
self.aces -= 1
def is_busted(self):
return self.total > 21
def is_blackjack(self):
return len(self.cards) == 2 and self.total == 21
def __str__(self):
return ', '.join([str(card) for card in self.cards]) + ' (' + str(self.total) + ')'
# Main game logic
def blackjack_game():
os.system('cls' if os.name == 'nt' else 'clear') # Clear the terminal screen
print("Welcome to Blackjack!")
# Create and shuffle the deck
deck = Deck()
deck.shuffle()
# Create player's and dealer's hands
player_hand = Hand()
dealer_hand = Hand()
# Deal initial cards
player_hand.add_card(deck.deal_card())
dealer_hand.add_card(deck.deal_card())
player_hand.add_card(deck.deal_card())
dealer_hand.add_card(deck.deal_card())
# Player's turn
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print("Player's Hand:", player_hand)
print("Dealer's Hand:", dealer_hand.cards[0])
if player_hand.is_blackjack():
print("Blackjack! You win!")
return
if player_hand.is_busted():
print("Busted! You lose.")
return
choice = input("Do you want to hit or stand? (h/s): ")
if choice.lower() == 'h':
player_hand.add_card(deck.deal_card())
player_hand.adjust_for_ace()
elif choice.lower() == 's':
break
# Dealer's turn
while dealer_hand.total < 17:
dealer_hand.add_card(deck.deal_card())
dealer_hand.adjust_for_ace()
# Determine the winner
os.system('cls' if os.name == 'nt' else 'clear')
print("Player's Hand:", player_hand)
print("Dealer's Hand:", dealer_hand)
if dealer_hand.is_busted():
print("Dealer busts! You win!")
elif dealer_hand.total > player_hand.total:
print("Dealer wins!")
elif dealer_hand.total < player_hand.total:
print("You win!")
else:
print("It's a tie!")
# Start the game
while True:
blackjack_game()
play_again = input("Do you want to play again? (y/n): ")
if play_again.lower() != 'y':
break