1 /*
2  *  eval_astud.h: a fast poker hand evaluator for asian stud
3  *
4  *  Copyright (C) 1999 Brian Goetz
5  *
6  * This program gives you software freedom; you can copy, convey,
7  * propagate, redistribute and/or modify this program under the terms of
8  * the GNU General Public License (GPL) as published by the Free Software
9  * Foundation (FSF), either version 3 of the License, or (at your option)
10  * any later version of the GPL published by the FSF.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program in a file in the toplevel directory called "GPLv3".
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef __EVAL_ASTUD_H__
23 #define __EVAL_ASTUD_H__
24 
25 #include "poker_defs.h"
26 #include "inlines/eval.h"
27 
28 #define SC AStudDeck_CardMask_CLUBS(cards)
29 #define SD AStudDeck_CardMask_DIAMONDS(cards)
30 #define SH AStudDeck_CardMask_HEARTS(cards)
31 #define SS AStudDeck_CardMask_SPADES(cards)
32 
33 static uint32 astudHandTypeMap[StdRules_HandType_LAST+1] = {
34   HandVal_HANDTYPE_VALUE(AStudRules_HandType_NOPAIR),
35   HandVal_HANDTYPE_VALUE(AStudRules_HandType_ONEPAIR),
36   HandVal_HANDTYPE_VALUE(AStudRules_HandType_TWOPAIR),
37   HandVal_HANDTYPE_VALUE(AStudRules_HandType_TRIPS),
38   HandVal_HANDTYPE_VALUE(AStudRules_HandType_STRAIGHT),
39   HandVal_HANDTYPE_VALUE(AStudRules_HandType_FLUSH),
40   HandVal_HANDTYPE_VALUE(AStudRules_HandType_FULLHOUSE),
41   HandVal_HANDTYPE_VALUE(AStudRules_HandType_QUADS),
42   HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
43 };
44 
45 
46 static inline HandVal
AStudDeck_AStudRules_EVAL_N(AStudDeck_CardMask cards,int n_cards)47 AStudDeck_AStudRules_EVAL_N( AStudDeck_CardMask cards, int n_cards )
48 {
49   StdDeck_CardMask stdcards;
50   HandVal retval;
51   uint32 ranks, n_ranks, stdHandType, stdCards;
52 
53   /* The strategy here is to first use the standard evaluator, and then
54    * make adjustments for the asian stud rules, which are
55    * "flush beats full house" and "A-7-8-9-T straight".
56    * We make use of the assumption that the standard card mask and astud
57    * card mask have same layout.
58    */
59   stdcards = cards;
60   retval = StdDeck_StdRules_EVAL_N(stdcards, n_cards);
61   stdHandType = HandVal_HANDTYPE(retval);
62   stdCards    = HandVal_CARDS(retval);
63 
64   ranks = SC | SD | SH | SS;
65   n_ranks = nBitsTable[ranks];
66 
67   switch (stdHandType) {
68   case StdRules_HandType_QUADS:
69   case StdRules_HandType_FLUSH:
70     if (n_ranks >= 5) {
71       if ((SS & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
72         return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
73           + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
74       else if ((SC & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
75         return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
76           + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
77       else if ((SD & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
78         return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
79           + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
80       else if ((SH & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
81         return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
82           + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
83     };
84     return astudHandTypeMap[stdHandType] + stdCards;
85     break;
86 
87   case StdRules_HandType_STFLUSH:
88   case StdRules_HandType_STRAIGHT:
89     return astudHandTypeMap[stdHandType] + stdCards;
90     break;
91 
92   case StdRules_HandType_FULLHOUSE:
93     if (n_ranks >= 5) {
94       if (nBitsTable[SS] >= 5) {
95         if ((SS & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
96           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
97             + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
98         else
99           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_FLUSH)
100             + topFiveCardsTable[SS];
101       }
102       else if (nBitsTable[SC] >= 5) {
103         if ((SC & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
104           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
105             + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
106         else
107           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_FLUSH)
108             + topFiveCardsTable[SC];
109       }
110       else if (nBitsTable[SD] >= 5) {
111         if ((SD & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
112           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
113             + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
114         else
115           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_FLUSH)
116             + topFiveCardsTable[SD];
117       }
118       else if (nBitsTable[SH] >= 5) {
119         if ((SH & AStudRules_TEN_STRAIGHT) == AStudRules_TEN_STRAIGHT)
120           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STFLUSH)
121             + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
122         else
123           return HandVal_HANDTYPE_VALUE(AStudRules_HandType_FLUSH)
124             + topFiveCardsTable[SH];
125       }
126     };
127     return HandVal_HANDTYPE_VALUE(AStudRules_HandType_FULLHOUSE) + stdCards;
128     break;
129 
130   case StdRules_HandType_TRIPS:
131   case StdRules_HandType_TWOPAIR:
132   case StdRules_HandType_ONEPAIR:
133   case StdRules_HandType_NOPAIR:
134     if ((ranks & AStudRules_TEN_STRAIGHT) ==  AStudRules_TEN_STRAIGHT)
135       return HandVal_HANDTYPE_VALUE(AStudRules_HandType_STRAIGHT)
136         + HandVal_TOP_CARD_VALUE(AStudDeck_Rank_TEN);
137     else
138       return astudHandTypeMap[stdHandType] + stdCards;
139   };
140 
141   return 0;
142 }
143 
144 #undef SC
145 #undef SH
146 #undef SD
147 #undef SS
148 
149 #endif
150 
151