-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand.py
77 lines (63 loc) · 2.04 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
"""
The class Hand
"""
from resolver import resolve
class Hand:
"""
Hand methods and operations
"""
def __init__(self, card1, card2):
self.cards = [card1, card2]
def options(self):
"""
which possible moves can i make?
"""
options_lst = []
sum_cards = sum(map(resolve, self.cards)) # with ACE
options_lst.append(sum_cards)
# no need to worry about 2 or more occurences of Ace in hand - sum of both two will be
# more than 22, and it gets out of the way by the final filtration anyway
if 1 in self.cards:
options_lst.append(sum_cards+10)
#final filter
filtered = filter(lambda score: score <= 21, options_lst)
return list(filtered)
def options_print(self):
"""
print possible moves
"""
str_card_deck = [str(int) for int in self.cards]
card_deck = " | ".join(str_card_deck)
possible_options = [str(int) for int in self.options()]
possible_options_filtered = []
for i in possible_options:
if i not in possible_options_filtered:
possible_options_filtered.append(i)
str_possible_options_filtered = " or ".join(possible_options_filtered)
print(f"Your cards on hand are:\n{card_deck} \nand hand total(s): \n{str_possible_options_filtered}")
def add(self, card):
"""
adds a card to the hand
"""
self.cards.append(card)
def is_win(self):
"""
returns T/F
returns True if user won
returns False if user did not win
"""
return 21 in self.options()
def is_bust(self):
"""
did user bust?
"""
return not self.options()
def total(self):
"""
what is the highest option in your hand?
"""
options = self.options()
if not options:
return options
else:
return max(options)