File: five_card_hands.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 (126 lines) | stat: -rw-r--r-- 3,015 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
/*
 *  five_card_hands.c: Enumerate and tabulate five-card hands
 *
 *  Copyright (C) 1993-99 Clifford T. Matthews, Brian Goetz
 *
 *  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	<stdlib.h>
#include	<string.h>
#include	<signal.h>

#include	"poker_defs.h"
#if defined(_MSC_VER) || defined(__MINGW32__) || defined( __BCPLUSPLUS__)  || defined( __MWERKS__)
#include	"getopt_w32.h"
#else
#include        <getopt.h>
#endif
#include	"inlines/eval.h"

/* #define VERBOSE */

uint32 totals[HandType_LAST+1];
int num_dead;
CardMask dead_cards;

const char *opts = "d:";

/*
 * returns number of dead cards, -1 on error
 *
 * argc, argv are the standard arguments to main
 * num_dead is a return value of the number of dead cards
 * dead_cards is a return value of the dead cards
 */
static int
parse_args(int argc, char **argv, int *num_dead, CardMask *dead_cards) {
  int i, c, o, rc, len;

  if (num_dead == NULL) {
    return (-1);
  }

  if (dead_cards == NULL) {
    return (-1);
  }

  *num_dead = 0;
  CardMask_RESET(*dead_cards);

  /*
   * parse any options passed to us.
   * -d "c1 [...]" is dead cards
   */
  while((o = getopt(argc, argv, opts)) != -1) {
    switch(o) {
    case 'd':
      len = strlen(optarg);
      for(i = 0;i < len;) {
	rc = StdDeck_stringToCard(optarg+i, &c);
	if (rc) {
	  StdDeck_CardMask_SET(*dead_cards, c);
	  (*num_dead)++;
	  i += 2;
	} else {
	  i++;
	}
      }
      break;
    }
  }

  return (*num_dead);
}

void dump_totals(void) {
  int i;
  
  for (i = HandType_FIRST; i <= HandType_LAST; i++)
    printf("%s:  %d\n", handTypeNamesPadded[i], totals[i]);
}


#ifdef VERBOSE
#define DUMP_HAND do {                          \
  Deck_printMask(cards);                        \
  printf(": ");                                 \
  HandVal_print(handval);                       \
  printf("\n");                                 \
} while (0)
#else
#define DUMP_HAND do { } while (0)
#endif

int main(int argc, char *argv[])
{
  CardMask cards, dead_cards;
  HandVal  handval;
  int      num_dead;

  parse_args(argc, argv, &num_dead, &dead_cards);

  ENUMERATE_5_CARDS_D(cards, dead_cards,
                    {
                      handval = Hand_EVAL_N(cards, 5);
                      ++totals[HandVal_HANDTYPE(handval)];
                      DUMP_HAND;
                    });

  dump_totals();
  exit(0);
}