-
Notifications
You must be signed in to change notification settings - Fork 0
/
deckOfCards.py
53 lines (42 loc) · 1.48 KB
/
deckOfCards.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
import random
class Card:
def __init__(self, suit, value):
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
if suit not in suits:
raise ValueError("incorrect suit")
if value not in values:
raise ValueError("bad card value")
self.suit = suit
self.value = value
def __repr__(self):
return f"{self.value} of {self.suit}"
class Deck:
def __init__(self):
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
cards = [Card(suit, value) for suit in suits for value in values]
self.cards = cards
def __repr__(self):
return f"Deck of {self.count()} cards"
def __iter__(self):
return iter(self.cards)
def count():
return len(self.cards)
def _deal(self, num=1):
count = self.count()
actual = min([count, num])
if count == 0:
raise ValueError("All cards have been dealt")
cards = self.cards[-actual:]
self.cards = self.cards[:-actual]
return cards
def _shuffle(self):
if self.count() < 52:
raise ValueError("Only full decks can be shuffled")
shuffle(self.cards)
def deal_card(self):
return self._deal()[0]
def deal_hand(self, num):
return self._deal(num)
print(Deck().deal_hand(5))