-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.py
122 lines (110 loc) · 3.54 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'''
# Deck Class
# Written by: Bhavjot Singh
'''
from card import Card
import random
SUITS = ["spades", "hearts", "clubs", "diamonds"]
RANKS = ["A","K","Q","J",2,3,4,5,6,7,8,9,10]
class Deck:
'''
Name of this class can be a bit deceiving.
Objects of this class can contain multiple decks of cards that can be used for various card games.
Available functions (call the function for more details):
- generate_deck()
- retrieve_random_card()
- get_number_of_cards()
- get_card(suit, rank)
- count_card(card)
'''
def __init__(self, num_of_decks:int) -> None:
"""
Initializes an instance of the deck class.
"""
self.num_of_decks = num_of_decks
self.deck = self.generate_deck()
self.num_of_cards = 52*num_of_decks
def generate_deck(self):
"""
Generates a deck of cards.
"""
deck = {}
for suit in SUITS:
temp = []
deck_id = 1
while deck_id != self.num_of_decks + 1:
for rank in RANKS:
temp.append(Card(suit,rank,deck_id))
deck_id += 1
deck[suit] = temp
return deck
def add_card(self, card:Card):
"""
Adds a card back to the deck.
"""
suit = card.get_suit()
self.deck[suit].append(card)
self.num_of_cards += 1
def retrieve_card(self):
"""
Retrieves a random card.
"""
if self.num_of_cards == 0: raise Exception("Deck is empty")
suit = random.choice(SUITS)
while len(self.deck[suit]) == 0: suit = random.choice(SUITS)
the_card = random.choice(self.deck[suit])
self.deck[suit].remove(the_card)
self.num_of_cards -= 1
return the_card
def get_number_of_cards(self):
"""
Returns the number of remaining cards in the deck.
"""
return self.num_of_cards
def get_card(self, suit:str, rank):
"""
Retrieves a card matching the description. (Does not mofify the deck.)
"""
the_suit = self.deck[suit]
for card in the_suit:
if card.get_rank() == rank:
return card
return ""
def count_card(self,card:Card):
'''
Counts and returns the number of cards which have same rank and suit.
'''
suit = card.get_suit()
rank = card.get_rank()
count = 0
for item in self.deck[suit]:
if item.get_rank() == rank:
count += 1
return count
def __str__(self) -> str:
"""
String representation of the deck.
"""
heading = """
-----------------------------------------------------------------
| ♠ Spades | ♥ Hearts | ♣ Clubs | ♦ Diamonds |
-----------------------------------------------------------------
"""
number = f"Number of Cards in deck: {self.num_of_cards}"
end = f"{'-'*65}\n{number:^65}\n{'-'*65}"
columns = ""; num = ""
for rank in RANKS:
for suit in SUITS:
if self.num_of_decks != 1:
num = f"{self.count_card(self.get_card(suit,rank))}x "
card = str(self.get_card(suit,rank))
columns += f"|{num+card:^15}"
columns += "|\n"
return heading + columns + end
# deck = Deck(6)
# card = deck.retrieve_card()
# print(deck)
# print(card)
# print("\nCHANGE\n")
# deck.add_card(card)
# print(deck)