solve_game() is passed the _initial_ game state, not the most recent
[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 #define TILE_SIZE 48
17 #define BORDER TILE_SIZE /* big border to fill with arrows */
18 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
19 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
20 #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
21
22 #define ANIM_TIME 0.13F
23 #define FLASH_FRAME 0.13F
24
25 #define X(state, i) ( (i) % (state)->w )
26 #define Y(state, i) ( (i) / (state)->w )
27 #define C(state, x, y) ( (y) * (state)->w + (x) )
28
29 enum {
30 COL_BACKGROUND,
31 COL_TEXT,
32 COL_HIGHLIGHT,
33 COL_LOWLIGHT,
34 NCOLOURS
35 };
36
37 struct game_params {
38 int w, h;
39 };
40
41 struct game_state {
42 int w, h, n;
43 int *tiles;
44 int completed;
45 int just_used_solve; /* used to suppress undo animation */
46 int used_solve; /* used to suppress completion flash */
47 int movecount;
48 int last_movement_sense;
49 };
50
51 static game_params *default_params(void)
52 {
53 game_params *ret = snew(game_params);
54
55 ret->w = ret->h = 4;
56
57 return ret;
58 }
59
60 static int game_fetch_preset(int i, char **name, game_params **params)
61 {
62 game_params *ret;
63 int w, h;
64 char buf[80];
65
66 switch (i) {
67 case 0: w = 3, h = 3; break;
68 case 1: w = 4, h = 3; break;
69 case 2: w = 4, h = 4; break;
70 case 3: w = 5, h = 4; break;
71 case 4: w = 5, h = 5; break;
72 default: return FALSE;
73 }
74
75 sprintf(buf, "%dx%d", w, h);
76 *name = dupstr(buf);
77 *params = ret = snew(game_params);
78 ret->w = w;
79 ret->h = h;
80 return TRUE;
81 }
82
83 static void free_params(game_params *params)
84 {
85 sfree(params);
86 }
87
88 static game_params *dup_params(game_params *params)
89 {
90 game_params *ret = snew(game_params);
91 *ret = *params; /* structure copy */
92 return ret;
93 }
94
95 static game_params *decode_params(char const *string)
96 {
97 game_params *ret = default_params();
98
99 ret->w = ret->h = atoi(string);
100 while (*string && isdigit(*string)) string++;
101 if (*string == 'x') {
102 string++;
103 ret->h = atoi(string);
104 }
105
106 return ret;
107 }
108
109 static char *encode_params(game_params *params)
110 {
111 char data[256];
112
113 sprintf(data, "%dx%d", params->w, params->h);
114
115 return dupstr(data);
116 }
117
118 static config_item *game_configure(game_params *params)
119 {
120 config_item *ret;
121 char buf[80];
122
123 ret = snewn(3, config_item);
124
125 ret[0].name = "Width";
126 ret[0].type = C_STRING;
127 sprintf(buf, "%d", params->w);
128 ret[0].sval = dupstr(buf);
129 ret[0].ival = 0;
130
131 ret[1].name = "Height";
132 ret[1].type = C_STRING;
133 sprintf(buf, "%d", params->h);
134 ret[1].sval = dupstr(buf);
135 ret[1].ival = 0;
136
137 ret[2].name = NULL;
138 ret[2].type = C_END;
139 ret[2].sval = NULL;
140 ret[2].ival = 0;
141
142 return ret;
143 }
144
145 static game_params *custom_params(config_item *cfg)
146 {
147 game_params *ret = snew(game_params);
148
149 ret->w = atoi(cfg[0].sval);
150 ret->h = atoi(cfg[1].sval);
151
152 return ret;
153 }
154
155 static char *validate_params(game_params *params)
156 {
157 if (params->w < 2 && params->h < 2)
158 return "Width and height must both be at least two";
159
160 return NULL;
161 }
162
163 static int perm_parity(int *perm, int n)
164 {
165 int i, j, ret;
166
167 ret = 0;
168
169 for (i = 0; i < n-1; i++)
170 for (j = i+1; j < n; j++)
171 if (perm[i] > perm[j])
172 ret = !ret;
173
174 return ret;
175 }
176
177 static char *new_game_seed(game_params *params, random_state *rs,
178 game_aux_info **aux)
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 static void game_free_aux_info(game_aux_info *aux)
285 {
286 assert(!"Shouldn't happen");
287 }
288
289
290 static char *validate_seed(game_params *params, char *seed)
291 {
292 char *p, *err;
293 int i, area;
294 int *used;
295
296 area = params->w * params->h;
297 p = seed;
298 err = NULL;
299
300 used = snewn(area, int);
301 for (i = 0; i < area; i++)
302 used[i] = FALSE;
303
304 for (i = 0; i < area; i++) {
305 char *q = p;
306 int n;
307
308 if (*p < '0' || *p > '9') {
309 err = "Not enough numbers in string";
310 goto leave;
311 }
312 while (*p >= '0' && *p <= '9')
313 p++;
314 if (i < area-1 && *p != ',') {
315 err = "Expected comma after number";
316 goto leave;
317 }
318 else if (i == area-1 && *p) {
319 err = "Excess junk at end of string";
320 goto leave;
321 }
322 n = atoi(q);
323 if (n < 1 || n > area) {
324 err = "Number out of range";
325 goto leave;
326 }
327 if (used[n-1]) {
328 err = "Number used twice";
329 goto leave;
330 }
331 used[n-1] = TRUE;
332
333 if (*p) p++; /* eat comma */
334 }
335
336 leave:
337 sfree(used);
338 return err;
339 }
340
341 static game_state *new_game(game_params *params, char *seed)
342 {
343 game_state *state = snew(game_state);
344 int i;
345 char *p;
346
347 state->w = params->w;
348 state->h = params->h;
349 state->n = params->w * params->h;
350 state->tiles = snewn(state->n, int);
351
352 p = seed;
353 i = 0;
354 for (i = 0; i < state->n; i++) {
355 assert(*p);
356 state->tiles[i] = atoi(p);
357 while (*p && *p != ',')
358 p++;
359 if (*p) p++; /* eat comma */
360 }
361 assert(!*p);
362
363 state->completed = state->movecount = 0;
364 state->used_solve = state->just_used_solve = FALSE;
365 state->last_movement_sense = 0;
366
367 return state;
368 }
369
370 static game_state *dup_game(game_state *state)
371 {
372 game_state *ret = snew(game_state);
373
374 ret->w = state->w;
375 ret->h = state->h;
376 ret->n = state->n;
377 ret->tiles = snewn(state->w * state->h, int);
378 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
379 ret->completed = state->completed;
380 ret->movecount = state->movecount;
381 ret->used_solve = state->used_solve;
382 ret->just_used_solve = state->just_used_solve;
383 ret->last_movement_sense = state->last_movement_sense;
384
385 return ret;
386 }
387
388 static void free_game(game_state *state)
389 {
390 sfree(state);
391 }
392
393 static game_state *solve_game(game_state *state, game_aux_info *aux,
394 char **error)
395 {
396 game_state *ret = dup_game(state);
397 int i;
398
399 /*
400 * Simply replace the grid with a solved one. For this game,
401 * this isn't a useful operation for actually telling the user
402 * what they should have done, but it is useful for
403 * conveniently being able to get hold of a clean state from
404 * which to practise manoeuvres.
405 */
406 for (i = 0; i < ret->n; i++)
407 ret->tiles[i] = i+1;
408 ret->used_solve = ret->just_used_solve = TRUE;
409 ret->completed = ret->movecount = 1;
410
411 return ret;
412 }
413
414 static char *game_text_format(game_state *state)
415 {
416 char *ret, *p, buf[80];
417 int x, y, col, maxlen;
418
419 /*
420 * First work out how many characters we need to display each
421 * number.
422 */
423 col = sprintf(buf, "%d", state->n);
424
425 /*
426 * Now we know the exact total size of the grid we're going to
427 * produce: it's got h rows, each containing w lots of col, w-1
428 * spaces and a trailing newline.
429 */
430 maxlen = state->h * state->w * (col+1);
431
432 ret = snewn(maxlen+1, char);
433 p = ret;
434
435 for (y = 0; y < state->h; y++) {
436 for (x = 0; x < state->w; x++) {
437 int v = state->tiles[state->w*y+x];
438 sprintf(buf, "%*d", col, v);
439 memcpy(p, buf, col);
440 p += col;
441 if (x+1 == state->w)
442 *p++ = '\n';
443 else
444 *p++ = ' ';
445 }
446 }
447
448 assert(p - ret == maxlen);
449 *p = '\0';
450 return ret;
451 }
452
453 static game_ui *new_ui(game_state *state)
454 {
455 return NULL;
456 }
457
458 static void free_ui(game_ui *ui)
459 {
460 }
461
462 static game_state *make_move(game_state *from, game_ui *ui,
463 int x, int y, int button)
464 {
465 int cx, cy;
466 int dx, dy, tx, ty, n;
467 game_state *ret;
468
469 if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
470 return NULL;
471
472 cx = FROMCOORD(x);
473 cy = FROMCOORD(y);
474 if (cx == -1 && cy >= 0 && cy < from->h)
475 n = from->w, dx = +1, dy = 0;
476 else if (cx == from->w && cy >= 0 && cy < from->h)
477 n = from->w, dx = -1, dy = 0;
478 else if (cy == -1 && cx >= 0 && cx < from->w)
479 n = from->h, dy = +1, dx = 0;
480 else if (cy == from->h && cx >= 0 && cx < from->w)
481 n = from->h, dy = -1, dx = 0;
482 else
483 return NULL; /* invalid click location */
484
485 /* reverse direction if right hand button is pressed */
486 if (button == RIGHT_BUTTON)
487 {
488 dx = -dx; if (dx) cx = from->w - 1 - cx;
489 dy = -dy; if (dy) cy = from->h - 1 - cy;
490 }
491
492 ret = dup_game(from);
493 ret->just_used_solve = FALSE; /* zero this in a hurry */
494
495 do {
496 cx += dx;
497 cy += dy;
498 tx = (cx + dx + from->w) % from->w;
499 ty = (cy + dy + from->h) % from->h;
500 ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
501 } while (--n > 0);
502
503 ret->movecount++;
504
505 ret->last_movement_sense = -(dx+dy);
506
507 /*
508 * See if the game has been completed.
509 */
510 if (!ret->completed) {
511 ret->completed = ret->movecount;
512 for (n = 0; n < ret->n; n++)
513 if (ret->tiles[n] != n+1)
514 ret->completed = FALSE;
515 }
516
517 return ret;
518 }
519
520 /* ----------------------------------------------------------------------
521 * Drawing routines.
522 */
523
524 struct game_drawstate {
525 int started;
526 int w, h, bgcolour;
527 int *tiles;
528 };
529
530 static void game_size(game_params *params, int *x, int *y)
531 {
532 *x = TILE_SIZE * params->w + 2 * BORDER;
533 *y = TILE_SIZE * params->h + 2 * BORDER;
534 }
535
536 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
537 {
538 float *ret = snewn(3 * NCOLOURS, float);
539 int i;
540 float max;
541
542 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
543
544 /*
545 * Drop the background colour so that the highlight is
546 * noticeably brighter than it while still being under 1.
547 */
548 max = ret[COL_BACKGROUND*3];
549 for (i = 1; i < 3; i++)
550 if (ret[COL_BACKGROUND*3+i] > max)
551 max = ret[COL_BACKGROUND*3+i];
552 if (max * 1.2F > 1.0F) {
553 for (i = 0; i < 3; i++)
554 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
555 }
556
557 for (i = 0; i < 3; i++) {
558 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
559 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
560 ret[COL_TEXT * 3 + i] = 0.0;
561 }
562
563 *ncolours = NCOLOURS;
564 return ret;
565 }
566
567 static game_drawstate *game_new_drawstate(game_state *state)
568 {
569 struct game_drawstate *ds = snew(struct game_drawstate);
570 int i;
571
572 ds->started = FALSE;
573 ds->w = state->w;
574 ds->h = state->h;
575 ds->bgcolour = COL_BACKGROUND;
576 ds->tiles = snewn(ds->w*ds->h, int);
577 for (i = 0; i < ds->w*ds->h; i++)
578 ds->tiles[i] = -1;
579
580 return ds;
581 }
582
583 static void game_free_drawstate(game_drawstate *ds)
584 {
585 sfree(ds->tiles);
586 sfree(ds);
587 }
588
589 static void draw_tile(frontend *fe, game_state *state, int x, int y,
590 int tile, int flash_colour)
591 {
592 if (tile == 0) {
593 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
594 flash_colour);
595 } else {
596 int coords[6];
597 char str[40];
598
599 coords[0] = x + TILE_SIZE - 1;
600 coords[1] = y + TILE_SIZE - 1;
601 coords[2] = x + TILE_SIZE - 1;
602 coords[3] = y;
603 coords[4] = x;
604 coords[5] = y + TILE_SIZE - 1;
605 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
606 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
607
608 coords[0] = x;
609 coords[1] = y;
610 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
611 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
612
613 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
614 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
615 flash_colour);
616
617 sprintf(str, "%d", tile);
618 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
619 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
620 COL_TEXT, str);
621 }
622 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
623 }
624
625 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
626 {
627 int coords[14];
628 int ydy = -xdx, ydx = xdy;
629
630 #define POINT(n, xx, yy) ( \
631 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
632 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
633
634 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
635 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
636 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
637 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
638 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
639 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
640 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
641
642 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
643 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
644 }
645
646 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
647 game_state *state, int dir, game_ui *ui,
648 float animtime, float flashtime)
649 {
650 int i, bgcolour;
651
652 if (flashtime > 0) {
653 int frame = (int)(flashtime / FLASH_FRAME);
654 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
655 } else
656 bgcolour = COL_BACKGROUND;
657
658 if (!ds->started) {
659 int coords[6];
660
661 draw_rect(fe, 0, 0,
662 TILE_SIZE * state->w + 2 * BORDER,
663 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
664 draw_update(fe, 0, 0,
665 TILE_SIZE * state->w + 2 * BORDER,
666 TILE_SIZE * state->h + 2 * BORDER);
667
668 /*
669 * Recessed area containing the whole puzzle.
670 */
671 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
672 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
673 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
674 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
675 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
676 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
677 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
678 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
679
680 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
681 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
682 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
683 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
684
685 /*
686 * Arrows for making moves.
687 */
688 for (i = 0; i < state->w; i++) {
689 draw_arrow(fe, COORD(i), COORD(0), +1, 0);
690 draw_arrow(fe, COORD(i+1), COORD(state->h), -1, 0);
691 }
692 for (i = 0; i < state->h; i++) {
693 draw_arrow(fe, COORD(state->w), COORD(i), 0, +1);
694 draw_arrow(fe, COORD(0), COORD(i+1), 0, -1);
695 }
696
697 ds->started = TRUE;
698 }
699
700 /*
701 * Now draw each tile.
702 */
703
704 clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
705
706 for (i = 0; i < state->n; i++) {
707 int t, t0;
708 /*
709 * Figure out what should be displayed at this
710 * location. It's either a simple tile, or it's a
711 * transition between two tiles (in which case we say
712 * -1 because it must always be drawn).
713 */
714
715 if (oldstate && oldstate->tiles[i] != state->tiles[i])
716 t = -1;
717 else
718 t = state->tiles[i];
719
720 t0 = t;
721
722 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
723 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
724 int x, y, x2, y2;
725
726 /*
727 * Figure out what to _actually_ draw, and where to
728 * draw it.
729 */
730 if (t == -1) {
731 int x0, y0, x1, y1, dx, dy;
732 int j;
733 float c;
734 int sense;
735
736 if (dir < 0) {
737 assert(oldstate);
738 sense = -oldstate->last_movement_sense;
739 } else {
740 sense = state->last_movement_sense;
741 }
742
743 t = state->tiles[i];
744
745 /*
746 * FIXME: must be prepared to draw a double
747 * tile in some situations.
748 */
749
750 /*
751 * Find the coordinates of this tile in the old and
752 * new states.
753 */
754 x1 = COORD(X(state, i));
755 y1 = COORD(Y(state, i));
756 for (j = 0; j < oldstate->n; j++)
757 if (oldstate->tiles[j] == state->tiles[i])
758 break;
759 assert(j < oldstate->n);
760 x0 = COORD(X(state, j));
761 y0 = COORD(Y(state, j));
762
763 dx = (x1 - x0);
764 if (dx != 0 &&
765 dx != TILE_SIZE * sense) {
766 dx = (dx < 0 ? dx + TILE_SIZE * state->w :
767 dx - TILE_SIZE * state->w);
768 assert(abs(dx) == TILE_SIZE);
769 }
770 dy = (y1 - y0);
771 if (dy != 0 &&
772 dy != TILE_SIZE * sense) {
773 dy = (dy < 0 ? dy + TILE_SIZE * state->h :
774 dy - TILE_SIZE * state->h);
775 assert(abs(dy) == TILE_SIZE);
776 }
777
778 c = (animtime / ANIM_TIME);
779 if (c < 0.0F) c = 0.0F;
780 if (c > 1.0F) c = 1.0F;
781
782 x = x0 + (int)(c * dx);
783 y = y0 + (int)(c * dy);
784 x2 = x1 - dx + (int)(c * dx);
785 y2 = y1 - dy + (int)(c * dy);
786 } else {
787 x = COORD(X(state, i));
788 y = COORD(Y(state, i));
789 x2 = y2 = -1;
790 }
791
792 draw_tile(fe, state, x, y, t, bgcolour);
793 if (x2 != -1 || y2 != -1)
794 draw_tile(fe, state, x2, y2, t, bgcolour);
795 }
796 ds->tiles[i] = t0;
797 }
798
799 unclip(fe);
800
801 ds->bgcolour = bgcolour;
802
803 /*
804 * Update the status bar.
805 */
806 {
807 char statusbuf[256];
808
809 /*
810 * Don't show the new status until we're also showing the
811 * new _state_ - after the game animation is complete.
812 */
813 if (oldstate)
814 state = oldstate;
815
816 if (state->used_solve)
817 sprintf(statusbuf, "Moves since auto-solve: %d",
818 state->movecount - state->completed);
819 else
820 sprintf(statusbuf, "%sMoves: %d",
821 (state->completed ? "COMPLETED! " : ""),
822 (state->completed ? state->completed : state->movecount));
823
824 status_bar(fe, statusbuf);
825 }
826 }
827
828 static float game_anim_length(game_state *oldstate,
829 game_state *newstate, int dir)
830 {
831 if ((dir > 0 && newstate->just_used_solve) ||
832 (dir < 0 && oldstate->just_used_solve))
833 return 0.0F;
834 else
835 return ANIM_TIME;
836 }
837
838 static float game_flash_length(game_state *oldstate,
839 game_state *newstate, int dir)
840 {
841 if (!oldstate->completed && newstate->completed &&
842 !oldstate->used_solve && !newstate->used_solve)
843 return 2 * FLASH_FRAME;
844 else
845 return 0.0F;
846 }
847
848 static int game_wants_statusbar(void)
849 {
850 return TRUE;
851 }
852
853 #ifdef COMBINED
854 #define thegame sixteen
855 #endif
856
857 const struct game thegame = {
858 "Sixteen", "games.sixteen",
859 default_params,
860 game_fetch_preset,
861 decode_params,
862 encode_params,
863 free_params,
864 dup_params,
865 TRUE, game_configure, custom_params,
866 validate_params,
867 new_game_seed,
868 game_free_aux_info,
869 validate_seed,
870 new_game,
871 dup_game,
872 free_game,
873 TRUE, solve_game,
874 TRUE, game_text_format,
875 new_ui,
876 free_ui,
877 make_move,
878 game_size,
879 game_colours,
880 game_new_drawstate,
881 game_free_drawstate,
882 game_redraw,
883 game_anim_length,
884 game_flash_length,
885 game_wants_statusbar,
886 };