1 /*
2  * Copyright (C) 2004-2006
3  *           Michael Maurer <mjmaurer@yahoo.com>
4  *           Brian Goetz <brian@quiotix.com>
5  *           Loic Dachary <loic@dachary.org>
6  *
7  * This program gives you software freedom; you can copy, convey,
8  * propagate, redistribute and/or modify this program under the terms of
9  * the GNU General Public License (GPL) as published by the Free Software
10  * Foundation (FSF), either version 3 of the License, or (at your option)
11  * any later version of the GPL published by the FSF.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program in a file in the toplevel directory called "GPLv3".
20  * If not, see <http://www.gnu.org/licenses/>.
21  */
22 #ifndef __DECK_JOKER_H__
23 #define __DECK_JOKER_H__
24 
25 #include "pokereval_export.h"
26 
27 /* MUST be lower than STRING_CARDS from lib/deck.c */
28 #define JokerDeck_N_CARDS      53
29 #define JokerDeck_MASK(index)  (JokerDeck_cardMasksTable[index])
30 
31 #define JokerDeck_Rank_2      StdDeck_Rank_2
32 #define JokerDeck_Rank_3      StdDeck_Rank_3
33 #define JokerDeck_Rank_4      StdDeck_Rank_4
34 #define JokerDeck_Rank_5      StdDeck_Rank_5
35 #define JokerDeck_Rank_6      StdDeck_Rank_6
36 #define JokerDeck_Rank_7      StdDeck_Rank_7
37 #define JokerDeck_Rank_8      StdDeck_Rank_8
38 #define JokerDeck_Rank_9      StdDeck_Rank_9
39 #define JokerDeck_Rank_TEN    StdDeck_Rank_TEN
40 #define JokerDeck_Rank_JACK   StdDeck_Rank_JACK
41 #define JokerDeck_Rank_QUEEN  StdDeck_Rank_QUEEN
42 #define JokerDeck_Rank_KING   StdDeck_Rank_KING
43 #define JokerDeck_Rank_ACE    StdDeck_Rank_ACE
44 #define JokerDeck_Rank_COUNT  StdDeck_Rank_COUNT
45 #define JokerDeck_Rank_FIRST  StdDeck_Rank_FIRST
46 #define JokerDeck_Rank_LAST   StdDeck_Rank_LAST
47 #define JokerDeck_N_RANKMASKS StdDeck_N_RANKMASKS
48 
49 #define JokerDeck_RANK          StdDeck_RANK
50 #define JokerDeck_SUIT          StdDeck_SUIT
51 #define JokerDeck_IS_JOKER(index) ((index) == JokerDeck_JOKER)
52 #define JokerDeck_MAKE_CARD     StdDeck_MAKE_CARD
53 #define JokerDeck_JOKER        (JokerDeck_N_CARDS - 1)
54 
55 #define JokerDeck_Suit_HEARTS   StdDeck_Suit_HEARTS
56 #define JokerDeck_Suit_DIAMONDS StdDeck_Suit_DIAMONDS
57 #define JokerDeck_Suit_CLUBS    StdDeck_Suit_CLUBS
58 #define JokerDeck_Suit_SPADES   StdDeck_Suit_SPADES
59 #define JokerDeck_Suit_FIRST    StdDeck_Suit_FIRST
60 #define JokerDeck_Suit_LAST     StdDeck_Suit_LAST
61 #define JokerDeck_Suit_COUNT    StdDeck_Suit_COUNT
62 
63 typedef StdDeck_RankMask JokerDeck_RankMask;
64 
65 /*
66    It is important that the hearts, spades, clubs, and diamonds fields
67    in JokerDeck_CardMask agree with the definition for StdDeck_CardMask!
68 */
69 typedef union {
70 #ifdef USE_INT64
71   uint64  cards_n;
72 #else
73   struct {
74     uint32 n1, n2;
75   } cards_nn;
76 #endif
77   struct {
78 #ifdef WORDS_BIGENDIAN
79     uint32         : 2;
80     uint32 joker   : 1;
81     uint32 spades  : 13;
82     uint32         : 3;
83     uint32 clubs   : 13;
84     uint32         : 3;
85     uint32 diamonds: 13;
86     uint32         : 3;
87     uint32 hearts  : 13;
88 #else
89     uint32 spades  : 13;
90     uint32         : 3;
91     uint32 clubs   : 13;
92     uint32         : 3;
93     uint32 diamonds: 13;
94     uint32         : 3;
95     uint32 hearts  : 13;
96     uint32 joker   : 1;
97     uint32         : 2;
98 #endif
99   } cards;
100 } JokerDeck_CardMask;
101 
102 #define JokerDeck_CardMask_SPADES(cm)   ((cm).cards.spades)
103 #define JokerDeck_CardMask_CLUBS(cm)    ((cm).cards.clubs)
104 #define JokerDeck_CardMask_DIAMONDS(cm) ((cm).cards.diamonds)
105 #define JokerDeck_CardMask_HEARTS(cm)   ((cm).cards.hearts)
106 #define JokerDeck_CardMask_JOKER(cm)    ((cm).cards.joker)
107 
108 #define JokerDeck_CardMask_SET_SPADES(cm, ranks)   ((cm).cards.spades=(ranks))
109 #define JokerDeck_CardMask_SET_CLUBS(cm, ranks)    ((cm).cards.clubs=(ranks))
110 #define JokerDeck_CardMask_SET_DIAMONDS(cm, ranks) \
111         ((cm).cards.diamonds=(ranks))
112 #define JokerDeck_CardMask_SET_HEARTS(cm, ranks)   ((cm).cards.hearts=(ranks))
113 #define JokerDeck_CardMask_SET_JOKER(cm, ranks)    ((cm).cards.joker=(ranks))
114 
115 #define JokerDeck_CardMask_NOT         StdDeck_CardMask_NOT
116 #define JokerDeck_CardMask_OR          StdDeck_CardMask_OR
117 #define JokerDeck_CardMask_AND         StdDeck_CardMask_AND
118 #define JokerDeck_CardMask_XOR         StdDeck_CardMask_XOR
119 #define JokerDeck_CardMask_ANY_SET     StdDeck_CardMask_ANY_SET
120 #define JokerDeck_CardMask_RESET       StdDeck_CardMask_RESET
121 #define JokerDeck_CardMask_UNSET       StdDeck_CardMask_UNSET
122 #define JokerDeck_CardMask_IS_EMPTY    StdDeck_CardMask_IS_EMPTY
123 
124 #define JokerDeck_CardMask_toStd(jCards, sCards) \
125 do { \
126   StdDeck_CardMask_RESET(sCards); \
127   StdDeck_CardMask_SET_SPADES(sCards, JokerDeck_CardMask_SPADES(jCards)); \
128   StdDeck_CardMask_SET_HEARTS(sCards, JokerDeck_CardMask_HEARTS(jCards)); \
129   StdDeck_CardMask_SET_CLUBS(sCards, JokerDeck_CardMask_CLUBS(jCards)); \
130   StdDeck_CardMask_SET_DIAMONDS(sCards, JokerDeck_CardMask_DIAMONDS(jCards)); \
131 } while (0)
132 
133 #define JokerDeck_CardMask_fromStd(jCards, sCards) \
134 do { \
135   JokerDeck_CardMask_RESET(jCards); \
136   JokerDeck_CardMask_SET_SPADES(jCards, StdDeck_CardMask_SPADES(sCards)); \
137   JokerDeck_CardMask_SET_HEARTS(jCards, StdDeck_CardMask_HEARTS(sCards)); \
138   JokerDeck_CardMask_SET_CLUBS(jCards, StdDeck_CardMask_CLUBS(sCards)); \
139   JokerDeck_CardMask_SET_DIAMONDS(jCards, StdDeck_CardMask_DIAMONDS(sCards)); \
140 } while (0)
141 
142 #define JokerDeck_CardMask_fromStd_N(jCards, sCards, N) \
143 do { \
144   int _i; \
145   for (_i=0; _i<N; _i++) {\
146     JokerDeck_CardMask_RESET(jCards[_i]); \
147     JokerDeck_CardMask_SET_SPADES(jCards[_i], StdDeck_CardMask_SPADES(sCards[_i])); \
148     JokerDeck_CardMask_SET_HEARTS(jCards[_i], StdDeck_CardMask_HEARTS(sCards[_i])); \
149     JokerDeck_CardMask_SET_CLUBS(jCards[_i], StdDeck_CardMask_CLUBS(sCards[_i])); \
150     JokerDeck_CardMask_SET_DIAMONDS(jCards[_i], StdDeck_CardMask_DIAMONDS(sCards[_i])); \
151   }\
152 } while (0)
153 
154 #ifdef USE_INT64
155 #define JokerDeck_CardMask_CARD_IS_SET(mask, index)                       \
156   (( (mask).cards_n & (JokerDeck_MASK(index).cards_n)) != 0 )
157 #else
158 #define JokerDeck_CardMask_CARD_IS_SET(mask, index)                       \
159   ((( (mask).cards_nn.n1 & (JokerDeck_MASK(index).cards_nn.n1)) != 0 )    \
160    || (( (mask).cards_nn.n2 & (JokerDeck_MASK(index).cards_nn.n2)) != 0 ))
161 #endif
162 
163 #define JokerDeck_CardMask_SET(mask, index)       \
164 do {                                              \
165   JokerDeck_CardMask _t1 = JokerDeck_MASK(index); \
166   JokerDeck_CardMask_OR((mask), (mask), _t1);     \
167 } while (0)
168 
169 extern POKEREVAL_EXPORT JokerDeck_CardMask JokerDeck_cardMasksTable[JokerDeck_N_CARDS];
170 
171 extern POKEREVAL_EXPORT uint8              jokerStraightTable[StdDeck_N_RANKMASKS];
172 
173 extern POKEREVAL_EXPORT int JokerDeck_cardToString(int cardIndex, char *outString);
174 extern POKEREVAL_EXPORT int JokerDeck_stringToCard(char *inString, int *outCard);
175 
176 #define JokerDeck_cardString(i) GenericDeck_cardString(&JokerDeck, (i))
177 #define JokerDeck_printCard(i)  GenericDeck_printCard(&JokerDeck, (i))
178 #define JokerDeck_printMask(m)  GenericDeck_printMask(&JokerDeck, ((void *) &(m)))
179 #define JokerDeck_maskString(m) GenericDeck_maskString(&JokerDeck, ((void *) &(m)))
180 #define JokerDeck_numCards(m) GenericDeck_numCards(&JokerDeck, ((void *) &(m)))
181 #define JokerDeck_maskToString(m, s) GenericDeck_maskToString(&JokerDeck, ((void *) &(m)), (s))
182 
183 extern POKEREVAL_EXPORT Deck JokerDeck;
184 
185 #endif
186 
187 
188 #ifdef DECK_JOKER
189 
190 #if defined(Deck_N_CARDS)
191 #include "deck_undef.h"
192 #endif
193 
194 #define Deck_N_CARDS      JokerDeck_N_CARDS
195 #define Deck_MASK         JokerDeck_MASK
196 #define Deck_RANK         JokerDeck_RANK
197 #define Deck_SUIT         JokerDeck_SUIT
198 
199 #define Deck_cardToString JokerDeck_cardToString
200 #define Deck_maskToString JokerDeck_maskToString
201 #define Deck_printCard    JokerDeck_printCard
202 #define Deck_printMask    JokerDeck_printMask
203 #define Deck_stringToMask JokerDeck_stringToMask
204 
205 #define Rank_2            JokerDeck_Rank_2
206 #define Rank_3            JokerDeck_Rank_3
207 #define Rank_4            JokerDeck_Rank_4
208 #define Rank_5            JokerDeck_Rank_5
209 #define Rank_6            JokerDeck_Rank_6
210 #define Rank_7            JokerDeck_Rank_7
211 #define Rank_8            JokerDeck_Rank_8
212 #define Rank_9            JokerDeck_Rank_9
213 #define Rank_TEN          JokerDeck_Rank_TEN
214 #define Rank_JACK         JokerDeck_Rank_JACK
215 #define Rank_QUEEN        JokerDeck_Rank_QUEEN
216 #define Rank_KING         JokerDeck_Rank_KING
217 #define Rank_ACE          JokerDeck_Rank_ACE
218 #define Rank_FIRST        JokerDeck_Rank_FIRST
219 #define Rank_COUNT        JokerDeck_Rank_COUNT
220 
221 #define Suit_HEARTS       JokerDeck_Suit_HEARTS
222 #define Suit_DIAMONDS     JokerDeck_Suit_DIAMONDS
223 #define Suit_CLUBS        JokerDeck_Suit_CLUBS
224 #define Suit_SPADES       JokerDeck_Suit_SPADES
225 #define Suit_FIRST        JokerDeck_Suit_FIRST
226 #define Suit_COUNT        JokerDeck_Suit_COUNT
227 
228 #define CardMask               JokerDeck_CardMask
229 #define CardMask_NOT           JokerDeck_CardMask_NOT
230 #define CardMask_OR            JokerDeck_CardMask_OR
231 #define CardMask_XOR           JokerDeck_CardMask_XOR
232 #define CardMask_AND           JokerDeck_CardMask_AND
233 #define CardMask_SET           JokerDeck_CardMask_SET
234 #define CardMask_CARD_IS_SET   JokerDeck_CardMask_CARD_IS_SET
235 #define CardMask_ANY_SET       JokerDeck_CardMask_ANY_SET
236 #define CardMask_RESET         JokerDeck_CardMask_RESET
237 #define CardMask_UNSET         JokerDeck_CardMask_UNSET
238 
239 #define CardMask_SPADES        JokerDeck_CardMask_SPADES
240 #define CardMask_HEARTS        JokerDeck_CardMask_HEARTS
241 #define CardMask_CLUBS         JokerDeck_CardMask_CLUBS
242 #define CardMask_DIAMONDS      JokerDeck_CardMask_DIAMONDS
243 
244 #define CardMask_SET_SPADES    JokerDeck_CardMask_SET_SPADES
245 #define CardMask_SET_HEARTS    JokerDeck_CardMask_SET_HEARTS
246 #define CardMask_SET_CLUBS     JokerDeck_CardMask_SET_CLUBS
247 #define CardMask_SET_DIAMONDS  JokerDeck_CardMask_SET_DIAMONDS
248 
249 #define CurDeck JokerDeck
250 
251 #endif
252 
253