-
Notifications
You must be signed in to change notification settings - Fork 17
/
pypokereval.c
1153 lines (1037 loc) · 33 KB
/
pypokereval.c
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
/*
*
* Copyright (C) 2007, 2008 Loic Dachary <loic@dachary.org>
* Copyright (C) 2004, 2005, 2006 Mekensleep
*
* Mekensleep
* 24 rue vieille du temple
* 75004 Paris
* licensing@mekensleep.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors:
* Loic Dachary <loic@dachary.org>
*
*/
#ifdef _DEBUG // for Windows python23_d.lib is not in distribution... ugly but works
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif
#include <bytesobject.h>
#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif
struct module_state {
PyObject *error;
};
#ifdef IS_PY3K
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#define PyInteger_Check PyLong_Check
#define PyInteger_AsLong PyLong_AsLong
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#define PyInteger_Check PyInt_Check
#define PyInteger_AsLong PyInt_AsLong
#endif
/* enumerate.c -- functions to compute pot equity by enumerating outcomes
Exports:
enumExhaustive() exhaustive enumeration of outcomes
enumGameParams() look up rule parameters by game type
enumResultClear() clear enumeration result object
enumResultPrint() print enumeration result object
enumResultPrintTerse() print enumeration result object, tersely
enumSample() monte carlo sampling of outcomes
Michael Maurer, Apr 2002
*/
#include "poker_defs.h"
#include "inlines/eval.h"
#include "inlines/eval_low.h"
#include "inlines/eval_low8.h"
#include "inlines/eval_joker_low.h"
#include "inlines/eval_omaha.h"
#include "deck_std.h"
#include "rules_std.h"
#include "enumerate.h"
#include "enumdefs.h"
#ifdef WIN32
#define VERSION_NAME(W) W##2_4
#define PYTHON_VERSION "2_4"
#endif /* WIN32 */
/* INNER_LOOP is executed in every iteration of the combinatorial enumerator
macros DECK_ENUMERATE_n_CARDS_D() and DECK_ENUMERATE_PERMUTATIONS_D. It
evaluates each player's hand based on the enumerated community cards and
accumulates statistics on wins, ties, losses, and pot equity.
Macro argument:
evalwrap -- code that evaluates pockets[i], board, sharedCards, and/or
unsharedCards[i] as a poker hand, then stores the result
in hival[i] and loval[i] and stores an error code in err
Loop variable: either of
StdDeck_CardMask sharedCards;
StdDeck_CardMask unsharedCards[];
Inputs:
StdDeck_CardMask pockets[];
StdDeck_CardMask board;
int npockets;
Outputs:
enum_result_t *result;
*/
#define INNER_LOOP(evalwrap) \
do { \
int i; \
HandVal hival[ENUM_MAXPLAYERS]; \
LowHandVal loval[ENUM_MAXPLAYERS]; \
HandVal besthi = HandVal_NOTHING; \
LowHandVal bestlo = LowHandVal_NOTHING; \
int hishare = 0; \
int loshare = 0; \
double hipot, lopot; \
/* find winning hands for high and low */ \
for (i=0; i<sizeToDeal-1; i++) { \
int err; \
{ evalwrap } \
if (err != 0) \
return 1000 + err; \
if (hival[i] != HandVal_NOTHING) { \
if (hival[i] > besthi) { \
besthi = hival[i]; \
hishare = 1; \
} else if (hival[i] == besthi) { \
hishare++; \
} \
} \
if (loval[i] != LowHandVal_NOTHING) { \
if (loval[i] < bestlo) { \
bestlo = loval[i]; \
loshare = 1; \
} else if (loval[i] == bestlo) { \
loshare++; \
} \
} \
} \
/* now award pot fractions to winning hands */ \
if (bestlo != LowHandVal_NOTHING && \
besthi != HandVal_NOTHING) { \
hipot = 0.5 / hishare; \
lopot = 0.5 / loshare; \
} else if (bestlo == LowHandVal_NOTHING && \
besthi != HandVal_NOTHING) { \
hipot = 1.0 / hishare; \
lopot = 0; \
} else if (bestlo != LowHandVal_NOTHING && \
besthi == HandVal_NOTHING) { \
hipot = 0; \
lopot = 1.0 / loshare; \
} else { \
hipot = lopot = 0; \
} \
for (i=0; i<sizeToDeal-1; i++) { \
double potfrac = 0; \
int H = 0, L = 0; \
if (hival[i] != HandVal_NOTHING) { \
if (hival[i] == besthi) { \
H = hishare; \
potfrac += hipot; \
if (hishare == 1) \
result->nwinhi[i]++; \
else \
result->ntiehi[i]++; \
} else { \
result->nlosehi[i]++; \
} \
} \
if (loval[i] != LowHandVal_NOTHING) { \
if (loval[i] == bestlo) { \
L = loshare; \
potfrac += lopot; \
if (loshare == 1) \
result->nwinlo[i]++; \
else \
result->ntielo[i]++; \
} else { \
result->nloselo[i]++; \
} \
} \
result->nsharehi[i][H]++; \
result->nsharelo[i][L]++; \
result->nshare[i][H][L]++; \
if (potfrac > 0.99) \
result->nscoop[i]++; \
result->ev[i] += potfrac; \
} \
result->nsamples++; \
} while (0);
#define INNER_LOOP_ANY_HIGH \
INNER_LOOP({ \
StdDeck_CardMask _hand; \
StdDeck_CardMask _finalBoard; \
StdDeck_CardMask_RESET(_hand); \
StdDeck_CardMask_RESET(_finalBoard); \
StdDeck_CardMask_OR(_finalBoard, board, cardsDealt[0]); \
StdDeck_CardMask_OR(_hand, pockets[i], _finalBoard); \
StdDeck_CardMask_OR(_hand, _hand, cardsDealt[i + 1]); \
hival[i] = StdDeck_StdRules_EVAL_N(_hand, 7); \
loval[i] = LowHandVal_NOTHING; \
err = 0; \
})
#define INNER_LOOP_ANY_HILO \
INNER_LOOP({ \
StdDeck_CardMask _hand; \
StdDeck_CardMask _finalBoard; \
StdDeck_CardMask_RESET(_hand); \
StdDeck_CardMask_RESET(_finalBoard); \
StdDeck_CardMask_OR(_finalBoard, board, cardsDealt[0]); \
StdDeck_CardMask_OR(_hand, pockets[i], _finalBoard); \
StdDeck_CardMask_OR(_hand, _hand, cardsDealt[i + 1]); \
hival[i] = StdDeck_StdRules_EVAL_N(_hand, 7); \
loval[i] = StdDeck_Lowball8_EVAL(_hand, 7); \
err = 0; \
})
#define INNER_LOOP_OMAHA \
INNER_LOOP({ \
StdDeck_CardMask _hand; \
StdDeck_CardMask _finalBoard; \
StdDeck_CardMask_RESET(_hand); \
StdDeck_CardMask_RESET(_finalBoard); \
StdDeck_CardMask_OR(_finalBoard, board, cardsDealt[0]); \
StdDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
err = StdDeck_OmahaHiLow8_EVAL(_hand, _finalBoard, \
&hival[i], NULL); \
loval[i] = LowHandVal_NOTHING; \
})
#define INNER_LOOP_OMAHA8 \
INNER_LOOP({ \
StdDeck_CardMask _hand; \
StdDeck_CardMask _finalBoard; \
StdDeck_CardMask_RESET(_hand); \
StdDeck_CardMask_RESET(_finalBoard); \
StdDeck_CardMask_OR(_finalBoard, board, cardsDealt[0]); \
StdDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
err = StdDeck_OmahaHiLow8_EVAL(_hand, _finalBoard, \
&hival[i], &loval[i]); \
})
#define INNER_LOOP_7STUDNSQ \
INNER_LOOP({ \
StdDeck_CardMask _hand; \
StdDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
hival[i] = StdDeck_StdRules_EVAL_N(_hand, 7); \
loval[i] = StdDeck_Lowball_EVAL(_hand, 7); \
err = 0; \
})
#define INNER_LOOP_RAZZ \
INNER_LOOP({ \
StdDeck_CardMask _hand; \
StdDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
hival[i] = HandVal_NOTHING; \
loval[i] = StdDeck_Lowball_EVAL(_hand, 7); \
err = 0; \
})
#define INNER_LOOP_5DRAW \
INNER_LOOP({ \
JokerDeck_CardMask _hand; \
JokerDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
hival[i] = JokerDeck_JokerRules_EVAL_N(_hand, 5); \
loval[i] = LowHandVal_NOTHING; \
err = 0; \
})
#define INNER_LOOP_5DRAW8 \
INNER_LOOP({ \
JokerDeck_CardMask _hand; \
JokerDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
hival[i] = JokerDeck_JokerRules_EVAL_N(_hand, 5); \
loval[i] = JokerDeck_Lowball8_EVAL(_hand, 5); \
err = 0; \
})
#define INNER_LOOP_5DRAWNSQ \
INNER_LOOP({ \
JokerDeck_CardMask _hand; \
JokerDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
hival[i] = JokerDeck_JokerRules_EVAL_N(_hand, 5); \
loval[i] = JokerDeck_Lowball_EVAL(_hand, 5); \
err = 0; \
})
#define INNER_LOOP_LOWBALL \
INNER_LOOP({ \
JokerDeck_CardMask _hand; \
JokerDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
hival[i] = HandVal_NOTHING; \
loval[i] = JokerDeck_Lowball_EVAL(_hand, 5); \
err = 0; \
})
#define INNER_LOOP_LOWBALL27 \
INNER_LOOP({ \
StdDeck_CardMask _hand; \
StdDeck_CardMask_OR(_hand, pockets[i], cardsDealt[i + 1]); \
hival[i] = HandVal_NOTHING; \
loval[i] = StdDeck_StdRules_EVAL_N(_hand, 5); \
err = 0; \
})
static int
pyenumExhaustive(enum_game_t game, StdDeck_CardMask pockets[],
int numToDeal[],
StdDeck_CardMask board, StdDeck_CardMask dead,
int sizeToDeal, enum_result_t *result) {
int totalToDeal = 0;
int i;
enumResultClear(result);
StdDeck_CardMask cardsDealt[ENUM_MAXPLAYERS + 1];
memset(cardsDealt, 0, sizeof(StdDeck_CardMask) * (ENUM_MAXPLAYERS + 1));
if (sizeToDeal - 1 > ENUM_MAXPLAYERS)
return 1;
for(i = 0; i < sizeToDeal; i++)
totalToDeal += numToDeal[i];
/*
* Cards in pockets or in the board must not be dealt
*/
StdDeck_CardMask_OR(dead, dead, board);
for(i = 0; i < sizeToDeal - 1; i++) {
StdDeck_CardMask_OR(dead, dead, pockets[i]);
}
if (game == game_holdem) {
if(totalToDeal > 0) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_ANY_HIGH);
} else {
INNER_LOOP_ANY_HIGH;
}
} else if (game == game_holdem8) {
if(totalToDeal > 0) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_ANY_HILO);
} else {
INNER_LOOP_ANY_HILO;
}
} else if (game == game_omaha) {
if(totalToDeal > 0) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_OMAHA);
} else {
INNER_LOOP_OMAHA;
}
} else if (game == game_omaha8) {
if(totalToDeal > 0) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_OMAHA8);
} else {
INNER_LOOP_OMAHA8;
}
} else if (game == game_7stud) {
if(totalToDeal > 0) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_ANY_HIGH);
} else {
INNER_LOOP_ANY_HIGH;
}
} else if (game == game_7stud8) {
if(totalToDeal > 0) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_ANY_HILO);
} else {
INNER_LOOP_ANY_HILO;
}
} else if (game == game_7studnsq) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_7STUDNSQ);
} else if (game == game_razz) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_RAZZ);
} else if (game == game_lowball27) {
DECK_ENUMERATE_COMBINATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, INNER_LOOP_LOWBALL27);
} else {
return 1;
}
result->game = game;
result->nplayers = sizeToDeal - 1;
result->sampleType = ENUM_EXHAUSTIVE;
return 0;
}
static int
pyenumSample(enum_game_t game, StdDeck_CardMask pockets[],
int numToDeal[],
StdDeck_CardMask board, StdDeck_CardMask dead,
int sizeToDeal, int iterations, enum_result_t *result) {
int i;
enumResultClear(result);
StdDeck_CardMask cardsDealt[ENUM_MAXPLAYERS + 1];
memset(cardsDealt, 0, sizeof(StdDeck_CardMask) * (ENUM_MAXPLAYERS + 1));
if (sizeToDeal - 1 > ENUM_MAXPLAYERS)
return 1;
/*
* Cards in pockets or in the board must not be dealt
*/
StdDeck_CardMask_OR(dead, dead, board);
for(i = 0; i < sizeToDeal - 1; i++) {
StdDeck_CardMask_OR(dead, dead, pockets[i]);
}
if (game == game_holdem) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_ANY_HIGH);
} else if (game == game_holdem8) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_ANY_HILO);
} else if (game == game_omaha) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_OMAHA);
} else if (game == game_omaha8) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_OMAHA8);
} else if (game == game_7stud) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_ANY_HIGH);
} else if (game == game_7stud8) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_ANY_HILO);
} else if (game == game_7studnsq) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_7STUDNSQ);
} else if (game == game_razz) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_RAZZ);
} else if (game == game_lowball27) {
DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, cardsDealt,
sizeToDeal, numToDeal,
dead, iterations, INNER_LOOP_LOWBALL27);
} else {
return 1;
}
result->game = game;
result->nplayers = sizeToDeal - 1;
result->sampleType = ENUM_SAMPLE;
return 0;
}
#define NOCARD 255
static int PyList2CardMask(PyObject* object, CardMask* cardsp)
{
CardMask cards;
int cards_size = 0;
int valid_cards_size = 0;
if(!PyList_Check(object)) {
PyErr_SetString(PyExc_TypeError, "expected a list of cards");
return -1;
}
valid_cards_size = cards_size = PyList_Size(object);
CardMask_RESET(cards);
int card;
int i;
for(i = 0; i < cards_size; i++) {
card = -1;
PyObject* pycard = PyList_GetItem(object, i);
if(PyErr_Occurred())
return -1;
#ifdef IS_PY3K
if(PyUnicode_Check(pycard)) {
PyObject * temp_bytes = PyUnicode_AsEncodedString(pycard, "ASCII", "strict");
if (temp_bytes == NULL) return -2;
char* card_string = PyBytes_AS_STRING(temp_bytes);
card_string = strdup(card_string);
Py_DECREF(temp_bytes);
#else
if(PyString_Check(pycard)) {
char* card_string = PyString_AsString(pycard);
#endif
if(!strcmp(card_string, "__")) {
card = 255;
} else {
if(Deck_stringToCard(card_string, &card) == 0) {
PyErr_Format(PyExc_RuntimeError, "card %s is not a valid card name", card_string);
return -1;
}
}
} else if(PyInteger_Check(pycard)) {
card = PyInteger_AsLong(pycard);
if(card != NOCARD && (card < 0 || card > StdDeck_N_CARDS)) {
PyErr_Format(PyExc_TypeError, "card value (%d) must be in the range [0-%d]", card, StdDeck_N_CARDS);
return -1;
}
} else {
PyErr_SetString(PyExc_TypeError, "card must be a string or an int");
return -1;
}
if(card == NOCARD)
valid_cards_size--;
else
CardMask_SET(cards, card);
}
*cardsp = cards;
return valid_cards_size;
}
#if 0
static PyObject* CardMask2PyList(CardMask* cardmask)
{
PyObject* result = 0;
int cardmask_size = StdDeck_numCards(cardmask);
int cards[64];
int i;
if((i = CurDeck.maskToCards(cardmask, cards)) != cardmask_size) {
PyErr_Format(PyExc_RuntimeError, "CardMask2PyList: maskToCards returns %d cards, expected %d\n", i, cardmask_size);
return 0;
}
result = PyList_New(0);
for(i = 0; i < cardmask_size; i++) {
PyObject* pycard = Py_BuildValue("i", cards[i]);
int status = PyList_Append(result, pycard);
Py_DECREF(pycard);
if(status < 0) return 0;
}
return result;
}
#endif
static char doc_poker_evaln[] =
"EvalN";
static PyObject*
poker_evaln(PyObject* self, PyObject *args)
{
CardMask cards;
PyObject* pycards;
HandVal handval;
if (!PyArg_ParseTuple(args, "O", &pycards))
return NULL;
if(PyList2CardMask(pycards, &cards) < 0)
return NULL;
handval = Hand_EVAL_N(cards, PyList_Size(pycards));
return Py_BuildValue("i", handval);
}
static char doc_string2card[] =
"return the numerical representation of a card";
static PyObject*
string2card(PyObject* self, PyObject *args)
{
char* card_string = 0;
if (!PyArg_ParseTuple(args, "s", &card_string))
return NULL;
{
int card = 0;
if(!strcmp(card_string, "__")) {
card = 255;
} else {
if(Deck_stringToCard(card_string, &card) == 0) {
PyErr_Format(PyExc_RuntimeError, "card %s is not a valid card name", card_string);
return NULL;
}
}
return Py_BuildValue("b", (unsigned char)card);
}
}
static char doc_card2string[] =
"return the string representation of a numerical card";
static PyObject*
card2string(PyObject* self, PyObject *args)
{
int card = 0;
if (!PyArg_ParseTuple(args, "i", &card))
return NULL;
if(card == 255) {
return Py_BuildValue("s", "__");
} else {
/*
* Avoid using GenericDeck_cardString as long as it insists
* on using the "static thread" hack (see lib/deck.c).
*/
char tmp[16];
StdDeck.cardToString(card, tmp);
return Py_BuildValue("s", tmp);
}
}
/*
* Find the card with highest suit matching rank in hand
* and remove it from hand. The removed card is returned.
*/
int findanddelete(CardMask* hand, int rank)
{
int suit;
for(suit = StdDeck_Suit_LAST; suit >= StdDeck_Suit_FIRST; suit--) {
int card = StdDeck_MAKE_CARD(rank, suit);
if(CardMask_CARD_IS_SET(*hand, card)) {
CardMask_UNSET(*hand, card);
return card;
}
}
return -1;
}
#define LOWRANK2RANK(c) ((c) == StdDeck_Rank_2 ? StdDeck_Rank_ACE : (c-1))
static PyObject*
CardMask2SortedPyList(CardMask hand, int low)
{
int i;
HandVal handval;
PyObject* result = PyList_New(0);
if(StdDeck_CardMask_IS_EMPTY(hand)) {
PyObject* pyvalue = Py_BuildValue("s", "Nothing");
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
return result;
}
if(low) {
handval = Hand_EVAL_LOW8(hand, 5);
} else {
handval = Hand_EVAL_N(hand, 5);
}
int htype = HandVal_HANDTYPE(handval);
{
PyObject* pyvalue = Py_BuildValue("s", StdRules_handTypeNames[htype]);
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
if(!low || htype != LowHandVal_NOTHING) {
if (StdRules_nSigCards[htype] >= 1) {
int rank = HandVal_TOP_CARD(handval);
if(low) rank = LOWRANK2RANK(rank);
if(htype == HandType_STRAIGHT || htype == HandType_STFLUSH) {
for(i = rank; rank - i < 5; i--) {
int rank_modulo = i < 0 ? StdDeck_Rank_ACE : i;
PyObject* pyvalue = Py_BuildValue("i", findanddelete(&hand, rank_modulo));
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
} else {
int count;
switch(htype) {
case HandType_ONEPAIR:
case HandType_TWOPAIR:
count = 2;
break;
case HandType_TRIPS:
case HandType_FULLHOUSE:
count = 3;
break;
case HandType_QUADS:
count = 4;
break;
default:
count = 1;
break;
}
for(i = 0; i < count; i++) {
PyObject* pyvalue = Py_BuildValue("i", findanddelete(&hand, rank));
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
}
}
if (StdRules_nSigCards[htype] >= 2) {
int rank = HandVal_SECOND_CARD(handval);
int count = 1;
if(low) rank = LOWRANK2RANK(rank);
if(htype == HandType_TWOPAIR ||
htype == HandType_FULLHOUSE)
count = 2;
for(i = 0; i < count; i++) {
PyObject* pyvalue = Py_BuildValue("i", findanddelete(&hand, rank));
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
}
if (StdRules_nSigCards[htype] >= 3) {
int rank = HandVal_THIRD_CARD(handval);
if(low) rank = LOWRANK2RANK(rank);
PyObject* pyvalue = Py_BuildValue("i", findanddelete(&hand, rank));
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
if (StdRules_nSigCards[htype] >= 4) {
int rank = HandVal_FOURTH_CARD(handval);
if(low) rank = LOWRANK2RANK(rank);
PyObject* pyvalue = Py_BuildValue("i", findanddelete(&hand, rank));
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
if (StdRules_nSigCards[htype] >= 5) {
int rank = HandVal_FIFTH_CARD(handval);
if(low) rank = LOWRANK2RANK(rank);
PyObject* pyvalue = Py_BuildValue("i", findanddelete(&hand, rank));
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
}
/*
* Append remaining cards, highest first
*/
for(i = Deck_N_CARDS - 1; i >= 0; i--) {
if (StdDeck_CardMask_CARD_IS_SET(hand, i)) {
PyObject* pyvalue = Py_BuildValue("i", i);
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
}
return result;
}
/* Evaluate an omaha hand for both high and low. Return nonzero on error.
If hival is NULL, skips high evaluation; if loval is NULL, skips
low evaluation. Low eval could be sped up with 256x256 rank table. */
static int
OmahaHiLow8_Best(StdDeck_CardMask hole, StdDeck_CardMask board,
HandVal *hival, LowHandVal *loval,
StdDeck_CardMask *hicards, StdDeck_CardMask *locards) {
StdDeck_CardMask allcards;
LowHandVal allval;
HandVal curhi, besthi;
LowHandVal curlo, bestlo;
StdDeck_CardMask hole1[OMAHA_MAXHOLE];
StdDeck_CardMask board1[OMAHA_MAXBOARD];
StdDeck_CardMask n1, n2, n3, n4, n5;
int nhole, nboard;
int eligible = 0;
int i, h1, h2, b1, b2, b3;
/* pluck out individual cards from hole and board masks, save in arrays */
nhole = nboard = 0;
for (i=0; i<StdDeck_N_CARDS; i++) {
if (StdDeck_CardMask_CARD_IS_SET(hole, i)) {
if (nhole >= OMAHA_MAXHOLE)
return 1; /* too many hole cards */
StdDeck_CardMask_RESET(hole1[nhole]);
StdDeck_CardMask_SET(hole1[nhole], i);
nhole++;
}
if (StdDeck_CardMask_CARD_IS_SET(board, i)) {
if (StdDeck_CardMask_CARD_IS_SET(hole, i)) /* same card in hole and board */
return 2;
if (nboard >= OMAHA_MAXBOARD)
return 3; /* too many board cards */
StdDeck_CardMask_RESET(board1[nboard]);
StdDeck_CardMask_SET(board1[nboard], i);
nboard++;
}
}
if (nhole < OMAHA_MINHOLE || nhole > OMAHA_MAXHOLE)
return 4; /* wrong # of hole cards */
if (nboard < OMAHA_MINBOARD || nboard > OMAHA_MAXBOARD)
return 5; /* wrong # of board cards */
/* quick test in case no low is possible with all 9 cards */
if (loval != NULL) {
StdDeck_CardMask_OR(allcards, hole, board);
allval = StdDeck_Lowball8_EVAL(allcards, nhole + nboard);
eligible = (allval != LowHandVal_NOTHING);
}
/* loop over all combinations of hole with board (60 for 4 hole cards
and 5 board cards). */
besthi = HandVal_NOTHING;
bestlo = LowHandVal_NOTHING;
/* {h1,h2} loop over all hole card combinations */
for (h1=0; h1<nhole-1; h1++) {
StdDeck_CardMask_RESET(n1);
StdDeck_CardMask_OR(n1, n1, hole1[h1]);
for (h2=h1+1; h2<nhole; h2++) {
StdDeck_CardMask_OR(n2, n1, hole1[h2]);
/* {b1,b2,b3} loop over all board card combinations */
for (b1=0; b1<nboard-2; b1++) {
StdDeck_CardMask_OR(n3, n2, board1[b1]);
for (b2=b1+1; b2<nboard-1; b2++) {
StdDeck_CardMask_OR(n4, n3, board1[b2]);
for (b3=b2+1; b3<nboard; b3++) {
if (hival != NULL) {
StdDeck_CardMask_OR(n5, n4, board1[b3]);
curhi = StdDeck_StdRules_EVAL_N(n5, 5);
if (curhi > besthi || besthi == HandVal_NOTHING) {
besthi = curhi;
*hicards = n5;
}
}
if (loval != NULL && eligible) {
curlo = StdDeck_Lowball8_EVAL(n5, 5);
if (curlo < bestlo || bestlo == LowHandVal_NOTHING) {
bestlo = curlo;
*locards = n5;
}
}
}
}
}
}
}
if (hival != NULL) *hival = besthi;
if (loval != NULL) *loval = bestlo;
return 0;
}
static char doc_eval_hand[] =
"return the evaluation of the hand, either low or hi. Result is a list, first element hand value, second element the best 5 card hand as a list of card values.";
static PyObject*
eval_hand(PyObject* self, PyObject *args)
{
PyObject* result = 0;
char* hilow_string = 0;
PyObject* pyhand = 0;
PyObject* pyboard = 0;
int low = 0;
CardMask hand;
CardMask board;
int board_size = 0;
CardMask best;
HandVal best_handval;
StdDeck_CardMask_RESET(best);
if (!PyArg_ParseTuple(args, "sOO", &hilow_string, &pyhand, &pyboard))
return NULL;
if(!strcmp(hilow_string, "low"))
low = 1;
if(PyList2CardMask(pyhand, &hand) < 0)
return NULL;
board_size = PyList2CardMask(pyboard, &board);
if(board_size > 0) {
CardMask hicards;
CardMask locards;
HandVal hival = 0;
HandVal loval = 0;
StdDeck_CardMask_RESET(hicards);
StdDeck_CardMask_RESET(locards);
OmahaHiLow8_Best(hand, board, &hival, &loval, &hicards, &locards);
if(low) {
best_handval = loval;
if(best_handval != LowHandVal_NOTHING)
best = locards;
} else {
best = hicards;
best_handval = hival;
}
} else {
CardMask cards;
CardMask dead;
StdDeck_CardMask_RESET(best);
StdDeck_CardMask_RESET(dead);
StdDeck_CardMask_OR(dead, dead, hand);
StdDeck_CardMask_NOT(dead, dead);
if(low) {
best_handval = LowHandVal_NOTHING;
} else {
best_handval = HandVal_NOTHING;
}
ENUMERATE_N_CARDS_D(cards, 5, dead,
{
HandVal handval;
if(low) {
handval = Hand_EVAL_LOW8(cards, 5);
} else {
handval = Hand_EVAL_N(cards, 5);
}
if(low ? (handval < best_handval) : (handval > best_handval)) {
best = cards;
best_handval = handval;
}
}
);
}
if(StdDeck_CardMask_IS_EMPTY(best)) {
best_handval = low ? 0x0FFFFFFF : 0;
}
result = PyList_New(0);
{
PyObject* pyvalue = Py_BuildValue("i", best_handval);
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
{
PyObject* pyvalue = CardMask2SortedPyList(best, low);
PyList_Append(result, pyvalue);
Py_DECREF(pyvalue);
}
return result;
}
static char doc_poker_eval[] =
"eval a poker game state";
static PyObject*
poker_eval(PyObject* self, PyObject *args, PyObject *keywds)
{
int i;
int pockets_size;
int fill_pockets = 0;
int iterations = 0;
PyObject* pypockets = 0;
PyObject* pyboard = 0;
char* game = 0;
PyObject* pydead = 0;
enum_gameparams_t* params = 0;
StdDeck_CardMask pockets[ENUM_MAXPLAYERS];
int numToDeal[ENUM_MAXPLAYERS];
CardMask dead_cards;
CardMask board_cards;
PyObject* result = NULL;
static char *kwlist[] = {"game", "pockets", "board", "dead", "fill_pockets", "iterations", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "sOO|Oii", kwlist,
&game, &pypockets, &pyboard, &pydead, &fill_pockets, &iterations))
return NULL;
if(!strcmp(game, "holdem")) {
params = enumGameParams(game_holdem);
} else if(!strcmp(game, "holdem8")) {
params = enumGameParams(game_holdem8);
} else if(!strcmp(game, "omaha")) {
params = enumGameParams(game_omaha);
} else if(!strcmp(game, "omaha8")) {
params = enumGameParams(game_omaha8);
} else if(!strcmp(game, "7stud")) {
params = enumGameParams(game_7stud);
} else if(!strcmp(game, "7stud8")) {
params = enumGameParams(game_7stud8);
} else if(!strcmp(game, "7studnsq")) {
params = enumGameParams(game_7studnsq);