-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeal_compare.py
401 lines (372 loc) · 12 KB
/
deal_compare.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
#-------------------------------------------------------------------------------
# Name: deal_compare.py
# Purpose: This file determines the type of five cards, compare two hands of five cards,
# and find out the largest five cards from seven cards (board cards + hole cards).
#-------------------------------------------------------------------------------
import random
# Transfer card representation from an int to a string.
def transfer(n):
'''
n: an int from [0, 12]
return: a string of a card
'''
if n>0 and n<9:
return str(n+1)
elif n==0:
return str('A')
elif n==9:
return str('T')
elif n==10:
return str('J')
elif n==11:
return str('Q')
else:
return str('K')
# Deal cards for a game
def texassim(p):
'''
p: number of players
return: table: board cards.
players: list of hole cards of each player
'''
cards_d=random.sample(range(52),5+p*2)
#S:%4==0; H:%4==1; D:%4==2; C:%4==3
table=[]
for i in range(5):
cardt=cards_d.pop(0)
if cardt%4==0:
table.append(transfer(cardt//4)+'S')
elif cardt%4==1:
table.append(transfer(cardt//4)+'H')
elif cardt%4==2:
table.append(transfer(cardt//4)+'D')
else:
table.append(transfer(cardt//4)+'C')
players=[]
for i in range(p):
players.append([])
for j in range(2):
cardt=cards_d.pop(0)
if cardt%4==0:
players[i].append(transfer(cardt//4)+'S')
elif cardt%4==1:
players[i].append(transfer(cardt//4)+'H')
elif cardt%4==2:
players[i].append(transfer(cardt//4)+'D')
else:
players[i].append(transfer(cardt//4)+'C')
return table,players
# Determine if the five cards type is highcard
def highcard(a):
'''
a: a list of five cards
return: sorted cards from large to small.
'''
al=sorted([a[0][0]]+[a[1][0]]+[a[2][0]]+[a[3][0]]+[a[4][0]])
dic={'2':0,'3':1,'4':2,'5':4,'6':8,'7':16,'8':32,'9':64,'T':128,'J':256,'Q':512,'K':1024,'A':2048}
tot=0
for i in range(5):
tot+=dic[al[i]]
return tot
# Determine if the five cards type is flush
def flush(a):
'''
a: a list of five cards
return: sorted cards from large to small. True if it is flush.
0. False if it is not flush.
'''
if a[0][1]==a[1][1]==a[2][1]==a[3][1]==a[4][1]:
val=highcard(a)
return val,True
else:
return 0,False
# Determine if the five cards type is straight
def straight(a):
'''
a: a list of five cards
return: the largest card. True if it is straight.
0. False if it is not straight.
'''
al=sorted([a[0][0]]+[a[1][0]]+[a[2][0]]+[a[3][0]]+[a[4][0]])
if al==['2','3','4','5','A']:
return 1,True
elif al==['2','3','4','5','6']:
return 2,True
elif al==['3','4','5','6','7']:
return 3,True
elif al==['4','5','6','7','8']:
return 4,True
elif al==['5','6','7','8','9']:
return 5,True
elif al==['6','7','8','9','T']:
return 6,True
elif al==['7','8','9','J','T']:
return 7,True
elif al==['8','9','J','Q','T']:
return 8,True
elif al==['9','J','K','Q','T']:
return 9,True
elif al==['A','J','K','Q','T']:
return 10,True
else:
return 0,False
# Determine if the five cards type is straight flush
def straightflush(a):
'''
a: a list of five cards
return: the largest card. True if it is straight flush.
0. False if it is not straight flush.
'''
fv,fb=flush(a)
sv,sb=straight(a)
if fb and sb:
return sv,True
else:
return 0,False
# Determine if the five cards type is four of a kind
def fourkind(a):
'''
a: a list of five cards
return: the card of four of a kind, the rest single card. True if it is four of a kind.
0, 0. False if it is not four of a kind.
'''
dic={'2':0,'3':1,'4':2,'5':4,'6':8,'7':16,'8':32,'9':64,'T':128,'J':256,'Q':512,'K':1024,'A':2048}
al=sorted([a[0][0]]+[a[1][0]]+[a[2][0]]+[a[3][0]]+[a[4][0]])
if al[0]==al[1]==al[2]==al[3]:
return dic[al[0]],dic[al[4]],True
elif al[1]==al[2]==al[3]==al[4]:
return dic[al[1]],dic[al[0]],True
else:
return 0,0,False
# Determine if the five cards type is full house
def fullhouse(a):
'''
a: a list of five cards
return: the card of three of a kind, the card of the pair. True if it is full house.
0, 0. False if it is not full house.
'''
dic={'2':0,'3':1,'4':2,'5':4,'6':8,'7':16,'8':32,'9':64,'T':128,'J':256,'Q':512,'K':1024,'A':2048}
al=sorted([a[0][0]]+[a[1][0]]+[a[2][0]]+[a[3][0]]+[a[4][0]])
if al[0]==al[1]==al[2] and al[3]==al[4]:
return dic[al[0]],dic[al[3]],True
elif al[0]==al[1] and al[2]==al[3]==al[4]:
return dic[al[2]],dic[al[0]],True
else:
return 0,0,False
# Determine if the five cards type is three of a kind
def threekind(a):
'''
a: a list of five cards
return: the card of three of a kind, a dictionary of the larger single card, the smaller single card. True if it is three of a kind.
0, 0. False if it is not three of a kind.
'''
dic={'2':0,'3':1,'4':2,'5':4,'6':8,'7':16,'8':32,'9':64,'T':128,'J':256,'Q':512,'K':1024,'A':2048}
al=sorted([a[0][0]]+[a[1][0]]+[a[2][0]]+[a[3][0]]+[a[4][0]])
if al[0]==al[1]==al[2]:
return dic[al[0]],dic[al[3]]+dic[al[4]],True
elif al[1]==al[2]==al[3]:
return dic[al[1]],dic[al[0]]+dic[al[4]],True
elif al[2]==al[3]==al[4]:
return dic[al[2]],dic[al[0]]+dic[al[1]],True
else:
return 0,0,False
# Determine if the five cards type is two pairs
def twopairs(a):
'''
a: a list of five cards
return: a dictionary of the cards of the larger pair, the smaller pair, the card of the rest single card. True if it is two-pair.
0, 0. False if it is not two-pair.
'''
dic={'2':0,'3':1,'4':2,'5':4,'6':8,'7':16,'8':32,'9':64,'T':128,'J':256,'Q':512,'K':1024,'A':2048}
al=sorted([a[0][0]]+[a[1][0]]+[a[2][0]]+[a[3][0]]+[a[4][0]])
if al[0]==al[1] and al[2]==al[3]:
return dic[al[0]]+dic[al[2]],dic[al[4]],True
elif al[0]==al[1] and al[3]==al[4]:
return dic[al[0]]+dic[al[3]],dic[al[2]],True
elif al[1]==al[2] and al[3]==al[4]:
return dic[al[1]]+dic[al[3]],dic[al[0]],True
else:
return 0,0,False
# Determine if the five cards type is one pair
def onepair(a):
'''
a: a list of five cards
return: the card of the pair, a dictionary of the rest cards sorted from large to small. True if it is one-pair.
0, 0. False if it is not one-pair.
'''
dic={'2':0,'3':1,'4':2,'5':4,'6':8,'7':16,'8':32,'9':64,'T':128,'J':256,'Q':512,'K':1024,'A':2048}
al=sorted([a[0][0]]+[a[1][0]]+[a[2][0]]+[a[3][0]]+[a[4][0]])
if al[0]==al[1]:
return dic[al[0]],dic[al[2]]+dic[al[3]]+dic[al[4]],True
elif al[1]==al[2]:
return dic[al[1]],dic[al[0]]+dic[al[3]]+dic[al[4]],True
elif al[2]==al[3]:
return dic[al[2]],dic[al[0]]+dic[al[1]]+dic[al[4]],True
elif al[3]==al[4]:
return dic[al[3]],dic[al[0]]+dic[al[1]]+dic[al[2]],True
else:
return 0,0,False
# Compare hands of two five cards.
def compare(a,b):
'''
a,b: two list of five cards.
return: the larger hand and its type.
'''
# Determine its type from the largest to the smallest.
# straight flush
sfva,sfba=straightflush(a)
sfvb,sfbb=straightflush(b)
if sfva or sfbb:
if sfva>=sfvb:
return a,'straightflush'
else:
return b,'straightflush'
# four of a kind
fourv4a,fourv1a,fourba=fourkind(a)
fourv4b,fourv1b,fourbb=fourkind(b)
if fourba and (not fourbb):
return a,'four-of-a-kind'
elif (not fourba) and fourbb:
return b,'four-of-a-kind'
elif fourba and fourbb:
if fourv4a>fourv4b:
return a,'four-of-a-kind'
elif fourv4a<fourv4b:
return b,'four-of-a-kind'
elif fourv4a==fourv4b:
if fourv1a>=fourv1b:
return a,'four-of-a-kind'
else:
return b,'four-of-a-kind'
# full house
fhousev4a,fhousev1a,fhouseba=fullhouse(a)
fhousev4b,fhousev1b,fhousebb=fullhouse(b)
if fhouseba and (not fhousebb):
return a,'full-house'
elif (not fhouseba) and fhousebb:
return b,'full-house'
elif fhouseba and fhousebb:
if fhousev4a>fhousev4b:
return a,'full-house'
elif fhousev4a<fhousev4b:
return b,'full-house'
elif fhousev4a==fhousev4b:
if fhousev1a>=fhousev1b:
return a,'full-house'
else:
return b,'full-house'
# flush
fva,fba=flush(a)
fvb,fbb=flush(b)
if fba or fbb:
if fva>=fvb:
return a,'flush'
else:
return b,'flush'
# straight
sva,sba=straight(a)
svb,sbb=straight(b)
if sba or sbb:
if sva>=svb:
return a,'straight'
else:
return b,'straight'
# three of a kind
tv3a,tvra,tba=threekind(a)
tv3b,tvrb,tbb=threekind(b)
if tba and (not tbb):
return a,'three-of-a-kind'
elif (not tba) and tbb:
return b,'three-of-a-kind'
elif tba and tbb:
if tv3a>tv3b:
return a,'three-of-a-kind'
elif tv3a<tv3b:
return b,'three-of-a-kind'
elif tv3a==tv3b:
if tvra>=tvrb:
return a,'three-of-a-kind'
else:
return b,'three-of-a-kind'
# two-pair
tuv2a,tuvra,tuba=twopairs(a)
tuv2b,tuvrb,tubb=twopairs(b)
if tuba and (not tubb):
return a,'two-pairs'
elif (not tuba) and tubb:
return b,'two-pairs'
elif tuba and tubb:
if tuv2a>tuv2b:
return a,'two-pairs'
elif tuv2a<tuv2b:
return b,'two-pairs'
elif tuv2a==tuv2b:
if tuvra>=tuvrb:
return a,'two-pairs'
else:
return b,'two-pairs'
# one-pair
onev1a,onevra,oneba=onepair(a)
onev1b,onevrb,onebb=onepair(b)
if oneba and (not onebb):
return a,'one-pair'
elif (not oneba) and onebb:
return b,'one-pair'
elif oneba and onebb:
if onev1a>onev1b:
return a,'one-pair'
elif onev1a<onev1b:
return b,'one-pair'
elif onev1a==onev1b:
if onevra>=onevrb:
return a,'one-pair'
else:
return b,'one-pair'
# high card
va=highcard(a)
vb=highcard(b)
if va>=vb:
return a,'high-card'
else:
return b,'high-card'
# Find out the best five cards from seven cards
def maxeach(table,each):
'''
table: five board cards.
each: two hole cards.
return: the best five card, the cards type.
'''
# Permute all possible combinations and keep the largest.
combine=table+each
sol=combine[:5]
for i in range(3):
for j in range(i+1,4):
for k in range(j+1,5):
for l in range(k+1,6):
for m in range(l+1,7):
each_set=[combine[i]]+[combine[j]]+[combine[k]]+[combine[l]]+[combine[m]]
sol,win_type=compare(sol,each_set)
return sol,win_type
# Find out the best five cards of all players
def maxall(table,allhand):
'''
table: five board cards.
allhand: the list of hole cards of eac player.
return: the best five card and the cards type of each player.
'''
eachhand=[]
for i in range(len(allhand)):
eachhand.append(maxeach(table,allhand[i]))
# print eachhand
sol={}
for j in range(len(eachhand)):
sol_j,w_t=eachhand[j]
sol_index=j
for jj in range(len(eachhand)):
sol_jj,w_tjj=eachhand[jj]
sol_j,w_t=compare(sol_j,sol_jj)
if sol_j==sol_jj:
sol_index=jj
sol[str(sol_index)]=sol_j,w_t
# print sol_j
return sol