-
Notifications
You must be signed in to change notification settings - Fork 0
/
poker.py
443 lines (356 loc) · 12.2 KB
/
poker.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
#Poker logic
from enum import Enum
import random
class Suits(Enum):
Hearts = 0
Diamond = 1
Clubs = 2
Spades = 3
class Faces(Enum):
Ace_Low = -1
Two = 0
Three = 1
Four = 2
Five = 3
Six = 4
Seven = 5
Eight = 6
Nine = 7
Ten = 8
Jack = 9
Queen = 10
King = 11
Ace = 12
class Actions(Enum):
Check = 0
Raise = 1
Call = 2
Fold = 3
class Hands(Enum):
HighCard = 0
Pair = 1
DoublePair = 2
ThreeKind = 3
Straight = 4
Flush = 5
FullHouse = 6
FourKind = 7
StraightFlush = 8
class Card:
def __init__(self, i):
if i < 13:
self.suit = Suits(0)
elif i < 26:
self.suit = Suits(1)
elif i < 39:
self.suit = Suits(2)
elif i < 52:
self.suit = Suits(3)
i = i % 13
self.face = Faces(i)
def prettyPrint(self):
print(self.suit,self.face)
class Player:
def __init__(self, uid, name):
self.uid = uid
self.name = name
self.chips = 0
self.inround = True
self.cards = []
def askAction(self, Game):
i = random.randint(0,3)
act = (Actions(i), 300)
print(self.name," choose action: ", Actions(i))
return act
class pokerGame():
def __init__(self):
self.h_all = [
self.h_StraightFlush,
self.h_FourKind,
self.h_FullHouse,
self.h_Flush,
self.h_Straight,
self.h_ThreeKind,
self.h_DoublePair,
self.h_Pair,
self.h_HighCard
]
self.usedCards = []
self.players = [
Player("uid1", "Alex"),
Player("uid2", "Buddy"),
Player("uid3", "Connel"),
Player("uid4", "Davy Jones"),
Player("uid5", "Elton"),
Player("uid8", "Fred")
]
self.startingValues = 1000
self.bigBlindValue = int(self.startingValues * 0.02)
self.lilBlindValue = int(self.startingValues * 0.01)
self.dealer = 0;
self.pot = 0;
self.tableCards = [];
self.playersCnt = len(self.players)
for p in self.players:
self.giveMoney(p, self.startingValues)
def printScoreBoard(self):
for p in self.players:
print(p.name, ":", p.chips)
def newCard(self):
i = random.randint(0,51)
if i in self.usedCards:
if len(self.usedCards <= 52):
return newCard()
else:
return -1
return Card(i)
def giveMoney(self, plyr, amount):
plyr.chips += amount
def addPot(self, plyr, amount):
self.pot += amount
self.giveMoney(plyr, -amount)
def moreThanOne(self):
inCnt = 0
for p in self.players:
if p.inround:
inCnt += 1
return (inCnt > 1)
def dealPlayers(self):
for p in self.players:
p.cards = [self.newCard(), self.newCard()]
p.inround = True
def roundOfBetting(self, rndNum):
bets = [0] * self.playersCnt
betValue = 0
#Calculates positions of which player has what role per round
if rndNum == 0:
self.dealer = (self.dealer + 1) % len(self.players)
lilBlind = self.dealer - 1
bigBlind = self.dealer - 2
if lilBlind < 0:
lilBlind = self.playersCnt - 1
bigBlind = self.playersCnt - 2
elif bigBlind < 0:
bigBlind = self.playersCnt - 1
self.addPot(self.players[bigBlind], self.bigBlindValue)
bets[bigBlind] += self.bigBlindValue
self.addPot(self.players[lilBlind], self.lilBlindValue)
bets[lilBlind] += self.lilBlindValue
self.folders = 0
betValue = self.bigBlindValue
curP = self.dealer
asksRemaining = self.playersCnt
while asksRemaining > 0:
curPlayer = self.players[curP]
if curPlayer.inround == False:
asksRemaining -= 1
curP = (curP + 1) % self.playersCnt
continue
(action, amount) = self.askAction(curPlayer)
if action is Actions.Raise:
difference = betValue - bets[curP] + amount
if difference <= curPlayer.chips:
bets[curP] += difference
betValue += amount
self.addPot(curPlayer, difference)
asksRemaining = self.playersCnt
else:
continue
elif action is Actions.Call:
callAmount = betValue - bets[curP]
if callAmount == 0:
continue
if callAmount > curPlayer.chips:
callAmount = curPlayer.chips
self.addPot(curPlayer, callAmount)
bets[curP] += callAmount
elif action is Actions.Check:
if bets[curP] != betValue:
continue
else: #only other action is a fold
if self.folders < self.playersCnt - 1:
curPlayer.inround = False
self.folders += 1
else:
continue
asksRemaining -= 1
curP = (curP + 1) % self.playersCnt
#print("betvalue is at:", betValue)
print("Heres what everyone has bet:")
i = 0
for p in self.players:
print(p.name, ":",bets[i])
i += 1
#print("Heres what everyone has bet:", bets)
print("The Pot is now at: ", self.pot, "dollars")
def sortHand(self, hand):
hand.sort(key=lambda x: x.face.value, reverse=True)
return hand
def calcWinner(self):
highScore = -1
winner = -1
for p in self.players:
if p.inround:
scr = self.calculateScore(p.cards)
print(p.name, scr)
if scr > highScore:
highScore = scr
winner = p
return winner
def removeLosers(self):
i = 0
while i < self.playersCnt:
if(self.players[i].chips <= 0):
del self.players[i]
self.playersCnt -= 1
else:
i+=1
def calculateScore(self, userCards):
hand = userCards + self.tableCards.copy()
score = 9
for h in self.h_all:
(has, used, remaining) = h(hand.copy())
if has:
tieBreakCards = used
diff = 5 - len(used)
if diff > 0:
tieBreakCards += self.sortHand(remaining)[0:diff]
tieBreakCards = self.sortHand(tieBreakCards)
mult = 0.01
for c in tieBreakCards:
score += c.face.value * mult
mult *= 0.01
return score
score -= 1
return -1
def h_HighCard(self, hand):
return (True, hand, [])
def h_Pair(self, hand):
for ind1, card1 in enumerate(hand):
for ind2, card2 in enumerate(hand):
if ind1 != ind2 and card1.face == card2.face:
del hand[ind2]
del hand[ind1]
return (True, [card1, card2], hand)
return (False, [], hand)
def h_DoublePair(self, hand):
(firstPair, firstUsed, firstRemaining) = self.h_Pair(hand)
if firstPair:
(secondPair, secondUsed, secondRemaining) = self.h_Pair(firstRemaining)
if secondPair:
return (True, firstUsed + secondUsed, secondRemaining)
return (False, [], hand)
def h_ThreeKind(self, hand):
for ind1, card1 in enumerate(hand):
for ind2, card2 in enumerate(hand):
for ind3, card3 in enumerate(hand):
if ind1 != ind2 and ind2 != ind3 and ind3 != ind1 and card1.face == card2.face and card2.face == card3.face:
del hand[ind3]
del hand[ind2]
del hand[ind1]
return (True, [card1, card2, card3], hand)
return (False, [], hand)
def h_Straight(self, hand):
usedCards = []
cardsInRow = 0
for ind1, card1 in enumerate(hand):
if cardsInRow >= 4:
usedCards.append(ind1)
break
nextInd = ind1 + 1
if nextInd < len(hand):
diff = card1.face.value - hand[nextInd].face.value
if diff == 1:
cardsInRow += 1
usedCards.append(ind1)
elif diff != 0:
usedCards = []
cardsInRow = 0
startInd = nextInd
if cardsInRow >= 4:
resultHand = []
usedCards.reverse()
for ind in usedCards:
resultHand.append(hand[ind])
del hand[ind]
return(True, resultHand, hand)
return (False, [], hand)
def h_Flush(self, hand):
counts = [0] * 4
for card in hand:
counts[card.suit.value] += 1
for indSuit, c in enumerate(counts):
if c >= 5:
resultHand = []
for indCard, card in enumerate(hand):
if card.suit.value == indSuit:
resultHand.append(card)
del hand[indCard]
return (True, resultHand, hand)
return (False, [], hand)
def h_FullHouse(self, hand):
(firstPair, firstUsed, firstRemaining) = self.h_ThreeKind(hand)
if firstPair:
(secondPair, secondUsed, secondRemaining) = self.h_Pair(firstRemaining)
if secondPair:
return (True, firstUsed + secondUsed, secondRemaining)
return (False, [], hand)
def h_FourKind(self, hand):
(firstPair, firstUsed, firstRemaining) = self.h_Pair(hand)
if firstPair:
(secondPair, secondUsed, secondRemaining) = self.h_Pair(firstRemaining)
if secondPair and firstUsed[0].face == secondUsed[1].face:
return (True, firstUsed + secondUsed, secondRemaining)
return (False, [], hand)
def h_StraightFlush(self, hand):
(hasStraight, straightCards, remaining) = self.h_Straight(hand)
if hasStraight:
(hasFlush, flushCards, remaining) = self.h_Flush(straightCards)
if hasFlush:
return (True, straightCards, remaining)
return (False, [], hand)
def round(self):
print("-----------------Round Begin-----------------")
self.dealPlayers()
self.roundOfBetting(0)
if self.moreThanOne():
self.tableCards = [self.newCard(), self.newCard(), self.newCard()]
self.roundOfBetting(1)
if self.moreThanOne():
self.tableCards.append(self.newCard())
self.roundOfBetting(2)
if self.moreThanOne():
self.tableCards.append(self.newCard())
self.roundOfBetting(3
)
winner = self.calcWinner()
self.giveMoney(winner,self.pot)
self.pot = 0
print("=======================Cards=======================")
print("Table:")
for c in self.tableCards:
c.prettyPrint()
print("Players:")
for u in self.players:
print(u.name)
u.cards[0].prettyPrint()
u.cards[1].prettyPrint()
print("===================Cards End=======================")
self.removeLosers()
print("round winner:",winner.name)
self.printScoreBoard()
print("-----------------Round End-----------------")
def askAction(self, plyr):
return plyr.askAction(self)
def prettyPrintScore(has, used, remaining):
print(has)
print("used:")
for u in used:
print(u.face,":",u.suit)
print("remaining:")
for r in remaining:
print(r.face,":",r.suit)
if __name__ == "__main__":
Game = pokerGame()
while Game.playersCnt > 1:
Game.round()