summaryrefslogtreecommitdiffstats
path: root/kpat/freecell-solver/card.c
blob: d4df80f7d883cfb3b259f2c5444458ce4067a3e1 (plain)
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
/*
 * card.c - functions to convert cards and card components to and from
 * its user representation.
 *
 * Written by Shlomi Fish (shlomif@vipe.technion.ac.il), 2000
 *
 * This file is in the public domain (it's uncopyrighted).
 */

#include <string.h>

#include "card.h"

#ifdef DMALLOC
#include "dmalloc.h"
#endif


#define uc(c) ( (((c)>='a') && ((c)<='z')) ?  ((c)+'A'-'a') : (c))

/*
 * This function converts a card number from its user representation
 * (e.g: "A", "K", "9") to its card number that can be used by
 * the program.
 * */
int freecell_solver_u2p_card_number(const char * string)
{
    char rest;

    while (1)
    {
        rest = uc(*string);

        if ((rest == '\0') || (rest == ' ') || (rest == '\t'))
        {
            return 0;
        }
        if (rest == 'A')
        {
            return 1;
        }
        else if (rest =='J')
        {
            return 11;
        }
        else if (rest == 'Q')
        {
            return 12;
        }
        else if (rest == 'K')
        {
            return 13;
        }
        else if (rest == '1')
        {
            return (*(string+1) == '0')?10:1;
        }
        else if ((rest == '0') || (rest == 'T'))
        {
            return 10;
        }
        else if ((rest >= '2') && (rest <= '9'))
        {
            return (rest-'0');
        }
        else
        {
            string++;
        }
    }
}


/*
 * This function converts a string containing a suit letter (that is
 * one of H,S,D,C) into its suit ID.
 *
 * The suit letter may come somewhat after the beginning of the string.
 *
 * */
int freecell_solver_u2p_suit(const char * suit)
{
    char c;

    c = uc(*suit);
    while (
            (c != 'H') &&
            (c != 'S') &&
            (c != 'D') &&
            (c != 'C') &&
            (c != ' ') &&
            (c != '\0'))
    {
        suit++;
        c = uc(*suit);
    }

    if (c == 'H')
        return 0;
    else if (c == 'C')
        return 1;
    else if (c == 'D')
        return 2;
    else if (c == 'S')
        return 3;
    else
        return 0;
}

static int fcs_u2p_flipped_status(const char * str)
{
    while (*str != '\0')
    {
        if ((*str != ' ') && (*str != '\t'))
        {
            return (*str == '<');
        }
        str++;
    }
    return 0;
}
/*
 * This function converts an entire card from its string representations
 * (e.g: "AH", "KS", "8D"), to a fcs_card_t data type.
 * */
fcs_card_t freecell_solver_card_user2perl(const char * str)
{
    fcs_card_t card;
#if defined(COMPACT_STATES)||defined(INDIRECT_STACK_STATES)
    card = 0;
#endif
    fcs_card_set_flipped(card, fcs_u2p_flipped_status(str));
    fcs_card_set_num(card, fcs_u2p_card_number(str));
    fcs_card_set_suit(card, fcs_u2p_suit(str));

    return card;
}


/*
 * Those strings contain the string representations of the different cards.
 * If CARD_DEBUG_PRES is defined then an asterisk is printed as an empty card.
 *
 * Notice that there are two of them: one prints 10 and one prints T for the
 * 10 card.
 *
 * */
#ifdef CARD_DEBUG_PRES
static char card_map_3_10[14][4] = { "*", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

static char card_map_3_T[14][4] = { "*", "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" };

#else
static char card_map_3_10[14][4] = { " ", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

static char card_map_3_T[14][4] = { " ", "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" };

#endif

/*
 * Converts a card_number from its internal representation to a string.
 *
 * num - the card number
 * str - the string to output to.
 * card_num_is_null - a pointer to a bool that indicates whether
 *      the card number is out of range or equal to zero
 * t - whether 10 should be printed as T or not.
 * flipped - whether the card is face down
 * */
char * freecell_solver_p2u_card_number(
    int num,
    char * str,
    int * card_num_is_null,
    int t,
    int flipped)
{
    char (*card_map_3) [4] = card_map_3_10;
    if (t)
    {
        card_map_3 = card_map_3_T;
    }
#ifdef CARD_DEBUG_PRES
    if (0)
    {
    }
#else
    if (flipped)
    {
        strncpy(str, "*", 2);
        *card_num_is_null = 0;
    }
#endif
    else
    {
        if ((num >= 0) && (num <= 13))
        {
            strncpy(str, card_map_3[num], strlen(card_map_3[num])+1);
            *card_num_is_null = (num == 0);
        }
        else
        {
            strncpy(str, card_map_3[0], strlen(card_map_3[0])+1);
            *card_num_is_null = 1;
        }
    }
    return str;
}

/*
 * Converts a suit to its user representation.
 *
 * */
char * freecell_solver_p2u_suit(int suit, char * str, int card_num_is_null, int flipped)
{
#ifndef CARD_DEBUG_PRES
    if (flipped)
    {
        strncpy(str, "*", 2);
    }
    else
#endif
    if (suit == 0)
    {
        if (card_num_is_null)
#ifdef CARD_DEBUG_PRES
            strncpy(str, "*", 2);
#else
            strncpy(str, " ", 2);
#endif
        else
            strncpy(str, "H", 2);
    }
    else if (suit == 1)
        strncpy(str, "C", 2);
    else if (suit == 2)
        strncpy(str, "D", 2);
    else if (suit == 3)
        strncpy(str, "S", 2);
    else
        strncpy(str, " ", 2);
    return str;
}

/*
 * Convert an entire card to its user representation.
 *
 * */
char * freecell_solver_card_perl2user(fcs_card_t card, char * str, int t)
{
    int card_num_is_null;
#ifdef CARD_DEBUG_PRES
    if (fcs_card_get_flipped(card))
    {
        *str = '<';
        str++;
    }
#endif

    fcs_p2u_card_number(
        fcs_card_card_num(card),
        str,
        &card_num_is_null,
        t,
        fcs_card_get_flipped(card)
        );
    /*
     * Notice that if card_num_is_null is found to be true
     * it will affect the output of the suit too.
     *
     * */
    fcs_p2u_suit(
        fcs_card_suit(card),
        str+strlen(str),
        card_num_is_null,
        fcs_card_get_flipped(card)
        );

#ifdef CARD_DEBUG_PRES
    if (fcs_card_get_flipped(card))
    {
        strcat(str, ">");
    }
#endif

    return str;
}