-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.py
More file actions
619 lines (505 loc) · 19.6 KB
/
Table.py
File metadata and controls
619 lines (505 loc) · 19.6 KB
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
##########################################################################
# Table is our Game Manager and runs the game.
# Table keeps a lot of information in state. It controls the game,
# creates Agents and checks which ones are active in game round or in
# game.
# This file also contains the functions that permit the reading
# of input through the command line. Do not delete
# The prints in this file allow us to view the proceedings of the game.
# To watch risk calculation activate the prints indicated in class Agent.
##########################################################################
import sys
import ratings
import keyboard
from Deck import Deck
from Agent import Agent
from GameState import GameState
class Table:
def __init__(self, numRounds, numAgents, bigBlind, chips, agentProfiles = None):
self.numRounds = numRounds
self.agents = []
self.activeAgents = []
self.deck = Deck()
self.pot = 0
self.tableCards = []
self.discardPile = []
self.gameStateClass = GameState()
self.gameState = None
self.bigBlind = bigBlind
self.smallBlind = round(bigBlind/2)
self.raiseAmount = bigBlind
self.betAmount = bigBlind
self.currentBet = 0
self.canCheck = True
self.canRaise = False
self.dealer = 0
self.turn = 0
self.agentProfiles = agentProfiles
i = 0
while i < numAgents:
if agentProfiles == None:
agent = Agent(i, self, chips)
else:
agent = Agent(i, self, chips, agentProfiles[i])
self.agents.append(agent)
self.activeAgents.append(agent)
i += 1
##################################################
#### GETTERS #####
##################################################
def getAgents(self):
return self.agents
def getDeck(self):
return self.deck
def getPot(self):
return self.pot
##################################################
#### DEALER ACTIONS #####
##################################################
def addToPot(self, amount):
self.pot += amount
# agents pay small blind and big blind
def blinds(self):
self.passTurn()
self.agents[self.turn].payBlind(self.smallBlind)
print("AGENT " + str(self.turn) + " PAYED SMALL BLIND: " + str(self.smallBlind))
self.addToPot(self.smallBlind)
self.passTurn()
self.agents[self.turn].payBlind(self.bigBlind)
print("AGENT " + str(self.turn) + " PAYED BIG BLIND: " + str(self.bigBlind))
self.addToPot(self.bigBlind)
# self.turn has the activeAgents index. returns agent id
def passTurn(self):
nextTurn = self.turn
if nextTurn >= len(self.activeAgents)-1:
nextTurn = 0
else:
nextTurn += 1
self.turn = nextTurn
# discards one card
def discardCard(self):
card = self.deck.dealCard()
self.discardPile.append(card)
# places one card on the table
def dealCardsTable(self, num):
n = 0
dealtCards = []
while n < num:
card = self.deck.dealCard()
self.tableCards.append(card)
dealtCards.append(card)
n += 1
for a in self.activeAgents:
a.receiveCards(dealtCards)
# deals two cards to agent
def dealCardsAgent(self, agent):
card1 = self.deck.dealCard()
card2 = self.deck.dealCard()
agent.receiveCards([card1, card2])
# doubles both bet amount and raise amount
def doubleAmounts(self):
self.raiseAmount = self.raiseAmount * 2
self.betAmount = self.betAmount * 2
def whatToDo(self):
actions = ["CALL","FOLD"]
if self.canCheck:
actions.append("CHECK")
if self.canRaise:
actions.append("RAISE")
return actions
def bettingRound(self, state, counter):
#which actions are allowed at beginning of a betting round
if counter == 0:
self.betAmount = self.bigBlind
self.canCheck = True
self.canRaise = False
nAgents = len(self.activeAgents)
toRemove = []
while nAgents > 0:
self.passTurn()
actions = self.whatToDo()
self.updateGameState(state, actions)
msg = self.sendMessage(self.turn)
if(len(toRemove) == len(self.activeAgents) - 1):
break
if "RAISE" == msg[0]:
self.betAmount += self.raiseAmount
self.addToPot(self.betAmount)
self.canCheck = False
print("AGENT " + str(msg[1]) + " DID " + msg[0] + " AND BET " + str(self.betAmount))
elif "CALL" == msg[0]:
self.addToPot(self.betAmount)
self.canCheck = False
self.canRaise = True
print("AGENT " + str(msg[1]) + " DID " + msg[0] + " AND BET " + str(self.betAmount))
elif "FOLD" == msg[0]:
self.activeAgents[self.turn].fold()
toRemove.append(self.activeAgents[self.turn])
print("AGENT " + str(msg[1]) + " DID " + msg[0] + " AND IS REMOVED FROM GAME ROUND")
elif "CHECK" == msg[0]:
print("AGENT " + str(msg[1]) + " DID " + msg[0] + " AND DID NOT BET")
if self.activeAgents[self.turn].getMoney() <= 0:
toRemove.append(self.activeAgents[self.turn])
print("AGENT " + str(msg[1]) + " RAN OUT OF MONEY AND IS REMOVED FROM GAME")
print("POT CURRENTLY AT " + str(self.pot))
#WARN OTHER AGENTS
self.sendWarn("warn", [self.turn, msg[0]])
nAgents -= 1
#removes agents who have folded
for el in toRemove:
self.activeAgents.remove(el)
#to guarantee agents have bet the same
bet = -1
newRound = False
for a in self.activeAgents:
if bet == -1:
bet = a.getRoundBet()
elif a.getRoundBet() != bet:
newRound = True
break
if newRound and counter < 3:
counter += 1
return self.bettingRound(state, counter)
else:
if len(self.activeAgents) <= 1:
return False
else:
for a in self.activeAgents:
a.resetRoundBet()
a.calculateRoundAverage(counter+1)
return True
#reset table structures for another gameround
def reset(self, bigBlind):
self.deck = Deck()
self.pot = 0
self.tableCards = []
self.gameState = None
self.raiseAmount = bigBlind
self.betAmount = bigBlind
self.currentBet = 0
self.dealer += 1
self.activeAgents.clear()
for a in self.agents:
a.reset()
self.activeAgents.append(a)
#################################################
#### COMMUNICATION #####
#################################################
def updateGameState(self, state, actions):
self.gameStateClass.setBetAmount(self.betAmount)
self.gameStateClass.setRaiseAmount(self.raiseAmount)
self.gameStateClass.setState(state)
self.gameStateClass.setCanCheck(self.canCheck)
self.gameStateClass.setCanRaise(self.canRaise)
self.gameStateClass.setActions(actions)
def sendMessage(self, agent):
self.activeAgents[agent].updateGameState(self.gameStateClass)
return self.activeAgents[agent].receiveMessage()
def sendWarn(self, flag, msg):
for a in self.activeAgents:
if(a.id != self.turn):
a.receiveWarn(flag,msg)
#################################################
#### GAME #####
#################################################
def gameRound(self):
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
################ PRE GAME PHASE ################
self.turn = self.dealer
# pay small blind and big blind
self.blinds()
self.deck.shuffle()
# deal 2 cards to each agent
for a in self.agents:
self.dealCardsAgent(a)
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("\nCARDS ARE DEALT TO THE AGENTS")
#EVALUATE STATE OF GAME
for a in self.activeAgents:
print( "AGENT: " + str(a.id))
for c in a.cardHistory:
print("CARD NUMBER: " + c.getName())
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
################ PRE FLOP PHASE ################
print("\n----- PRE-FLOP -----")
self.gameState = "PRE-FLOP"
print("\nBETTING STARTS")
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
# pre flop: betting round
if not self.bettingRound(self.gameState, 0):
if len(self.activeAgents) == 1:
self.activeAgents[0].receivePot(self.pot)
print("WINNING AGENT: " + str(self.activeAgents[0].id) + " RECEIVES: " + str(self.pot) + "\n")
return
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
for a in self.activeAgents:
print( "AGENT: " + str(a.id) + "; PROFILE " + a.getProfile() + "; MONEY: " + str(a.money.getCurrent()) + "; TOTAL BET:" + str(a.money.getGameBet()))
for a in self.activeAgents:
if a.getMoney() <= 0:
self.activeAgents.remove(a)
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
################ FLOP PHASE ################
print("\n----- FLOP -----")
self.gameState = "FLOP"
# discard card
self.discardCard()
# flop: place 3 cards showing on table
self.dealCardsTable(3)
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
#print(self.gameState)
print("\nCARDS ARE DEALT TO THE TABLE: ")
for c in self.tableCards:
print("CARD NUMBER: " + c.getName())
print("\nPLAYERS HOLD:")
for a in self.activeAgents:
print("->AGENT " + str(a.id) + " HAS: " + str(ratings.handName[a.showHand()]))
for c in a.cardHistory:
print("CARD NUMBER: " + c.getName())
print("Press 1 to continue")
while(True):
b = keyboard.read_key()
if b == "1":
break
# flop: betting round
print("\nBETTING STARTS")
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
if not self.bettingRound(self.gameState, 0):
if len(self.activeAgents) == 1:
self.activeAgents[0].receivePot(self.pot)
print("WINNING AGENT: " + str(self.activeAgents[0].id) + " RECEIVES: " + str(self.pot) + "\n")
return
print("\nBETTING ENDS")
print("Press 1 to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("STATE OF THE AGENTS AFTER THE FLOP BETTING ROUND")
for a in self.activeAgents:
print( "AGENT: " + str(a.id) + "; PROFILE " + a.getProfile() + "; MONEY: " + str(a.money.getCurrent()) + "; TOTAL BET:" + str(a.money.getGameBet()))
for a in self.activeAgents:
if a.money.getCurrent() <= 0:
self.activeAgents.remove(a)
################ TURN PHASE ################
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("\n----- TURN -----")
self.gameState = "TURN"
# turn: double bet ammount and raise ammount
self.doubleAmounts()
# turn: add 1 card showing on table
self.dealCardsTable(1)
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("TABLE CARDS: ")
for c in self.tableCards:
print("CARD NUMBER: " + c.getName())
print("\nPLAYERS HOLD:")
for a in self.activeAgents:
print("Press to continue")
while(True):
b = keyboard.read_key()
if b == '1':
break
print("->AGENT " + str(a.id) + " HAS: " + str(ratings.handName[a.showHand()]))
for c in a.cardHistory:
print("CARD NUMBER: " + c.getName())
# turn: betting round
print("\nBETTING STARTS")
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
if not self.bettingRound(self.gameState, 0):
if len(self.activeAgents) == 1:
self.activeAgents[0].receivePot(self.pot)
print("WINNING AGENT: " + str(self.activeAgents[0].id) + " RECEIVES: " + str(self.pot) + "\n")
return
print("\nBETTING ENDS")
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("STATE OF THE AGENTS AFTER THE TURN BETTING ROUND")
for a in self.activeAgents:
print( "AGENT: " + str(a.id) + "; PROFILE " + a.getProfile() + "; MONEY: " + str(a.money.getCurrent()) + "; TOTAL FAR: " + str(a.money.getGameBet()))
for a in self.activeAgents:
if a.money.getCurrent() <= 0:
self.activeAgents.remove(a)
################ RIVER PHASE ################
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("\n----- RIVER -----")
self.gameState = "RIVER"
# river: double bet ammount and raise ammount
self.doubleAmounts()
# river: add 1 card showing on table
self.dealCardsTable(1)
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("TABLE CARDS: ")
for c in self.tableCards:
print("CARD NUMBER: " + c.getName())
print("\nPLAYERS HOLD:")
for a in self.activeAgents:
print("Press to continue")
while(True):
b = keyboard.read_key()
if b == '1':
break
print("->AGENT " + str(a.id) + " HAS: " + str(ratings.handName[a.showHand()]))
for c in a.cardHistory:
print("CARD NUMBER: " + c.getName())
# river: betting round
print("\nBETTING STARTS")
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
if not self.bettingRound(self.gameState, 0):
if len(self.activeAgents) == 1:
self.activeAgents[0].receivePot(self.pot)
print("WINNING AGENT: " + str(self.activeAgents[0].id) + " RECEIVES: " + str(self.pot) + "\n")
return
print("\nBETTING ENDS")
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("STATE OF THE AGENTS AFTER THE RIVER BETTING ROUND")
for a in self.activeAgents:
print( "AGENT: " + str(a.id) + "; PROFILE " + a.getProfile() + "; MONEY: " + str(a.money.getCurrent()) + "; TOTAL FAR: " + str(a.money.getGameBet()))
for a in self.activeAgents:
if a.money.getCurrent() <= 0:
self.activeAgents.remove(a)
################ SHOWDOWN PHASE ################
self.gameState = "SHOWDOWN"
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("\n----- SHOWDOWN -----")
best = []
cardRank = 0
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("TABLE CARDS: ")
for c in self.tableCards:
print("CARD NUMBER: " + c.getName())
print("Press to continue")
while(True):
a = keyboard.read_key()
if a == '1':
break
print("\nPLAYERS HOLD:")
for a in self.activeAgents:
print("Press to continue")
while(True):
b = keyboard.read_key()
if b == '1':
break
print("->AGENT " + str(a.id) + " HAS: " + str(ratings.handName[a.showHand()]))
for c in a.cardHistory:
print("CARD NUMBER: " + c.getName())
for a in self.activeAgents:
print( "AGENT: " + str(a.id) + "; PROFILE: " + a.getProfile() + "; MONEY: " + str(a.money.getCurrent()) + "; TOTAL BET: " + str(a.money.getGameBet()))
for a in self.activeAgents:
if a.money.getCurrent() <= 0:
self.activeAgents.remove(a)
for a in self.activeAgents:
if cardRank < a.findHand(True):
cardRank = a.findHand(True)
best.clear()
best.append(a)
elif cardRank == a.findHand(True):
best.append(a)
for a in best:
a.receivePot(self.pot/len(best))
print("WINNING AGENT: " + str(a.id) + " RECEIVES: " + str(self.pot) + "\n")
return
def game(self, rounds):
print("##################### GAME BEGINS #####################")
r = 0
while r < rounds:
if len(self.agents) <= 1:
break
print("============= GAME ROUND " + str(r+1) + " =============")
self.gameRound()
for a in self.agents:
if a.getMoney() <= 0:
self.agents.pop(self.agents.index(a))
self.reset(self.bigBlind)
r += 1
print("##################### GAME ENDS #####################")
line = sys.stdin.readline()
numRounds = int(line.split(' ')[0])
numAgents = int(line.split(' ')[1])
bigBlind = int(line.split(' ')[2])
chips = int(line.split(' ')[3])
if len(line.split(' ')) == 4:
table = Table(numRounds, numAgents, bigBlind, chips)
table.game(numRounds)
else:
i = 0
profiles = []
while i < numAgents:
profiles.append(int(line.split(" ")[4].split(",")[i]))
i += 1
print(profiles)
if len(profiles) != numAgents:
print("Wrong number of agent profiles")
else:
table = Table(numRounds, numAgents, bigBlind, chips, profiles)
table.game(numRounds)