chiark / gitweb /
ac3191c899d7baff1eaa9f8ead707d283bba4273
[sgt-puzzles.git] / 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 PREFERRED_TILE_SIZE 48
17 #define TILE_SIZE (ds->tilesize)
18 #define BORDER TILE_SIZE
19 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
20 #define COORD(x)  ( (x) * TILE_SIZE + BORDER )
21 #define FROMCOORD(x)  ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
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     int movetarget;
41 };
42
43 struct game_state {
44     int w, h, n;
45     int *tiles;
46     int completed;
47     int used_solve;                    /* used to suppress completion flash */
48     int movecount, movetarget;
49     int last_movement_sense;
50 };
51
52 static game_params *default_params(void)
53 {
54     game_params *ret = snew(game_params);
55
56     ret->w = ret->h = 4;
57     ret->movetarget = 0;
58
59     return ret;
60 }
61
62 static int game_fetch_preset(int i, char **name, game_params **params)
63 {
64     game_params *ret;
65     int w, h;
66     char buf[80];
67
68     switch (i) {
69       case 0: w = 3, h = 3; break;
70       case 1: w = 4, h = 3; break;
71       case 2: w = 4, h = 4; break;
72       case 3: w = 5, h = 4; break;
73       case 4: w = 5, h = 5; break;
74       default: return FALSE;
75     }
76
77     sprintf(buf, "%dx%d", w, h);
78     *name = dupstr(buf);
79     *params = ret = snew(game_params);
80     ret->w = w;
81     ret->h = h;
82     ret->movetarget = 0;
83     return TRUE;
84 }
85
86 static void free_params(game_params *params)
87 {
88     sfree(params);
89 }
90
91 static game_params *dup_params(game_params *params)
92 {
93     game_params *ret = snew(game_params);
94     *ret = *params;                    /* structure copy */
95     return ret;
96 }
97
98 static void decode_params(game_params *ret, char const *string)
99 {
100     ret->w = ret->h = atoi(string);
101     ret->movetarget = 0;
102     while (*string && isdigit((unsigned char)*string)) string++;
103     if (*string == 'x') {
104         string++;
105         ret->h = atoi(string);
106         while (*string && isdigit((unsigned char)*string))
107             string++;
108     }
109     if (*string == 'm') {
110         string++;
111         ret->movetarget = atoi(string);
112         while (*string && isdigit((unsigned char)*string))
113             string++;
114     }
115 }
116
117 static char *encode_params(game_params *params, int full)
118 {
119     char data[256];
120
121     sprintf(data, "%dx%d", params->w, params->h);
122     /* Shuffle limit is part of the limited parameters, because we have to
123      * supply the target move count. */
124     if (params->movetarget)
125         sprintf(data + strlen(data), "m%d", params->movetarget);
126
127     return dupstr(data);
128 }
129
130 static config_item *game_configure(game_params *params)
131 {
132     config_item *ret;
133     char buf[80];
134
135     ret = snewn(4, config_item);
136
137     ret[0].name = "Width";
138     ret[0].type = C_STRING;
139     sprintf(buf, "%d", params->w);
140     ret[0].sval = dupstr(buf);
141     ret[0].ival = 0;
142
143     ret[1].name = "Height";
144     ret[1].type = C_STRING;
145     sprintf(buf, "%d", params->h);
146     ret[1].sval = dupstr(buf);
147     ret[1].ival = 0;
148
149     ret[2].name = "Number of shuffling moves";
150     ret[2].type = C_STRING;
151     sprintf(buf, "%d", params->movetarget);
152     ret[2].sval = dupstr(buf);
153     ret[2].ival = 0;
154
155     ret[3].name = NULL;
156     ret[3].type = C_END;
157     ret[3].sval = NULL;
158     ret[3].ival = 0;
159
160     return ret;
161 }
162
163 static game_params *custom_params(config_item *cfg)
164 {
165     game_params *ret = snew(game_params);
166
167     ret->w = atoi(cfg[0].sval);
168     ret->h = atoi(cfg[1].sval);
169     ret->movetarget = atoi(cfg[2].sval);
170
171     return ret;
172 }
173
174 static char *validate_params(game_params *params, int full)
175 {
176     if (params->w < 2 || params->h < 2)
177         return "Width and height must both be at least two";
178
179     return NULL;
180 }
181
182 static int perm_parity(int *perm, int n)
183 {
184     int i, j, ret;
185
186     ret = 0;
187
188     for (i = 0; i < n-1; i++)
189         for (j = i+1; j < n; j++)
190             if (perm[i] > perm[j])
191                 ret = !ret;
192
193     return ret;
194 }
195
196 static char *new_game_desc(game_params *params, random_state *rs,
197                            char **aux, int interactive)
198 {
199     int stop, n, i, x;
200     int x1, x2, p1, p2;
201     int *tiles, *used;
202     char *ret;
203     int retlen;
204
205     n = params->w * params->h;
206
207     tiles = snewn(n, int);
208
209     if (params->movetarget) {
210         int prevoffset = -1;
211         int max = (params->w > params->h ? params->w : params->h);
212         int *prevmoves = snewn(max, int);
213
214         /*
215          * Shuffle the old-fashioned way, by making a series of
216          * single moves on the grid.
217          */
218
219         for (i = 0; i < n; i++)
220             tiles[i] = i;
221
222         for (i = 0; i < params->movetarget; i++) {
223             int start, offset, len, direction, index;
224             int j, tmp;
225
226             /*
227              * Choose a move to make. We can choose from any row
228              * or any column.
229              */
230             while (1) {
231                 j = random_upto(rs, params->w + params->h);
232
233                 if (j < params->w) {
234                     /* Column. */
235                     index = j;
236                     start = j;
237                     offset = params->w;
238                     len = params->h;
239                 } else {
240                     /* Row. */
241                     index = j - params->w;
242                     start = index * params->w;
243                     offset = 1;
244                     len = params->w;
245                 }
246
247                 direction = -1 + 2 * random_upto(rs, 2);
248
249                 /*
250                  * To at least _try_ to avoid boring cases, check
251                  * that this move doesn't directly undo a previous
252                  * one, or repeat it so many times as to turn it
253                  * into fewer moves in the opposite direction. (For
254                  * example, in a row of length 4, we're allowed to
255                  * move it the same way twice, but not three
256                  * times.)
257                  * 
258                  * We track this for each individual row/column,
259                  * and clear all the counters as soon as a
260                  * perpendicular move is made. This isn't perfect
261                  * (it _can't_ guaranteeably be perfect - there
262                  * will always come a move count beyond which a
263                  * shorter solution will be possible than the one
264                  * which constructed the position) but it should
265                  * sort out all the obvious cases.
266                  */
267                 if (offset == prevoffset) {
268                     tmp = prevmoves[index] + direction;
269                     if (abs(2*tmp) > len || abs(tmp) < abs(prevmoves[index]))
270                         continue;
271                 }
272
273                 /* If we didn't `continue', we've found an OK move to make. */
274                 if (offset != prevoffset) {
275                     int i;
276                     for (i = 0; i < max; i++)
277                         prevmoves[i] = 0;
278                     prevoffset = offset;
279                 }
280                 prevmoves[index] += direction;
281                 break;
282             }
283
284             /*
285              * Make the move.
286              */
287             if (direction < 0) {
288                 start += (len-1) * offset;
289                 offset = -offset;
290             }
291             tmp = tiles[start];
292             for (j = 0; j+1 < len; j++)
293                 tiles[start + j*offset] = tiles[start + (j+1)*offset];
294             tiles[start + (len-1) * offset] = tmp;
295         }
296
297         sfree(prevmoves);
298
299     } else {
300
301         used = snewn(n, int);
302
303         for (i = 0; i < n; i++) {
304             tiles[i] = -1;
305             used[i] = FALSE;
306         }
307
308         /*
309          * If both dimensions are odd, there is a parity
310          * constraint.
311          */
312         if (params->w & params->h & 1)
313             stop = 2;
314         else
315             stop = 0;
316
317         /*
318          * Place everything except (possibly) the last two tiles.
319          */
320         for (x = 0, i = n; i > stop; i--) {
321             int k = i > 1 ? random_upto(rs, i) : 0;
322             int j;
323
324             for (j = 0; j < n; j++)
325                 if (!used[j] && (k-- == 0))
326                     break;
327
328             assert(j < n && !used[j]);
329             used[j] = TRUE;
330
331             while (tiles[x] >= 0)
332                 x++;
333             assert(x < n);
334             tiles[x] = j;
335         }
336
337         if (stop) {
338             /*
339              * Find the last two locations, and the last two
340              * pieces.
341              */
342             while (tiles[x] >= 0)
343                 x++;
344             assert(x < n);
345             x1 = x;
346             x++;
347             while (tiles[x] >= 0)
348                 x++;
349             assert(x < n);
350             x2 = x;
351
352             for (i = 0; i < n; i++)
353                 if (!used[i])
354                     break;
355             p1 = i;
356             for (i = p1+1; i < n; i++)
357                 if (!used[i])
358                     break;
359             p2 = i;
360
361             /*
362              * Try the last two tiles one way round. If that fails,
363              * swap them.
364              */
365             tiles[x1] = p1;
366             tiles[x2] = p2;
367             if (perm_parity(tiles, n) != 0) {
368                 tiles[x1] = p2;
369                 tiles[x2] = p1;
370                 assert(perm_parity(tiles, n) == 0);
371             }
372         }
373
374         sfree(used);
375     }
376
377     /*
378      * Now construct the game description, by describing the tile
379      * array as a simple sequence of comma-separated integers.
380      */
381     ret = NULL;
382     retlen = 0;
383     for (i = 0; i < n; i++) {
384         char buf[80];
385         int k;
386
387         k = sprintf(buf, "%d,", tiles[i]+1);
388
389         ret = sresize(ret, retlen + k + 1, char);
390         strcpy(ret + retlen, buf);
391         retlen += k;
392     }
393     ret[retlen-1] = '\0';              /* delete last comma */
394
395     sfree(tiles);
396
397     return ret;
398 }
399
400
401 static char *validate_desc(game_params *params, char *desc)
402 {
403     char *p, *err;
404     int i, area;
405     int *used;
406
407     area = params->w * params->h;
408     p = desc;
409     err = NULL;
410
411     used = snewn(area, int);
412     for (i = 0; i < area; i++)
413         used[i] = FALSE;
414
415     for (i = 0; i < area; i++) {
416         char *q = p;
417         int n;
418
419         if (*p < '0' || *p > '9') {
420             err = "Not enough numbers in string";
421             goto leave;
422         }
423         while (*p >= '0' && *p <= '9')
424             p++;
425         if (i < area-1 && *p != ',') {
426             err = "Expected comma after number";
427             goto leave;
428         }
429         else if (i == area-1 && *p) {
430             err = "Excess junk at end of string";
431             goto leave;
432         }
433         n = atoi(q);
434         if (n < 1 || n > area) {
435             err = "Number out of range";
436             goto leave;
437         }
438         if (used[n-1]) {
439             err = "Number used twice";
440             goto leave;
441         }
442         used[n-1] = TRUE;
443
444         if (*p) p++;                   /* eat comma */
445     }
446
447     leave:
448     sfree(used);
449     return err;
450 }
451
452 static game_state *new_game(midend *me, game_params *params, char *desc)
453 {
454     game_state *state = snew(game_state);
455     int i;
456     char *p;
457
458     state->w = params->w;
459     state->h = params->h;
460     state->n = params->w * params->h;
461     state->tiles = snewn(state->n, int);
462
463     p = desc;
464     i = 0;
465     for (i = 0; i < state->n; i++) {
466         assert(*p);
467         state->tiles[i] = atoi(p);
468         while (*p && *p != ',')
469             p++;
470         if (*p) p++;                   /* eat comma */
471     }
472     assert(!*p);
473
474     state->completed = state->movecount = 0;
475     state->movetarget = params->movetarget;
476     state->used_solve = FALSE;
477     state->last_movement_sense = 0;
478
479     return state;
480 }
481
482 static game_state *dup_game(game_state *state)
483 {
484     game_state *ret = snew(game_state);
485
486     ret->w = state->w;
487     ret->h = state->h;
488     ret->n = state->n;
489     ret->tiles = snewn(state->w * state->h, int);
490     memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
491     ret->completed = state->completed;
492     ret->movecount = state->movecount;
493     ret->movetarget = state->movetarget;
494     ret->used_solve = state->used_solve;
495     ret->last_movement_sense = state->last_movement_sense;
496
497     return ret;
498 }
499
500 static void free_game(game_state *state)
501 {
502     sfree(state->tiles);
503     sfree(state);
504 }
505
506 static char *solve_game(game_state *state, game_state *currstate,
507                         char *aux, char **error)
508 {
509     return dupstr("S");
510 }
511
512 static int game_can_format_as_text_now(game_params *params)
513 {
514     return TRUE;
515 }
516
517 static char *game_text_format(game_state *state)
518 {
519     char *ret, *p, buf[80];
520     int x, y, col, maxlen;
521
522     /*
523      * First work out how many characters we need to display each
524      * number.
525      */
526     col = sprintf(buf, "%d", state->n);
527
528     /*
529      * Now we know the exact total size of the grid we're going to
530      * produce: it's got h rows, each containing w lots of col, w-1
531      * spaces and a trailing newline.
532      */
533     maxlen = state->h * state->w * (col+1);
534
535     ret = snewn(maxlen+1, char);
536     p = ret;
537
538     for (y = 0; y < state->h; y++) {
539         for (x = 0; x < state->w; x++) {
540             int v = state->tiles[state->w*y+x];
541             sprintf(buf, "%*d", col, v);
542             memcpy(p, buf, col);
543             p += col;
544             if (x+1 == state->w)
545                 *p++ = '\n';
546             else
547                 *p++ = ' ';
548         }
549     }
550
551     assert(p - ret == maxlen);
552     *p = '\0';
553     return ret;
554 }
555
556 static game_ui *new_ui(game_state *state)
557 {
558     return NULL;
559 }
560
561 static void free_ui(game_ui *ui)
562 {
563 }
564
565 static char *encode_ui(game_ui *ui)
566 {
567     return NULL;
568 }
569
570 static void decode_ui(game_ui *ui, char *encoding)
571 {
572 }
573
574 static void game_changed_state(game_ui *ui, game_state *oldstate,
575                                game_state *newstate)
576 {
577 }
578
579 struct game_drawstate {
580     int started;
581     int w, h, bgcolour;
582     int *tiles;
583     int tilesize;
584 };
585
586 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
587                             int x, int y, int button)
588 {
589     int cx, cy, dx, dy;
590     char buf[80];
591
592     button &= ~MOD_MASK;
593     if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
594         return NULL;
595
596     cx = FROMCOORD(x);
597     cy = FROMCOORD(y);
598     if (cx == -1 && cy >= 0 && cy < state->h)
599         dx = -1, dy = 0;
600     else if (cx == state->w && cy >= 0 && cy < state->h)
601         dx = +1, dy = 0;
602     else if (cy == -1 && cx >= 0 && cx < state->w)
603         dy = -1, dx = 0;
604     else if (cy == state->h && cx >= 0 && cx < state->w)
605         dy = +1, dx = 0;
606     else
607         return NULL;                   /* invalid click location */
608
609     /* reverse direction if right hand button is pressed */
610     if (button == RIGHT_BUTTON) {
611         dx = -dx;
612         dy = -dy;
613     }
614
615     if (dx)
616         sprintf(buf, "R%d,%d", cy, dx);
617     else
618         sprintf(buf, "C%d,%d", cx, dy);
619     return dupstr(buf);
620 }
621
622 static game_state *execute_move(game_state *from, char *move)
623 {
624     int cx, cy, dx, dy;
625     int tx, ty, n;
626     game_state *ret;
627
628     if (!strcmp(move, "S")) {
629         int i;
630
631         ret = dup_game(from);
632
633         /*
634          * Simply replace the grid with a solved one. For this game,
635          * this isn't a useful operation for actually telling the user
636          * what they should have done, but it is useful for
637          * conveniently being able to get hold of a clean state from
638          * which to practise manoeuvres.
639          */
640         for (i = 0; i < ret->n; i++)
641             ret->tiles[i] = i+1;
642         ret->used_solve = TRUE;
643         ret->completed = ret->movecount = 1;
644
645         return ret;
646     }
647
648     if (move[0] == 'R' && sscanf(move+1, "%d,%d", &cy, &dx) == 2 &&
649         cy >= 0 && cy < from->h) {
650         cx = dy = 0;
651         n = from->w;
652     } else if (move[0] == 'C' && sscanf(move+1, "%d,%d", &cx, &dy) == 2 &&
653                cx >= 0 && cx < from->w) {
654         cy = dx = 0;
655         n = from->h;
656     } else
657         return NULL;
658
659     ret = dup_game(from);
660
661     do {
662         tx = (cx - dx + from->w) % from->w;
663         ty = (cy - dy + from->h) % from->h;
664         ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
665         cx = tx;
666         cy = ty;
667     } while (--n > 0);
668
669     ret->movecount++;
670
671     ret->last_movement_sense = dx+dy;
672
673     /*
674      * See if the game has been completed.
675      */
676     if (!ret->completed) {
677         ret->completed = ret->movecount;
678         for (n = 0; n < ret->n; n++)
679             if (ret->tiles[n] != n+1)
680                 ret->completed = FALSE;
681     }
682
683     return ret;
684 }
685
686 /* ----------------------------------------------------------------------
687  * Drawing routines.
688  */
689
690 static void game_compute_size(game_params *params, int tilesize,
691                               int *x, int *y)
692 {
693     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
694     struct { int tilesize; } ads, *ds = &ads;
695     ads.tilesize = tilesize;
696
697     *x = TILE_SIZE * params->w + 2 * BORDER;
698     *y = TILE_SIZE * params->h + 2 * BORDER;
699 }
700
701 static void game_set_size(drawing *dr, game_drawstate *ds,
702                           game_params *params, int tilesize)
703 {
704     ds->tilesize = tilesize;
705 }
706
707 static float *game_colours(frontend *fe, int *ncolours)
708 {
709     float *ret = snewn(3 * NCOLOURS, float);
710     int i;
711
712     game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
713
714     for (i = 0; i < 3; i++)
715         ret[COL_TEXT * 3 + i] = 0.0;
716
717     *ncolours = NCOLOURS;
718     return ret;
719 }
720
721 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
722 {
723     struct game_drawstate *ds = snew(struct game_drawstate);
724     int i;
725
726     ds->started = FALSE;
727     ds->w = state->w;
728     ds->h = state->h;
729     ds->bgcolour = COL_BACKGROUND;
730     ds->tiles = snewn(ds->w*ds->h, int);
731     ds->tilesize = 0;                  /* haven't decided yet */
732     for (i = 0; i < ds->w*ds->h; i++)
733         ds->tiles[i] = -1;
734
735     return ds;
736 }
737
738 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
739 {
740     sfree(ds->tiles);
741     sfree(ds);
742 }
743
744 static void draw_tile(drawing *dr, game_drawstate *ds,
745                       game_state *state, int x, int y,
746                       int tile, int flash_colour)
747 {
748     if (tile == 0) {
749         draw_rect(dr, x, y, TILE_SIZE, TILE_SIZE,
750                   flash_colour);
751     } else {
752         int coords[6];
753         char str[40];
754
755         coords[0] = x + TILE_SIZE - 1;
756         coords[1] = y + TILE_SIZE - 1;
757         coords[2] = x + TILE_SIZE - 1;
758         coords[3] = y;
759         coords[4] = x;
760         coords[5] = y + TILE_SIZE - 1;
761         draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
762
763         coords[0] = x;
764         coords[1] = y;
765         draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
766
767         draw_rect(dr, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
768                   TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
769                   flash_colour);
770
771         sprintf(str, "%d", tile);
772         draw_text(dr, x + TILE_SIZE/2, y + TILE_SIZE/2,
773                   FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
774                   COL_TEXT, str);
775     }
776     draw_update(dr, x, y, TILE_SIZE, TILE_SIZE);
777 }
778
779 static void draw_arrow(drawing *dr, game_drawstate *ds,
780                        int x, int y, int xdx, int xdy)
781 {
782     int coords[14];
783     int ydy = -xdx, ydx = xdy;
784
785 #define POINT(n, xx, yy) ( \
786     coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
787     coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
788
789     POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4);   /* top of arrow */
790     POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2);   /* right corner */
791     POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2);   /* right concave */
792     POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4);   /* bottom right */
793     POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4);   /* bottom left */
794     POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2);   /* left concave */
795     POINT(6,     TILE_SIZE / 4, TILE_SIZE / 2);   /* left corner */
796
797     draw_polygon(dr, coords, 7, COL_LOWLIGHT, COL_TEXT);
798 }
799
800 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
801                  game_state *state, int dir, game_ui *ui,
802                  float animtime, float flashtime)
803 {
804     int i, bgcolour;
805
806     if (flashtime > 0) {
807         int frame = (int)(flashtime / FLASH_FRAME);
808         bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
809     } else
810         bgcolour = COL_BACKGROUND;
811
812     if (!ds->started) {
813         int coords[10];
814
815         draw_rect(dr, 0, 0,
816                   TILE_SIZE * state->w + 2 * BORDER,
817                   TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
818         draw_update(dr, 0, 0,
819                     TILE_SIZE * state->w + 2 * BORDER,
820                     TILE_SIZE * state->h + 2 * BORDER);
821
822         /*
823          * Recessed area containing the whole puzzle.
824          */
825         coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
826         coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
827         coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
828         coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
829         coords[4] = coords[2] - TILE_SIZE;
830         coords[5] = coords[3] + TILE_SIZE;
831         coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
832         coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
833         coords[6] = coords[8] + TILE_SIZE;
834         coords[7] = coords[9] - TILE_SIZE;
835         draw_polygon(dr, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
836
837         coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
838         coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
839         draw_polygon(dr, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
840
841         /*
842          * Arrows for making moves.
843          */
844         for (i = 0; i < state->w; i++) {
845             draw_arrow(dr, ds, COORD(i), COORD(0), +1, 0);
846             draw_arrow(dr, ds, COORD(i+1), COORD(state->h), -1, 0);
847         }
848         for (i = 0; i < state->h; i++) {
849             draw_arrow(dr, ds, COORD(state->w), COORD(i), 0, +1);
850             draw_arrow(dr, ds, COORD(0), COORD(i+1), 0, -1);
851         }
852
853         ds->started = TRUE;
854     }
855
856     /*
857      * Now draw each tile.
858      */
859
860     clip(dr, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
861
862     for (i = 0; i < state->n; i++) {
863         int t, t0;
864         /*
865          * Figure out what should be displayed at this
866          * location. It's either a simple tile, or it's a
867          * transition between two tiles (in which case we say
868          * -1 because it must always be drawn).
869          */
870
871         if (oldstate && oldstate->tiles[i] != state->tiles[i])
872             t = -1;
873         else
874             t = state->tiles[i];
875
876         t0 = t;
877
878         if (ds->bgcolour != bgcolour ||   /* always redraw when flashing */
879             ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
880             int x, y, x2, y2;
881
882             /*
883              * Figure out what to _actually_ draw, and where to
884              * draw it.
885              */
886             if (t == -1) {
887                 int x0, y0, x1, y1, dx, dy;
888                 int j;
889                 float c;
890                 int sense;
891
892                 if (dir < 0) {
893                     assert(oldstate);
894                     sense = -oldstate->last_movement_sense;
895                 } else {
896                     sense = state->last_movement_sense;
897                 }
898
899                 t = state->tiles[i];
900
901                 /*
902                  * FIXME: must be prepared to draw a double
903                  * tile in some situations.
904                  */
905
906                 /*
907                  * Find the coordinates of this tile in the old and
908                  * new states.
909                  */
910                 x1 = COORD(X(state, i));
911                 y1 = COORD(Y(state, i));
912                 for (j = 0; j < oldstate->n; j++)
913                     if (oldstate->tiles[j] == state->tiles[i])
914                         break;
915                 assert(j < oldstate->n);
916                 x0 = COORD(X(state, j));
917                 y0 = COORD(Y(state, j));
918
919                 dx = (x1 - x0);
920                 if (dx != 0 &&
921                     dx != TILE_SIZE * sense) {
922                     dx = (dx < 0 ? dx + TILE_SIZE * state->w :
923                           dx - TILE_SIZE * state->w);
924                     assert(abs(dx) == TILE_SIZE);
925                 }
926                 dy = (y1 - y0);
927                 if (dy != 0 &&
928                     dy != TILE_SIZE * sense) {
929                     dy = (dy < 0 ? dy + TILE_SIZE * state->h :
930                           dy - TILE_SIZE * state->h);
931                     assert(abs(dy) == TILE_SIZE);
932                 }
933
934                 c = (animtime / ANIM_TIME);
935                 if (c < 0.0F) c = 0.0F;
936                 if (c > 1.0F) c = 1.0F;
937
938                 x = x0 + (int)(c * dx);
939                 y = y0 + (int)(c * dy);
940                 x2 = x1 - dx + (int)(c * dx);
941                 y2 = y1 - dy + (int)(c * dy);
942             } else {
943                 x = COORD(X(state, i));
944                 y = COORD(Y(state, i));
945                 x2 = y2 = -1;
946             }
947
948             draw_tile(dr, ds, state, x, y, t, bgcolour);
949             if (x2 != -1 || y2 != -1)
950                 draw_tile(dr, ds, state, x2, y2, t, bgcolour);
951         }
952         ds->tiles[i] = t0;
953     }
954
955     unclip(dr);
956
957     ds->bgcolour = bgcolour;
958
959     /*
960      * Update the status bar.
961      */
962     {
963         char statusbuf[256];
964
965         /*
966          * Don't show the new status until we're also showing the
967          * new _state_ - after the game animation is complete.
968          */
969         if (oldstate)
970             state = oldstate;
971
972         if (state->used_solve)
973             sprintf(statusbuf, "Moves since auto-solve: %d",
974                     state->movecount - state->completed);
975         else {
976             sprintf(statusbuf, "%sMoves: %d",
977                     (state->completed ? "COMPLETED! " : ""),
978                     (state->completed ? state->completed : state->movecount));
979             if (state->movetarget)
980                 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
981                         state->movetarget);
982         }
983
984         status_bar(dr, statusbuf);
985     }
986 }
987
988 static float game_anim_length(game_state *oldstate,
989                               game_state *newstate, int dir, game_ui *ui)
990 {
991     return ANIM_TIME;
992 }
993
994 static float game_flash_length(game_state *oldstate,
995                                game_state *newstate, int dir, game_ui *ui)
996 {
997     if (!oldstate->completed && newstate->completed &&
998         !oldstate->used_solve && !newstate->used_solve)
999         return 2 * FLASH_FRAME;
1000     else
1001         return 0.0F;
1002 }
1003
1004 static int game_timing_state(game_state *state, game_ui *ui)
1005 {
1006     return TRUE;
1007 }
1008
1009 static void game_print_size(game_params *params, float *x, float *y)
1010 {
1011 }
1012
1013 static void game_print(drawing *dr, game_state *state, int tilesize)
1014 {
1015 }
1016
1017 #ifdef COMBINED
1018 #define thegame sixteen
1019 #endif
1020
1021 const struct game thegame = {
1022     "Sixteen", "games.sixteen", "sixteen",
1023     default_params,
1024     game_fetch_preset,
1025     decode_params,
1026     encode_params,
1027     free_params,
1028     dup_params,
1029     TRUE, game_configure, custom_params,
1030     validate_params,
1031     new_game_desc,
1032     validate_desc,
1033     new_game,
1034     dup_game,
1035     free_game,
1036     TRUE, solve_game,
1037     TRUE, game_can_format_as_text_now, game_text_format,
1038     new_ui,
1039     free_ui,
1040     encode_ui,
1041     decode_ui,
1042     game_changed_state,
1043     interpret_move,
1044     execute_move,
1045     PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
1046     game_colours,
1047     game_new_drawstate,
1048     game_free_drawstate,
1049     game_redraw,
1050     game_anim_length,
1051     game_flash_length,
1052     FALSE, FALSE, game_print_size, game_print,
1053     TRUE,                              /* wants_statusbar */
1054     FALSE, game_timing_state,
1055     0,                                 /* flags */
1056 };