-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.py
492 lines (359 loc) · 12.2 KB
/
card.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""
This file handles the Classes Deck, Player, Table, and Bot.
"""
from random import randint # Used for shuffling and popping off random cards
import bot
class Deck:
"""
Deck Class:
This class handles a deck of cards and ensures it holds all of a deck's
properties. The deck will resemble an actual deck of playing cards.
"""
def __init__(self):
"""
Creates a deck of cards.
Input: self - the object on which this method is done on
Output: None
Runtime: O(n*m) where n is the number of the card(2,3,...,ace) and m is
the different suits (Hearts,...)
"""
self._cards = []
for i in range(13):
for name in ['Hearts', 'Diamonds', 'Spades', 'Clubs']:
self._cards.append((i + 2, name))
def print_cards(self):
"""
Prints out a deck of cards.
Input: self - the object on which this method is done on
Output: None
Runtime: O(1)
"""
print('Length of deck: ', len(self._cards))
print('Cards in deck: ', self._cards)
def get_cards(self):
"""
Receives all the cards in a deck.
Input: self - the object on which this method is done on
Output: A list of all the cards the deck contains.
Runtime: O(1)
"""
return self._cards
def shuffle(self):
"""
Shuffles all the cards in a deck.
Input: self - the object on which this method is done on
Output: None.
Runtime: O(n) where n is the total number of cards in the deck
"""
new_cards = []
size = len(self._cards)
for i in range(size - 1, -1, -1):
new_cards.append(self._cards.pop(randint(0, i)))
self._cards = new_cards
def remove_random_card(self):
"""
Pops off a random card in the deck.
Input: self - the object on which this method is done on
Output: the card that is removed.
Runtime: O(n) where n is the number of cards in the deck
"""
if len(self._cards) == 0:
print('Deck is empty')
return
index = randint(0, len(self._cards) - 1)
random_card = self._cards[index]
self._cards.remove(random_card) # O(n)
return random_card
def remove_top_card(self):
"""
Pops off the top card in the deck.
Input: self - the object on which this method is done on
Output: the card that is removed.
Runtime: O(1)
"""
if len(self._cards) == 0:
print('Deck is empty')
return
return self._cards.pop(0)
def remove_card(self, current):
"""
Pops off a specified card in the deck.
Input: self - the object on which this method is done on
Output: the card that is removed.
Runtime: O(n) where n is the number of cards the deck has
"""
if len(self._cards) == 0:
print('Deck is empty')
return
return self._cards.remove(current) # O(n)
def deal(self, players, cards_per_player):
"""
Given a list of all the player objects the deck deals a specified number
of cards to each player
Inputs:
self - the object on which this method is done on
players - a list of all the player objects that we will deal cards to
cards_per_player - the number of cards to be dealt to each player
Output: None.
Runtime: O(n*m*x) where n is the cards_per_player and m is the number of
players and x is the number of cards in the deck
"""
assert cards_per_player * len(players) <= len(self._cards)
for i in range(cards_per_player):
for p in players:
card = self.remove_top_card() # O(x)
p.add_card(card)
def collect(self, players):
"""
Collects all the cards from the players and puts them back in the deck
Input:
self - the object on which this method is done on
players - a list of all the player objects that we will collect the cards
from
Output: None.
Runtime: O(n*m) where n is the number of cards each player has and m is
the number of players
"""
for p in players:
while len(p.get_cards()) > 0:
self._cards.append(p.pop_card())
def have_all_cards(self):
"""
Determines if all the cards are in the deck.
Input: self - the object on which this method is done on
Output: whether or not the deck of cards contains every card.
Runtime: O(n) where n is the number of cards in the deck
"""
cards = set()
for c in self._cards:
cards.add(c)
if len(cards) == 52:
return True
else:
return False
def push_to_table(self, table, number_of_cards):
"""
Pushes a specified number of cards to the table
Input:
self - the object on which this method is done on
table - the table object which we will be pushing cards too
number_of_cards - the number of cards to be pushed to the table
Output: None.
Runtime: O(n*m) where n is the number of cards we would like to push to
the table and m is the number of cards in the deck
"""
for i in range(number_of_cards):
table.add_card(self.remove_top_card()) # O(m)
def __str__(self):
"""
Used to print the Deck
Input: self - the object on which this method is done on
Output: A string of what to print
Runtime: O(1)
"""
return 'Deck of cards'
class Player:
"""
Player Class:
This class handles a Player object that has cards, chips,
and ID to identify it. The Player class acts as a base for both the Bot and
Table classes
"""
def __init__(self, ID, cards=None):
"""
Creates a Player object and initializes its ID and if given a list of
cards it will give those cards to the player object and gives the player
2000 chips.
Input:
self - the object on which this method is done on
ID - The identification of the player
cards=None - The cards that the player starts out with, initialized to
the empty list.
Output: None
Runtime: O(1)
"""
if cards == None:
self._cards = []
else:
self._cards = cards
self._ID = ID
self._hand = -1
# Total starting chips is 2000
self._chips = 2000
def get_hand(self):
"""
Receives the hand (Royal Flush(10),...,Two Pair(2), One Pair(1)) the player has.
Input: self - the object on which this method is done on
Output: The hand the player has.
Runtime: O(1)
"""
return self._hand
def set_hand(self, hand):
"""
Sets the hand (Royal Flush(10),...,Two Pair(2), One Pair(1)) the player has.
Input:
self - the object on which this method is done on.
hand - the hand we will set the players hand at.
Output: None.
Runtime: O(1)
"""
self._hand = hand
def get_ID(self):
"""
Receives the ID the player has.
Input: self - the object on which this method is done on
Output: The ID of the player.
Runtime: O(1)
"""
return self._ID
def get_cards(self):
"""
Receives all the cards the player has.
Input: self - the object on which this method is done on
Output: A list of all the cards the player has.
Runtime: O(1)
"""
return self._cards
def print_cards(self):
"""
Prints all the cards the player has.
Input: self - the object on which this method is done on
Output: None.
Runtime: O(1)
"""
print(self, '\b:\t', end='')
print('Cards : {}\n'.format(self._cards))
def add_card(self, card):
"""
Gives a card to the player.
Input:
self - the object on which this method is done on
card - the card to be added
Output: None
Runtime: O(1)
"""
self._cards.append(card)
def remove_card(self, card):
"""
Removes a card from the player.
Input:
self - the object on which this method is done on
card - the card to be removed
Output: None
Runtime: O(n) where n is the number of cards the player has
"""
if card not in self._cards:
print('you dont have that card')
self._cards.remove(card) # O(n)
def pop_card(self):
"""
Pops a card from the player.
Input:
self - the object on which this method is done on
Output: The card Popped off
Runtime: O(1)
"""
try:
return self._cards.pop(0)
except:
print('No cards left')
def __str__(self):
"""
Used to print the Player
Input: self - the object on which this method is done on
Output: A string of what to print
Runtime: O(1)
"""
return 'Player'
def get_chips(self):
"""
Get the number of chips the player has
Input: self - the object on which this method is done on
Output: The number of chips the player has
Runtime: O(1)
"""
return self._chips
def remove_chips(self, value):
"""
Remove a certain number of chips from the player
Input: self - the object on which this method is done on
value - the number of chips to be removed
Output: None
Runtime: O(1)
"""
self._chips -= value
def add_chips(self, value):
"""
Add a certain number of chips to the player
Input: self - the object on which this method is done on
value - the number of chips to be added
Output: None
Runtime: O(1)
"""
self._chips += value
class Table(Player):
"""
Table Class:
This class is the child of the Player class and holds all the same properties
"""
def __init__(self, cards=None):
"""
Creates a Table object and initializes it by calling the initilization
of the Player class. Then we assign its chips to be 0
Input:
self - the object on which this method is done on
cards=None - The cards that the table starts out with, initialized to
the empty list.
Output: None
Runtime: O(1)
"""
super().__init__('t', cards) # O(1)
self._chips = 0
def __str__(self):
"""
Used to print the Table
Input: self - the object on which this method is done on
Output: A string of what to print
Runtime: O(1)
"""
return 'Table'
def give_pot(self, player):
"""
Gives a player all the Tables chips
Input:
self - the object on which this method is done on
player - a player to whom the chips will be given to
Output: None
Runtime: O(1)
"""
player.add_chips(self._chips)
self.remove_chips(self._chips)
class Bot(Player):
"""
Bot Class:
This class is the child of the Player class and holds all the same properties
"""
def __init__(self, cards=None):
"""
Creates a Bot object and initializes it by calling the initilization
of the Player class.
Input:
self - the object on which this method is done on
cards=None - The cards that the table starts out with, initialized to
the empty list.
Output: None
Runtime: O(1)
"""
super().__init__('b', cards)
def __str__(self):
"""
Used to print the Bot
Input: self - the object on which this method is done on
Output: A string of what to print
Runtime: O(1)
"""
return 'Bot'
def bet(self, current_call, table, can_raise=True):
return bot.bot_bet(current_call, self, table, can_raise)
def rand_number_in_range(self, a, b):
return randint(a,b)