forked from ZipCodeCore/PythonFundamentals.Labs.BlackJack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
62 lines (49 loc) · 1.6 KB
/
classes.py
File metadata and controls
62 lines (49 loc) · 1.6 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
56
57
58
59
60
61
62
import random
'''
Classes needed for Black Jack Game
'''
suits = ["\u2663", "\u2665", "\u2666", "\u2660"]
ranks = ('2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A')
values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'J': 10, 'K': 10, 'Q': 10, 'A': 11}
class Card:
def __init__(self, suit: str, rank: str):
self.suit = suit
self.rank = rank
def __str__(self):
return "[" + self.rank + self.suit + "]"
def __repr__(self):
return "[" + self.rank + self.suit + "]"
class Deck:
def __init__(self):
self.deck = []
for suit in suits:
for rank in ranks:
self.deck.append(Card(suit, rank))
self.deck.append(Card(suit, rank))
self.deck.append(Card(suit, rank))
self.deck.append(Card(suit, rank))
def shuffle(self):
random.shuffle(self.deck)
def deal(self):
singe_card = self.deck.pop()
return singe_card
def __str__(self):
return f"{self.deck}"
class Hand:
def __init__(self, name: str):
self.name = name
self.cards = []
self.count = 0
self.aces = 0
def draw_card(self, card: Card):
self.cards.append(card)
self.count += values[card.rank]
if card.rank == 'A':
self.aces += 1
self.ace()
def ace(self):
while self.count > 21 and self.aces:
self.count -= 10
self.aces -= 1
def __str__(self):
return f"{self.name},{self.cards}, {self.count}"