-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeckofcards-PROJECT.py
64 lines (48 loc) · 1.56 KB
/
deckofcards-PROJECT.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
from random import shuffle, choice
class Card:
def __init__(self, value, suit):
self.value = value
self.suit = suit
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"]
self.cards = [Card(value, suit) for suit in suits for value in values]
# THE OTHER WAY FOR self.cards:
# for suit in suits:
# for value in values:
# self.cards.append(Card(value, suit))
# print(self.cards)
def __repr__(self):
return f"Deck of {self.count()} cards"
def __iter__(self):
return iter(self.cards)
def count(self):
return len(self.cards)
def _deal(self, num):
count = self.count()
actual = min([count, num])
if count == 0:
raise ValueError("All cards have been finished")
hand = self.cards[-actual:]
self.cards = self.cards[:-actual]
return hand
def deal_card(self):
return self._deal(1)[0]
def deal_hand(self, hand_size):
return self._deal(hand_size)
def shuffle(self):
if self.count() < 52:
raise ValueError("Only full decks can be shuffled")
shuffle(self.cards)
return self
d = Deck()
d.shuffle()
card = d.deal_card()
print(card)
card2 = d.deal_hand(5)
print(card2)
for dec in d:
print(dec)