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