-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand.py
42 lines (34 loc) · 1.16 KB
/
hand.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
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_dealer_card = 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_dealer_card \
and not self.is_blackjack():
print("hidden")
else:
print(card)
if not self.dealer:
print("Value:", self.get_value())
print()