-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.py
178 lines (138 loc) · 4.17 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'''
Created on 14 Jul 2018
@author: sReeall
'''
import random
class card():
'''
used to represent a single card and is basic component of deck class.
constructor:
suit
rank
attributes:
suit: should be one of the following 'H', 'C', 'D', 'S'
rank: 2 - 10, 'J', 'Q', 'K', 'A'
value: tuple used as rank 'A' has 1 and 11
methods:
isAce() - returns true if ace
'''
def __init__ (self,suit,rank):
self.suit = suit
self.rank = rank
# values assigned automatically based on rank
try: # add cast to int
self.value = int(rank),
except: # must not be 2 - 10 rank
# check if ace
if rank != 'A' : self.value = 10,
else: self.value = 11,1
def isAce(self):
return self.rank == 'A'
def __str__(self):
return f'{self.rank}{self.suit}'
class deck():
'''
represents a deck of 52 card
can contain multiples 52 cards if num is specified (not implimented yet)
attributes:
cards - list of 52 cards as found in a deck
methods:
shuffle - mix up the cards
nextcard - takes the top card
isempty - checks to see if there are any cards in the deck
'''
def __init__(self,num=1):
'''
constructor setups an list of card objects called cards
'''
# setup hearts
hearts = [card]*13
# setup ace
hearts[0] = card('H','A')
#setup 2 - 10 cards
for i in range(1,10):
hearts[i] = card('H',i+1)
# setup jack, queen and king
hearts[10] = card('H','J')
hearts[11] = card('H','Q')
hearts[12] = card('H','K')
# setup diamonds
dia = [card]*13
# setup ace
dia[0] = card('D','A')
#setup 2 - 10 cards
for i in range(1,10):
dia[i] = card('D',i+1)
# setup jack, queen and king
dia[10] = card('D','J')
dia[11] = card('D','Q')
dia[12] = card('D','K')
# setup clubs
club = [card]*13
# setup ace
club[0] = card('C','A')
#setup 2 - 10 cards
for i in range(1,10):
club[i] = card('C',i+1)
# setup jack, queen and king
club[10] = card('C','J')
club[11] = card('C','Q')
club[12] = card('C','K')
# setup spades
spade = [card]*13
# setup ace
spade[0] = card('S','A')
#setup 2 - 10 cards
for i in range(1,10):
spade[i] = card('S',i+1)
# setup jack, queen and king
spade[10] = card('S','J')
spade[11] = card('S','Q')
spade[12] = card('S','K')
self.cards = hearts + dia + club + spade
def shuffe(self):
# randomly shuffles list cards
random.shuffle(self.cards)
def nextcard(self):
'''
uses pop to take the top card from deck.cards list.
returns None if deck is empty
'''
if not self.isempty():
return self.cards.pop()
else:
return None
def isempty(self):
return len(self.cards) == 0
# if __name__=='__main__':
# # pass
# # test class deck
# # mydeck = deck()
# pass
# for i in mydeck.cards:
# print(i)
# mydeck.shuffe()
#
# print('shuffled deck:')
#
# for i in mydeck.cards:
# print(i)
#
# for i in range(len(mydeck.cards)+1):
# print(f'top card is: {mydeck.nextcard()}')
# print(f'mydeck now has {len(mydeck.cards)}')
# test class card
# print('Create a card with suit H and rank J')
# card1 = card('H','J')
# print(card1)
# print(card1.value)
#
# print('Create a card with suit C and rank A')
# card2 = card('C','A')
# print(card2)
# print(card2.value)
#
# print('Ceate a card with suit D and rank 2')
# card3 = card('D','2')
# print(card3)
# print(card3.value)