-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.py
437 lines (363 loc) · 20.9 KB
/
deck.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
import numpy as np
import pyodbc
class Deck:
rng = np.random.default_rng()
# CARDS = ['A(S)', '2(S)', '3(S)', '4(S)', '5(S)', '6(S)', '7(S)', '8(S)', '9(S)', 'T(S)', 'J(S)', 'Q(S)', 'K(S)',
# 'A(H)', '2(H)', '3(H)', '4(H)', '5(H)', '6(H)', '7(H)', '8(H)', '9(H)', 'T(H)', 'J(H)', 'Q(H)', 'K(H)',
# 'A(C)', '2(C)', '3(C)', '4(C)', '5(C)', '6(C)', '7(C)', '8(C)', '9(C)', 'T(C)', 'J(C)', 'Q(C)', 'K(C)',
# 'A(D)', '2(D)', '3(D)', '4(D)', '5(D)', '6(D)', '7(D)', '8(D)', '9(D)', 'T(D)', 'J(D)', 'Q(D)', 'K(D)', ]
CARDS = ['A(S)', 'K(S)', 'Q(S)', 'J(S)', 'T(S)', '9(S)', '8(S)', '7(S)', '6(S)', '5(S)', '4(S)', '3(S)', '2(S)',
'A(H)', 'K(H)', 'Q(H)', 'J(H)', 'T(H)', '9(H)', '8(H)', '7(H)', '6(H)', '5(H)', '4(H)', '3(H)', '2(H)',
'A(C)', 'K(C)', 'Q(C)', 'J(C)', 'T(C)', '9(C)', '8(C)', '7(C)', '6(C)', '5(C)', '4(C)', '3(C)', '2(C)',
'A(D)', 'K(D)', 'Q(D)', 'J(D)', 'T(D)', '9(D)', '8(D)', '7(D)', '6(D)', '5(D)', '4(D)', '3(D)', '2(D)', ]
POSITION = {2: ['bb', 'dealer'],
3: ['sb', 'bb', 'dealer'],
4: ['sb', 'bb', 'utg', 'dealer'],
5: ['sb', 'bb', 'utg', 'utg+1', 'dealer'],
6: ['sb', 'bb', 'utg', 'utg+1', 'utg+2', 'dealer'],
7: ['sb', 'bb', 'utg', 'utg+1', 'utg+2', 'utg+3', 'dealer'],
8: ['sb', 'bb', 'utg', 'utg+1', 'utg+2', 'utg+3', 'utg+4', 'dealer'],
9: ['sb', 'bb', 'utg', 'utg+1', 'utg+2', 'utg+3', 'utg+4', 'utg+5', 'dealer'],
10: ['sb', 'bb', 'utg', 'utg+1', 'utg+2', 'utg+3', 'utg+4', 'utg+5', 'utg+6', 'dealer'],
}
HANDS_RANK = {
1: [[CARDS[0+13*0], CARDS[12+13*0], CARDS[11+13*0], CARDS[10+13*0], CARDS[9+13*0]],
[CARDS[0+13*1], CARDS[12+13*1], CARDS[11+13*1], CARDS[10+13*1], CARDS[9+13*1]],
[CARDS[0+13*2], CARDS[12+13*2], CARDS[11+13*2], CARDS[10+13*2], CARDS[9+13*2]],
[CARDS[0+13*3], CARDS[12+13*3], CARDS[11+13*3], CARDS[10+13*3], CARDS[9+13*3]]]
}
def write_to_db():
conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=LIN9400F\SQL2ETL;"
"Database=Poker;"
"Trusted_Connection=yes;")
cursor = conn.cursor()
cursor.execute('TRUNCATE TABLE [dbo].[T_Card]')
for cid, c in enumerate(Deck.CARDS):
cursor.execute(
"INSERT INTO [dbo].[T_Card] ([CID],[STR]) VALUES ({},'{}') \
".format(cid, c)
)
cursor.commit()
conn.close()
class Pop:
def __init__(self, deck, number_of_players):
self.players = deck[:number_of_players]
self.flop = deck[number_of_players:number_of_players+2].reshape((4,))
self.turn = deck[number_of_players+2:number_of_players+3].reshape((2,))
self.river = deck[number_of_players+3:number_of_players+4].reshape((2,))
self.public = [self.flop[1], self.flop[2], self.flop[3], self.turn[1], self.river[1]]
def to_string(self):
pop_str = ''
_position = Deck.POSITION[len(self.players)]
for idx, p in enumerate(self.players):
pop_str = pop_str + _position[idx] + '[' + Deck.CARDS[p[0]] + ',' + Deck.CARDS[p[1]] + ']\n'
pop_str = pop_str + 'flop' + '[' \
+ Deck.CARDS[self.flop[1]] + ',' \
+ Deck.CARDS[self.flop[2]] + ',' \
+ Deck.CARDS[self.flop[3]] + ']\n'
pop_str = pop_str + 'turn' + '[' + Deck.CARDS[self.turn[1]] + ']\n'
pop_str = pop_str + 'river' + '[' + Deck.CARDS[self.river[1]] + ']\n'
return pop_str
def print(self):
_position = Deck.POSITION[len(self.players)]
for idx, p in enumerate(self.players):
print(_position[idx], '[', Deck.CARDS[p[0]], ',', Deck.CARDS[p[1]], ']')
print('flop', '[', Deck.CARDS[self.flop[1]], ',', Deck.CARDS[self.flop[2]], ',', Deck.CARDS[self.flop[3]], ']')
print('turn', '[', Deck.CARDS[self.turn[1]], ']')
print('river', '[', Deck.CARDS[self.river[1]], ']')
def get_winner_rank(self):
pass
def __init__(self):
self.cards = np.arange(52)
self.shuffled_deck = None
def shuffle(self):
np.random.shuffle(self.cards)
self.shuffled_deck = self.cards.reshape((26, 2))
# print(self.shuffled_deck)
return self
def pop(self, number_of_players):
import copy
_pop = self.Pop(self.shuffled_deck, number_of_players)
# print(pop.players)
# print(pop.flop)
# print(pop.turn)
# print(pop.river)
return copy.deepcopy(_pop)
def straight_flush(rank, rank_index):
for x in range(0, 13):
if x <= 9:
rank[rank_index] = []
for c in range(0, 4):
rank[rank_index].append([(c*13)+x, (c*13)+x+1, (c*13)+x+2, (c*13)+x+3, (((c*13)+x+4) % 13) + (c*13),
f'Straight Flush[{Deck.CARDS[(c*13)+x]}, {Deck.CARDS[(c*13)+x+1]}, '
f'{Deck.CARDS[(c*13)+x+2]}, {Deck.CARDS[(c*13)+x+3]}, '
f'{Deck.CARDS[(((c*13)+x+4) % 13) + (c*13)]}]', rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def four_of_a_kind(rank, rank_index):
for x in range(0, 13):
for y in range(0, 13):
if x != y:
rank[rank_index] = []
for c in range(0, 4):
#print(x, x+13, x+13*2, x+13*3, y+c*13)
rank[rank_index].append([x, x+13, x+13*2, x+13*3, y+c*13,
f'Four Of A Kind[{Deck.CARDS[x]}, {Deck.CARDS[x+13]}, {Deck.CARDS[x+13*2]},'
f'{Deck.CARDS[x+13*3]}, {Deck.CARDS[y+c*13]}]', rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def full_house(rank, rank_index):
for x in range(0, 13):
for y in range(0, 13):
if x != y:
rank[rank_index] = []
for c in range(0, 4):
for c2 in range(0, 6):
rank[rank_index].append([x+13*(1 if c > 2 else 0), x+13*(2 if c > 1 else 1),
x+13*(3 if c > 0 else 2),
y+13*(0 if c2 < 3 else 1 if c2 < 5 else 2),
y+13*(c2+1 if c2 < 3 else (c2 % 3) + 2 if c2 < 5 else 3),
f'Full House[{Deck.CARDS[x+13*(1 if c > 2 else 0)]}, '
f'{Deck.CARDS[x+13*(2 if c > 1 else 1)]}, '
f'{Deck.CARDS[x+13*(3 if c > 0 else 2)]},'
f'{Deck.CARDS[y+13*(0 if c2 < 3 else 1 if c2 < 5 else 2)]}, '
f'{Deck.CARDS[y+13*(c2+1 if c2 < 3 else (c2 % 3) + 2 if c2 < 5 else 3)]}]',
rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def flush(rank, rank_index):
for x in range(0, 13):
for x1 in range(x, 13):
for x2 in range(x1, 13):
for x3 in range(x2, 13):
for x4 in range(x3, 13):
if x != x1 and x1 != x2 and x2 != x3 and x3 != x4:
if not ((x1 == x+1 and x2 == x1+1 and x3 == x2+1 and x4 == x3+1)
or (x == 0 and x1 == 9 and x2 == 10 and x3 == 11 and x4 == 12)):
rank[rank_index] = []
for c in range(0, 4):
rank[rank_index].append([(c*13)+x, (c*13)+x1, (c*13)+x2, (c*13)+x3, (c*13)+x4,
f'Flush[{Deck.CARDS[(c*13)+x]}, '
f'{Deck.CARDS[(c*13)+x1]}, '
f'{Deck.CARDS[(c*13)+x2]}, '
f'{Deck.CARDS[(c*13)+x3]}, '
f'{Deck.CARDS[(c*13)+x4]}]', rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def straight(rank, rank_index):
for x in range(0, 13):
if x <= 9:
rank[rank_index] = []
for c1 in range(0, 4):
for c2 in range(0, 4):
for c3 in range(0, 4):
for c4 in range(0, 4):
for c5 in range(0, 4):
if c1 != c2 or c2 != c3 or c3 != c4 or c4 != c5 or c5 != c1:
rank[rank_index].append([(c1*13)+x,
(c2*13)+x+1,
(c3*13)+x+2,
(c4*13)+x+3,
(((c5*13)+x+4) % 13) + (c5*13),
f'Straight[{Deck.CARDS[(c1*13)+x]}, '
f'{Deck.CARDS[(c2*13)+x+1]}, '
f'{Deck.CARDS[(c3*13)+x+2]}, '
f'{Deck.CARDS[(c4*13)+x+3]}, '
f'{Deck.CARDS[(((c5*13)+x+4) % 13) + (c5*13)]}]',
rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def three_of_a_kind(rank, rank_index):
for x in range(0, 13):
for y in range(0, 13):
if x != y:
for z in range(y, 13):
if x != z and y!=z:
rank[rank_index] = []
for c in range(0, 4):
for c1 in range(0, 4):
for c2 in range(0, 4):
rank[rank_index].append([x+13*(1 if c > 2 else 0), x+13*(2 if c > 1 else 1),
x+13*(3 if c > 0 else 2),
y+13*c1,
z+13*c2,
f'Three Of A Kind[{Deck.CARDS[x+13*(1 if c > 2 else 0)]}, '
f'{Deck.CARDS[x+13*(2 if c > 1 else 1)]}, '
f'{Deck.CARDS[x+13*(3 if c > 0 else 2)]},'
f'{Deck.CARDS[y+13*c1]}, '
f'{Deck.CARDS[z+13*c2]}]',
rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def two_pair(rank, rank_index):
for x in range(0, 13):
for y in range(x, 13):
if x != y:
for z in range(0, 13):
if x != z and y != z:
rank[rank_index] = []
for cx1 in range(0, 4):
for cx2 in range(cx1, 4):
if cx1 != cx2:
for cy1 in range(0, 4):
for cy2 in range(cy1, 4):
if cy1 != cy2:
for cz1 in range(0, 4):
rank[rank_index].append([x+13*cx1, x+13*cx2,
y+13*cy1, y+13*cy2,
z+13*cz1,
f'Two Pair[{Deck.CARDS[x+13*cx1]}, '
f'{Deck.CARDS[x+13*cx2]}, '
f'{Deck.CARDS[y+13*cy1]},'
f'{Deck.CARDS[y+13*cy2]}, '
f'{Deck.CARDS[z+13*cz1]}]',
rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def one_pair(rank, rank_index):
for x in range(0, 13):
for y1 in range(0, 13):
for y2 in range(y1, 13):
for y3 in range(y2, 13):
if x != y1 and x != y2 and x != y3 and y1 < y2 and y2 < y3:
rank[rank_index] = []
for cx1 in range(0, 4):
for cx2 in range(cx1, 4):
if cx1 != cx2:
for cy1 in range(0, 4):
for cy2 in range(0, 4):
for cy3 in range(0, 4):
rank[rank_index].append([x+13*cx1, x+13*cx2,
y1+13*cy1, y2+13*cy2,
y3+13*cy3,
f'One Pair[{Deck.CARDS[x+13*cx1]}, '
f'{Deck.CARDS[x+13*cx2]}, '
f'{Deck.CARDS[y1+13*cy1]},'
f'{Deck.CARDS[y2+13*cy2]}, '
f'{Deck.CARDS[y3+13*cy3]}]',
rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def high_card(rank, rank_index):
for x1 in range(0, 13):
for x2 in range(x1, 13):
for x3 in range(x2, 13):
for x4 in range(x3, 13):
for x5 in range(x4, 13):
if x1 != x2 != x3 != x4 != x5 \
and not ((x1+1) == x2 and (x2+1) == x3 and (x3+1) == x4 and ((x4+1) == x5)) \
and not (x1 == 0 and (x2+1) == x3 and (x3+1) == x4 and ((x4+1) == x5) and ((x5+1) % 13 == x1)):
rank[rank_index] = []
for cx1 in range(0, 4):
for cx2 in range(0, 4):
for cx3 in range(0, 4):
for cx4 in range(0, 4):
for cx5 in range(0, 4):
if not(cx1 == cx2 == cx3 == cx4 == cx5):
rank[rank_index].append([x1+13*cx1, x2+13*cx2,
x3+13*cx3, x4+13*cx4,
x5+13*cx5,
f'High Card[{Deck.CARDS[x1+13*cx1]}, '
f'{Deck.CARDS[x2+13*cx2]}, '
f'{Deck.CARDS[x3+13*cx3]},'
f'{Deck.CARDS[x4+13*cx4]}, '
f'{Deck.CARDS[x5+13*cx5]}]',
rank_index])
rank['total'] = rank['total'] + 1
rank_index = rank_index - 1
return rank, rank_index
def combinations():
#https://zh.wikipedia.org/zh-cn/%E6%92%B2%E5%85%8B%E7%89%8C%E5%9E%8B
rank = {'total': 0}
rank_index = 10000000
rank, rank_index = straight_flush(rank, rank_index)
rank, rank_index = four_of_a_kind(rank, rank_index)
rank, rank_index = full_house(rank, rank_index)
rank, rank_index = flush(rank, rank_index)
rank, rank_index = straight(rank, rank_index)
rank, rank_index = three_of_a_kind(rank, rank_index)
rank, rank_index = two_pair(rank, rank_index)
rank, rank_index = one_pair(rank, rank_index)
rank, rank_index = high_card(rank, rank_index)
write_rank_to_db(rank)
#print(rank)
def random_hands(num_of_players, num_of_hands):
# Use a breakpoint in the code line below to debug your script.
new_deck = Deck()
generated_hands = []
for t in range(0, num_of_hands):
new_deck.shuffle()
hand = new_deck.pop(num_of_players)
#print(hand.to_string())
generated_hands.append(hand)
return generated_hands
def write_rank_to_db(rank):
conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=LIN9400F\SQL2ETL;"
"Database=Poker;"
"Trusted_Connection=yes;")
cursor = conn.cursor()
cursor.execute('TRUNCATE TABLE [dbo].[T_CRank]')
for k in rank.keys():
if k == 'total':
continue
combs = rank[k]
for c in combs:
# print(c)
# print("INSERT INTO [dbo].[T_CRank] ([C1],[C2],[C3],[C4],[C5],[R],[STR]) VALUES ({},{},{},{},{},{},'{}') \
# ".format(c[0], c[1], c[2], c[3], c[4], c[6], c[5]))
cursor.execute(
"INSERT INTO [dbo].[T_CRank] ([C1],[C2],[C3],[C4],[C5],[R],[STR]) VALUES ({},{},{},{},{},{},'{}') \
".format(c[0], c[1], c[2], c[3], c[4], c[6], c[5])
)
cursor.commit()
conn.close()
def write_hand_to_db(hands, players_count):
conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=LIN9400F\SQL2ETL;"
"Database=Poker;"
"Trusted_Connection=yes;")
cursor = conn.cursor()
#cursor.execute('TRUNCATE TABLE [dbo].[T_RHand_{}MAX]'.format(players_count))
for h in hands:
str_insert_stmt = "INSERT INTO [dbo].[T_RHand_{}MAX] ([Player_Count], [P1_C1], [P1_C2]".format(players_count)
if players_count < 2:
continue
else:
for pc in range(2, players_count + 1):
str_insert_stmt = str_insert_stmt + ',[P{}_C1], [P{}_C2]'.format(pc, pc)
str_insert_stmt = str_insert_stmt + ',[Flop_B], [Flop_C1], [Flop_C2], [Flop_C3], [Turn_B], [Turn_C1], [River_B], [River_C1], [STR])'
str_insert_stmt = str_insert_stmt + ' VALUES ({},{},{},{},{}'.format(players_count, h.players[0][0], h.players[0][1], h.players[1][0], h.players[1][1])
for pc in range(2, players_count):
str_insert_stmt = str_insert_stmt + ',{},{}'.format(h.players[pc][0], h.players[pc][1])
str_insert_stmt = str_insert_stmt + \
",{}, {}, {}, {}, {}, {}, {}, {}, '{}')"\
.format(h.flop[0], h.flop[1], h.flop[2], h.flop[3], h.turn[0], h.turn[1], h.river[0], h.river[1], h.to_string())
#print(str_insert_stmt)
cursor.execute(str_insert_stmt)
cursor.commit()
conn.close()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
import time
start_time = time.time()
#Deck.write_to_db()
#combinations()
#random_hands(8, 10000000)
pc = 6
for _x in range(0, 5):
_hands = random_hands(pc, 1000000)
write_hand_to_db(_hands, pc)
print("--- %s seconds ---" % (time.time() - start_time))
print("--- %s seconds ---" % (time.time() - start_time))
#print_rank()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/