-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
120 lines (89 loc) · 2.83 KB
/
classes.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
from cards import blackjack_cards
class Deck:
def __init__(self, amount=1):
self.cards = []
for card in (x for _ in range(4) for x in blackjack_cards):
self.cards.append(Card(card['name'], card['value']))
self.cards *= amount
def deal_card(self):
return self.cards.pop()
class Card:
def __init__(self, name, value):
self.name = name
self.value = value
def get_card_value(self):
return self.value
def get_card_name(self):
return self.name
def set_card_value(self, value):
self.value = value
def is_ace(self):
if self.get_card_name() == 'Ace':
return True
return False
class Hand:
def __init__(self, cards=None):
if cards:
self.cards = cards
else:
self.cards = []
def get_hand_value(self):
value = 0
for card in self.cards:
value += card.get_card_value()
return value
def get_amount_of_cards(self):
return len(self.cards)
def get_card_names(self):
names = []
for card in self.cards:
names.append(card.get_card_name())
return names
def add_card_to_hand(self, card):
self.cards += [card]
def empty_hand(self):
self.cards = []
def check_if_hand_is_not_bust(self):
if self.get_hand_value() > 21:
return False
return True
def check_hand_for_matching_cards(self):
if len(self.cards) > 1 and self.cards[0].get_card_name() == self.cards[1].get_card_name():
return True
return False
def split_hand_when_duplicate_cards(self):
return Hand([self.cards[0]]), Hand([self.cards[1]])
def has_ace(self):
if 'Ace' in self.get_card_names():
return True
return False
def get_ace_card(self):
for card in self.cards:
if card.get_card_name() == 'Ace':
return card
class Player:
def __init__(self, name):
self.name = name
self.money = 1000
self.hand = Hand()
self.extra_hand = Hand() # used when player splits hand in case of matching cards
self.playing = True
def set_money(self, amount):
self.money = amount
def get_money_amount(self):
return self.money
def get_status(self):
return self.playing
def set_status(self, status):
self.playing = status
def get_name(self):
return self.name
def get_highest_hand_value(self):
hand_value = self.hand.get_hand_value()
extra_hand_value = self.extra_hand.get_hand_value()
if hand_value > extra_hand_value:
return hand_value
return extra_hand_value
def has_extra_hand(self):
if self.extra_hand.get_card_names():
return True