-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand.py
96 lines (77 loc) · 2.44 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
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
from card import Card
class Hand():
def __init__(self, bet=0):
self.card_list = []
self.bet = bet
def play_hand(self, deck):
value = 0 #extra bet
action = 'a'
print(self)
action = input("Would you like to double down? (y/n): ").lower()
if action == 'y':
value += self.bet
self.bet *= 2
print(self)
elif action != 'n':
print("You're too stupid. Guess you can't double down.")
while (action != 's') and (self.sum() <=21):
if action != 'y': # doubled down
print('h: Hit')
print('s: Stand')
action = input().lower()
if (action == 'h') or (action == 'y'):
self.add(deck.deal())
print(self)
elif action !='s':
print('Come on, enter one of the options next time.')
if action == 'y':
break
if self.sum() > 21:
print('Bust!')
return value
def add(self, cards):
for card in cards:
self.card_list.append(card)
def sum(self):
hand_sum = 0
for card in self.card_list:
hand_sum += card.value()
if hand_sum > 21:
for card in self.card_list:
if card.is_ace():
hand_sum -= 10
if hand_sum <= 21:
break
return hand_sum
def get_top(self):
if (len(self.card_list) == 0):
return None
return self.card_list[0]
def discard(self, deck):
deck.discard(self.card_list.copy())
self.card_list.clear()
def can_split(self):
if len(self) != 2:
return False
return self.card_list[0] == self.card_list[1]
def split(self):
split_hand = Hand(bet=self.bet)
split_hand.add(self.card_list.pop())
return split_hand
def __str__(self) -> str:
string = ""
if self.bet > 0:
string += f"${self.bet}- "
for card in self.card_list:
string += str(card) + ", "
string = string[:-2]
return string
def __len__(self) -> int:
return len(self.card_list)
if __name__ == "__main__":
hand = Hand()
hand.add(Card('3', 'Spades'))
hand.add(Card('3', 'Spades'))
hand.add(Card('3', 'Spades'))
hand.add(Card('3', 'Spades'))
print(hand)