-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.py
58 lines (46 loc) · 1.4 KB
/
deck.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
from constants import VALUES, SUITS
from card import Card
from random import shuffle
class CardAlreadyInDeckError(Exception):
def __init__(
self,
card: Card,
message: str = "Given card is already present in deck",
) -> None:
super().__init__(message, repr(card))
class DeckAlreadyEmptyError(IndexError):
def __init__(
self,
message: str = "The deck is already empty, cannot draw card",
) -> None:
super().__init__(message)
class Deck:
def __init__(self, empty: bool = False, shuffle: bool = True):
"""
Class representing a deck of cards
"""
self._deck: list[Card] = (
[]
if empty
else [Card(value, suit) for value in VALUES for suit in SUITS]
)
if shuffle:
self.shuffle_deck()
@property
def deck(self) -> list[Card]:
return self._deck
def __str__(self) -> str:
return " ".join([str(card) for card in self.deck])
def add_card(self, card: Card):
if card in self.deck:
raise CardAlreadyInDeckError(card)
self.deck.append(card)
def shuffle_deck(self) -> None:
shuffle(self.deck)
def deal(self) -> Card:
try:
return self.deck.pop()
except IndexError:
raise DeckAlreadyEmptyError
def __len__(self) -> int:
return len(self.deck)