-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
184 lines (150 loc) · 5.63 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import random
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return f"{self.rank['rank']} of {self.suit}"
class Deck:
def __init__(self):
self.cards = []
suits = ["spades", "clubs", "hearts", "diamonds"]
ranks = [
{"rank": "A", "value": 11},
{"rank": "2", "value": 2},
{"rank": "3", "value": 3},
{"rank": "4", "value": 4},
{"rank": "5", "value": 5},
{"rank": "6", "value": 6},
{"rank": "7", "value": 7},
{"rank": "8", "value": 8},
{"rank": "9", "value": 9},
{"rank": "10", "value": 10},
{"rank": "J", "value": 10},
{"rank": "Q", "value": 10},
{"rank": "K", "value": 10},
]
for suit in suits:
for rank in ranks:
self.cards.append(Card(suit, rank))
def shuffle(self):
if len(self.cards) > 1:
random.shuffle(self.cards)
def deal(self, number):
cards_dealt = []
for _ in range(number):
if len(self.cards) > 0:
card = self.cards.pop()
cards_dealt.append(card)
return cards_dealt
class Hand:
def __init__(self, dealer=False):
self.cards = []
self.value = 0
self.dealer = dealer
def add_card(self, card_list):
self.cards.extend(card_list)
def calculate_value(self):
self.value = 0
has_ace = False
for card in self.cards:
card_value = int(card.rank["value"])
self.value += card_value
if card.rank["rank"] == "A":
has_ace = True
if has_ace and self.value > 21:
self.value -= 10
def get_value(self):
self.calculate_value()
return self.value
def is_blackjack(self):
return self.get_value() == 21
def display(self, show_all_dealers_cards=False):
print(f'''{"Dealer's" if self.dealer else "Your"} hand:''')
for index, card in enumerate(self.cards):
if index == 0 and self.dealer and not show_all_dealers_cards and not self.is_blackjack():
print("hidden")
else:
print(card)
if not self.dealer:
print("Value:", self.get_value())
print()
class Game:
def play(self):
game_number = 0
games_to_play = 0
while games_to_play <= 0:
try:
games_to_play = int(input("How many games do you want to play? "))
except ValueError:
print("You must enter a number.")
while game_number < games_to_play:
game_number += 1
deck = Deck()
deck.shuffle()
player_hand = Hand()
dealer_hand = Hand(dealer=True)
for _ in range(2):
player_hand.add_card(deck.deal(1))
dealer_hand.add_card(deck.deal(1))
print()
print("*" * 30)
print(f"Game {game_number} of {games_to_play}")
print("*" * 30)
player_hand.display()
dealer_hand.display()
if self.check_winner(player_hand, dealer_hand):
continue
choice = ""
while player_hand.get_value() < 21 and choice not in ["s", "stand"]:
choice = input("Please choose 'Hit' or 'Stand': ").lower()
print()
while choice not in ["h", "s", "hit", "stand"]:
choice = input("Please enter 'Hit' or 'Stand' (or H/S): ").lower()
print()
if choice in ["hit", "h"]:
player_hand.add_card(deck.deal(1))
player_hand.display()
if self.check_winner(player_hand, dealer_hand):
continue
player_hand_value = player_hand.get_value()
dealer_hand_value = dealer_hand.get_value()
while dealer_hand_value < 17:
dealer_hand.add_card(deck.deal(1))
dealer_hand_value = dealer_hand.get_value()
dealer_hand.display(show_all_dealers_cards=True)
if self.check_winner(player_hand, dealer_hand):
continue
print("Final Results")
print("Your hand:", player_hand_value)
print("Dealer's hand:", dealer_hand_value)
self.check_winner(player_hand, dealer_hand, True)
print("\nThanks for playing!")
def check_winner(self, player_hand, dealer_hand, game_over=False):
if not game_over:
if player_hand.get_value() > 21:
print("You busted. Dealer wins! 😭")
return True
elif dealer_hand.get_value() > 21:
print("Dealer busted. You win! 😀")
return True
elif dealer_hand.is_blackjack() and player_hand.is_blackjack():
print("Both players have blackjack! Tie! 😐")
return True
elif player_hand.is_blackjack():
print("You have blackjack. You win! 😀")
return True
elif dealer_hand.is_blackjack():
print("Dealer has blackjack. Dealer wins! 😭")
return True
else:
if player_hand.get_value() > dealer_hand.get_value():
print("You win! 😀")
elif player_hand.get_value() == dealer_hand.get_value():
print("Tie! 😐")
else:
print("Dealer wins. 😭")
return True
return False
g = Game()
g.play()