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
|
/* Copyright (C) 2019 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <SDL.h>
#include <cards.h>
#include "asset.h"
#include "stack.h"
typedef unsigned uint;
enum {
Foundation1,
Foundation2,
Foundation3,
Foundation4,
Cell1,
Cell2,
Cell3,
Cell4,
Tableau1,
Tableau2,
Tableau3,
Tableau4,
Tableau5,
Tableau6,
Tableau7,
Tableau8,
StacksLen,
};
static struct Stack stacks[StacksLen];
static uint kingIndex = Cards_KingRight;
struct Move {
uint dst;
uint src;
};
enum { QueueLen = 16 };
static struct {
struct Move moves[QueueLen];
uint r, w, u;
} queue;
static void enqueue(uint dst, uint src) {
queue.moves[queue.w % QueueLen].dst = dst;
queue.moves[queue.w % QueueLen].src = src;
queue.w++;
}
static void dequeue(void) {
struct Move move = queue.moves[queue.r++ % QueueLen];
push(&stacks[move.dst], pop(&stacks[move.src]));
if (move.dst <= Foundation4) {
kingIndex = Cards_KingRight;
} else if (move.dst <= Cell4) {
kingIndex = Cards_KingLeft;
}
}
static bool undo(void) {
uint len = queue.w - queue.u;
if (!len || len > QueueLen) return false;
for (uint i = len - 1; i < len; --i) {
struct Move move = queue.moves[(queue.u + i) % QueueLen];
push(&stacks[move.src], pop(&stacks[move.dst]));
}
queue.r = queue.w = queue.u;
return true;
}
static uint lcgState = 1;
static uint lcgRand(void) {
lcgState = (214013 * lcgState + 2531011) % (1 << 31);
return lcgState / (1 << 16);
}
// <https://rosettacode.org/wiki/Deal_cards_for_FreeCell>
static void deal(uint game) {
lcgState = game;
queue.r = queue.w = queue.u = 0;
for (uint i = 0; i < StacksLen; ++i) {
clear(&stacks[i]);
}
kingIndex = Cards_KingRight;
struct Stack deck = {0};
for (Card i = Cards_A; i <= Cards_K; ++i) {
push(&deck, Cards_Club + i);
push(&deck, Cards_Diamond + i);
push(&deck, Cards_Heart + i);
push(&deck, Cards_Spade + i);
}
uint stack = 0;
while (deck.len) {
uint i = lcgRand() % deck.len;
Card card = deck.cards[i];
deck.cards[i] = deck.cards[--deck.len];
push(&stacks[Tableau1 + stack++ % 8], card);
}
}
static bool win(void) {
for (uint i = Foundation1; i <= Foundation4; ++i) {
if (stacks[i].len != 13) return false;
}
return true;
}
static bool valid(uint dst, Card card) {
Card top = peek(&stacks[dst]);
if (dst <= Foundation4) {
if (!top) return rank(card) == Cards_A;
return suit(card) == suit(top)
&& rank(card) == rank(top) + 1;
}
if (!top) return true;
if (dst >= Tableau1) {
return color(card) != color(top)
&& rank(card) == rank(top) - 1;
}
return false;
}
static void autoEnqueue(void) {
Card min[] = { Cards_K, Cards_K };
for (uint i = Cell1; i <= Tableau8; ++i) {
for (uint j = 0; j < stacks[i].len; ++j) {
Card card = stacks[i].cards[j];
if (rank(card) < min[color(card)]) {
min[color(card)] = rank(card);
}
}
}
for (uint src = Cell1; src <= Tableau8; ++src) {
Card card = peek(&stacks[src]);
if (!card) continue;
if (rank(card) > Cards_2) {
if (min[!color(card)] < rank(card)) continue;
}
for (uint dst = Foundation1; dst <= Foundation4; ++dst) {
if (valid(dst, card)) {
enqueue(dst, src);
return;
}
}
}
}
static void moveSingle(uint dst, uint src) {
if (valid(dst, peek(&stacks[src]))) {
queue.u = queue.w;
enqueue(dst, src);
}
}
static uint moveDepth(uint src) {
struct Stack stack = stacks[src];
if (stack.len < 2) return stack.len;
uint n = 1;
for (uint i = stack.len - 2; i < stack.len; --i, ++n) {
if (color(stack.cards[i]) == color(stack.cards[i + 1])) break;
if (rank(stack.cards[i]) != rank(stack.cards[i + 1]) + 1) break;
}
return n;
}
static uint freeCells(uint cells[], uint dst) {
uint len = 0;
for (uint i = Cell1; i <= Tableau8; ++i) {
if (i == dst) continue;
if (!stacks[i].len) cells[len++] = i;
}
return len;
}
static void moveColumn(uint dst, uint src) {
uint cells[StacksLen];
uint free = freeCells(cells, dst);
uint depth;
for (depth = moveDepth(src); depth; --depth) {
if (free < depth - 1) continue;
if (valid(dst, stacks[src].cards[stacks[src].len - depth])) break;
}
if (depth < 2 || dst <= Cell4) {
moveSingle(dst, src);
return;
}
queue.u = queue.w;
for (uint i = 0; i < depth - 1; ++i) {
enqueue(cells[i], src);
}
enqueue(dst, src);
for (uint i = depth - 2; i < depth - 1; --i) {
enqueue(dst, cells[i]);
}
}
enum {
CardWidth = Cards_CardWidth,
CardHeight = Cards_CardHeight,
CellX = 0,
CellY = 0,
KingMarginX = 13,
KingX = CellX + 4 * CardWidth + KingMarginX,
KingY = 18,
KingPadX = 3,
KingPadY = 3,
KingWidth = Cards_KingWidth + 2 * KingPadX,
KingHeight = Cards_KingHeight + 2 * KingPadY,
FoundationX = KingX + KingWidth + KingMarginX,
FoundationY = CellY,
StackMarginX = 7,
StackMarginY = 10,
StackDeltaY = 17,
TableauX = StackMarginX,
TableauY = CellY + CardHeight + StackMarginY,
KingWinMarginX = 10,
KingWinMarginY = 10,
KingWinX = KingWinMarginX,
KingWinY = CellY + CardHeight + KingWinMarginY,
KingWinWidth = 320,
KingWinHeight = 320,
WindowWidth = 8 * CardWidth + 9 * StackMarginX + 1,
WindowHeight = KingWinY + KingWinHeight + KingWinMarginY,
};
static struct SDL_Rect rects[StacksLen];
static void initRects(void) {
SDL_Rect rect = { CellX, CellY, CardWidth, CardHeight };
for (uint i = Cell1; i <= Cell4; ++i) {
rects[i] = rect;
rect.x += CardWidth;
}
rect.x = FoundationX;
rect.y = FoundationY;
for (uint i = Foundation1; i <= Foundation4; ++i) {
rects[i] = rect;
rect.x += CardWidth;
}
rect.x = TableauX;
rect.y = TableauY;
rect.h = WindowHeight - TableauY;
for (uint i = Tableau1; i <= Tableau8; ++i) {
rects[i] = rect;
rect.x += CardWidth + StackMarginX;
}
}
static uint pointStack(SDL_Point point) {
uint i;
for (i = 0; i < StacksLen; ++i) {
if (SDL_PointInRect(&point, &rects[i])) break;
}
return i;
}
static SDL_Window *window;
static void err(const char *title) {
int error = SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, title, SDL_GetError(), window
);
if (error) fprintf(stderr, "%s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
static bool playAgain(void) {
SDL_MessageBoxButtonData buttons[] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, true, "Yes" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, false, "No" },
};
SDL_MessageBoxData data = {
.window = window,
.title = "Game Over",
.message = "Congratulations, you win!\n\nDo you want to play again?",
.buttons = buttons,
.numbuttons = SDL_arraysize(buttons),
};
int choice;
if (SDL_ShowMessageBox(&data, &choice) < 0) err("SDL_ShowMessageBox");
return choice;
}
enum Choice {
Cancel,
Column,
Single,
};
static enum Choice chooseMove(void) {
SDL_MessageBoxButtonData buttons[] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, Column, "Move column" },
{ 0, Single, "Move single card" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, Cancel, "Cancel" },
};
SDL_MessageBoxData data = {
.window = window,
.title = "Move to Empty Column...",
.message = "",
.buttons = buttons,
.numbuttons = SDL_arraysize(buttons),
};
int choice;
if (SDL_ShowMessageBox(&data, &choice) < 0) err("SDL_ShowMessageBox");
return choice;
}
static void newGame(uint game) {
if (!game) game = 1 + randUniform(32000);
deal(game);
char buf[sizeof("FreeCell Game #32000")];
snprintf(buf, sizeof(buf), "FreeCell Game #%u", game);
SDL_SetWindowTitle(window, buf);
}
static Card hiliteRank;
static SDL_Point revealPoint;
static uint fromStack = StacksLen;
static bool keyDown(SDL_KeyboardEvent key) {
switch (key.keysym.sym) {
case SDLK_F2: newGame(0); return true;
case SDLK_BACKSPACE: return undo();
}
if (key.repeat) return false;
switch (key.keysym.sym) {
break; case SDLK_a: hiliteRank = Cards_A;
break; case SDLK_2: hiliteRank = Cards_2;
break; case SDLK_3: hiliteRank = Cards_3;
break; case SDLK_4: hiliteRank = Cards_4;
break; case SDLK_5: hiliteRank = Cards_5;
break; case SDLK_6: hiliteRank = Cards_6;
break; case SDLK_7: hiliteRank = Cards_7;
break; case SDLK_8: hiliteRank = Cards_8;
break; case SDLK_9: hiliteRank = Cards_9;
break; case SDLK_1: hiliteRank = Cards_10;
break; case SDLK_0: hiliteRank = Cards_10;
break; case SDLK_j: hiliteRank = Cards_J;
break; case SDLK_q: hiliteRank = Cards_Q;
break; case SDLK_k: hiliteRank = Cards_K;
break; default: return false;
}
return true;
}
static bool keyUp(SDL_KeyboardEvent key) {
(void)key;
if (hiliteRank) {
hiliteRank = 0;
return true;
}
return false;
}
static bool mouseButtonDown(SDL_MouseButtonEvent button) {
if (button.button == SDL_BUTTON_RIGHT) {
revealPoint.x = button.x;
revealPoint.y = button.y;
return true;
}
return false;
}
static bool mouseButtonUp(SDL_MouseButtonEvent button) {
if (win() && playAgain()) {
newGame(0);
return true;
}
if (button.button == SDL_BUTTON_RIGHT && revealPoint.x) {
revealPoint.x = 0;
revealPoint.y = 0;
return true;
}
SDL_Point point = { button.x, button.y };
uint stack = pointStack(point);
if (button.clicks % 2 == 0) {
for (stack = Cell1; stack <= Cell4; ++stack) {
if (!stacks[stack].len) break;
}
}
if (fromStack < StacksLen && stack < StacksLen) {
if (moveDepth(fromStack) > 1 && stack > Cell4 && !stacks[stack].len) {
switch (chooseMove()) {
break; case Single: moveSingle(stack, fromStack);
break; case Column: moveColumn(stack, fromStack);
break; case Cancel: break;
}
} else {
moveColumn(stack, fromStack);
}
}
if (fromStack < StacksLen) {
fromStack = StacksLen;
return true;
}
if (stack < StacksLen && stack > Foundation4 && stacks[stack].len) {
fromStack = stack;
return true;
}
return false;
}
static SDL_Renderer *render;
static struct {
SDL_Texture *cards[Cards_Empty];
SDL_Texture *hilites[Cards_Empty];
SDL_Texture *kings[Cards_FreeCellCount];
} tex;
static void renderOutline(SDL_Rect rect, bool out) {
int right = rect.x + rect.w - 1;
int bottom = rect.y + rect.h - 1;
SDL_Point topLeft[3] = {
{ rect.x, bottom - 1 },
{ rect.x, rect.y },
{ right - 1, rect.y },
};
SDL_Point bottomRight[3] = {
{ rect.x + 1, bottom },
{ right, bottom },
{ right, rect.y + 1 },
};
SDL_SetRenderDrawColor(render, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderDrawLines(render, out ? bottomRight : topLeft, 3);
SDL_SetRenderDrawColor(render, 0x00, 0xFF, 0x00, 0xFF);
SDL_RenderDrawLines(render, out ? topLeft : bottomRight, 3);
}
static void renderOutlines(void) {
for (uint i = Foundation1; i <= Cell4; ++i) {
renderOutline(rects[i], false);
}
}
static void renderKing(void) {
SDL_Rect box = { KingX, KingY, KingWidth, KingHeight };
renderOutline(box, true);
if (win()) {
SDL_Rect king = { KingWinX, KingWinY, KingWinWidth, KingWinHeight };
SDL_RenderCopy(render, tex.kings[Cards_KingWin], NULL, &king);
} else {
SDL_Rect king = {
KingX + KingPadX, KingY + KingPadY,
Cards_KingWidth, Cards_KingHeight,
};
SDL_RenderCopy(render, tex.kings[kingIndex], NULL, &king);
}
}
static void renderCard(SDL_Rect rect, Card card, bool hilite) {
SDL_Texture *texture = (hilite ? tex.hilites : tex.cards)[card];
SDL_RenderCopy(render, texture, NULL, &rect);
}
static void renderStack(uint stack) {
Card revealCard = 0;
SDL_Rect revealRect = {0};
SDL_Rect rect = { rects[stack].x, rects[stack].y, CardWidth, CardHeight };
for (uint i = 0; i < stacks[stack].len; ++i) {
Card card = stacks[stack].cards[i];
if (SDL_PointInRect(&revealPoint, &rect)) {
revealCard = card;
revealRect = rect;
}
bool hilite = (stack == fromStack && i == stacks[stack].len - 1);
renderCard(rect, card, hilite || rank(card) == hiliteRank);
rect.y += StackDeltaY;
}
if (revealCard) {
renderCard(revealRect, revealCard, false);
}
}
static void renderStacks(void) {
for (uint i = Foundation1; i <= Cell4; ++i) {
Card card = peek(&stacks[i]);
if (!card) continue;
bool hilite = (i == fromStack || rank(card) == hiliteRank);
renderCard(rects[i], card, hilite);
}
for (uint i = Tableau1; i <= Tableau8; ++i) {
renderStack(i);
}
}
int main(int argc, char *argv[]) {
int error;
if (SDL_Init(SDL_INIT_VIDEO) < 0) err("SDL_Init");
atexit(SDL_Quit);
struct Paths paths;
if (assetPaths(&paths) < 0) err("SDL_GetPrefPath");
bool haveFreeCell = false;
struct {
SDL_Surface *cards[Cards_Empty];
SDL_Surface *freeCell[Cards_FreeCellCount];
} res;
SDL_RWops *rw = assetOpenCards(&paths);
if (!rw) return EXIT_FAILURE;
error = Cards_LoadCards(
res.cards, Cards_Empty, rw,
Cards_AlphaCorners | Cards_BlackBorders
);
if (error) err("Cards_LoadCards");
SDL_RWclose(rw);
rw = assetOpenFreeCell(&paths);
if (rw) {
haveFreeCell = true;
error = Cards_LoadFreeCell(
res.freeCell, Cards_FreeCellCount, rw,
Cards_ColorKey
);
if (error) err("Cards_LoadFreeCell");
SDL_RWclose(rw);
}
error = SDL_CreateWindowAndRenderer(
WindowWidth, WindowHeight, SDL_WINDOW_ALLOW_HIGHDPI,
&window, &render
);
if (error) err("SDL_CreateWindowAndRenderer");
SDL_SetWindowTitle(window, "FreeCell");
SDL_RenderSetIntegerScale(render, SDL_TRUE);
SDL_RenderSetLogicalSize(render, WindowWidth, WindowHeight);
for (uint i = 0; i < Cards_Empty; ++i) {
if (!res.cards[i]) continue;
tex.cards[i] = SDL_CreateTextureFromSurface(render, res.cards[i]);
if (!tex.cards[i]) err("SDL_CreateTextureFromSurface");
if (Cards_InvertSurface(res.cards[i]) < 0) err("Cards_hilitesurface");
tex.hilites[i] = SDL_CreateTextureFromSurface(render, res.cards[i]);
if (!tex.hilites[i]) err("SDL_CreateTextureFromSurface");
SDL_FreeSurface(res.cards[i]);
}
if (haveFreeCell) {
for (uint i = 0; i < Cards_FreeCellCount; ++i) {
if (!res.freeCell[i]) continue;
tex.kings[i] = SDL_CreateTextureFromSurface(render, res.freeCell[i]);
if (!tex.kings[i]) err("SDL_CreateTextureFromSurface");
SDL_FreeSurface(res.freeCell[i]);
}
}
srand(time(NULL));
newGame(argc > 1 ? strtoul(argv[1], NULL, 10) : 0);
initRects();
for (;;) {
SDL_SetRenderDrawColor(render, 0x00, 0xAA, 0x55, 0xFF);
SDL_RenderClear(render);
renderOutlines();
renderStacks();
if (haveFreeCell) renderKing();
SDL_RenderPresent(render);
if (queue.r < queue.w) {
dequeue();
if (queue.r == queue.w) autoEnqueue();
if (queue.r < queue.w) SDL_Delay(50);
continue;
}
bool update = false;
while (!update) {
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
break; case SDL_QUIT: return EXIT_SUCCESS;
break; case SDL_KEYDOWN: update = keyDown(event.key);
break; case SDL_KEYUP: update = keyUp(event.key);
break; case SDL_MOUSEBUTTONDOWN: {
update = mouseButtonDown(event.button);
}
break; case SDL_MOUSEBUTTONUP: {
update = mouseButtonUp(event.button);
}
}
}
}
}
|