File: enumerate.c

package info (click to toggle)
poker-eval 134.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,544 kB
  • ctags: 1,587
  • sloc: ansic: 15,340; sh: 8,928; cs: 785; makefile: 276
file content (944 lines) | stat: -rw-r--r-- 32,793 bytes parent folder | download | duplicates (2)
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
/* enumerate.c -- functions to compute pot equity by enumerating outcomes
  Exports:
        enumExhaustive()	exhaustive enumeration of outcomes
        enumGameParams()	look up rule parameters by game type
        enumResultAlloc()	allocate ordering histograms in result object
        enumResultClear()	clear enumeration result object
        enumResultFree()	free ordering histograms in result object
        enumResultPrint()	print enumeration result object
        enumResultPrintTerse()	print enumeration result object, tersely
        enumSample()		monte carlo sampling of outcomes

   Michael Maurer, Apr 2002

 *  This package 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; version 2 dated June, 1991.
 *
 *  This package 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 package; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "poker_defs.h"
#include "inlines/eval.h"
#include "inlines/eval_low.h"
#include "inlines/eval_low8.h"
#include "inlines/eval_low27.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"

static enum_gameparams_t enum_gameparams[] = {
  /* must be in same order as enum_game_t */
  { game_holdem,    2,  2,  5, 0, 1, "Holdem Hi" },
  { game_holdem8,   2,  2,  5, 1, 1, "Holdem Hi/Low 8-or-better" },
  { game_omaha,     4,  4,  5, 0, 1, "Omaha Hi" },
  { game_omaha8,    4,  4,  5, 1, 1, "Omaha Hi/Low 8-or-better" },
  { game_7stud,     3,  7,  0, 0, 1, "7-card Stud Hi" },
  { game_7stud8,    3,  7,  0, 1, 1, "7-card Stud Hi/Low 8-or-better" },
  { game_7studnsq,  3,  7,  0, 1, 1, "7-card Stud Hi/Low no qualifier" },
  { game_razz,      3,  7,  0, 1, 0, "Razz (7-card Stud A-5 Low)" },
  { game_5draw,     0,  5,  0, 0, 1, "5-card Draw Hi with joker" },
  { game_5draw8,    0,  5,  0, 1, 1, "5-card Draw Hi/Low 8-or-better with joker" },
  { game_5drawnsq,  0,  5,  0, 1, 1, "5-card Draw Hi/Low no qualifier with joker" },
  { game_lowball,   0,  5,  0, 1, 0, "5-card Draw A-5 Lowball with joker" },
  { game_lowball27, 0,  5,  0, 1, 0, "5-card Draw 2-7 Lowball" }
};

