-
Notifications
You must be signed in to change notification settings - Fork 72
/
hw5_cards.py
149 lines (129 loc) · 4.11 KB
/
hw5_cards.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import random
import unittest
VERSION = 0.01
class Card:
'''a standard playing card
cards will have a suit and a rank
Class Attributes
----------------
suit_names: list
the four suit names in order
0:Diamonds, 1:Clubs, 2: Hearts, 3: Spades
faces: dict
maps face cards' rank name
1:Ace, 11:Jack, 12:Queen, 13:King
Instance Attributes
-------------------
suit: int
the numerical index into the suit_names list
suit_name: string
the name of the card's suit
rank: int
the numerical rank of the card
rank_name: string
the name of the card's rank (e.g., "King" or "3")
'''
suit_names = ["Diamonds","Clubs","Hearts","Spades"]
faces = {1:"Ace",11:"Jack",12:"Queen",13:"King"}
def __init__(self, suit=0,rank=2):
self.suit = suit
self.suit_name = Card.suit_names[self.suit]
self.rank = rank
if self.rank in Card.faces:
self.rank_name = Card.faces[self.rank]
else:
self.rank_name = str(self.rank)
def __str__(self):
return f"{self.rank_name} of {self.suit_name}"
class Deck:
'''a deck of Cards
Instance Attributes
-------------------
cards: list
the list of Cards currently in the Deck. Initialized to contain
all 52 cards in a standard deck
'''
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1,14):
card = Card(suit,rank)
self.cards.append(card) # appends in a sorted order
def deal_card(self, i=-1):
'''remove a card from the Deck
Parameters
-------------------
i: int (optional)
the index of the ard to remove. Default (-1) will remove the "top" card
Returns
-------
Card
the Card that was removed
'''
return self.cards.pop(i)
def shuffle(self):
'''shuffles (randomizes the order) of the Cards
self.cards is modified in place
Parameters
----------
None
Returns
-------
None
'''
random.shuffle(self.cards)
def replace_card(self, card):
card_strs = [] # forming an empty list
for c in self.cards: # each card in self.cards (the initial list)
card_strs.append(c.__str__()) # appends the string that represents that card to the empty list
if card.__str__() not in card_strs: # if the string representing this card is not in the list already
self.cards.append(card) # append it to the list
def sort_cards(self):
'''returns the Deck to its original order
Cards will be in the same order as when Deck was constructed.
self.cards is modified in place.
Parameters
----------
None
Returns
-------
None
'''
self.cards = []
for suit in range(4):
for rank in range(1,14):
card = Card(suit,rank)
self.cards.append(card)
def deal_hand(self, hand_size):
'''removes and returns hand_size cards from the Deck
self.cards is modified in place. Deck size will be reduced
by hand_size
Parameters
-------------------
hand_size: int
the number of cards to deal
Returns
-------
list
the top hand_size cards from the Deck
'''
hand_cards = []
for i in range(hand_size):
hand_cards.append(self.deal_card())
return hand_cards
def print_hand(hand):
'''prints a hand in a compact form
Parameters
-------------------
hand: list
list of Cards to print
Returns
-------
none
'''
hand_str = '/ '
for c in hand:
s = c.suit_name[0]
r = c.rank_name[0]
hand_str += r + "of" + s + ' / '
print(hand_str)