-
Notifications
You must be signed in to change notification settings - Fork 1
/
freecell.c
694 lines (574 loc) · 19 KB
/
freecell.c
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//
// Created by haita on 2020/05/07.
//
#include "freecell.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
char assist_mode;
int initGame() {
char src, dest; // User input
int move_result = SUCCESS;
assist_mode = '0';
Deck *src_deck, *dest_deck;
srand((unsigned) time(NULL));
Zone *zone1 = createZone(ZONE1_SIZE),
*zone2 = createZone(ZONE23_SIZE),
*zone3 = createZone(ZONE23_SIZE);
fillZone1(zone1, 15);
while (assist_mode != 'Y' && assist_mode != 'N') {
printf("Enable Assist Mode ? (Y/N)");
scanf("%c", &assist_mode);
clearBuffer();
}
while (TRUE) {
printLayout(zone1, zone2, zone3);
if (!movePossible(zone1, zone2)) {
printr("No possible moves :(\n");
if (askNewGame())
return 2;
return 3;
}
printf("\n\n\n\nAssist Mode : %s\n",
assist_mode == 'Y'
? ANSI_COLOR_GREEN"Enabled"ANSI_COLOR_RESET
: ANSI_COLOR_RED"Disabled"ANSI_COLOR_RESET);
printf("[End game: 0 0]\n");
printf("[New game: 1 1]\n");
printf("Next move (src dest): ");
scanf("%c %c", &src, &dest);
clearBuffer();
if (src == '0')
return 3;
if (src == '1')
return 2;
prompt:
while ((getDeckIndex(src) < 0) || (getDeckIndex(dest) < 0) || (move_result != SUCCESS)) {
printr(GO_UP"Next move (src dest): ");
scanf("%c %c", &src, &dest);
clearBuffer();
if (src == '0')
return 3;
if (src == '1')
return 2;
move_result = SUCCESS; // updating move result to break loop
}
src_deck = getDeck(zone1, zone2, src);
dest_deck = getDeck(zone1, zone2, dest);
if (dest_deck->capacity == 1 && dest_deck->size == 1) {
move_result = 0;
goto prompt;
}
if ((move_result = moveCard(src_deck, dest_deck)) != SUCCESS)
goto prompt;
if (isGameWon(zone3))
return 1;
}
}
int askNewGame() {
char answer = ' ';
while (answer != 'Y' && answer != 'N') {
printf("New game (Y/N) :");
scanf("%c", &answer);
clearBuffer();
}
return answer == 'Y';
}
bool isGameWon(Zone *zone3) {
return zone3->decks[0]->tail && zone3->decks[0]->tail->number == K
&& zone3->decks[1]->tail && zone3->decks[1]->tail->number == K
&& zone3->decks[2]->tail && zone3->decks[2]->tail->number == K
&& zone3->decks[3]->tail && zone3->decks[3]->tail->number == K;
}
void gameWonScreen() {
printg("GAME WON !!\n");
}
void gameLostScreen() {
printr("GAME LOST");
}
int autoCheckCards(Zone *zone1, Zone *zone2, Zone *zone3) {
int moved = 0;
// Checking zone 1
for (int i = 0; i < ZONE1_SIZE; i++) {
if (isZone3Compatible(zone1->decks[i], zone3)) {
assert(moveCard(zone1->decks[i], zone3->decks[zone1->decks[i]->tail->type]) == SUCCESS);
moved = 1;
}
}
// Checking zone 2
for (int i = 0; i < ZONE23_SIZE; i++) {
if (isZone3Compatible(zone2->decks[i], zone3)) {
assert(moveCard(zone2->decks[i], zone3->decks[zone2->decks[i]->tail->type]) == SUCCESS);
moved = 1;
}
}
return moved;
}
int movePossible(Zone *zone1, Zone *zone2) {
// Zone 1 <-> 2
for (int i = 0; i < ZONE23_SIZE; i++) {
for (int j = 0; j < ZONE1_SIZE; j++) {
if (!zone1->decks[j]->size
|| !zone2->decks[i]->size
|| isCompatible(zone2->decks[i]->tail, zone1->decks[j]->tail))
return 1;
}
}
// inter-Zone 1
for (int i = 0; i < ZONE1_SIZE; i++) {
for (int j = 0; j < ZONE23_SIZE; j++) {
if (isCompatible(zone1->decks[j]->tail, zone1->decks[i]->tail)) {
if (assist_mode == 'Y') // print in assist mode
printf("\n\nnext possible move : %s %s -> %s %s\n",
getNumber(zone1->decks[j]->tail->number),
getType(zone1->decks[j]->tail->type),
getNumber(zone1->decks[i]->tail->number),
getType(zone1->decks[i]->tail->type));
return 1;
}
}
}
return 0;
}
Deck *createDeck(size_t capacity) {
Deck *deck = malloc(sizeof(Deck));
deck->capacity = capacity;
deck->size = 0;
return deck;
}
void fillInitialDeck(Deck *deck) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
addCard(createCard(i, j), deck);
}
}
}
void fillDeckFrom(Deck *srcDeck, Deck *destDeck) {
if (!srcDeck || !srcDeck->size)
return;
size_t i = 0;
Card *pCard = srcDeck->head;
destDeck->head = pCard;
for (; (i < (destDeck->capacity - 1)) && (i < srcDeck->size); i++) {
pCard = pCard->next;
}
srcDeck->head = pCard->next;
srcDeck->size -= ++i;
pCard->next = NULL;
destDeck->tail = pCard;
destDeck->size = i;
}
Deck *getDeck(Zone *zone1, Zone *zone2, char position) {
int deck_index = getDeckIndex(position);
if (deck_index < 0)
return NULL;
if (deck_index < ZONE1_SIZE)
return zone1->decks[deck_index];
return zone2->decks[deck_index - ZONE1_SIZE];
}
int getDeckIndex(char position) {
return (int) (strchr(ZONE1_CONTROLS ZONE2_CONTROLS, position) - (ZONE1_CONTROLS ZONE2_CONTROLS));
}
Card *createCard(Card_Type type, Card_number number) {
Card *card = malloc(sizeof(Card));
card->number = number;
card->type = type;
card->next = NULL;
card->cardShape = malloc(sizeof(CardShape));
createCardShape(card);
return card;
}
char *getNumber(Card_number cardNumber) {
switch (cardNumber) {
case A:
return "A";
case Two:
return "2";
case Three:
return "3";
case Four:
return "4";
case Five:
return "5";
case Six:
return "6";
case Seven:
return "7";
case Eight:
return "8";
case Nine:
return "9";
case Ten:
return "10";
case J:
return "J";
case Q:
return "Q";
case K:
return "K";
default:
return "X";
}
}
char *getType(Card_Type cardType) {
switch (cardType) {
case Clubs:
return "♣";
case Diamonds:
return ANSI_COLOR_RED"♦"ANSI_COLOR_RESET;
case Hearts:
return ANSI_COLOR_RED"♥"ANSI_COLOR_RESET;
case Spades:
return "♠";
default:
return "$";
}
}
void createCardShape(Card *card) {
char *n = getNumber(card->number),
*s = getType(card->type);
char *middle1 = malloc(sizeof(char) * CARD_WIDTH),
*middle2 = malloc(sizeof(char) * CARD_WIDTH);
assert(sprintf(middle1, "│%s %2s│", s, n) > 0);
assert(sprintf(middle2, "│ %s│", s) > 0);
card->cardShape->top = CARD_TOP;
card->cardShape->middle1 = middle1;
card->cardShape->middle2 = middle2;
card->cardShape->bottom = CARD_BOTTOM;
}
void printCardShape(Card *card) {
printf("%s\n%s\n%s\n%s\n",
card->cardShape->top,
card->cardShape->middle1,
card->cardShape->middle2,
card->cardShape->bottom);
}
bool addCard(Card *card, Deck *deck) {
if (deck->size >= deck->capacity)
return FALSE;
if (deck->size == 0) {
deck->head = card;
deck->tail = card;
deck->head->next = NULL;
deck->size++;
return TRUE;
}
Card *p_card = cardAt(deck, deck->size - 1);
p_card->next = card;
deck->tail = card;
deck->size++;
return TRUE;
}
void shuffleDeck(Deck *deck, size_t times) {
if (!deck->size)
return;
int n = deck->size, j = 0, prevj = j;
while (times--) {
for (int i = 0; i < n; i++) {
while (j == i || j == prevj || j < 0)
j = rand() % n - 1;
swap(&deck->head, cardAt(deck, i), i ? cardAt(deck, i - 1) : NULL, cardAt(deck, j), cardAt(deck, j - 1));
prevj = j; // previously swapped index
}
}
deck->tail = cardAt(deck, n - 1);
}
Card *cardAt(Deck *deck, size_t index) {
if (index > deck->size)
return NULL;
Card *p_card = deck->head;
for (int j = 0; j < index; j++) {
p_card = p_card->next;
}
return p_card;
}
int moveCard(Deck *src, Deck *dest) {
// Check if destination if full, if so return DESTINATION_FULL
if (!src->size)
return SOURCE_FULL;
if ((src->capacity == 1) && (dest->capacity == 1)
&& src->size && dest->size
&& (src->tail->type != dest->tail->type)) // zone 2/3 <-> zone 3
return MOVE_ILLEGAL;
if ((dest->size == dest->capacity) && (dest->capacity != 1))
return DESTINATION_FULL;
if (!dest->size || isCompatible(src->tail, dest->tail)
|| (dest->capacity == 1)) { // The compatibility is already checked in case of zone 3
plainMoveCard(src, dest);
return SUCCESS;
}
// update size of 2 decks
return DESTINATION_FULL;
}
void plainMoveCard(Deck *src, Deck *dest) {
if (!dest->size)
dest->head = src->tail;
dest->tail = src->tail;
if (dest->capacity != 1 || !dest->size) // Case when destination is zone 3
dest->size++;
if (src->size > 1)
cardAt(src, src->size - 2)->next = NULL;
if (dest->size > 1)
cardAt(dest, dest->size - 2)->next = dest->tail;
src->tail = cardAt(src, src->size - 2);
src->size--;
}
bool isCompatible(Card *card1, Card *card2) {
return !card1 || !card2
|| (((card1->type < 2 && card2->type >= 2) || (card1->type >= 2 && card2->type < 2))
&& card1->number == (card2->number - 1));
}
bool isZone3Compatible(Deck *deck, Zone *zone3) {
// if (deck->tail->number == A && !zone3->decks[deck->tail->type]->tail) return TRUE; // if it's A then zone 3 is necessarily empty
return deck->tail
&& (deck->tail->number == A
|| (zone3->decks[deck->tail->type]->tail
&& (deck->tail->number == (zone3->decks[deck->tail->type]->tail->number + 1))));
}
Zone *createZone(size_t zone_size) {
Zone *zone = malloc(sizeof(Zone));
zone->size = zone_size;
zone->decks = malloc(sizeof(Deck *) * zone_size);
for (int i = 0; i < zone->size; i++) {
zone->decks[i] = createDeck(zone_size == ZONE1_SIZE ? (i < ZONE1_SIZE / 2 ? 7 : 6) : 1);
}
return zone;
}
void swap(Card **head, Card *card1, Card *prevCard1, Card *card2, Card *prevCard2) {
if (prevCard1) prevCard1->next = card2;
if (prevCard2) prevCard2->next = card1;
Card *temp = NULL;
temp = card1->next;
card1->next = card2->next;
card2->next = temp;
if ((*head) == card1)
*head = card2;
else if ((*head) == card2)
*head = card1;
}
void printDeck(Deck *deck) {
Card *pCard = deck->head;
while (pCard) {
if (pCard->next)
printf("%s\n%s\n", pCard->cardShape->top, pCard->cardShape->middle1);
else
printCardShape(pCard);
pCard = pCard->next;
}
}
void fillZone1(Zone *zone, size_t shuffles) {
Deck *deck = createDeck(DECK_CAPACITY);
fillInitialDeck(deck);
shuffleDeck(deck, shuffles);
for (int i = 0; i < ZONE1_SIZE; i++) {
fillDeckFrom(deck, zone->decks[i]);
zone->decks[i]->capacity = SET_CAPACITY;
}
}
void printZone1(Zone *zone) {
Card *pCard = NULL;
Deck *currDeck = NULL;
int skips[ZONE1_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0},
isEmpty[ZONE1_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0},
newLine = 1;
Card *pCards[ZONE1_SIZE];
size_t max_size = getTopSize(zone);
for (size_t i = 0; i < ZONE1_SIZE; i++) {
pCards[i] = zone->decks[i]->head;
}
for (size_t i = 0; i < max_size / 2 + 1; i++) {
if (!newLine)
break;
for (int k = 0; k < 4 && newLine; k++) {
newLine = 0;
for (int j = 0; j < ZONE1_SIZE; j++) {
if (!j)
printf(ZONE1_PADDING);
currDeck = zone->decks[j];
if (!currDeck->size)
isEmpty[j] = 1;
pCard = pCards[j];
if (!pCard && !isEmpty[j]) {
if (k == (max_size / 2 + 1)) // Not print new line when all cards are printed
newLine = 0;
printf("\t ");
continue;
}
newLine = 1;
switch (k) {
case 0:
if (isEmpty[j]) {
if (!i)
printf("\t%s", CARD_TOP);
else
printf("\t ");
break;
}
if (i * 2 >= currDeck->size) {
printf("\t%s", pCard->cardShape->middle2);
} else
printf("\t%s", CARD_TOP);
skips[j] = 0;
break;
case 1:
if (isEmpty[j]) {
if (!i)
printf("\t%s", CARD_PLACEHOLDER_EMPTY.middle1);
else {
printf("\t ");
}
break;
}
if (i * 2 >= currDeck->size) {
printf("\t%s", CARD_BOTTOM);
pCards[j] = NULL;
} else
printf("\t%s", pCard->cardShape->middle1);
skips[j] = 0;
break;
case 2:
if (isEmpty[j]) {
if (!i)
printf("\t%s", CARD_PLACEHOLDER_EMPTY.middle2);
else{
printf("\t ");
}
break;
}
if (pCard->next) {
printf("\t%s", CARD_TOP);
} else
printf("\t%s", pCard->cardShape->middle2);
break;
case 3:
if (isEmpty[j]) {
if (!i)
printf("\t%s", CARD_BOTTOM);
else
printf("\t ");
break;
}
if (!i && isEmpty[j]) {
printf("\t%s", CARD_BOTTOM);
break;
}
if (pCard->next) {
printf("\t%s", pCard->next->cardShape->middle1);
if (pCard->next->next)
pCards[j] = pCard->next->next;
else if (pCard->next)
pCards[j] = pCard->next;
skips[j] = 1;
} else
printf("\t%s", CARD_BOTTOM);
break;
default:
break;
}
fflush(stdout);
}
printf("\n");
}
for (int s = 0; s < ZONE1_SIZE; s++) {
if (pCards[s]) {
if (!skips[s])
pCards[s] = pCards[s]->next;
skips[s] = 0;
}
}
}
printf(ZONE1_PADDING);
for (int i = 0; i < ZONE1_SIZE; i++) {
printf("\t %c ", ZONE1_CONTROLS[i]);
}
}
void printZone23(Zone *zone2, Zone *zone3) {
Card *pCard = NULL;
Deck *currDeck = NULL;
int skips[ZONE1_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0},
isEmpty[ZONE1_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
Card *pCards[ZONE23_SIZE * 2];
for (int i = 0; i < ZONE23_SIZE * 2; i++) {
if (i < ZONE23_SIZE)
pCards[i] = zone2->decks[i]->tail;
else
pCards[i] = zone3->decks[i - ZONE23_SIZE]->tail;
}
for (int k = 0; k < ZONE23_SIZE; k++) {
for (int j = 0; j < ZONE23_SIZE * 2; j++) {
if (!j)
printf(ZONE23_PADDING);
else if (j == ZONE23_SIZE)
printf(ZONE23_PADDING ZONE23_PADDING);
if (j < ZONE23_SIZE)
currDeck = zone2->decks[j];
else
currDeck = zone3->decks[j - ZONE23_SIZE];
if (currDeck->size == 0)
isEmpty[j] = 1;
pCard = pCards[j];
switch (k) {
case 0:
printf("\t%s", CARD_TOP);
skips[j] = 0;
break;
case 1:
if (isEmpty[j]) {
printf("\t%s",
j < ZONE23_SIZE ? CARD_PLACEHOLDER_EMPTY.middle1 : CARD_PLACEHOLDER.middle1);
break;
}
printf("\t%s", pCard->cardShape->middle1);
skips[j] = 0;
break;
case 2:
if (isEmpty[j]) {
printf("\t%s",
j < ZONE23_SIZE ? CARD_PLACEHOLDER_EMPTY.middle2 : CARD_PLACEHOLDER.middle2);
break;
}
printf("\t%s", pCard->cardShape->middle2);
break;
case 3:
if (isEmpty[j]) {
printf("\t%s", CARD_BOTTOM);
continue;
}
printf("\t%s", CARD_BOTTOM);
break;
default:
continue;
}
fflush(stdout);
}
printf("\n");
}
printf(ZONE23_PADDING);
for (int i = 0; i < ZONE23_SIZE * 2; i++) {
if (i == 4)
printf(ZONE23_PADDING ZONE23_PADDING);
if (i < ZONE23_SIZE)
printf("\t %c ", ZONE2_CONTROLS[i]);
else
printf("\t %s ", getType(i - ZONE23_SIZE));
}
}
void printLayout(Zone *zone1, Zone *zone2, Zone *zone3) {
clrscr();
printZone23(zone2, zone3);
printf("\n\n\n");
printZone1(zone1);
printf("\n");
if (autoCheckCards(zone1, zone2, zone3))
printLayout(zone1, zone2, zone3);
}
size_t getTopSize(Zone *zone) {
size_t max = 0;
for (int i = 0; i < zone->size; i++) {
if (zone->decks[i]->size > max)
max = zone->decks[i]->size;
}
return max;
}