/* 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<npockets; 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<npockets; 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 == 1)						\
          result->nscoop[i]++;						\
        result->ev[i] += potfrac;					\
      }									\
      if (result->ordering != NULL) {					\
        if (result->ordering->mode == enum_ordering_mode_hi) {		\
          int hiranks[ENUM_ORDERING_MAXPLAYERS];			\
          ENUM_ORDERING_RANK_HI(hival, HandVal_NOTHING, npockets, hiranks);\
          ENUM_ORDERING_INCREMENT(result->ordering, npockets, hiranks);	\
        }								\
        if (result->ordering->mode == enum_ordering_mode_lo) {		\
          int loranks[ENUM_ORDERING_MAXPLAYERS];			\
          ENUM_ORDERING_RANK_LO(loval, LowHandVal_NOTHING, npockets, loranks);\
          ENUM_ORDERING_INCREMENT(result->ordering, npockets, loranks);	\
        }								\
        if (result->ordering->mode == enum_ordering_mode_hilo) {	\
          int hiranks[ENUM_ORDERING_MAXPLAYERS_HILO];			\
          int loranks[ENUM_ORDERING_MAXPLAYERS_HILO];			\
          ENUM_ORDERING_RANK_HI(hival, HandVal_NOTHING, npockets, hiranks);\
          ENUM_ORDERING_RANK_LO(loval, LowHandVal_NOTHING, npockets, loranks);\
          ENUM_ORDERING_INCREMENT_HILO(result->ordering, npockets,	\
                                       hiranks, loranks);		\
        }								\
      }									\
      result->nsamples++;						\
    } while (0);

#define INNER_LOOP_HOLDEM						\
  INNER_LOOP({								\
    StdDeck_CardMask _hand;						\
    StdDeck_CardMask _finalBoard;					\
    StdDeck_CardMask_OR(_finalBoard, board, sharedCards);		\
    StdDeck_CardMask_OR(_hand, pockets[i], _finalBoard);		\
    hival[i] = StdDeck_StdRules_EVAL_N(_hand, 7);			\
    loval[i] = LowHandVal_NOTHING;					\
    err = 0;								\
  })

#define INNER_LOOP_HOLDEM8						\
  INNER_LOOP({								\
    StdDeck_CardMask _hand;						\
    StdDeck_CardMask _finalBoard;					\
    StdDeck_CardMask_OR(_finalBoard, board, sharedCards);		\
    StdDeck_CardMask_OR(_hand, pockets[i], _finalBoard);		\
    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 _finalBoard;					\
    StdDeck_CardMask_OR(_finalBoard, board, sharedCards);		\
    err = StdDeck_OmahaHiLow8_EVAL(pockets[i], _finalBoard,		\
                                   &hival[i], NULL);			\
    loval[i] = LowHandVal_NOTHING;					\
  })

#define INNER_LOOP_OMAHA8						\
  INNER_LOOP({								\
    StdDeck_CardMask _finalBoard;					\
    StdDeck_CardMask_OR(_finalBoard, board, sharedCards);		\
    err = StdDeck_OmahaHiLow8_EVAL(pockets[i], _finalBoard,		\
                                   &hival[i], &loval[i]);		\
  })

#define INNER_LOOP_7STUD						\
  INNER_LOOP({								\
    StdDeck_CardMask _hand;						\
    StdDeck_CardMask_OR(_hand, pockets[i], unsharedCards[i]);		\
    hival[i] = StdDeck_StdRules_EVAL_N(_hand, 7);			\
    loval[i] = LowHandVal_NOTHING;					\
    err = 0;								\
  })

#define INNER_LOOP_7STUD8						\
  INNER_LOOP({								\
    StdDeck_CardMask _hand;						\
    StdDeck_CardMask_OR(_hand, pockets[i], unsharedCards[i]);		\
    hival[i] = StdDeck_StdRules_EVAL_N(_hand, 7);			\
    loval[i] = StdDeck_Lowball8_EVAL(_hand, 7);				\
    err = 0;								\
  })

#define INNER_LOOP_7STUDNSQ						\
  INNER_LOOP({								\
    StdDeck_CardMask _hand;						\
    StdDeck_CardMask_OR(_hand, pockets[i], unsharedCards[i]);		\
    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], unsharedCards[i]);		\
    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], unsharedCards[i]);		\
    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], unsharedCards[i]);		\
    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], unsharedCards[i]);		\
    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], unsharedCards[i]);		\
    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], unsharedCards[i]);		\
    hival[i] = HandVal_NOTHING;						\
    loval[i] = StdDeck_Lowball27_EVAL_N(_hand, 5);			\
    err = 0;								\
  })

int 
enumExhaustive(enum_game_t game, StdDeck_CardMask pockets[],
               StdDeck_CardMask board, StdDeck_CardMask dead,
               int npockets, int nboard, int orderflag,
               enum_result_t *result) {
  int i;

  enumResultClear(result);
  if (npockets > ENUM_MAXPLAYERS)
    return 1;
  if (orderflag) {
    enum_ordering_mode_t mode;
    switch (game) {
    case game_holdem:
    case game_omaha:
    case game_7stud:
    case game_5draw:
      mode = enum_ordering_mode_hi;
      break;
    case game_razz:
    case game_lowball:
    case game_lowball27:
      mode = enum_ordering_mode_lo;
      break;
    case game_holdem8:
    case game_omaha8:
    case game_7stud8:
    case game_7studnsq:
    case game_5draw8:
    case game_5drawnsq:
      mode = enum_ordering_mode_hilo;
      break;
    default:
      return 1;
    }
    if (enumResultAlloc(result, npockets, mode))
      return 1;
  }

  if (game == game_holdem) {
    StdDeck_CardMask sharedCards;
    if (nboard == 0) {
      DECK_ENUMERATE_5_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_HOLDEM);
    } else if (nboard == 3) {
      DECK_ENUMERATE_2_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_HOLDEM);
    } else if (nboard == 4) {
      DECK_ENUMERATE_1_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_HOLDEM);
    } else if (nboard == 5) {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_HOLDEM;
    } else {
      return 1;
    }

  } else if (game == game_holdem8) {
    StdDeck_CardMask sharedCards;
    if (nboard == 0) {
      DECK_ENUMERATE_5_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_HOLDEM8);
    } else if (nboard == 3) {
      DECK_ENUMERATE_2_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_HOLDEM8);
    } else if (nboard == 4) {
      DECK_ENUMERATE_1_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_HOLDEM8);
    } else if (nboard == 5) {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_HOLDEM8;
    } else {
      return 1;
    }


  } else if (game == game_omaha) {
    StdDeck_CardMask sharedCards;
    if (nboard == 0) {
      DECK_ENUMERATE_5_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_OMAHA);
    } else if (nboard == 3) {
      DECK_ENUMERATE_2_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_OMAHA);
    } else if (nboard == 4) {
      DECK_ENUMERATE_1_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_OMAHA);
    } else if (nboard == 5) {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_OMAHA;
    } else {
      return 1;
    }

  } else if (game == game_omaha8) {
    StdDeck_CardMask sharedCards;
    if (nboard == 0) {
      DECK_ENUMERATE_5_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_OMAHA8);
    } else if (nboard == 3) {
      DECK_ENUMERATE_2_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_OMAHA8);
    } else if (nboard == 4) {
      DECK_ENUMERATE_1_CARDS_D(StdDeck, sharedCards, dead, INNER_LOOP_OMAHA8);
    } else if (nboard == 5) {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_OMAHA8;
    } else {
      return 1;
    }

  } else if (game == game_7stud) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(StdDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_7STUD);

  } else if (game == game_7stud8) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(StdDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_7STUD8);

  } else if (game == game_7studnsq) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(StdDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_7STUDNSQ);

  } else if (game == game_razz) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(StdDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_RAZZ);

  } else if (game == game_5draw) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(JokerDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_5DRAW);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_5draw8) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(JokerDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_5DRAW8);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_5drawnsq) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(JokerDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_5DRAWNSQ);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_lowball) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(JokerDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_LOWBALL);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_lowball27) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - StdDeck_numCards(pockets[i]);
    DECK_ENUMERATE_COMBINATIONS_D(StdDeck, unsharedCards,
                                  npockets, numToDeal,
                                  dead, INNER_LOOP_LOWBALL27);

  } else {
    return 1;
  }

  result->game = game;
  result->nplayers = npockets;
  result->sampleType = ENUM_EXHAUSTIVE;
  return 0;  
}

int 
enumSample(enum_game_t game, StdDeck_CardMask pockets[],
           StdDeck_CardMask board, StdDeck_CardMask dead,
           int npockets, int nboard, int niter, int orderflag,
           enum_result_t *result) {
  int i;
  int numCards;

  enumResultClear(result);
  if (npockets > ENUM_MAXPLAYERS)
    return 1;
  if (orderflag) {
    enum_ordering_mode_t mode;
    switch (game) {
    case game_holdem:
    case game_omaha:
    case game_7stud:
    case game_5draw:
      mode = enum_ordering_mode_hi;
      break;
    case game_razz:
    case game_lowball:
    case game_lowball27:
      mode = enum_ordering_mode_lo;
      break;
    case game_holdem8:
    case game_omaha8:
    case game_7stud8:
    case game_7studnsq:
    case game_5draw8:
    case game_5drawnsq:
      mode = enum_ordering_mode_hilo;
      break;
    default:
      return 1;
    }
    if (enumResultAlloc(result, npockets, mode))
      return 1;
  }

  if (game == game_holdem) {
    StdDeck_CardMask sharedCards;
    numCards = 5 - nboard;
    if (numCards > 0) {
      DECK_MONTECARLO_N_CARDS_D(StdDeck, sharedCards, dead, numCards,
                                niter, INNER_LOOP_HOLDEM);
    } else {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_HOLDEM;
      return 1;
    }

  } else if (game == game_holdem8) {
    StdDeck_CardMask sharedCards;
    numCards = 5 - nboard;
    if (numCards > 0) {
      DECK_MONTECARLO_N_CARDS_D(StdDeck, sharedCards, dead, numCards,
                                niter, INNER_LOOP_HOLDEM8);
    } else {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_HOLDEM8;
      return 1;
    }

  } else if (game == game_omaha) {
    StdDeck_CardMask sharedCards;
    numCards = 5 - nboard;
    if (numCards > 0) {
      DECK_MONTECARLO_N_CARDS_D(StdDeck, sharedCards, dead, numCards,
                                niter, INNER_LOOP_OMAHA);
    } else {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_OMAHA;
      return 1;
    }

  } else if (game == game_omaha8) {
    StdDeck_CardMask sharedCards;
    numCards = 5 - nboard;
    if (numCards > 0) {
      DECK_MONTECARLO_N_CARDS_D(StdDeck, sharedCards, dead, numCards,
                                niter, INNER_LOOP_OMAHA8);
    } else {
      StdDeck_CardMask_RESET(sharedCards);
      INNER_LOOP_OMAHA8;
      return 1;
    }

  } else if (game == game_7stud) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_7STUD);

  } else if (game == game_7stud8) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_7STUD8);

  } else if (game == game_7studnsq) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_7STUDNSQ);

  } else if (game == game_razz) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 7 - StdDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_RAZZ);

  } else if (game == game_5draw) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(JokerDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_5DRAW);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_5draw8) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(JokerDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_5DRAW8);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_5drawnsq) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(JokerDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_5DRAWNSQ);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_lowball) {
#if 0
    /* we have a type problem: pockets should be JokerDeck_CardMask[] */
    JokerDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - JokerDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(JokerDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_LOWBALL);
