-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackTheDeck.py
More file actions
1959 lines (1810 loc) · 80.6 KB
/
Copy pathStackTheDeck.py
File metadata and controls
1959 lines (1810 loc) · 80.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 20:08:18 2020
@author: berube
"""
import os
from random import choice
from itertools import combinations, product
import pygame
from array import array
import ctypes
from ctypes import c_int, c_float
import pickle
from math import log
import sys
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def choose(n, k):
if k < 0 or k > n:
return 0
res = 1
for i in range(k):
res *= (n-i)/(k-i)
return int(res+0.5)
def bisect(func, a, b, args=None, xtol=10**-8):
if args is None:
args = []
x0 = a
x1 = b
if func(x0, *args)*func(x1, *args) > 0:
raise NameError('No zeros in given interval')
while abs(x1-x0) > xtol:
xt = (x0 + x1)/2
if func(x0, *args)*func((x0 + x1)/2, *args) > 0:
x0 = xt
else:
x1 = xt
return (x0+x1)/2
class hashcards():
def __init__(self,
hand_size=7,
deck_size=52):
self.hand_size = hand_size
self.deck_size = deck_size
# self.handidx_table = np_zeros((self.hand_size, self.deck_size),
# dtype='i4')
self.handidx_table = [[0]*self.deck_size
for _ in range(self.hand_size)]
for i in range(self.hand_size):
for j in range(self.deck_size-1):
if j == 0:
self.handidx_table[i][j] = choose(self.deck_size-1-j,
self.hand_size-1-i)
else:
self.handidx_table[i][j] = (
self.handidx_table[i][j-1] *
(self.deck_size-j-self.hand_size+1+i) //
(self.deck_size-j)
)
# self.handidx_table = self.handidx_table.tolist()
def idx(self,
cards):
ordered_cards = [-1] + sorted(cards)
handidx = 0
for i, card in enumerate(ordered_cards[1:]):
handidx += sum(self.handidx_table[i][ordered_cards[i]+1:card])
return handidx
def detect_straight(cards):
# Should be between 0 and 12
holes = []
if len(cards) < 3:
return holes
loop_cards = sorted([c+1 for c in cards], key=lambda x: -x)
if loop_cards[-1] == 1:
loop_cards = [14]+loop_cards
# If the straight already there
if len(cards) >= 5:
diffs = [loop_cards[i]-loop_cards[i+4]
for i in range(len(loop_cards)-4)]
for i, d in enumerate(diffs):
if d == 4:
hole = None
holes.append([hole, loop_cards[i]])
# If one card is missing
if len(cards) >= 4:
diffs = [loop_cards[i]-loop_cards[i+3]
for i in range(len(loop_cards)-3)]
for i, d in enumerate(diffs):
if d == 4:
for h in range(loop_cards[i+3]+1, loop_cards[i]):
if h != loop_cards[i+1] and h != loop_cards[i+2]:
hole = h
break
holes.append([hole, loop_cards[i]])
if d == 3:
hole = loop_cards[i+3]-1
if hole >= 1:
holes.append([hole, loop_cards[i]])
hole = loop_cards[i]+1
if hole <= 14:
holes.append([hole, hole])
# If two cards are missing
diffs = [loop_cards[i]-loop_cards[i+2]
for i in range(len(loop_cards)-2)]
for i, d in enumerate(diffs):
if d == 4:
hole = [c for c in range(loop_cards[i+2]+1, loop_cards[i])
if c != loop_cards[i+1]]
holes.append([hole, loop_cards[i]])
if d == 3:
hole = [loop_cards[i+2] - 1,
loop_cards[i+2] + loop_cards[i] - loop_cards[i+1]]
if hole[0] >= 1:
holes.append([hole, loop_cards[i]])
hole = [loop_cards[i+2] + loop_cards[i] - loop_cards[i+1],
loop_cards[i] + 1]
if hole[1] <= 14:
holes.append([hole, hole[1]])
if d == 2:
hole = [loop_cards[i+2]-2, loop_cards[i+2]-1]
if hole[0] >= 1:
holes.append([hole, loop_cards[i]])
hole = [loop_cards[i+2]-1, loop_cards[i]+1]
if hole[0] >= 1 and hole[1] <= 14:
holes.append([hole, hole[1]])
hole = [loop_cards[i]+1, loop_cards[i]+2]
if hole[1] <= 14:
holes.append([hole, hole[1]])
return holes
class cards_functions():
def __init__(self):
self.hash_5num = hashcards(hand_size=5, deck_size=13)
self.hash_2dim = hashcards(hand_size=2, deck_size=12)
self.hash_2num = hashcards(hand_size=2, deck_size=13)
self.hash_3dim = hashcards(hand_size=3, deck_size=12)
self.hash_7all = hashcards(hand_size=7, deck_size=52)
self.hash_5all = hashcards(hand_size=5, deck_size=52)
self.hole_idx = {}
for i, (c1, c2) in enumerate(combinations(range(52), 2)):
self.hole_idx[(c1, c2)] = i
self.hole_idx[(c2, c1)] = i
self.c_single = [[] for _ in range(52)]
self.c_single_n = [[] for _ in range(13)]
for idx, (c1, c2) in enumerate(combinations(range(52), 2)):
self.c_single[c1].append(idx)
self.c_single[c2].append(idx)
self.c_single_n[c1 % 13].append(idx)
self.c_single_n[c2 % 13].append(idx)
self.c_double_n = [[] for _ in range(13*13)]
for num1, num2 in product(range(13), repeat=2):
idx = num1*13 + num2
for s1, s2 in product(range(4), repeat=2):
hole = (num1+(s1*13), num2+(s2*13))
if num1 == num2:
if s1 < s2:
self.c_double_n[idx].append(self.hole_idx[hole])
else:
self.c_double_n[idx].append(self.hole_idx[hole])
self.c_all = array('i', range(choose(52, 2)))
self.c_single = [array('i', row) for row in self.c_single]
self.c_single_n = [array('i', row) for row in self.c_single_n]
self.c_double_n = [array('i', row) for row in self.c_double_n]
self.all_p = ctypes.cast(self.c_all.buffer_info()[0],
ctypes.POINTER(c_float))
self.single_p = [ctypes.cast(row.buffer_info()[0],
ctypes.POINTER(c_float))
for row in self.c_single]
self.single_n_p = [ctypes.cast(row.buffer_info()[0],
ctypes.POINTER(c_float))
for row in self.c_single_n]
self.double_n_p = [ctypes.cast(row.buffer_info()[0],
ctypes.POINTER(c_float))
for row in self.c_double_n]
with open(resource_path('preflop.pkl'), 'rb') as f:
self.preflop = pickle.load(f)
self.c_holes_score = array('i', [-1]*choose(52, 2))
self.holes_score_p = ctypes.cast(self.c_holes_score.buffer_info()[0],
ctypes.POINTER(c_float))
if os.name == 'nt':
dll = ctypes.CDLL(resource_path('holes_operations.dll'))
winftype = ctypes.WINFUNCTYPE(None)
self.min_update = winftype(("min_update", dll))
self.is_under = winftype(("is_under", dll))
self.is_equal = winftype(("is_equal", dll))
elif os.name == 'posix':
holes_op = ctypes.cdll.LoadLibrary('holes_operations.so')
self.min_update = holes_op.min_update
self.is_under = holes_op.is_under
self.is_equal = holes_op.is_equal
else:
raise NameError('Unknown OS')
# Pointer to holes float array
# Pointer to sub_index int array
# int Score value to update
# int Size of the sub_index
self.min_update.argtypes = [ctypes.POINTER(c_float),
ctypes.POINTER(c_float),
c_int,
c_int]
self.min_update.restype = None
# Pointer to holes float array
# int Score value to compare against
# int Size of the holes array
self.is_under.argtypes = [ctypes.POINTER(c_float),
c_int,
c_int]
# Number of elements under the value
self.is_under.restype = c_int
# Pointer to holes float array
# int Score value to compare against
# int Size of the holes array
self.is_equal.argtypes = [ctypes.POINTER(c_float),
c_int,
c_int]
# Number of elements under the value
self.is_equal.restype = c_int
def cards_to_idx(self, cards):
hand_size = len(cards)
if hand_size == 7:
return self.hash_7all.idx(cards)
elif hand_size == 5:
return self.hash_5all.idx(cards)
elif hand_size == 2:
# c1, c2 = sorted(cards)
# return int(c1*(51-(c1-1)/2-1)+c2-0.5)
return self.hole_idx[tuple(cards)]
else:
raise NameError('Invalid hand_size')
def scores_to_hand(self, score):
if score == 0:
return 'Royal flush', 14
if score < 10:
return 'Straight flush', 14-score
score -= 10
if score < 156:
values = [14 - (score // 12), 14-(score % 12)]
if values[1] <= values[0]:
values[1] -= 1
return 'Four of a kind', values
score -= 156
if score < 156:
values = [14 - (score // 12), 14-(score % 12)]
if values[1] <= values[0]:
values[1] -= 1
return 'Full house', values
score -= 156
if score < 1277:
for cards in combinations(range(13), 5):
if cards != (0, 9, 10, 11, 12):
s = self.hash_5num.idx(cards) - cards[0] - 2
if cards[0] == 0:
s += 1
if s == score:
return 'Flush', [14-c for c in cards]
raise NameError('score bug')
score -= 1277
if score < 10:
return 'Straight', 14-score
score -= 10
if score < 858:
c1 = score//66
for i, hv in enumerate(combinations(range(12), 2)):
if i == score % 66:
values = [14-c1] + [14 - c - (c >= c1) for c in hv]
return 'Three of a kind', values
score -= 858
if score < 858:
for i, hv in enumerate(combinations(range(13), 2)):
if i == score//11:
values = [14 - c for c in hv] + [14 - (score % 11)]
values[2] -= values[2] <= values[0]
values[2] -= values[2] <= values[1]
return 'Two pairs', values
score -= 858
if score < 2860:
for i, hv in enumerate(combinations(range(12), 3)):
if i == score % 220:
values = ([14 - score//220] +
[14 - c - (c >= score//220) for c in hv])
return 'Pair', values
score -= 2860
if score < 1277:
for cards in combinations(range(13), 5):
if cards != (0, 9, 10, 11, 12):
s = self.hash_5num.idx(cards) - cards[0] - 2
if cards[0] == 0:
s += 1
if s == score:
return 'High card', [14-c for c in cards]
raise NameError('invalid score')
def cards_to_score(self, cards):
score = 0
# Royal & straight-flush
loop_cards = [c+c//13 for c in cards]
for c in [0, 14, 28, 42]:
if c in loop_cards:
loop_cards.append(c+13)
loop_cards = sorted(loop_cards)
diffs = [0 for _ in range(len(loop_cards)-4)]
for i in range(1, len(loop_cards)):
if loop_cards[i]-loop_cards[i-1] == 1:
for k in range(len(loop_cards)-4):
if k+1 <= i <= k+4:
diffs[k] += 1
wrong_idx = {10, 11, 12, 13,
24, 25, 26, 27,
38, 39, 40, 41}
hand_values = [loop_cards[i] % 14 + 5 for i, d in enumerate(diffs)
if d == 4 and loop_cards[i] not in wrong_idx]
if hand_values:
hand_values = [max(hand_values)]
score += 14 - hand_values[0]
if score == 0:
return score, 'Royal flush', hand_values
else:
return score, 'Straight flush', hand_values
score += 10
# Four of a kind
hand = [[0]*13 for _ in range(4)]
for card in cards:
suit = card//13
numb = card-suit*13
hand[suit][numb] = 1
num_groups = [sum(n) for n in zip(*hand)]
num_groups = num_groups[1:] + num_groups[0:1]
num_present = [i+2 for i, x in enumerate(num_groups) if x != 0][::-1]
fours = [i+2 for i, x in enumerate(num_groups) if x == 4][::-1]
if fours:
if num_present[0] == fours[0]:
high_card = num_present[1]
else:
high_card = num_present[0]
hand_values = [fours[0], high_card]
score += 12*(14-hand_values[0]) + (14-hand_values[1])
if hand_values[0] > hand_values[1]:
score -= 1
return score, 'Four of a kind', hand_values
score += 156
# Full house
trios = [i+2 for i, x in enumerate(num_groups) if x == 3][::-1]
pairs = [i+2 for i, x in enumerate(num_groups) if x == 2][::-1]
if len(trios) and len(trios)+len(pairs) >= 2:
if len(trios) >= 2:
hand_values = [trios[0], trios[1]]
else:
hand_values = [trios[0], pairs[0]]
score += 12*(14-hand_values[0]) + (14-hand_values[1])
if hand_values[1] < hand_values[0]:
score -= 1
return score, 'Full house', hand_values
score += 156
# Flush
suit_groups = [sum(s) for s in hand]
flush = [i for i, x in enumerate(suit_groups) if x >= 5]
if flush:
hand_values = hand[flush[0]]
hand_values = hand_values[1:] + hand_values[0:1]
hand_values = [i for i, x in enumerate(hand_values)
if x != 0][::-1][:5]
score += (self.hash_5num.idx([12-x for x in hand_values]) +
hand_values[0] - 14)
if hand_values[0] == 12:
score += 1
hand_values = [x+2 for x in hand_values]
return score, 'Flush', hand_values
score += 1277
# Straight
loop_cards = [x-1 for x in num_present[::-1]]
if 13 in loop_cards:
loop_cards = [0] + loop_cards
diffs = [0 for _ in range(len(loop_cards)-4)]
for i in range(1, len(loop_cards)):
if loop_cards[i]-loop_cards[i-1] == 1:
for k in range(len(loop_cards)-4):
if k+1 <= i <= k+4:
diffs[k] += 1
hand_values = [loop_cards[i] + 5 for i, d in enumerate(diffs)
if d == 4 and loop_cards[i] not in wrong_idx]
if hand_values:
hand_values = [max(hand_values)]
score += 14 - hand_values[0]
return score, 'Straight', hand_values
score += 10
# Threes
if trios:
hand_values = [c for c in num_present
if c not in trios]
hand_values = [trios[0]]+hand_values[:2]
hv = [14-v-(v < hand_values[0]) for v in hand_values[1:]]
score += 66*(14-hand_values[0]) + self.hash_2dim.idx(hv)
return score, 'Three of a kind', hand_values
score += 858
# Two pairs
if len(pairs) >= 2:
hand_values = [c for c in num_present
if c not in pairs[:2]]
hand_values = [pairs[0], pairs[1], hand_values[0]]
hv = [14-v for v in hand_values[:2]]
score += 11*self.hash_2num.idx(hv) + 14-hand_values[2]
score -= (hand_values[2] < hand_values[0])
score -= (hand_values[2] < hand_values[1])
return score, 'Two pairs', hand_values
score += 858
# Pairs
if pairs:
hand_values = [c for c in num_present
if c != pairs[0]]
hand_values = [pairs[0]]+hand_values[:3]
hv = [14-v-(v < hand_values[0]) for v in hand_values[1:]]
score += 220*(14-hand_values[0]) + self.hash_3dim.idx(hv)
return score, 'Pair', hand_values
score += 2860
# High card
hand_values = [x-2 for x in num_present[:5]]
score += (self.hash_5num.idx([12-x for x in hand_values]) +
hand_values[0] - 14)
if hand_values[0] == 12:
score += 1
hand_values = [x+2 for x in hand_values]
return score, 'High card', hand_values
def update_holes_score(self,
hole,
score,
hole_type='hole'):
if hole is None or hole_type == 'all':
self.min_update(self.holes_score_p,
self.all_p,
score,
len(self.c_all))
self.break_flag = True
elif hole_type == 'hole':
idx = self.cards_to_idx(hole)
if self.c_holes_score[idx] > score:
self.c_holes_score[idx] = score
elif hole_type == 'card':
self.min_update(self.holes_score_p,
self.single_p[hole],
score,
len(self.c_single[hole]))
elif hole_type == 'num':
num = ((hole - 1) % 13)
self.min_update(self.holes_score_p,
self.single_n_p[num],
score,
len(self.c_single_n[num]))
elif hole_type == 'numnum':
nums = sorted(hole)
idx = ((nums[0] - 1) % 13)*13 + ((nums[1] - 1) % 13)
self.min_update(self.holes_score_p,
self.double_n_p[idx],
score,
len(self.c_double_n[idx]))
else:
raise NameError('Unknown hole_type')
def table_holes(self,
cards):
if len(cards) != 5:
raise NameError('Should be table (n=5) cards')
hand_score = 0
self.c_holes_score = array('i', [7462]*choose(52, 2))
self.holes_score_p = ctypes.cast(self.c_holes_score.buffer_info()[0],
ctypes.POINTER(c_float))
self.break_flag = False
# Royal & straight-flush
for s in range(4):
sub_cards = [c % 13 for c in cards if c//13 == s]
for miss_cards, high_card in detect_straight(sub_cards):
score = hand_score + 14 - high_card
if miss_cards is None:
self.update_holes_score(None, score, 'all')
elif isinstance(miss_cards, int):
hole = (miss_cards - 1) % 13 + s*13
self.update_holes_score(hole, score, 'card')
else:
hole = [(c - 1) % 13 + s*13 for c in miss_cards]
self.update_holes_score(hole, score, 'hole')
if self.break_flag:
for c in cards:
self.update_holes_score(c, -1, 'card')
return self.c_holes_score
hand_score += 10
# Four of a kind
numbs = [((c-1) % 13) + 2 for c in cards]
numbs_count = {}
for n in numbs:
if n in numbs_count:
numbs_count[n] += 1
else:
numbs_count[n] = 1
suits = [c//13 for c in cards]
suits_count = {}
for s in suits:
if s in suits_count:
suits_count[s] += 1
else:
suits_count[s] = 1
for num in [k for k, v in numbs_count.items() if v >= 2]:
high_card = max([n for n in numbs_count if n != num])
score = (hand_score + 12*(14-num) +
(14-high_card) - 1*(num > high_card))
suits_present = [suits[i] for i, n in enumerate(numbs)
if n == num]
if len(suits_present) == 4:
self.update_holes_score(None, score, 'all')
for new_high_card in range(high_card+1, 15):
if new_high_card != num:
score = (hand_score + 12*(14-num) +
(14-new_high_card) - 1*(num > new_high_card))
self.update_holes_score(new_high_card,
score,
'num')
elif len(suits_present) == 3:
miss_suits = [s for s in range(4) if s not in suits_present]
miss_card = (num-1) % 13 + miss_suits[0]*13
self.update_holes_score(miss_card, score, 'card')
for new_high_card in range(high_card+1, 15):
if new_high_card != num:
score = (hand_score + 12*(14-num) +
(14-new_high_card) - 1*(num > new_high_card))
for s in range(4):
hole = (miss_card, (new_high_card-1) % 13 + s*13)
self.update_holes_score(hole, score, 'hole')
else:
miss_suits = [s for s in range(4) if s not in suits_present]
hole = ((num-1) % 13 + miss_suits[0]*13,
(num-1) % 13 + miss_suits[1]*13)
self.update_holes_score(hole, score, 'hole')
if self.break_flag:
for c in cards:
self.update_holes_score(c, -1, 'card')
return self.c_holes_score
hand_score += 156
# Full house
trios = [k for k, v in numbs_count.items() if v == 3]
pairs = [k for k, v in numbs_count.items() if v == 2]
ones = [k for k, v in numbs_count.items() if v == 1]
if len(trios) == 1:
high_card_1 = trios[0]
if len(pairs) == 1:
high_card_2 = pairs[0]
score = (hand_score + 12*(14-high_card_1) +
(14-high_card_2) - 1*(high_card_2 < high_card_1))
self.update_holes_score(None, score, 'all')
if high_card_2 > high_card_1:
score = (hand_score + 12*(14-high_card_2) +
(14-high_card_1) - 1*(high_card_1 < high_card_2))
self.update_holes_score(high_card_2, score, 'num')
for new_high_card_2 in range(high_card_2 + 1, 15):
if new_high_card_2 != high_card_1:
score = (hand_score + 12*(14-high_card_1) +
(14-new_high_card_2) -
1*(new_high_card_2 < high_card_1))
self.update_holes_score([new_high_card_2,
new_high_card_2],
score,
'numnum')
else:
for high_card_2 in ones:
score = (hand_score + 12*(14-high_card_1) +
(14-high_card_2) - 1*(high_card_1 > high_card_2))
self.update_holes_score(high_card_2, score, 'num')
for new_high_card_2 in range(2, 15):
if (new_high_card_2 != high_card_1 and
new_high_card_2 not in ones):
score = (hand_score + 12*(14-high_card_1) +
(14-new_high_card_2) -
1*(new_high_card_2 < high_card_1))
self.update_holes_score([new_high_card_2,
new_high_card_2],
score,
'numnum')
if (new_high_card_2 in ones and
new_high_card_2 > high_card_1):
score = (hand_score + 12*(14-new_high_card_2) +
(14-high_card_1) -
1*(high_card_1 < new_high_card_2))
self.update_holes_score([new_high_card_2,
new_high_card_2],
score,
'numnum')
elif len(pairs) == 2:
for i, high_card_1 in enumerate(pairs):
high_card_2 = pairs[1-i]
score = (hand_score + 12*(14-high_card_1) +
(14-high_card_2) - 1*(high_card_1 > high_card_2))
self.update_holes_score(high_card_1, score, 'num')
if ones[0] > high_card_2:
new_high_card_2 = ones[0]
score = (hand_score + 12*(14-high_card_1) +
(14-new_high_card_2) -
1*(new_high_card_2 < high_card_1))
self.update_holes_score([high_card_1, new_high_card_2],
score,
'numnum')
high_card_1 = ones[0]
high_card_2 = max(pairs)
score = (hand_score + 12*(14-high_card_1) +
(14-high_card_2) - 1*(high_card_1 > high_card_2))
self.update_holes_score([high_card_1, high_card_1],
score,
'numnum')
elif len(pairs) == 1:
high_card_1 = pairs[0]
for high_card_2 in ones:
score = (hand_score + 12*(14-high_card_1) +
(14-high_card_2) - 1*(high_card_2 < high_card_1))
self.update_holes_score([high_card_1, high_card_2],
score,
'numnum')
score = (hand_score + 12*(14-high_card_2) +
(14-high_card_1) - 1*(high_card_1 < high_card_2))
self.update_holes_score([high_card_2, high_card_2],
score,
'numnum')
if self.break_flag:
for c in cards:
self.update_holes_score(c, -1, 'card')
return self.c_holes_score
hand_score += 156
# Flush
possible_flush = [k for k, v in suits_count.items() if v >= 3]
for suit in possible_flush:
numbs_present = [numbs[i] for i, s in enumerate(suits)
if s == suit]
if len(numbs_present) == 5:
high_cards = sorted(numbs_present, key=lambda x: -x)
high_cards_idx = [14-c for c in high_cards]
score = (hand_score + self.hash_5num.idx(high_cards_idx) +
high_cards[0] - 16 + 1*(high_cards[0] == 14))
self.update_holes_score(None, score, 'all')
for new_1 in range(high_cards[4]+1, 15):
if new_1 not in high_cards:
new_card_1 = (new_1-1) % 13 + suit*13
new_high_cards_idx = sorted(
high_cards_idx + [14 - new_1]
)[:5]
h0 = max(high_cards[0], new_1)
score = (hand_score +
self.hash_5num.idx(new_high_cards_idx) +
h0 - 16 + 1*(h0 == 14))
self.update_holes_score(new_card_1, score, 'card')
for new_2 in range(new_1+1, 15):
if new_2 not in high_cards:
new_card_2 = (new_2-1) % 13 + suit*13
hole = (new_card_1, new_card_2)
new_high_cards_idx = sorted(
high_cards_idx + [14 - new_1, 14 - new_2]
)[:5]
h0 = max(high_cards[0], new_1, new_2)
score = (
hand_score +
self.hash_5num.idx(new_high_cards_idx) +
h0 - 16 + 1*(h0 == 14))
self.update_holes_score(hole,
score,
'hole')
elif len(numbs_present) == 4:
for new_1 in range(2, 15):
if new_1 not in numbs_present:
new_card_1 = (new_1-1) % 13 + suit*13
high_cards = sorted(numbs_present+[new_1],
key=lambda x: -x)
high_cards_idx = [14-c for c in high_cards]
score = (hand_score +
self.hash_5num.idx(high_cards_idx) +
high_cards[0] - 16 + 1*(high_cards[0] == 14))
self.update_holes_score(new_card_1, score, 'card')
for new_2 in range(new_1+1, 15):
if new_2 not in numbs_present:
new_card_2 = (new_2-1) % 13 + suit*13
hole = (new_card_1, new_card_2)
new_high_cards_idx = sorted(
high_cards_idx + [14 - new_2]
)[:5]
h0 = max(high_cards[0], new_2)
score = (
hand_score +
self.hash_5num.idx(new_high_cards_idx) +
h0 - 16 + 1*(h0 == 14))
self.update_holes_score(hole,
score,
'hole')
elif len(numbs_present) == 3:
for new_1 in range(2, 15):
new_card_1 = (new_1-1) % 13 + suit*13
for new_2 in range(new_1+1, 15):
if (new_1 not in numbs_present and
new_2 not in numbs_present):
new_card_2 = (new_2-1) % 13 + suit*13
hole = (new_card_1, new_card_2)
high_cards = sorted(numbs_present+[new_1, new_2],
key=lambda x: -x)
high_cards_idx = [14-c for c in high_cards]
score = (hand_score +
self.hash_5num.idx(high_cards_idx) +
high_cards[0] - 16 +
1*(high_cards[0] == 14))
self.update_holes_score(hole, score, 'hole')
if self.break_flag:
for c in cards:
self.update_holes_score(c, -1, 'card')
return self.c_holes_score
hand_score += 1277
# Straight
sub_cards = list(set([c % 13 for c in cards]))
for miss_cards, high_card in detect_straight(sub_cards):
score = hand_score + 14 - high_card
if miss_cards is None:
self.update_holes_score(None, score, 'all')
elif isinstance(miss_cards, int):
for s in range(4):
hole = (miss_cards - 1) % 13 + s*13
self.update_holes_score(hole, score, 'card')
else:
self.update_holes_score([miss_cards[0], miss_cards[1]],
score,
'numnum')
if self.break_flag:
for c in cards:
self.update_holes_score(c, -1, 'card')
return self.c_holes_score
hand_score += 10
# Three of a kind
if len(trios) == 1:
high_card_1 = trios[0]
high_cards = sorted(ones, key=lambda x: -x)
high_cards_idx = [14-v-(v < high_card_1) for v in high_cards]
score = (hand_score + 66*(14-high_card_1) +
self.hash_2dim.idx(high_cards_idx))
self.update_holes_score(None, score, 'all')
for new_1 in [c1 for c1 in range(high_cards[1]+1, 15)
if c1 not in numbs_count]:
new_high_cards_idx = sorted(
high_cards_idx + [14-new_1-(new_1 < high_card_1)]
)[:2]
score = (hand_score + 66*(14-high_card_1) +
self.hash_2dim.idx(new_high_cards_idx))
self.update_holes_score(new_1, score, 'num')
for new_2 in [c2 for c2 in range(new_1+1, 15)
if c2 not in numbs_count]:
new_high_cards_idx = sorted(
high_cards_idx + [14-new_1-(new_1 < high_card_1),
14-new_2-(new_2 < high_card_1)]
)[:2]
score = (hand_score + 66*(14-high_card_1) +
self.hash_2dim.idx(new_high_cards_idx))
self.update_holes_score([new_1, new_2], score, 'numnum')
elif len(pairs) == 1:
high_card_1 = pairs[0]
high_cards = sorted(ones, key=lambda x: -x)[:2]
high_cards_idx = [14-v-(v < high_card_1) for v in high_cards]
score = (hand_score + 66*(14-high_card_1) +
self.hash_2dim.idx(high_cards_idx))
self.update_holes_score(high_card_1, score, 'num')
for new_1 in [c1 for c1 in range(high_cards[1]+1, 15)
if c1 not in numbs_count]:
new_high_cards_idx = sorted(
high_cards_idx + [14-new_1-(new_1 < high_card_1)]
)[:2]
score = (hand_score + 66*(14-high_card_1) +
self.hash_2dim.idx(new_high_cards_idx))
self.update_holes_score([high_card_1, new_1], score, 'numnum')
else:
for high_card_1 in ones:
high_cards = sorted([c for c in ones if c != high_card_1],
key=lambda x: -x)[:2]
high_cards_idx = [14-v-(v < high_card_1) for v in high_cards]
score = (hand_score + 66*(14-high_card_1) +
self.hash_2dim.idx(high_cards_idx))
self.update_holes_score([high_card_1, high_card_1],
score,
'numnum')
if self.break_flag:
for c in cards:
self.update_holes_score(c, -1, 'card')
return self.c_holes_score
hand_score += 858
# Two pairs
if len(pairs) == 2:
high_cards = sorted(pairs, key=lambda x: -x)
high_cards_idx = [14-v for v in high_cards]
high_card_3 = ones[0]
score = (hand_score +
11*self.hash_2num.idx(high_cards_idx) +
14-high_card_3 -
1*(high_card_3 < high_cards[0]) -
1*(high_card_3 < high_cards[1]))
self.update_holes_score(None, score, 'all')
for new_1 in [c1 for c1 in range(high_card_3+1, 15)
if c1 not in numbs_count]:
score = (hand_score +
11*self.hash_2num.idx(high_cards_idx) +
14-new_1 -
1*(new_1 < high_cards[0]) -
1*(new_1 < high_cards[1]))
self.update_holes_score(new_1, score, 'num')
if ones[0] > high_cards[1]:
new_high_cards = sorted([high_cards[0], ones[0]],
key=lambda x: -x)
new_high_cards_idx = [14-v for v in new_high_cards]
new_high_card_3 = high_cards[1]
score = (hand_score +
11*self.hash_2num.idx(new_high_cards_idx) +
14-new_high_card_3 -
1*(new_high_card_3 < new_high_cards[0]) -
1*(new_high_card_3 < new_high_cards[1]))
self.update_holes_score(ones[0], score, 'num')
for new_1 in [c1 for c1 in range(new_high_card_3+1, 15)
if c1 not in numbs_count]:
score = (hand_score +
11*self.hash_2num.idx(new_high_cards_idx) +
14-new_1 -
1*(new_1 < new_high_cards[0]) -
1*(new_1 < new_high_cards[1]))
self.update_holes_score([new_1, ones[0]], score, 'numnum')
for new_1 in [c2 for c2 in range(high_cards[1]+1, 15)
if c2 not in numbs_count]:
new_high_cards = sorted([high_cards[0], new_1],
key=lambda x: -x)
new_high_cards_idx = [14-v for v in new_high_cards]
new_high_card_3 = max(ones[0], high_cards[1])
score = (hand_score +
11*self.hash_2num.idx(new_high_cards_idx) +
14-new_high_card_3 -
1*(new_high_card_3 < new_high_cards[0]) -
1*(new_high_card_3 < new_high_cards[1]))
self.update_holes_score([new_1, new_1], score, 'numnum')
elif len(pairs) == 1:
high_card_1 = pairs[0]
for high_card_2 in ones:
for new_1 in [c1 for c1 in ones
if (c1 != high_card_2 and c1 > high_card_1)]:
high_cards = sorted([new_1, high_card_2],
key=lambda x: -x)
high_card_3 = max([c for c in ones if c not in high_cards])
high_cards_idx = [14-v for v in high_cards]
score = (hand_score +
11*self.hash_2num.idx(high_cards_idx) +
14-high_card_3 -
1*(high_card_3 < high_cards[0]) -
1*(high_card_3 < high_cards[1]))
self.update_holes_score([new_1, high_card_2],
score,
'numnum')
high_cards = sorted([high_card_1, high_card_2],
key=lambda x: -x)
high_card_3 = max([c for c in ones if c != high_card_2])
high_cards_idx = [14-v for v in high_cards]
score = (hand_score +
11*self.hash_2num.idx(high_cards_idx) +
14-high_card_3 -
1*(high_card_3 < high_cards[0]) -
1*(high_card_3 < high_cards[1]))
self.update_holes_score(high_card_2, score, 'num')
for new_1 in [c1 for c1 in range(high_card_3+1, 15)
if c1 not in numbs_count]:
score = (hand_score +
11*self.hash_2num.idx(high_cards_idx) +
14-new_1 -
1*(new_1 < high_cards[0]) -
1*(new_1 < high_cards[1]))
self.update_holes_score([high_card_2, new_1],
score,
'numnum')
for high_card_2 in [c for c in range(2, 15)
if c not in numbs_count]:
high_cards = sorted([high_card_1, high_card_2],
key=lambda x: -x)
high_card_3 = max(ones)
high_cards_idx = [14-v for v in high_cards]
score = (hand_score +
11*self.hash_2num.idx(high_cards_idx) +
14-high_card_3 -
1*(high_card_3 < high_cards[0]) -
1*(high_card_3 < high_cards[1]))
self.update_holes_score([high_card_2, high_card_2],
score,
'numnum')
else:
for i, high_card_1 in enumerate(ones):
for high_card_2 in ones[i+1:]:
high_cards = sorted([high_card_1, high_card_2],
key=lambda x: -x)
high_cards_idx = [14-v for v in high_cards]
high_card_3 = max([c for c in ones if c not in high_cards])
score = (hand_score +
11*self.hash_2num.idx(high_cards_idx) +
14-high_card_3 -
1*(high_card_3 < high_cards[0]) -
1*(high_card_3 < high_cards[1]))
self.update_holes_score([high_card_1, high_card_2],
score,
'numnum')
if self.break_flag:
for c in cards:
self.update_holes_score(c, -1, 'card')
return self.c_holes_score
hand_score += 858
# Pair
if len(pairs) == 1:
high_card_1 = pairs[0]
high_cards = sorted(ones, key=lambda x: -x)
high_cards_idx = [14-v-(v < high_card_1) for v in high_cards]
score = (hand_score + 220*(14-high_card_1) +
self.hash_3dim.idx(high_cards_idx))
self.update_holes_score(None, score, 'all')
for new_1 in [c for c in range(high_cards[2]+1, 15)
if c not in numbs_count]:
new_high_cards_idx = sorted(
high_cards_idx + [14-new_1-(new_1 < high_card_1)]
)[:3]
score = (hand_score + 220*(14-high_card_1) +
self.hash_3dim.idx(new_high_cards_idx))
self.update_holes_score(new_1, score, 'num')
for new_2 in [c for c in range(new_1+1, 15)
if c not in numbs_count]:
new_high_cards_idx = sorted(
high_cards_idx + [14-new_1-(new_1 < high_card_1),
14-new_2-(new_2 < high_card_1)]
)[:3]
score = (hand_score + 220*(14-high_card_1) +
self.hash_3dim.idx(new_high_cards_idx))
self.update_holes_score([new_1, new_2], score, 'numnum')
else:
for high_card_1 in ones:
high_cards = sorted([c for c in ones if c != high_card_1],
key=lambda x: -x)[:3]
high_cards_idx = [14-v-(v < high_card_1) for v in high_cards]