-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand.py
More file actions
55 lines (43 loc) · 1.54 KB
/
hand.py
File metadata and controls
55 lines (43 loc) · 1.54 KB
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
from card import Card
class Hand:
def __init__(self) -> None:
self.cards = []
self.score = 0
self.ace_score = 0
def add_card(self, card: Card) -> None:
self.cards.append(card)
self.update_scores(card)
def update_scores(self, card: Card) -> None:
self.score += card.value
if self.ace_score != 0:
self.ace_score += card.value
elif card.is_ace:
self.ace_score = self.score + 10
def is_21(self) -> bool:
return self.score == 21 or self.ace_score == 21
def is_blackjack(self) -> bool:
return self.is_21() and len(self.cards) == 2
def is_bust(self) -> bool:
return self.score > 21
def max_score(self) -> int:
return max(self.score, self.ace_score) if self.ace_score <= 21 else self.score
def str_scores(self) -> str:
if self.ace_score != 0 and self.ace_score <= 21:
return "{} or {}".format(self.score, self.ace_score)
return str(self.score)
def __str__(self) -> str:
return " ".join(str(card) for card in self.cards)
class DealerHand(Hand):
def __init__(self) -> None:
super().__init__()
self.face_down = True
def str_scores(self) -> str:
if not self.face_down:
return super().str_scores()
if self.cards[0].is_ace:
return "1 or 11"
return str(self.cards[0].value)
def __str__(self) -> str:
if not self.face_down:
return super().__str__()
return str(self.cards[0])