#endif
    fprintf(stderr, "Joker enumeration not yet implemented\n");
    return 1;

  } else if (game == game_lowball27) {
    StdDeck_CardMask unsharedCards[ENUM_MAXPLAYERS];
    int numToDeal[ENUM_MAXPLAYERS];
    for (i=0; i<npockets; i++)
      numToDeal[i] = 5 - StdDeck_numCards(pockets[i]);
    DECK_MONTECARLO_PERMUTATIONS_D(StdDeck, unsharedCards,
                                   npockets, numToDeal,
                                   dead, niter, INNER_LOOP_LOWBALL27);

  } else {
    return 1;
  }

  result->game = game;
  result->nplayers = npockets;
  result->sampleType = ENUM_SAMPLE;
  return 0;  
}

void
enumResultClear(enum_result_t *result) {
  memset(result, 0, sizeof(enum_result_t));
}

void
enumResultFree(enum_result_t *result) {
  if (result->ordering != NULL) {
    if (result->ordering->hist != NULL)
      free(result->ordering->hist);
    free(result->ordering);
    result->ordering = NULL;
  }
}

int
enumResultAlloc(enum_result_t *result, int nplayers,
                enum_ordering_mode_t mode) {
  int nentries;
  switch (mode) {
  case enum_ordering_mode_hi:
  case enum_ordering_mode_lo:
    nentries = ENUM_ORDERING_NENTRIES(nplayers);
    break;
  case enum_ordering_mode_hilo:
    nentries = ENUM_ORDERING_NENTRIES_HILO(nplayers);
    break;
  case enum_ordering_mode_none:
    return 0;
  default:
    return 1;
  }
  if (nentries <= 0)
    return 1;
  result->ordering = (enum_ordering_t *) malloc(sizeof(enum_ordering_t));
  if (result->ordering == NULL)
    return 1;
  result->ordering->mode = mode;
  result->ordering->nplayers = nplayers;
  result->ordering->nentries = nentries;
  result->ordering->hist = (unsigned int *) calloc(nentries, sizeof(int));
  if (result->ordering->hist == NULL) {
    free(result->ordering);
    result->ordering = NULL;
    return 1;
  }
  return 0;
}

