1 /*
2   Copyright © 2004 Callum McKenzie
3   Copyright © 2007, 2008 Christian Persch
4 
5   This library is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /* Authors:   Callum McKenzie <callum@physics.otago.ac.nz> */
20 
21 /* Common definitions for all card handling routines. */
22 
23 #ifndef AR_CARD_H
24 #define AR_CARD_H
25 
26 #include <glib.h>
27 
28 G_BEGIN_DECLS
29 
30 /* __attribute__((__packed__)) is needed on some archs to make
31  * the Card type below fit into one byte. See bug #512799.
32  */
33 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 6)
34 #define GNOME_GAMES_GNUC_PACKED \
35   __attribute__((__packed__))
36 #else
37 #define GNOME_GAMES_GNUC_PACKED
38 #endif  /* !__GNUC__ */
39 
40 /* A card */
41 
42 /* Black Joker: value = 0, suit = spade or club
43  * Red Joker: value = 0, suit = heart or diamond
44  */
45 typedef union {
46   guint8 value;
47   struct {
48     guint8 face_down : 1;
49     guint8 suit : 2;
50     guint8 rank : 4;
51   } attr;
52 } GNOME_GAMES_GNUC_PACKED Card;
53 
54 #ifndef __GI_SCANNER__
55 G_STATIC_ASSERT(sizeof (Card) == sizeof (guint8));
56 #endif
57 
58 #define CARD(c)               ((Card) c)
59 #define CARD_UINT(c)          (c.value)
60 
61 #define CARD_GET_SUIT(c)      (c.attr.suit)
62 #define CARD_GET_RANK(c)      (c.attr.rank)
63 #define CARD_GET_FACE_DOWN(c) (c.attr.face_down)
64 
65 #define POINTER_TO_CARD(ptr)  ((Card) (guint8) GPOINTER_TO_UINT (ptr))
66 #define CARD_TO_POINTER(card) (GUINT_TO_POINTER((guint) card.value))
67 
68 /* Some defines */
69 
70 typedef enum {
71   /* Cards */
72   AR_CARD_JOKER = 0,
73   AR_CARD_ACE = 1,
74   AR_CARD_TWO = 2,
75   AR_CARD_THREE = 3,
76   AR_CARD_FOUR = 4,
77   AR_CARD_FIVE = 5,
78   AR_CARD_SIX = 6,
79   AR_CARD_SEVEN = 7,
80   AR_CARD_EIGHT = 8,
81   AR_CARD_NINE = 9,
82   AR_CARD_TEN = 10,
83   AR_CARD_JACK = 11,
84   AR_CARD_QUEEN = 12,
85   AR_CARD_KING = 13,
86   AR_CARD_ACE_HIGH = 14,
87 
88   /* Suites */
89   AR_CARDS_CLUBS = 0,
90   AR_CARDS_DIAMONDS = 1,
91   AR_CARDS_HEARTS = 2,
92   AR_CARDS_SPADES = 3,
93 
94   /* Jokers */
95   AR_CARD_BLACK_JOKER = 52,
96   AR_CARD_RED_JOKER = 53,
97 
98   /* Special */
99   AR_CARD_BACK = 54,
100   AR_CARD_SLOT = 55,
101   AR_CARDS_TOTAL = 56,
102 } ArCardIDType;
103 
104 int ar_card_get_node_by_suit_and_rank_snprintf (char *buffer,
105                                                 gsize bufsize,
106                                                 int suit,
107                                                 int rank);
108 
109 int ar_card_get_legacy_node_by_suit_and_rank_snprintf (char *buffer,
110                                                        gsize bufsize,
111                                                        int suit,
112                                                        int rank);
113 
114 int ar_card_get_node_by_id_snprintf (char *buffer,
115                                      gsize bufsize,
116                                      int card_id);
117 
118 int ar_card_get_legacy_node_by_id_snprintf (char *buffer,
119                                             gsize bufsize,
120                                             int card_id);
121 
122 int ar_card_get_name_by_id_snprintf (char *buffer,
123                                         gsize bufsize,
124                                         int card_id);
125 
126 char * ar_card_get_name_by_id (int card_id);
127 
128 const char * ar_card_get_localised_rank_symbol (int rank);
129 
130 const char * ar_card_get_locale_name (Card card);
131 
132 G_END_DECLS
133 
134 #endif /* !AR_CARD_H */
135