Introduce routines in each game module to encode a set of game
[sgt/puzzles] / fifteen.c
1 /*
2 * fifteen.c: standard 15-puzzle.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <ctype.h>
10 #include <math.h>
11
12 #include "puzzles.h"
13
14 const char *const game_name = "Fifteen";
15 const int game_can_configure = TRUE;
16
17 #define TILE_SIZE 48
18 #define BORDER (TILE_SIZE / 2)
19 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
20 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
21 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
22
23 #define ANIM_TIME 0.13F
24 #define FLASH_FRAME 0.13F
25
26 #define X(state, i) ( (i) % (state)->w )
27 #define Y(state, i) ( (i) / (state)->w )
28 #define C(state, x, y) ( (y) * (state)->w + (x) )
29
30 enum {
31 COL_BACKGROUND,
32 COL_TEXT,
33 COL_HIGHLIGHT,
34 COL_LOWLIGHT,
35 NCOLOURS
36 };
37
38 struct game_params {
39 int w, h;
40 };
41
42 struct game_state {
43 int w, h, n;
44 int *tiles;
45 int gap_pos;
46 int completed;
47 int movecount;
48 };
49
50 game_params *default_params(void)
51 {
52 game_params *ret = snew(game_params);
53
54 ret->w = ret->h = 4;
55
56 return ret;
57 }
58
59 int game_fetch_preset(int i, char **name, game_params **params)
60 {
61 return FALSE;
62 }
63
64 void free_params(game_params *params)
65 {
66 sfree(params);
67 }
68
69 game_params *dup_params(game_params *params)
70 {
71 game_params *ret = snew(game_params);
72 *ret = *params; /* structure copy */
73 return ret;
74 }
75
76 game_params *decode_params(char const *string)
77 {
78 game_params *ret = default_params();
79
80 ret->w = ret->h = atoi(string);
81 while (*string && isdigit(*string)) string++;
82 if (*string == 'x') {
83 string++;
84 ret->h = atoi(string);
85 }
86
87 return ret;
88 }
89
90 char *encode_params(game_params *params)
91 {
92 char data[256];
93
94 sprintf(data, "%dx%d", params->w, params->h);
95
96 return dupstr(data);
97 }
98
99 config_item *game_configure(game_params *params)
100 {
101 config_item *ret;
102 char buf[80];
103
104 ret = snewn(3, config_item);
105
106 ret[0].name = "Width";
107 ret[0].type = C_STRING;
108 sprintf(buf, "%d", params->w);
109 ret[0].sval = dupstr(buf);
110 ret[0].ival = 0;
111
112 ret[1].name = "Height";
113 ret[1].type = C_STRING;
114 sprintf(buf, "%d", params->h);
115 ret[1].sval = dupstr(buf);
116 ret[1].ival = 0;
117
118 ret[2].name = NULL;
119 ret[2].type = C_END;
120 ret[2].sval = NULL;
121 ret[2].ival = 0;
122
123 return ret;
124 }
125
126 game_params *custom_params(config_item *cfg)
127 {
128 game_params *ret = snew(game_params);
129
130 ret->w = atoi(cfg[0].sval);
131 ret->h = atoi(cfg[1].sval);
132
133 return ret;
134 }
135
136 char *validate_params(game_params *params)
137 {
138 if (params->w < 2 && params->h < 2)
139 return "Width and height must both be at least two";
140
141 return NULL;
142 }
143
144 int perm_parity(int *perm, int n)
145 {
146 int i, j, ret;
147
148 ret = 0;
149
150 for (i = 0; i < n-1; i++)
151 for (j = i+1; j < n; j++)
152 if (perm[i] > perm[j])
153 ret = !ret;
154
155 return ret;
156 }
157
158 char *new_game_seed(game_params *params, random_state *rs)
159 {
160 int gap, n, i, x;
161 int x1, x2, p1, p2, parity;
162 int *tiles, *used;
163 char *ret;
164 int retlen;
165
166 n = params->w * params->h;
167
168 tiles = snewn(n, int);
169 used = snewn(n, int);
170
171 for (i = 0; i < n; i++) {
172 tiles[i] = -1;
173 used[i] = FALSE;
174 }
175
176 gap = random_upto(rs, n);
177 tiles[gap] = 0;
178 used[0] = TRUE;
179
180 /*
181 * Place everything else except the last two tiles.
182 */
183 for (x = 0, i = n-1; i > 2; i--) {
184 int k = random_upto(rs, i);
185 int j;
186
187 for (j = 0; j < n; j++)
188 if (!used[j] && (k-- == 0))
189 break;
190
191 assert(j < n && !used[j]);
192 used[j] = TRUE;
193
194 while (tiles[x] >= 0)
195 x++;
196 assert(x < n);
197 tiles[x] = j;
198 }
199
200 /*
201 * Find the last two locations, and the last two pieces.
202 */
203 while (tiles[x] >= 0)
204 x++;
205 assert(x < n);
206 x1 = x;
207 x++;
208 while (tiles[x] >= 0)
209 x++;
210 assert(x < n);
211 x2 = x;
212
213 for (i = 0; i < n; i++)
214 if (!used[i])
215 break;
216 p1 = i;
217 for (i = p1+1; i < n; i++)
218 if (!used[i])
219 break;
220 p2 = i;
221
222 /*
223 * Determine the required parity of the overall permutation.
224 * This is the XOR of:
225 *
226 * - The chessboard parity ((x^y)&1) of the gap square. The
227 * bottom right counts as even.
228 *
229 * - The parity of n. (The target permutation is 1,...,n-1,0
230 * rather than 0,...,n-1; this is a cyclic permutation of
231 * the starting point and hence is odd iff n is even.)
232 */
233 parity = ((X(params, gap) - (params->w-1)) ^
234 (Y(params, gap) - (params->h-1)) ^
235 (n+1)) & 1;
236
237 /*
238 * Try the last two tiles one way round. If that fails, swap
239 * them.
240 */
241 tiles[x1] = p1;
242 tiles[x2] = p2;
243 if (perm_parity(tiles, n) != parity) {
244 tiles[x1] = p2;
245 tiles[x2] = p1;
246 assert(perm_parity(tiles, n) == parity);
247 }
248
249 /*
250 * Now construct the game seed, by describing the tile array as
251 * a simple sequence of comma-separated integers.
252 */
253 ret = NULL;
254 retlen = 0;
255 for (i = 0; i < n; i++) {
256 char buf[80];
257 int k;
258
259 k = sprintf(buf, "%d,", tiles[i]);
260
261 ret = sresize(ret, retlen + k + 1, char);
262 strcpy(ret + retlen, buf);
263 retlen += k;
264 }
265 ret[retlen-1] = '\0'; /* delete last comma */
266
267 sfree(tiles);
268 sfree(used);
269
270 return ret;
271 }
272
273 char *validate_seed(game_params *params, char *seed)
274 {
275 char *p, *err;
276 int i, area;
277 int *used;
278
279 area = params->w * params->h;
280 p = seed;
281 err = NULL;
282
283 used = snewn(area, int);
284 for (i = 0; i < area; i++)
285 used[i] = FALSE;
286
287 for (i = 0; i < area; i++) {
288 char *q = p;
289 int n;
290
291 if (*p < '0' || *p > '9') {
292 err = "Not enough numbers in string";
293 goto leave;
294 }
295 while (*p >= '0' && *p <= '9')
296 p++;
297 if (i < area-1 && *p != ',') {
298 err = "Expected comma after number";
299 goto leave;
300 }
301 else if (i == area-1 && *p) {
302 err = "Excess junk at end of string";
303 goto leave;
304 }
305 n = atoi(q);
306 if (n < 0 || n >= area) {
307 err = "Number out of range";
308 goto leave;
309 }
310 if (used[n]) {
311 err = "Number used twice";
312 goto leave;
313 }
314 used[n] = TRUE;
315
316 if (*p) p++; /* eat comma */
317 }
318
319 leave:
320 sfree(used);
321 return err;
322 }
323
324 game_state *new_game(game_params *params, char *seed)
325 {
326 game_state *state = snew(game_state);
327 int i;
328 char *p;
329
330 state->w = params->w;
331 state->h = params->h;
332 state->n = params->w * params->h;
333 state->tiles = snewn(state->n, int);
334
335 state->gap_pos = 0;
336 p = seed;
337 i = 0;
338 for (i = 0; i < state->n; i++) {
339 assert(*p);
340 state->tiles[i] = atoi(p);
341 if (state->tiles[i] == 0)
342 state->gap_pos = i;
343 while (*p && *p != ',')
344 p++;
345 if (*p) p++; /* eat comma */
346 }
347 assert(!*p);
348 assert(state->tiles[state->gap_pos] == 0);
349
350 state->completed = state->movecount = 0;
351
352 return state;
353 }
354
355 game_state *dup_game(game_state *state)
356 {
357 game_state *ret = snew(game_state);
358
359 ret->w = state->w;
360 ret->h = state->h;
361 ret->n = state->n;
362 ret->tiles = snewn(state->w * state->h, int);
363 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
364 ret->gap_pos = state->gap_pos;
365 ret->completed = state->completed;
366 ret->movecount = state->movecount;
367
368 return ret;
369 }
370
371 void free_game(game_state *state)
372 {
373 sfree(state);
374 }
375
376 game_ui *new_ui(game_state *state)
377 {
378 return NULL;
379 }
380
381 void free_ui(game_ui *ui)
382 {
383 }
384
385 game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
386 {
387 int gx, gy, dx, dy, ux, uy, up, p;
388 game_state *ret;
389
390 gx = X(from, from->gap_pos);
391 gy = Y(from, from->gap_pos);
392
393 if (button == CURSOR_RIGHT && gx > 0)
394 dx = gx - 1, dy = gy;
395 else if (button == CURSOR_LEFT && gx < from->w-1)
396 dx = gx + 1, dy = gy;
397 else if (button == CURSOR_DOWN && gy > 0)
398 dy = gy - 1, dx = gx;
399 else if (button == CURSOR_UP && gy < from->h-1)
400 dy = gy + 1, dx = gx;
401 else if (button == LEFT_BUTTON) {
402 dx = FROMCOORD(x);
403 dy = FROMCOORD(y);
404 if (dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
405 return NULL; /* out of bounds */
406 /*
407 * Any click location should be equal to the gap location
408 * in _precisely_ one coordinate.
409 */
410 if ((dx == gx && dy == gy) || (dx != gx && dy != gy))
411 return NULL;
412 } else
413 return NULL; /* no move */
414
415 /*
416 * Find the unit displacement from the original gap
417 * position towards this one.
418 */
419 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
420 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
421 up = C(from, ux, uy);
422
423 ret = dup_game(from);
424
425 ret->gap_pos = C(from, dx, dy);
426 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
427
428 ret->tiles[ret->gap_pos] = 0;
429
430 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
431 assert(p >= 0 && p < from->n);
432 ret->tiles[p] = from->tiles[p + up];
433 ret->movecount++;
434 }
435
436 /*
437 * See if the game has been completed.
438 */
439 if (!ret->completed) {
440 ret->completed = ret->movecount;
441 for (p = 0; p < ret->n; p++)
442 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
443 ret->completed = 0;
444 }
445
446 return ret;
447 }
448
449 /* ----------------------------------------------------------------------
450 * Drawing routines.
451 */
452
453 struct game_drawstate {
454 int started;
455 int w, h, bgcolour;
456 int *tiles;
457 };
458
459 void game_size(game_params *params, int *x, int *y)
460 {
461 *x = TILE_SIZE * params->w + 2 * BORDER;
462 *y = TILE_SIZE * params->h + 2 * BORDER;
463 }
464
465 float *game_colours(frontend *fe, game_state *state, int *ncolours)
466 {
467 float *ret = snewn(3 * NCOLOURS, float);
468 int i;
469 float max;
470
471 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
472
473 /*
474 * Drop the background colour so that the highlight is
475 * noticeably brighter than it while still being under 1.
476 */
477 max = ret[COL_BACKGROUND*3];
478 for (i = 1; i < 3; i++)
479 if (ret[COL_BACKGROUND*3+i] > max)
480 max = ret[COL_BACKGROUND*3+i];
481 if (max * 1.2F > 1.0F) {
482 for (i = 0; i < 3; i++)
483 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
484 }
485
486 for (i = 0; i < 3; i++) {
487 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
488 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
489 ret[COL_TEXT * 3 + i] = 0.0;
490 }
491
492 *ncolours = NCOLOURS;
493 return ret;
494 }
495
496 game_drawstate *game_new_drawstate(game_state *state)
497 {
498 struct game_drawstate *ds = snew(struct game_drawstate);
499 int i;
500
501 ds->started = FALSE;
502 ds->w = state->w;
503 ds->h = state->h;
504 ds->bgcolour = COL_BACKGROUND;
505 ds->tiles = snewn(ds->w*ds->h, int);
506 for (i = 0; i < ds->w*ds->h; i++)
507 ds->tiles[i] = -1;
508
509 return ds;
510 }
511
512 void game_free_drawstate(game_drawstate *ds)
513 {
514 sfree(ds->tiles);
515 sfree(ds);
516 }
517
518 static void draw_tile(frontend *fe, game_state *state, int x, int y,
519 int tile, int flash_colour)
520 {
521 if (tile == 0) {
522 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
523 flash_colour);
524 } else {
525 int coords[6];
526 char str[40];
527
528 coords[0] = x + TILE_SIZE - 1;
529 coords[1] = y + TILE_SIZE - 1;
530 coords[2] = x + TILE_SIZE - 1;
531 coords[3] = y;
532 coords[4] = x;
533 coords[5] = y + TILE_SIZE - 1;
534 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
535 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
536
537 coords[0] = x;
538 coords[1] = y;
539 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
540 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
541
542 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
543 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
544 flash_colour);
545
546 sprintf(str, "%d", tile);
547 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
548 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
549 COL_TEXT, str);
550 }
551 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
552 }
553
554 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
555 game_state *state, game_ui *ui,
556 float animtime, float flashtime)
557 {
558 int i, pass, bgcolour;
559
560 if (flashtime > 0) {
561 int frame = (int)(flashtime / FLASH_FRAME);
562 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
563 } else
564 bgcolour = COL_BACKGROUND;
565
566 if (!ds->started) {
567 int coords[6];
568
569 draw_rect(fe, 0, 0,
570 TILE_SIZE * state->w + 2 * BORDER,
571 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
572 draw_update(fe, 0, 0,
573 TILE_SIZE * state->w + 2 * BORDER,
574 TILE_SIZE * state->h + 2 * BORDER);
575
576 /*
577 * Recessed area containing the whole puzzle.
578 */
579 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
580 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
581 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
582 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
583 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
584 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
585 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
586 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
587
588 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
589 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
590 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
591 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
592
593 ds->started = TRUE;
594 }
595
596 /*
597 * Now draw each tile. We do this in two passes to make
598 * animation easy.
599 */
600 for (pass = 0; pass < 2; pass++) {
601 for (i = 0; i < state->n; i++) {
602 int t, t0;
603 /*
604 * Figure out what should be displayed at this
605 * location. It's either a simple tile, or it's a
606 * transition between two tiles (in which case we say
607 * -1 because it must always be drawn).
608 */
609
610 if (oldstate && oldstate->tiles[i] != state->tiles[i])
611 t = -1;
612 else
613 t = state->tiles[i];
614
615 t0 = t;
616
617 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
618 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
619 int x, y;
620
621 /*
622 * Figure out what to _actually_ draw, and where to
623 * draw it.
624 */
625 if (t == -1) {
626 int x0, y0, x1, y1;
627 int j;
628
629 /*
630 * On the first pass, just blank the tile.
631 */
632 if (pass == 0) {
633 x = COORD(X(state, i));
634 y = COORD(Y(state, i));
635 t = 0;
636 } else {
637 float c;
638
639 t = state->tiles[i];
640
641 /*
642 * Don't bother moving the gap; just don't
643 * draw it.
644 */
645 if (t == 0)
646 continue;
647
648 /*
649 * Find the coordinates of this tile in the old and
650 * new states.
651 */
652 x1 = COORD(X(state, i));
653 y1 = COORD(Y(state, i));
654 for (j = 0; j < oldstate->n; j++)
655 if (oldstate->tiles[j] == state->tiles[i])
656 break;
657 assert(j < oldstate->n);
658 x0 = COORD(X(state, j));
659 y0 = COORD(Y(state, j));
660
661 c = (animtime / ANIM_TIME);
662 if (c < 0.0F) c = 0.0F;
663 if (c > 1.0F) c = 1.0F;
664
665 x = x0 + (int)(c * (x1 - x0));
666 y = y0 + (int)(c * (y1 - y0));
667 }
668
669 } else {
670 if (pass == 0)
671 continue;
672 x = COORD(X(state, i));
673 y = COORD(Y(state, i));
674 }
675
676 draw_tile(fe, state, x, y, t, bgcolour);
677 }
678 ds->tiles[i] = t0;
679 }
680 }
681 ds->bgcolour = bgcolour;
682
683 /*
684 * Update the status bar.
685 */
686 {
687 char statusbuf[256];
688
689 /*
690 * Don't show the new status until we're also showing the
691 * new _state_ - after the game animation is complete.
692 */
693 if (oldstate)
694 state = oldstate;
695
696 sprintf(statusbuf, "%sMoves: %d",
697 (state->completed ? "COMPLETED! " : ""),
698 (state->completed ? state->completed : state->movecount));
699
700 status_bar(fe, statusbuf);
701 }
702 }
703
704 float game_anim_length(game_state *oldstate, game_state *newstate)
705 {
706 return ANIM_TIME;
707 }
708
709 float game_flash_length(game_state *oldstate, game_state *newstate)
710 {
711 if (!oldstate->completed && newstate->completed)
712 return 2 * FLASH_FRAME;
713 else
714 return 0.0F;
715 }
716
717 int game_wants_statusbar(void)
718 {
719 return TRUE;
720 }