enum_gameparams_t *
enumGameParams(enum_game_t game) {
  if (game >= 0 && game < game_NUMGAMES)
    return &enum_gameparams[game];
  else
    return NULL;
}

static void
enumResultPrintOrdering(enum_result_t *result, int terse) {
  int i, k;

  if (!terse)
    printf("Histogram of relative hand ranks:\n");
  if (result->ordering->mode == enum_ordering_mode_hi ||
      result->ordering->mode == enum_ordering_mode_lo) {
    if (!terse) {
      for (k=0; k<result->ordering->nplayers; k++)
        printf(" %2c", 'A' + k);
      printf(" %8s\n", "Freq");
    } else
      printf("ORD %d %d:", result->ordering->mode, result->ordering->nplayers);
    for (i=0; i<result->ordering->nentries; i++) {
      if (result->ordering->hist[i] > 0) {
        for (k=0; k<result->ordering->nplayers; k++) {
          int rank = ENUM_ORDERING_DECODE_K(i,
                     result->ordering->nplayers, k);
          if (rank == result->ordering->nplayers)
            printf(" NQ");
          else {
            printf(" %2d", rank + 1);
          }
        }
        printf(" %8d", result->ordering->hist[i]);
        printf(terse ? "|" : "\n");
      }
    }
  } else if (result->ordering->mode == enum_ordering_mode_hilo) {
    if (!terse) {
      printf("HI:");
      for (k=0; k<result->ordering->nplayers; k++)
        printf(" %2c", 'A' + k);
      printf("  LO:");
      for (k=0; k<result->ordering->nplayers; k++)
        printf(" %2c", 'A' + k);
      printf(" %8s\n", "Freq");
    } else
      printf("ORD %d %d:", result->ordering->mode, result->ordering->nplayers);
    for (i=0; i<result->ordering->nentries; i++) {
      if (result->ordering->hist[i] > 0) {
        if (!terse)
          printf("   ");
        for (k=0; k<result->ordering->nplayers; k++) {
          int rankhi = ENUM_ORDERING_DECODE_HILO_K_HI(i,
                       result->ordering->nplayers, k);
          if (rankhi == result->ordering->nplayers)
            printf(" NQ");
          else {
            printf(" %2d", rankhi + 1);
          }
        }
        if (!terse)
          printf("     ");
        for (k=0; k<result->ordering->nplayers; k++) {
          int ranklo = ENUM_ORDERING_DECODE_HILO_K_LO(i,
                       result->ordering->nplayers, k);
          if (ranklo == result->ordering->nplayers)
            printf(" NQ");
          else {
            printf(" %2d", ranklo + 1);
          }
        }
        printf(" %8d", result->ordering->hist[i]);
        printf(terse ? "|" : "\n");
      }
    }
  }
  if (terse)
    printf("\n");
}


