-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_round.py
554 lines (437 loc) · 18.6 KB
/
game_round.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
import cards
# ===============================================================================
# #STOPER - IF TRUE GAME WILL NEED ANY KEY TO CONTINIUE AFTER EACH ROUND
# ===============================================================================
debug_breaks = False
class Round:
def __init__(self, players, blinds):
# BLINDS
self.blinds = blinds
# blinds as an int
# PLAYERS:
self.players = []
for a in players:
self.players.append(a)
# players as a list
# STATE
self.stage = 'preflop'
# status of the game 'preflop' 'flop' 'turn' 'river'
# DECK
self.deck = cards.CardDeck()
self.cardsontable = []
self.cardsgraveyard = []
# cardsgraveyard will be used to store folded cards
# POT / BETS
self.pot = 0
self.bets = {'best': 0}
# bets will be used to store players bets
# WINNERS
self.winners = []
# winners as a list
# SPLIT POT
self.sidelevel = 0
self.levelpot = {}
self.levelbets = {}
self.levelplayers = {}
# ===========================================================================
# #STATUS
# ===========================================================================
def get_stage(self):
return self.stage
# ===========================================================================
# #BETS
# ===========================================================================
def add_to_bets(self, player, value):
# value has to be an int
# player must be an instance of player class
old = self.get_player_bets(player)
self.bets[player] = value + old
best = self.bets['best']
if value + old > best:
self.bets['best'] = value + old
def get_bets(self):
return self.bets
def check_bets(self):
# returns True if every player still in round has the same amount of money in bets = best
# else returns False
if 'best' not in self.bets.keys():
return False
for player in self.getRoundPlayers():
if self.get_player_bet_difference(player) != 0:
if not player.getAllIn():
return False
return True
def get_player_bets(self, player):
if player not in self.bets.keys():
return 0
else:
return self.bets[player]
def get_player_bet_difference(self, player):
# returns difference between player bets and the best bet so far
if self.get_player_bets(player) == self.bets['best']:
return 0
else:
if self.bets['best'] - self.get_player_bets(player) < 0:
print self.bets['best'] - self.get_player_bets(player)
raise ValueError('difference smaller than 0')
else:
return self.bets['best'] - self.get_player_bets(player)
def get_bets_sum(self):
sumka = 0
for a in self.bets.keys():
if a != 'best':
if self.get_player_bets(a) > 0:
sumka += self.get_player_bets(a)
return sumka
def get_sum_of_pot_and_bets(self):
return self.get_bets_sum() + self.get_pot()
# ===========================================================================
# #ALLIN
# ===========================================================================
def get_level(self):
return self.sidelevel
def make_all_in_bet(self, player, value):
# value = value of allinbet
# sumka - sum of money in pot
sumka = self.get_pot()
sumkaside = 0
payers = []
payers.append(player)
best = self.get_bets()['best']
# iterate through every player that betted
for aplayer in self.get_bets().keys():
if aplayer != 'best':
payers.append(aplayer)
hisbet = self.bets[aplayer]
if best <= value:
sumka += hisbet
else:
if hisbet > value:
sumka += value
sumkaside += hisbet - value
else:
sumka += hisbet
self.bets[aplayer] = hisbet - value
self.bets['best'] = abs(best - value)
self.levelpot[self.get_level()] = sumka + value
self.levelplayers[self.get_level()] = payers
self.sidelevel += 1
self.pot = sumkaside
print 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
print 'players'
print self.levelplayers
print 'level pot'
print self.levelpot
print 'new pot'
print self.pot
print 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
def add_bets_to_highest_level(self):
payers = self.getRoundPlayers()
pot = self.get_pot()
self.levelplayers[self.get_level()] = payers
self.levelpot[self.get_level()] = pot
# ===========================================================================
# #POT
# ===========================================================================
def add_bets_to_pot(self):
self.add_to_pot(self.get_bets_sum())
self.bets = {'best': 0}
def get_pot(self):
return self.pot
def add_to_pot(self, value):
old = self.get_pot()
self.pot = old + value
# ===========================================================================
# #DECK
# ===========================================================================
def get_deck(self):
return self.deck
def deal_card_from_deck(self):
return self.get_deck().deal()
# ===========================================================================
# #CARDS
# ===========================================================================
def get_cards_on_table(self):
return self.cardsontable
def add_card_to_table(self):
self.cardsontable.append(self.deal_card_from_deck())
self.cardsontable.sort()
def burn_card(self):
self.cardsgraveyard.append(self.deal_card_from_deck())
def deal_card_to_player(self, player):
player.addToHand(self.deal_card_from_deck())
# ============================================================================
# #PLAYERS
# ============================================================================
def remove_players_from_round(self, player):
self.players.remove(player)
def getRoundPlayers(self):
return self.players
def addPlayerToWinners(self, player):
self.winners.append(player)
def getNextRoundPlayer(self, player):
# player must be an instance of player class
# returns next player in the self.players list
index = self.getRoundPlayers().index(player)
if index + 1 == len(self.getRoundPlayers()):
index = -1
return self.getRoundPlayers()[index + 1]
# ===========================================================================
# #BLINDS:
# ===========================================================================
def getRoundBlinds(self):
return self.blinds
# ===========================================================================
# #DISPLAY
# ===========================================================================
def displayPlayers(self):
for asorter in self.getRoundPlayers():
print '%s has %s chips' % (asorter.getName(), asorter.getMoney())
print
def displayCards(self):
print 'Cards on the Table are %s' % (self.get_cards_on_table())
def displayPot(self):
print
if self.get_level() == 0:
print 'The pot is %s' % (self.get_pot())
else:
print 'Current pot is %s' % (self.get_pot())
for a in self.levelpot.keys():
print '%s Sidepot for %s is %s' % (a, self.levelplayers[a], self.levelpot[a])
print
# ===========================================================================
# #KEY BREAKS
# ===========================================================================
def debug_break(self):
if debug_breaks:
print
raw_input(' ***press any key to continiue*** ')
# ===========================================================================
# #PLAY
# ===========================================================================
def play(self):
skip = False
# =======================================================================
# #preflop
# =======================================================================
self.deck.shuffle()
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print 'Preflop: '
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print
self.displayPlayers()
# burn a card and deal 2 cards to each player:
self.burn_card()
self.stage = 'preflop'
for astarter in self.getRoundPlayers():
self.deal_card_to_player(astarter)
self.deal_card_to_player(astarter)
# keep track of which players already taken an action
initialplayers = len(self.getRoundPlayers())
stageactionlist = []
# iterate through first prebets
for netplayer in self.getRoundPlayers():
if netplayer.getStatus() in ('smallblind', 'smallblind & dealer'):
netplayer.call(self, self.getRoundBlinds())
print
elif netplayer.getStatus() == 'bigblind':
netplayer.call(self, 2 * self.getRoundBlinds())
nextplayer = self.getNextRoundPlayer(netplayer)
print
else:
nextplayer = self.getNextRoundPlayer(netplayer)
netplayer.update(self)
stageactionlist.append(netplayer)
# itarate through normal actions
while self.check_bets() != True or len(stageactionlist) != initialplayers:
aplayer = nextplayer
nextplayer = self.getNextRoundPlayer(aplayer)
aplayer.update(self)
if aplayer not in stageactionlist:
stageactionlist.append(aplayer)
# update Pot
self.add_bets_to_pot()
# check if there are still players in game
if len(self.getRoundPlayers()) == 1:
self.addPlayerToWinners(self.getRoundPlayers()[0])
skip = True
# skip if there is only one player left
if skip != True:
self.debug_break()
# =======================================================================
# #flop
# =======================================================================
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print 'Flop: '
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print
# change staus
self.stage = 'flop'
# burn a card and deal 3 card to table
self.burn_card()
self.add_card_to_table()
self.add_card_to_table()
self.add_card_to_table()
# DISPLAY:
self.displayCards()
self.displayPot()
self.displayPlayers()
# keep track of which players already taken an action
initialplayers = len(self.getRoundPlayers())
stageactionlist = []
nextplayer = self.getRoundPlayers()[0]
# iterate through first round
while self.check_bets() != True or len(stageactionlist) != initialplayers:
aplayer = nextplayer
nextplayer = self.getNextRoundPlayer(aplayer)
aplayer.update(self)
if aplayer not in stageactionlist:
stageactionlist.append(aplayer)
# update Pot
self.add_bets_to_pot()
# check if there are still players in game
if len(self.getRoundPlayers()) == 1:
self.addPlayerToWinners(self.getRoundPlayers()[0])
skip = True
if skip != True:
# =======================================================================
# #turn
# =======================================================================
self.debug_break()
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print 'Turn: '
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print
# change staus
self.stage = 'turn'
# burn a card and deal 1 card to table
self.burn_card()
self.add_card_to_table()
# DISPLAY:
self.displayCards()
self.displayPot()
self.displayPlayers()
# keep track of which players already taken an action
initialplayers = len(self.getRoundPlayers())
stageactionlist = []
nextplayer = self.getRoundPlayers()[0]
# iterate through first round
while self.check_bets() != True or len(stageactionlist) != initialplayers:
aplayer = nextplayer
nextplayer = self.getNextRoundPlayer(aplayer)
aplayer.update(self)
if aplayer not in stageactionlist:
stageactionlist.append(aplayer)
# update Pot
self.add_bets_to_pot()
# check if there are still players in game
if len(self.getRoundPlayers()) == 1:
self.addPlayerToWinners(self.getRoundPlayers()[0])
skip = True
# skip if there is only one player left
if skip != True:
# =======================================================================
# #river
# =======================================================================
self.debug_break()
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print 'River: '
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print
# change staus
self.stage = 'river'
# burn a card and deal 1 card to table
self.burn_card()
self.add_card_to_table()
# DISPLAY:
self.displayCards()
self.displayPot()
self.displayPlayers()
# keep track of which players already taken an action
initialplayers = len(self.getRoundPlayers())
stageactionlist = []
nextplayer = self.getRoundPlayers()[0]
# iterate through all players
while self.check_bets() != True or len(stageactionlist) != initialplayers:
aplayer = nextplayer
nextplayer = self.getNextRoundPlayer(aplayer)
aplayer.update(self)
if aplayer not in stageactionlist:
stageactionlist.append(aplayer)
# check if there are still players in game
if len(self.getRoundPlayers()) == 1:
self.addPlayerToWinners(self.getRoundPlayers()[0])
skip = True
# Convert system of pot into LEVLES:
self.add_bets_to_highest_level()
# =======================================================================
# #END
# =======================================================================
self.debug_break()
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print 'Round Evaluation: '
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
print
self.displayCards()
self.displayPot()
print self.getRoundPlayers()
print
# ===================================================================
# #EVALUATE HANDS
# ===================================================================
# byplayer - dictionary sorting hands by player
byplayer = {}
for aplayer in self.getRoundPlayers():
# display hands and chips
aplayer.displayHand()
print '%s has %s chips' % (aplayer.getName(), aplayer.getMoney())
# create a sum of players hand and table cards
aplayerendhand = cards.listUnion(aplayer.getHand(), self.get_cards_on_table())
# generate best hand of aplayer
endhand = cards.find_final_hands(aplayerendhand)[-1]
print '%s best hand is: %s' % (aplayer.getName(), endhand)
print
# store player in byplayers dictionary
byplayer[aplayer] = endhand
# ===================================================================
# #KOMENTY ROBOCZE
# ===================================================================
print 'levels:'
print self.levelplayers
print self.levelpot
print
# ===================================================================
# #ANALYSE LEVELS:
# ===================================================================
for level in self.levelplayers.keys():
print 'Level is %s' % (level)
print 'Level Players are: %s' % (self.levelplayers[level])
# FIND ALL HANDS AT LEVEL:
levelhands = []
for awinn in self.levelplayers[level]:
if awinn in self.getRoundPlayers():
levelhands.append(byplayer[awinn])
# FIND BEST HAND
levelhands.sort()
bestatlevel = levelhands[-1]
# FIND ALL PLAYERS AT LEVEL THAT HAVE THE SAME HAND:
levelwinners = []
for bwinn in self.levelplayers[level]:
if byplayer[bwinn] == bestatlevel:
levelwinners.append(bwinn)
# COMPUTE NUMBER OF WINNERS/PRIZE:
count = len(levelwinners)
wygrana = int(self.levelpot[level] * 1.0 / count)
# GIVE PRIZE TO EVERY WINNER:
for winner in levelwinners:
winner.addToMoney(wygrana)
if count == 1:
print 'Player %s won and gets %s chips' % (winner.getName(), wygrana)
else:
print 'Remis: %s gets %s chips' % (winner.getName(), wygrana)
self.debug_break()
print
print '##########################################################################'
print 'Round ends'
print '##########################################################################'
print