-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfreecell.c
More file actions
364 lines (335 loc) · 6.71 KB
/
freecell.c
File metadata and controls
364 lines (335 loc) · 6.71 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* This file contains C code that can be used for the freecell
programming problem in ECEn 521. This is a modified version
of code I developed for my own solution to the problem.
This code contains routines and data structures to read input,
initialize a compact representation of the game state, display a
given state, test for a solution, etc. (It assumes that the input
comes from stdin; if "freecell" is the executable, you type
"freecell < game1.txt" to run the program on that test case.)
I don't want anything in this code to constrain your solution.
Feel free to use or modify any portion of this code as you see
fit. You might, for example, create a data structure that
consists of the sequence of states reachable from the initial state.
*/
#include "freecell.h"
//Special print Characters for shapes
char Spade[4] = {0xE2,0x99,0xA0,0};
char Heart[4] = {0xE2,0x99,0xA1,0};
char Diamond[4] = {0xE2,0x99,0xA2,0};
char Club[4] = {0xE2,0x99,0xA3,0};
State initial; /* the starting state, loaded from stdin */
Card deck[DECKSIZE]; /* card values in program are indices to deck[] */
int main(int argc, char *argv[]) {
int i;
States *k, *kk;
initdeck();
readinitconfig();
printf("\n");
printf("Created state 0\n");
printf("Size of State %u\n", (int) sizeof(State));
printf("Possible Moves : %i\n", possibleMoves(&initial));
dumpstate(&initial);
printf("Try 1 of 4\n");
search();
return 0;
}
void initdeck(void) {
/* initialize all card values in deck */
int i, j;
i = 0;
for (j = 0; j < SUITSIZE; j++, i++) {
deck[i].num = j + 1;
deck[i].suit = DIAMONDS;
deck[i].color = RED;
deck[i].played = 0;
}
for (j = 0; j < SUITSIZE; j++, i++) {
deck[i].num = j + 1;
deck[i].suit = HEARTS;
deck[i].color = RED;
deck[i].played = 0;
}
for (j = 0; j < SUITSIZE; j++, i++) {
deck[i].num = j + 1;
deck[i].suit = SPADES;
deck[i].color = BLACK;
deck[i].played = 0;
}
for (j = 0; j < SUITSIZE; j++, i++) {
deck[i].num = j + 1;
deck[i].suit = CLUBS;
deck[i].color = BLACK;
deck[i].played = 0;
}
}
int getinputline(char *s, int n) { /* reads line from stdin, copies up to n chars to s, returns
number of chars written */
int i, c;
i = 1;
while ((c = getchar()) != '\n') {
if (i <= n)
*s++ = c;
i++;
}
*s = '\0';
return ((i > n) ? n : i);
}
int mapnum(char c) {
/* takes single char card identifier from input and gives the card
number used throughout the program */
switch (c) {
case 'A':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'T':
return 10;
case 'J':
return 11;
case 'Q':
return 12;
case 'K':
return 13;
default:
printf("Bad value in mapnum\n");
exit(-1);
}
}
int mapsuit(char c) {
/* takes single char suit identifier and returns the appropriate
enumerated type */
switch (c) {
case 'D':
return DIAMONDS;
case 'H':
return HEARTS;
case 'S':
return SPADES;
case 'C':
return CLUBS;
default:
printf("Bad value in mapsuit\n");
exit(-1);
}
}
int getindex(int num, int suit) {
/* returns index to deck for card of this num and suit */
int base;
switch (suit) {
case DIAMONDS:
base = 0;
break;
case HEARTS:
base = SUITSIZE;
break;
case SPADES:
base = SUITSIZE * 2;
break;
case CLUBS:
base = SUITSIZE * 3;
break;
}
return base + num - 1;
}
void readinitconfig(void) {
/* input assumed to come via stdin */
int i, j, k, num, suit, len, index;
char buf[BUFSIZE];
initial.p_size = 0;
initial.path = NULL;
/* first, clear initial state */
for (i = 0; i < NUMCOLS; i++) {
for (j = 0; j < COLSIZE; j++)
initial.column[i][j] = (char) -1;
initial.colheight[i] = 0;
}
for (i = 0; i < CELLS; i++)
initial.freecell[i] = (char) -1;
for (i = 0; i < CELLS; i++)
initial.stack[i] = (char) -1;
/* read one line of input per column */
for (i = 0; i < NUMCOLS; i++) {
len = getinputline(buf, BUFSIZE - 1);
j = 0; // index to buffer
k = 0; // index to column slot
while (j < len) {
num = mapnum(buf[j]);
suit = mapsuit(buf[j + 1]);
index = getindex(num, suit);
if (deck[index].played) {
printf("Error: card repeated (%c%c)\n", buf[j], buf[j + 1]);
exit(-1);
}
deck[index].played = 1;
initial.column[i][k++] = index;
initial.colheight[i]++;
j += 3;
}
}
/* there should be no repeated cards at this point, but make sure
that there are no missing cards */
for (i = 0; i < DECKSIZE; i++) {
if (deck[i].played == 0) {
printf("Error: card (index: %d) missing from deck\n", i);
exit(-1);
}
}
printf("Input game verified and loaded\n");
}
void dumpcard(char index) {
/* output a single card in the specified output format, add space
* at the end */
if (index < 0 || index >= DECKSIZE) {
if (index == -1) {
printf(".. ");
return;
}
printf("Bad value in dumpcard %i\n", index);
exit(-1);
}
printf(ANSI_COLOR_BG_WHITE);
if(deck[(int) index].color == RED) {
printf(ANSI_COLOR_RED);
} else {
printf(ANSI_COLOR_BLACK);
}
switch (deck[(int) index].num) {
case 1:
printf("A");
break;
case 2:
printf("2");
break;
case 3:
printf("3");
break;
case 4:
printf("4");
break;
case 5:
printf("5");
break;
case 6:
printf("6");
break;
case 7:
printf("7");
break;
case 8:
printf("8");
break;
case 9:
printf("9");
break;
case 10:
printf("T");
break;
case 11:
printf("J");
break;
case 12:
printf("Q");
break;
case 13:
printf("K");
break;
}
switch (deck[(int) index].suit) {
case DIAMONDS:
printf("%s",Diamond);
break;
case HEARTS:
printf("%s",Heart);
break;
case SPADES:
printf("%s", Spade);
break;
case CLUBS:
printf("%s", Club);
break;
}
printf(" ");
printf(ANSI_COLOR_RESET);
}
void dumpcardPlain(char * src, char index, char *message) {
/* output a single card in the specified output format, add space
* at the end */
if (index < 0 || index >= DECKSIZE) {
if (index == -1) {
printf(".. ");
}
//printf("%s Bad value in dumpcard %i\n", message, index);
src[0] = '0';
src[1] = '0';
//exit(-1);
}
switch (deck[(int) index].num) {
case 1:
src[0] = 'A';
break;
case 2:
src[0] = '2';
break;
case 3:
src[0] = '3';
break;
case 4:
src[0] = '4';
break;
case 5:
src[0] = '5';
break;
case 6:
src[0] = '6';
break;
case 7:
src[0] = '7';
break;
case 8:
src[0] = '8';
break;
case 9:
src[0] = '9';
break;
case 10:
src[0] = 'T';
break;
case 11:
src[0] = 'J';
break;
case 12:
src[0] = 'Q';
break;
case 13:
src[0] = 'K';
break;
}
switch (deck[(int) index].suit) {
case DIAMONDS:
src[1] = 'D';
break;
case HEARTS:
src[1] = 'H';
break;
case SPADES:
src[1] = 'S';
break;
case CLUBS:
src[1] = 'C';
break;
}
}