summary refs log tree commit diff
path: root/sol.c
blob: b4a323fcde12aa26d24789cc39119340353b5c91 (plain) (blame)
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
/* 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 <SDL.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "cards.h"
#include "stack.h"

typedef unsigned uint;

enum {
	Stock,
	Waste1,
	Waste3,
	Foundation1,
	Foundation2,
	Foundation3,
	Foundation4,
	Tableau1,
	Tableau2,
	Tableau3,
	Tableau4,
	Tableau5,
	Tableau6,
	Tableau7,
	StacksLen,
};

static struct Stack stacks[StacksLen];

// TODO: Scoring method, score, time.
static struct {
	uint draw;
} game = {
	.draw = 3,
};

typedef void StackFn(struct Stack *dst, struct Stack *src, Uint8 len);

static struct {
	StackFn *fn;
	uint dst;
	uint src;
	uint len;
} undo;

static void gameDo(StackFn *fn, uint dst, uint src, uint len) {
	undo.fn = fn;
	undo.dst = src;
	undo.src = dst;
	undo.len = len;
	fn(&stacks[dst], &stacks[src], len);
}

static bool gameUndo(void) {
	if (!undo.fn) return false;
	undo.fn(&stacks[undo.dst], &stacks[undo.src], undo.len);
	undo.fn = NULL;
	return true;
}

static void gameDeal(void) {
	undo.fn = NULL;
	for (uint i = 0; i < StacksLen; ++i) {
		stackClear(&stacks[i]);
	}
	stackDeck(&stacks[Stock]);
	stackShuffle(&stacks[Stock]);
	for (uint i = Tableau1; i <= Tableau7; ++i) {
		stackMoveTo(&stacks[i], &stacks[Stock], i - Tableau1);
		stackFlipTo(&stacks[i], &stacks[Stock], 1);
	}
}

static void gameDraw(void) {
	stackMoveTo(&stacks[Waste1], &stacks[Waste3], stacks[Waste3].len);
	if (stacks[Stock].len) {
		if (game.draw > 1) {
			gameDo(stackFlipTo, Waste3, Stock, game.draw);
		} else {
			gameDo(stackFlipTo, Waste1, Stock, 1);
		}
	} else {
		gameDo(stackFlipTo, Stock, Waste1, stacks[Waste1].len);
	}
}

static bool gameFind(uint *stack, uint *index, Card card) {
	for (*stack = 0; *stack < StacksLen; ++*stack) {
		for (*index = 0; *index < stacks[*stack].len; ++*index) {
			if (stacks[*stack].cards[*index] == card) return true;
		}
	}
	return false;
}

static bool gameAvail(Card card) {
	uint stack, index;
	if (card < 0) return false;
	if (!gameFind(&stack, &index, card)) return false;

	bool top = (card == stackTop(&stacks[stack]));
	if (stack == Waste1) {
		return top && !stacks[Waste3].len;
	} else if (stack == Waste3) {
		return top;
	} else if (stack >= Foundation1 && stack <= Foundation4) {
		return top;
	} else if (stack >= Tableau1 && stack <= Tableau7) {
		return true;
	} else {
		return false;
	}
}

static bool gameReveal(Card card) {
	uint stack, index;
	if (!gameFind(&stack, &index, card)) return false;
	if (stack < Tableau1 || stack > Tableau7) return false;
	if (card != stackTop(&stacks[stack])) return false;
	if (card > 0) return false;
	gameDo(stackFlipTo, stack, stack, 1);
	return true;
}

static bool gameMove(uint dest, Card card) {
	uint source, index;
	if (!gameFind(&source, &index, card)) return false;
	if (source == dest) return false;

	uint count = stacks[source].len - index;
	Card destTop = stackTop(&stacks[dest]);

	if (dest >= Foundation1 && dest <= Foundation4) {
		if (count > 1) return false;
		if (!destTop && cardRank(card) != Cards_A) return false;
		if (destTop && cardSuit(card) != cardSuit(destTop)) return false;
		if (destTop && cardRank(card) != cardRank(destTop) + 1) return false;
		gameDo(stackMoveTo, dest, source, 1);
		return true;
	}

	if (dest >= Tableau1 && dest <= Tableau7) {
		if (!destTop && cardRank(card) != Cards_K) return false;
		if (destTop && cardColor(card) == cardColor(destTop)) return false;
		if (destTop && cardRank(card) != cardRank(destTop) - 1) return false;
		gameDo(stackMoveTo, dest, source, count);
		return true;
	}

	return false;
}

enum {
	StackMarginX = 11,
	StackMarginY = 6,

	StockX = StackMarginX,
	StockY = StackMarginY,

	WasteX = StockX + Cards_Width + StackMarginX,
	WasteY = StackMarginY,

	FoundationX = WasteX + 2 * (Cards_Width + StackMarginX),
	FoundationY = StackMarginY,

	TableauX = StackMarginX,
	TableauY = StockY + Cards_Height + StackMarginY,

	StackDeltaX = 2,
	StackDeltaY = 1,

	Waste3DeltaX = 14,
	Waste3DeltaY = StackDeltaY,

	TableauDeltaX = 0,
	TableauDeltaYBack = 3,
	TableauDeltaYFront = 15,

	WindowWidth = 7 * Cards_Width + 8 * StackMarginX,
	WindowHeight = 2 * (StackMarginY + Cards_Height)
		+ 6 * TableauDeltaYBack
		+ 12 * TableauDeltaYFront
		+ StackMarginY,
};

struct Item {
	SDL_Rect rect;
	uint texture;
	Card card;
};

struct List {
	uint len;
	struct Item items[52];
};

static void listPush(struct List *list, struct Item item) {
	assert(list->len < 52);
	list->items[list->len++] = item;
}

static struct {
	SDL_Rect stacks[StacksLen];
	struct List base;
	struct List main;
	struct List drag;
	struct Item dragItem;
	uint backTexture;
} layout;

static struct Item *layoutFind(const SDL_Point *point) {
	for (uint i = layout.main.len - 1; i < layout.main.len; --i) {
		if (SDL_PointInRect(point, &layout.main.items[i].rect)) {
			return &layout.main.items[i];
		}
	}
	return NULL;
}

static void layoutClear(void) {
	layout.base.len = 0;
	layout.main.len = 0;
	layout.drag.len = 0;
}

static void layoutCard(struct List **list, SDL_Rect *rect, Card card) {
	if (card == layout.dragItem.card) {
		*list = &layout.drag;
		*rect = layout.dragItem.rect;
	}
	uint texture = (card > 0 ? (uint)card : layout.backTexture);
	struct Item item = { *rect, texture, card };
	listPush(*list, item);
}

static void layoutStack(SDL_Rect *rect, const struct Stack *stack, uint depth) {
	struct List *list = &layout.main;
	for (uint i = 0; i < stack->len; ++i) {
		layoutCard(&list, rect, stack->cards[i]);
		if (i > 0 && i % (depth / 3) == 0) {
			rect->x += StackDeltaX;
			rect->y += StackDeltaY;
		}
	}
}

static void layoutStock(void) {
	SDL_Rect rect = { StockX, StockY, Cards_Width, Cards_Height };
	struct Item item = { rect, Cards_O, 0 };
	listPush(&layout.base, item);
	layout.stacks[Stock] = rect;
	layoutStack(&rect, &stacks[Stock], 24);
}

static void layoutWaste(void) {
	SDL_Rect rect = { WasteX, WasteY, Cards_Width, Cards_Height };
	layoutStack(&rect, &stacks[Waste1], 24);
	struct List *list = &layout.main;
	for (uint i = 0; i < stacks[Waste3].len; ++i) {
		layoutCard(&list, &rect, stacks[Waste3].cards[i]);
		rect.x += Waste3DeltaX;
		rect.y += Waste3DeltaY;
	}
}

static void layoutFoundations(void) {
	SDL_Rect base = { FoundationX, FoundationY, Cards_Width, Cards_Height };
	for (uint i = Foundation1; i <= Foundation4; ++i) {
		struct Item item = { base, Cards_Empty, 0 };
		listPush(&layout.base, item);
		layout.stacks[i] = base;
		SDL_Rect rect = base;
		layoutStack(&rect, &stacks[i], 13);
		base.x += Cards_Width + StackMarginX;
	}
}

static void layoutTableau(void) {
	SDL_Rect base = { TableauX, TableauY, Cards_Width, Cards_Height };
	for (uint i = Tableau1; i <= Tableau7; ++i) {
		SDL_Rect stack = { base.x, base.y, base.w, WindowHeight };
		layout.stacks[i] = stack;
		struct List *list = &layout.main;
		SDL_Rect rect = base;
		for (uint j = 0; j < stacks[i].len; ++j) {
			Card card = stacks[i].cards[j];
			layoutCard(&list, &rect, card);
			rect.x += TableauDeltaX;
			rect.y += (card > 0 ? TableauDeltaYFront : TableauDeltaYBack);
		}
		base.x += Cards_Width + StackMarginX;
	}
}

static bool keyDown(SDL_KeyboardEvent key) {
	switch (key.keysym.sym) {
		case SDLK_F2: gameDeal(); return true;
		case SDLK_BACKSPACE: return gameUndo();
		case SDLK_b: {
			layout.backTexture -= Cards_Back1;
			layout.backTexture += 1;
			layout.backTexture %= 12;
			layout.backTexture += Cards_Back1;
			return true;
		}
		case SDLK_d: {
			game.draw = (game.draw == 1 ? 3 : 1);
			gameDeal();
			return true;
		}
		default: return false;
	}
}

static bool mouseButtonDown(SDL_MouseButtonEvent button) {
	struct SDL_Point point = { button.x, button.y };
	if (SDL_PointInRect(&point, &layout.stacks[Stock])) {
		gameDraw();
		return true;
	}
	struct Item *item = layoutFind(&point);
	if (!item) return false;
	if (!gameAvail(item->card)) return gameReveal(item->card);
	if (button.clicks % 2 == 0) {
		for (uint dest = Foundation1; dest <= Foundation4; ++dest) {
			if (gameMove(dest, item->card)) return true;
		}
		return false;
	}
	layout.dragItem = *item;
	return true;
}

static bool mouseButtonUp(SDL_MouseButtonEvent button) {
	(void)button;
	if (!layout.dragItem.card) return false;
	for (uint dest = 0; dest < StacksLen; ++dest) {
		if (SDL_HasIntersection(&layout.dragItem.rect, &layout.stacks[dest])) {
			if (gameMove(dest, layout.dragItem.card)) break;
		}
	}
	layout.dragItem.card = 0;
	return true;
}

static bool mouseMotion(SDL_MouseMotionEvent motion) {
	if (!motion.state) return false;
	layout.dragItem.rect.x += motion.xrel;
	layout.dragItem.rect.y += motion.yrel;
	return true;
}

static SDL_Window *window;
static SDL_Renderer *render;
static SDL_Texture *textures[Cards_Count];

static void renderList(const struct List *list) {
	for (uint i = 0; i < list->len; ++i) {
		SDL_RenderCopy(
			render,
			textures[list->items[i].texture],
			NULL, &list->items[i].rect
		);
	}
}

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);
}

int main(void) {
	if (SDL_Init(SDL_INIT_VIDEO) < 0) err("SDL_Init");
	atexit(SDL_Quit);

	char *prefPath = SDL_GetPrefPath("Causal Agency", "Cards");
	if (!prefPath) err("SDL_GetPrefPath");

	char *basePath = SDL_GetBasePath();
	if (!basePath) err("SDL_GetBasePath");

	char paths[4][1024];
	snprintf(paths[0], sizeof(paths[0]), "%sCARDS.DLL", prefPath);
	snprintf(paths[1], sizeof(paths[1]), "%sSOL.EXE", prefPath);
	snprintf(paths[2], sizeof(paths[2]), "%sCARDS.DLL", basePath);
	snprintf(paths[3], sizeof(paths[3]), "%sSOL.EXE", basePath);

	SDL_RWops *rw;
	for (uint i = 0; i < 4; ++i) {
		rw = SDL_RWFromFile(paths[i], "rb");
		if (rw) break;
	}
	if (!rw) {
		char buf[4096];
		snprintf(
			buf, sizeof(buf), "CARDS.DLL not found in:\n%s\n%s",
			prefPath, basePath
		);
		SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Cards", buf, NULL);
		return EXIT_FAILURE;
	}

	struct Cards *cards = Cards_Load(
		rw, Cards_ColorKey | Cards_AlphaCorners | Cards_BlackBorders
	);
	if (!cards) err("Cards_Load");
	SDL_RWclose(rw);

	int error = SDL_CreateWindowAndRenderer(
		WindowWidth, WindowHeight, SDL_WINDOW_ALLOW_HIGHDPI,
		&window, &render
	);
	if (error) err("SDL_CreateWindowAndRenderer");
	SDL_SetWindowTitle(window, "Solitaire");

	SDL_RenderSetIntegerScale(render, SDL_TRUE);
	SDL_RenderSetLogicalSize(render, WindowWidth, WindowHeight);

	for (uint i = 0; i < Cards_Count; ++i) {
		textures[i] = NULL;
		if (!cards->surfaces[i]) continue;
		textures[i] = SDL_CreateTextureFromSurface(render, cards->surfaces[i]);
		if (!textures[i]) err("SDL_CreateTextureFromSurface");
	}
	Cards_Free(cards);

	srand(time(NULL));
	layout.backTexture = Cards_Back1 + randUniform(12);
	gameDeal();

	for (;;) {
		layoutClear();
		layoutStock();
		layoutWaste();
		layoutFoundations();
		layoutTableau();

		SDL_SetRenderDrawColor(render, 0x00, 0xAA, 0x55, 0xFF);
		SDL_RenderClear(render);
		renderList(&layout.base);
		renderList(&layout.main);
		renderList(&layout.drag);
		SDL_RenderPresent(render);

		SDL_Event event;
		for (;;) {
			SDL_WaitEvent(&event);
			if (event.type == SDL_QUIT) {
				goto quit;
			} else if (event.type == SDL_KEYDOWN) {
				if (keyDown(event.key)) break;
			} else if (event.type == SDL_MOUSEBUTTONDOWN) {
				if (mouseButtonDown(event.button)) break;
			} else if (event.type == SDL_MOUSEBUTTONUP) {
				if (mouseButtonUp(event.button)) break;
			} else if (event.type == SDL_MOUSEMOTION) {
				if (mouseMotion(event.motion)) break;
			}
		}
	}

quit:
	SDL_Quit();
}