void
enumResultPrint(enum_result_t *result, StdDeck_CardMask pockets[],
                StdDeck_CardMask board) {
  int i;
  enum_gameparams_t *gp;
  int width;

  gp = enumGameParams(result->game);
  if (gp == NULL) {
    printf("enumResultPrint: invalid game type\n");
    return;
  }
  width = gp->maxpocket * 3 - 1;
  printf("%s: %d %s %s%s", gp->name, result->nsamples,
         (result->sampleType == ENUM_SAMPLE) ? "sampled" : "enumerated",
         (gp->maxboard > 0) ? "board" : "outcome",
         (result->nsamples == 1 ? "" : "s"));
  if (!StdDeck_CardMask_IS_EMPTY(board))
    printf(" containing %s", DmaskString(StdDeck, board));
  printf("\n");

  if (gp->haslopot && gp->hashipot) {
    printf("%*s %7s   %7s %7s %7s   %7s %7s %7s   %5s\n",
           -width, "cards", "scoop",
           "HIwin", "HIlos", "HItie",
           "LOwin", "LOlos", "LOtie",
           "EV");
    for (i=0; i<result->nplayers; i++) {
      printf("%*s %7d   %7d %7d %7d   %7d %7d %7d   %5.3f\n",
             -width, DmaskString(StdDeck, pockets[i]), result->nscoop[i],
             result->nwinhi[i], result->nlosehi[i], result->ntiehi[i],
             result->nwinlo[i], result->nloselo[i], result->ntielo[i],
             result->ev[i] / result->nsamples);
    }
#if 0
    {
    int j;
    /* experimental output format to show pot splitting */
    printf("\n%*s", -width, "cards");
    for (j=0; j<=result->nplayers; j++)
      printf(" %6s%d", "HI", j);
    for (j=0; j<=result->nplayers; j++)
      printf(" %6s%d", "LO", j);
    printf("\n");
    for (i=0; i<result->nplayers; i++) {
      printf("%*s", -width, DmaskString(StdDeck, pockets[i]));
      for (j=0; j<=result->nplayers; j++)
        printf(" %7d", result->nsharehi[i][j]);
      for (j=0; j<=result->nplayers; j++)
        printf(" %7d", result->nsharelo[i][j]);
      printf("\n");
    }
    }
#endif
    
  } else {
    printf("%*s %7s %6s   %7s %6s   %7s %6s     %5s\n",
           -width, "cards", "win", "%win", "lose", "%lose", "tie", "%tie", "EV");
    if (gp->haslopot) {
      for (i=0; i<result->nplayers; i++) {
        printf("%*s %7d %6.2f   %7d %6.2f   %7d %6.2f     %5.3f\n",
               -width, DmaskString(StdDeck, pockets[i]),
               result->nwinlo[i], 100.0 * result->nwinlo[i] / result->nsamples,
               result->nloselo[i], 100.0 * result->nloselo[i] / result->nsamples,
               result->ntielo[i], 100.0 * result->ntielo[i] / result->nsamples,
               result->ev[i] / result->nsamples);
      }
    } else if (gp->hashipot) {
      for (i=0; i<result->nplayers; i++) {
        printf("%*s %7d %6.2f   %7d %6.2f   %7d %6.2f     %5.3f\n",
               -width, DmaskString(StdDeck, pockets[i]),
               result->nwinhi[i], 100.0 * result->nwinhi[i] / result->nsamples,
               result->nlosehi[i], 100.0 * result->nlosehi[i] / result->nsamples,
               result->ntiehi[i], 100.0 * result->ntiehi[i] / result->nsamples,
               result->ev[i] / result->nsamples);
      }
    }
  }

  if (result->ordering != NULL)
    enumResultPrintOrdering(result, 0);
}

void
enumResultPrintTerse(enum_result_t *result, StdDeck_CardMask pockets[],
                     StdDeck_CardMask board) {
  int i;

  printf("EV %d:", result->nplayers);
  for (i=0; i<result->nplayers; i++)
    printf(" %8.6f", result->ev[i] / result->nsamples);
  printf("\n");
  if (result->ordering != NULL)
    enumResultPrintOrdering(result, 1);
}