Mouse-based interface for Cube: you left-click anywhere on the grid
[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 int movetarget;
40 };
41
42 struct game_state {
43 int w, h, n;
44 int *tiles;
45 int completed;
46 int just_used_solve; /* used to suppress undo animation */
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(*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)
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 game_aux_info **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 static void game_free_aux_info(game_aux_info *aux)
401 {
402 assert(!"Shouldn't happen");
403 }
404
405
406 static char *validate_desc(game_params *params, char *desc)
407 {
408 char *p, *err;
409 int i, area;
410 int *used;
411
412 area = params->w * params->h;
413 p = desc;
414 err = NULL;
415
416 used = snewn(area, int);
417 for (i = 0; i < area; i++)
418 used[i] = FALSE;
419
420 for (i = 0; i < area; i++) {
421 char *q = p;
422 int n;
423
424 if (*p < '0' || *p > '9') {
425 err = "Not enough numbers in string";
426 goto leave;
427 }
428 while (*p >= '0' && *p <= '9')
429 p++;
430 if (i < area-1 && *p != ',') {
431 err = "Expected comma after number";
432 goto leave;
433 }
434 else if (i == area-1 && *p) {
435 err = "Excess junk at end of string";
436 goto leave;
437 }
438 n = atoi(q);
439 if (n < 1 || n > area) {
440 err = "Number out of range";
441 goto leave;
442 }
443 if (used[n-1]) {
444 err = "Number used twice";
445 goto leave;
446 }
447 used[n-1] = TRUE;
448
449 if (*p) p++; /* eat comma */
450 }
451
452 leave:
453 sfree(used);
454 return err;
455 }
456
457 static game_state *new_game(midend_data *me, game_params *params, char *desc)
458 {
459 game_state *state = snew(game_state);
460 int i;
461 char *p;
462
463 state->w = params->w;
464 state->h = params->h;
465 state->n = params->w * params->h;
466 state->tiles = snewn(state->n, int);
467
468 p = desc;
469 i = 0;
470 for (i = 0; i < state->n; i++) {
471 assert(*p);
472 state->tiles[i] = atoi(p);
473 while (*p && *p != ',')
474 p++;
475 if (*p) p++; /* eat comma */
476 }
477 assert(!*p);
478
479 state->completed = state->movecount = 0;
480 state->movetarget = params->movetarget;
481 state->used_solve = state->just_used_solve = FALSE;
482 state->last_movement_sense = 0;
483
484 return state;
485 }
486
487 static game_state *dup_game(game_state *state)
488 {
489 game_state *ret = snew(game_state);
490
491 ret->w = state->w;
492 ret->h = state->h;
493 ret->n = state->n;
494 ret->tiles = snewn(state->w * state->h, int);
495 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
496 ret->completed = state->completed;
497 ret->movecount = state->movecount;
498 ret->movetarget = state->movetarget;
499 ret->used_solve = state->used_solve;
500 ret->just_used_solve = state->just_used_solve;
501 ret->last_movement_sense = state->last_movement_sense;
502
503 return ret;
504 }
505
506 static void free_game(game_state *state)
507 {
508 sfree(state);
509 }
510
511 static game_state *solve_game(game_state *state, game_aux_info *aux,
512 char **error)
513 {
514 game_state *ret = dup_game(state);
515 int i;
516
517 /*
518 * Simply replace the grid with a solved one. For this game,
519 * this isn't a useful operation for actually telling the user
520 * what they should have done, but it is useful for
521 * conveniently being able to get hold of a clean state from
522 * which to practise manoeuvres.
523 */
524 for (i = 0; i < ret->n; i++)
525 ret->tiles[i] = i+1;
526 ret->used_solve = ret->just_used_solve = TRUE;
527 ret->completed = ret->movecount = 1;
528
529 return ret;
530 }
531
532 static char *game_text_format(game_state *state)
533 {
534 char *ret, *p, buf[80];
535 int x, y, col, maxlen;
536
537 /*
538 * First work out how many characters we need to display each
539 * number.
540 */
541 col = sprintf(buf, "%d", state->n);
542
543 /*
544 * Now we know the exact total size of the grid we're going to
545 * produce: it's got h rows, each containing w lots of col, w-1
546 * spaces and a trailing newline.
547 */
548 maxlen = state->h * state->w * (col+1);
549
550 ret = snewn(maxlen+1, char);
551 p = ret;
552
553 for (y = 0; y < state->h; y++) {
554 for (x = 0; x < state->w; x++) {
555 int v = state->tiles[state->w*y+x];
556 sprintf(buf, "%*d", col, v);
557 memcpy(p, buf, col);
558 p += col;
559 if (x+1 == state->w)
560 *p++ = '\n';
561 else
562 *p++ = ' ';
563 }
564 }
565
566 assert(p - ret == maxlen);
567 *p = '\0';
568 return ret;
569 }
570
571 static game_ui *new_ui(game_state *state)
572 {
573 return NULL;
574 }
575
576 static void free_ui(game_ui *ui)
577 {
578 }
579
580 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
581 int x, int y, int button) {
582 int cx, cy;
583 int dx, dy, tx, ty, n;
584 game_state *ret;
585
586 button &= ~MOD_MASK;
587 if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
588 return NULL;
589
590 cx = FROMCOORD(x);
591 cy = FROMCOORD(y);
592 if (cx == -1 && cy >= 0 && cy < from->h)
593 n = from->w, dx = +1, dy = 0;
594 else if (cx == from->w && cy >= 0 && cy < from->h)
595 n = from->w, dx = -1, dy = 0;
596 else if (cy == -1 && cx >= 0 && cx < from->w)
597 n = from->h, dy = +1, dx = 0;
598 else if (cy == from->h && cx >= 0 && cx < from->w)
599 n = from->h, dy = -1, dx = 0;
600 else
601 return NULL; /* invalid click location */
602
603 /* reverse direction if right hand button is pressed */
604 if (button == RIGHT_BUTTON)
605 {
606 dx = -dx; if (dx) cx = from->w - 1 - cx;
607 dy = -dy; if (dy) cy = from->h - 1 - cy;
608 }
609
610 ret = dup_game(from);
611 ret->just_used_solve = FALSE; /* zero this in a hurry */
612
613 do {
614 cx += dx;
615 cy += dy;
616 tx = (cx + dx + from->w) % from->w;
617 ty = (cy + dy + from->h) % from->h;
618 ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
619 } while (--n > 0);
620
621 ret->movecount++;
622
623 ret->last_movement_sense = -(dx+dy);
624
625 /*
626 * See if the game has been completed.
627 */
628 if (!ret->completed) {
629 ret->completed = ret->movecount;
630 for (n = 0; n < ret->n; n++)
631 if (ret->tiles[n] != n+1)
632 ret->completed = FALSE;
633 }
634
635 return ret;
636 }
637
638 /* ----------------------------------------------------------------------
639 * Drawing routines.
640 */
641
642 struct game_drawstate {
643 int started;
644 int w, h, bgcolour;
645 int *tiles;
646 };
647
648 static void game_size(game_params *params, int *x, int *y)
649 {
650 *x = TILE_SIZE * params->w + 2 * BORDER;
651 *y = TILE_SIZE * params->h + 2 * BORDER;
652 }
653
654 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
655 {
656 float *ret = snewn(3 * NCOLOURS, float);
657 int i;
658 float max;
659
660 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
661
662 /*
663 * Drop the background colour so that the highlight is
664 * noticeably brighter than it while still being under 1.
665 */
666 max = ret[COL_BACKGROUND*3];
667 for (i = 1; i < 3; i++)
668 if (ret[COL_BACKGROUND*3+i] > max)
669 max = ret[COL_BACKGROUND*3+i];
670 if (max * 1.2F > 1.0F) {
671 for (i = 0; i < 3; i++)
672 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
673 }
674
675 for (i = 0; i < 3; i++) {
676 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
677 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
678 ret[COL_TEXT * 3 + i] = 0.0;
679 }
680
681 *ncolours = NCOLOURS;
682 return ret;
683 }
684
685 static game_drawstate *game_new_drawstate(game_state *state)
686 {
687 struct game_drawstate *ds = snew(struct game_drawstate);
688 int i;
689
690 ds->started = FALSE;
691 ds->w = state->w;
692 ds->h = state->h;
693 ds->bgcolour = COL_BACKGROUND;
694 ds->tiles = snewn(ds->w*ds->h, int);
695 for (i = 0; i < ds->w*ds->h; i++)
696 ds->tiles[i] = -1;
697
698 return ds;
699 }
700
701 static void game_free_drawstate(game_drawstate *ds)
702 {
703 sfree(ds->tiles);
704 sfree(ds);
705 }
706
707 static void draw_tile(frontend *fe, game_state *state, int x, int y,
708 int tile, int flash_colour)
709 {
710 if (tile == 0) {
711 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
712 flash_colour);
713 } else {
714 int coords[6];
715 char str[40];
716
717 coords[0] = x + TILE_SIZE - 1;
718 coords[1] = y + TILE_SIZE - 1;
719 coords[2] = x + TILE_SIZE - 1;
720 coords[3] = y;
721 coords[4] = x;
722 coords[5] = y + TILE_SIZE - 1;
723 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
724 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
725
726 coords[0] = x;
727 coords[1] = y;
728 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
729 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
730
731 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
732 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
733 flash_colour);
734
735 sprintf(str, "%d", tile);
736 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
737 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
738 COL_TEXT, str);
739 }
740 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
741 }
742
743 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
744 {
745 int coords[14];
746 int ydy = -xdx, ydx = xdy;
747
748 #define POINT(n, xx, yy) ( \
749 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
750 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
751
752 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
753 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
754 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
755 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
756 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
757 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
758 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
759
760 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
761 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
762 }
763
764 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
765 game_state *state, int dir, game_ui *ui,
766 float animtime, float flashtime)
767 {
768 int i, bgcolour;
769
770 if (flashtime > 0) {
771 int frame = (int)(flashtime / FLASH_FRAME);
772 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
773 } else
774 bgcolour = COL_BACKGROUND;
775
776 if (!ds->started) {
777 int coords[6];
778
779 draw_rect(fe, 0, 0,
780 TILE_SIZE * state->w + 2 * BORDER,
781 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
782 draw_update(fe, 0, 0,
783 TILE_SIZE * state->w + 2 * BORDER,
784 TILE_SIZE * state->h + 2 * BORDER);
785
786 /*
787 * Recessed area containing the whole puzzle.
788 */
789 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
790 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
791 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
792 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
793 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
794 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
795 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
796 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
797
798 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
799 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
800 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
801 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
802
803 /*
804 * Arrows for making moves.
805 */
806 for (i = 0; i < state->w; i++) {
807 draw_arrow(fe, COORD(i), COORD(0), +1, 0);
808 draw_arrow(fe, COORD(i+1), COORD(state->h), -1, 0);
809 }
810 for (i = 0; i < state->h; i++) {
811 draw_arrow(fe, COORD(state->w), COORD(i), 0, +1);
812 draw_arrow(fe, COORD(0), COORD(i+1), 0, -1);
813 }
814
815 ds->started = TRUE;
816 }
817
818 /*
819 * Now draw each tile.
820 */
821
822 clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
823
824 for (i = 0; i < state->n; i++) {
825 int t, t0;
826 /*
827 * Figure out what should be displayed at this
828 * location. It's either a simple tile, or it's a
829 * transition between two tiles (in which case we say
830 * -1 because it must always be drawn).
831 */
832
833 if (oldstate && oldstate->tiles[i] != state->tiles[i])
834 t = -1;
835 else
836 t = state->tiles[i];
837
838 t0 = t;
839
840 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
841 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
842 int x, y, x2, y2;
843
844 /*
845 * Figure out what to _actually_ draw, and where to
846 * draw it.
847 */
848 if (t == -1) {
849 int x0, y0, x1, y1, dx, dy;
850 int j;
851 float c;
852 int sense;
853
854 if (dir < 0) {
855 assert(oldstate);
856 sense = -oldstate->last_movement_sense;
857 } else {
858 sense = state->last_movement_sense;
859 }
860
861 t = state->tiles[i];
862
863 /*
864 * FIXME: must be prepared to draw a double
865 * tile in some situations.
866 */
867
868 /*
869 * Find the coordinates of this tile in the old and
870 * new states.
871 */
872 x1 = COORD(X(state, i));
873 y1 = COORD(Y(state, i));
874 for (j = 0; j < oldstate->n; j++)
875 if (oldstate->tiles[j] == state->tiles[i])
876 break;
877 assert(j < oldstate->n);
878 x0 = COORD(X(state, j));
879 y0 = COORD(Y(state, j));
880
881 dx = (x1 - x0);
882 if (dx != 0 &&
883 dx != TILE_SIZE * sense) {
884 dx = (dx < 0 ? dx + TILE_SIZE * state->w :
885 dx - TILE_SIZE * state->w);
886 assert(abs(dx) == TILE_SIZE);
887 }
888 dy = (y1 - y0);
889 if (dy != 0 &&
890 dy != TILE_SIZE * sense) {
891 dy = (dy < 0 ? dy + TILE_SIZE * state->h :
892 dy - TILE_SIZE * state->h);
893 assert(abs(dy) == TILE_SIZE);
894 }
895
896 c = (animtime / ANIM_TIME);
897 if (c < 0.0F) c = 0.0F;
898 if (c > 1.0F) c = 1.0F;
899
900 x = x0 + (int)(c * dx);
901 y = y0 + (int)(c * dy);
902 x2 = x1 - dx + (int)(c * dx);
903 y2 = y1 - dy + (int)(c * dy);
904 } else {
905 x = COORD(X(state, i));
906 y = COORD(Y(state, i));
907 x2 = y2 = -1;
908 }
909
910 draw_tile(fe, state, x, y, t, bgcolour);
911 if (x2 != -1 || y2 != -1)
912 draw_tile(fe, state, x2, y2, t, bgcolour);
913 }
914 ds->tiles[i] = t0;
915 }
916
917 unclip(fe);
918
919 ds->bgcolour = bgcolour;
920
921 /*
922 * Update the status bar.
923 */
924 {
925 char statusbuf[256];
926
927 /*
928 * Don't show the new status until we're also showing the
929 * new _state_ - after the game animation is complete.
930 */
931 if (oldstate)
932 state = oldstate;
933
934 if (state->used_solve)
935 sprintf(statusbuf, "Moves since auto-solve: %d",
936 state->movecount - state->completed);
937 else {
938 sprintf(statusbuf, "%sMoves: %d",
939 (state->completed ? "COMPLETED! " : ""),
940 (state->completed ? state->completed : state->movecount));
941 if (state->movetarget)
942 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
943 state->movetarget);
944 }
945
946 status_bar(fe, statusbuf);
947 }
948 }
949
950 static float game_anim_length(game_state *oldstate,
951 game_state *newstate, int dir, game_ui *ui)
952 {
953 if ((dir > 0 && newstate->just_used_solve) ||
954 (dir < 0 && oldstate->just_used_solve))
955 return 0.0F;
956 else
957 return ANIM_TIME;
958 }
959
960 static float game_flash_length(game_state *oldstate,
961 game_state *newstate, int dir, game_ui *ui)
962 {
963 if (!oldstate->completed && newstate->completed &&
964 !oldstate->used_solve && !newstate->used_solve)
965 return 2 * FLASH_FRAME;
966 else
967 return 0.0F;
968 }
969
970 static int game_wants_statusbar(void)
971 {
972 return TRUE;
973 }
974
975 static int game_timing_state(game_state *state)
976 {
977 return TRUE;
978 }
979
980 #ifdef COMBINED
981 #define thegame sixteen
982 #endif
983
984 const struct game thegame = {
985 "Sixteen", "games.sixteen",
986 default_params,
987 game_fetch_preset,
988 decode_params,
989 encode_params,
990 free_params,
991 dup_params,
992 TRUE, game_configure, custom_params,
993 validate_params,
994 new_game_desc,
995 game_free_aux_info,
996 validate_desc,
997 new_game,
998 dup_game,
999 free_game,
1000 TRUE, solve_game,
1001 TRUE, game_text_format,
1002 new_ui,
1003 free_ui,
1004 make_move,
1005 game_size,
1006 game_colours,
1007 game_new_drawstate,
1008 game_free_drawstate,
1009 game_redraw,
1010 game_anim_length,
1011 game_flash_length,
1012 game_wants_statusbar,
1013 FALSE, game_timing_state,
1014 };