-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.py
More file actions
39 lines (32 loc) · 1.01 KB
/
deck.py
File metadata and controls
39 lines (32 loc) · 1.01 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
import random
from typing import Iterator, List
from card import Card
class Deck:
def __init__(self, auto_refresh=True):
self.auto_refresh = auto_refresh
self._cards = self.new_deck()
def __len__(self):
return len(self._cards)
def __iter__(self) -> Iterator[Card]:
return self._cards.__iter__()
def random_card(self) -> Card:
if not self._cards:
if self.auto_refresh:
self._cards = self.new_deck()
else:
raise Exception("Out of cards")
card = random.choice(self._cards)
self._cards.remove(card)
return card
@staticmethod
def new_deck() -> List[Card]:
"""
A new deck of 52 cards
"""
cards = []
for suit in ["♠", "♥", "♦", "♣"]:
for symbol in range(2, 11):
cards.append(Card(symbol, suit))
for symbol in ["J", "Q", "K", "A"]:
cards.append(Card(symbol, suit))
return cards