Tweak a conditional expression in pearl.c to work around a display bug
[sgt/puzzles] / fifteen.c
CommitLineData
4efb3868 1/*
2 * fifteen.c: standard 15-puzzle.
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <assert.h>
b0e26073 9#include <ctype.h>
4efb3868 10#include <math.h>
11
12#include "puzzles.h"
13
1e3e152d 14#define PREFERRED_TILE_SIZE 48
15#define TILE_SIZE (ds->tilesize)
4efb3868 16#define BORDER (TILE_SIZE / 2)
17#define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
18#define COORD(x) ( (x) * TILE_SIZE + BORDER )
19#define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
20
8c1fd974 21#define ANIM_TIME 0.13F
22#define FLASH_FRAME 0.13F
4efb3868 23
24#define X(state, i) ( (i) % (state)->w )
25#define Y(state, i) ( (i) / (state)->w )
26#define C(state, x, y) ( (y) * (state)->w + (x) )
27
28enum {
29 COL_BACKGROUND,
30 COL_TEXT,
31 COL_HIGHLIGHT,
32 COL_LOWLIGHT,
33 NCOLOURS
34};
35
36struct game_params {
37 int w, h;
38};
39
40struct game_state {
41 int w, h, n;
42 int *tiles;
43 int gap_pos;
44 int completed;
2ac6d24e 45 int used_solve; /* used to suppress completion flash */
fd1a1a2b 46 int movecount;
4efb3868 47};
48
be8d5aa1 49static game_params *default_params(void)
4efb3868 50{
51 game_params *ret = snew(game_params);
52
53 ret->w = ret->h = 4;
54
55 return ret;
56}
57
be8d5aa1 58static int game_fetch_preset(int i, char **name, game_params **params)
4efb3868 59{
bc578afe 60 if (i == 0) {
61 *params = default_params();
62 *name = dupstr("4x4");
63 return TRUE;
64 }
4efb3868 65 return FALSE;
66}
67
be8d5aa1 68static void free_params(game_params *params)
4efb3868 69{
70 sfree(params);
71}
72
be8d5aa1 73static game_params *dup_params(game_params *params)
4efb3868 74{
75 game_params *ret = snew(game_params);
76 *ret = *params; /* structure copy */
77 return ret;
78}
79
1185e3c5 80static void decode_params(game_params *ret, char const *string)
b0e26073 81{
b0e26073 82 ret->w = ret->h = atoi(string);
89167dad 83 while (*string && isdigit((unsigned char)*string)) string++;
b0e26073 84 if (*string == 'x') {
85 string++;
86 ret->h = atoi(string);
87 }
b0e26073 88}
89
1185e3c5 90static char *encode_params(game_params *params, int full)
b0e26073 91{
92 char data[256];
93
94 sprintf(data, "%dx%d", params->w, params->h);
95
96 return dupstr(data);
97}
98
be8d5aa1 99static config_item *game_configure(game_params *params)
c8230524 100{
101 config_item *ret;
102 char buf[80];
103
104 ret = snewn(3, config_item);
105
106 ret[0].name = "Width";
95709966 107 ret[0].type = C_STRING;
c8230524 108 sprintf(buf, "%d", params->w);
109 ret[0].sval = dupstr(buf);
110 ret[0].ival = 0;
111
112 ret[1].name = "Height";
95709966 113 ret[1].type = C_STRING;
c8230524 114 sprintf(buf, "%d", params->h);
115 ret[1].sval = dupstr(buf);
116 ret[1].ival = 0;
117
118 ret[2].name = NULL;
95709966 119 ret[2].type = C_END;
c8230524 120 ret[2].sval = NULL;
121 ret[2].ival = 0;
122
123 return ret;
124}
125
be8d5aa1 126static game_params *custom_params(config_item *cfg)
c8230524 127{
128 game_params *ret = snew(game_params);
129
130 ret->w = atoi(cfg[0].sval);
131 ret->h = atoi(cfg[1].sval);
132
133 return ret;
134}
135
3ff276f2 136static char *validate_params(game_params *params, int full)
c8230524 137{
ab53eb64 138 if (params->w < 2 || params->h < 2)
c8230524 139 return "Width and height must both be at least two";
140
141 return NULL;
142}
143
be8d5aa1 144static int perm_parity(int *perm, int n)
4efb3868 145{
146 int i, j, ret;
147
148 ret = 0;
149
150 for (i = 0; i < n-1; i++)
151 for (j = i+1; j < n; j++)
152 if (perm[i] > perm[j])
153 ret = !ret;
154
155 return ret;
156}
157
1185e3c5 158static char *new_game_desc(game_params *params, random_state *rs,
c566778e 159 char **aux, int interactive)
4efb3868 160{
161 int gap, n, i, x;
162 int x1, x2, p1, p2, parity;
163 int *tiles, *used;
164 char *ret;
165 int retlen;
166
167 n = params->w * params->h;
168
169 tiles = snewn(n, int);
170 used = snewn(n, int);
171
172 for (i = 0; i < n; i++) {
173 tiles[i] = -1;
174 used[i] = FALSE;
175 }
176
48d70ca9 177 gap = random_upto(rs, n);
4efb3868 178 tiles[gap] = 0;
179 used[0] = TRUE;
180
181 /*
182 * Place everything else except the last two tiles.
183 */
184 for (x = 0, i = n-1; i > 2; i--) {
48d70ca9 185 int k = random_upto(rs, i);
4efb3868 186 int j;
187
188 for (j = 0; j < n; j++)
189 if (!used[j] && (k-- == 0))
190 break;
191
192 assert(j < n && !used[j]);
193 used[j] = TRUE;
194
195 while (tiles[x] >= 0)
196 x++;
197 assert(x < n);
198 tiles[x] = j;
199 }
200
201 /*
202 * Find the last two locations, and the last two pieces.
203 */
204 while (tiles[x] >= 0)
205 x++;
206 assert(x < n);
207 x1 = x;
208 x++;
209 while (tiles[x] >= 0)
210 x++;
211 assert(x < n);
212 x2 = x;
213
214 for (i = 0; i < n; i++)
215 if (!used[i])
216 break;
217 p1 = i;
218 for (i = p1+1; i < n; i++)
219 if (!used[i])
220 break;
221 p2 = i;
222
223 /*
224 * Determine the required parity of the overall permutation.
225 * This is the XOR of:
226 *
d3a026ed 227 * - The chessboard parity ((x^y)&1) of the gap square. The
228 * bottom right counts as even.
4efb3868 229 *
230 * - The parity of n. (The target permutation is 1,...,n-1,0
231 * rather than 0,...,n-1; this is a cyclic permutation of
232 * the starting point and hence is odd iff n is even.)
233 */
d3a026ed 234 parity = ((X(params, gap) - (params->w-1)) ^
235 (Y(params, gap) - (params->h-1)) ^
236 (n+1)) & 1;
4efb3868 237
238 /*
239 * Try the last two tiles one way round. If that fails, swap
240 * them.
241 */
242 tiles[x1] = p1;
243 tiles[x2] = p2;
244 if (perm_parity(tiles, n) != parity) {
245 tiles[x1] = p2;
246 tiles[x2] = p1;
247 assert(perm_parity(tiles, n) == parity);
248 }
249
250 /*
1185e3c5 251 * Now construct the game description, by describing the tile
252 * array as a simple sequence of comma-separated integers.
4efb3868 253 */
254 ret = NULL;
255 retlen = 0;
256 for (i = 0; i < n; i++) {
257 char buf[80];
258 int k;
259
260 k = sprintf(buf, "%d,", tiles[i]);
261
262 ret = sresize(ret, retlen + k + 1, char);
263 strcpy(ret + retlen, buf);
264 retlen += k;
265 }
266 ret[retlen-1] = '\0'; /* delete last comma */
267
268 sfree(tiles);
269 sfree(used);
270
271 return ret;
272}
273
1185e3c5 274static char *validate_desc(game_params *params, char *desc)
5928817c 275{
276 char *p, *err;
277 int i, area;
278 int *used;
279
280 area = params->w * params->h;
1185e3c5 281 p = desc;
5928817c 282 err = NULL;
283
284 used = snewn(area, int);
285 for (i = 0; i < area; i++)
286 used[i] = FALSE;
287
288 for (i = 0; i < area; i++) {
289 char *q = p;
290 int n;
291
292 if (*p < '0' || *p > '9') {
293 err = "Not enough numbers in string";
294 goto leave;
295 }
296 while (*p >= '0' && *p <= '9')
297 p++;
298 if (i < area-1 && *p != ',') {
299 err = "Expected comma after number";
300 goto leave;
301 }
302 else if (i == area-1 && *p) {
303 err = "Excess junk at end of string";
304 goto leave;
305 }
306 n = atoi(q);
307 if (n < 0 || n >= area) {
308 err = "Number out of range";
309 goto leave;
310 }
311 if (used[n]) {
312 err = "Number used twice";
313 goto leave;
314 }
315 used[n] = TRUE;
316
317 if (*p) p++; /* eat comma */
318 }
319
320 leave:
321 sfree(used);
322 return err;
323}
324
dafd6cf6 325static game_state *new_game(midend *me, game_params *params, char *desc)
4efb3868 326{
327 game_state *state = snew(game_state);
328 int i;
329 char *p;
330
331 state->w = params->w;
332 state->h = params->h;
333 state->n = params->w * params->h;
334 state->tiles = snewn(state->n, int);
335
336 state->gap_pos = 0;
1185e3c5 337 p = desc;
4efb3868 338 i = 0;
339 for (i = 0; i < state->n; i++) {
340 assert(*p);
341 state->tiles[i] = atoi(p);
342 if (state->tiles[i] == 0)
343 state->gap_pos = i;
344 while (*p && *p != ',')
345 p++;
346 if (*p) p++; /* eat comma */
347 }
348 assert(!*p);
349 assert(state->tiles[state->gap_pos] == 0);
350
fd1a1a2b 351 state->completed = state->movecount = 0;
a440f184 352 state->used_solve = FALSE;
4efb3868 353
354 return state;
355}
356
be8d5aa1 357static game_state *dup_game(game_state *state)
4efb3868 358{
359 game_state *ret = snew(game_state);
360
361 ret->w = state->w;
362 ret->h = state->h;
363 ret->n = state->n;
364 ret->tiles = snewn(state->w * state->h, int);
365 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
366 ret->gap_pos = state->gap_pos;
367 ret->completed = state->completed;
fd1a1a2b 368 ret->movecount = state->movecount;
2ac6d24e 369 ret->used_solve = state->used_solve;
4efb3868 370
371 return ret;
372}
373
be8d5aa1 374static void free_game(game_state *state)
4efb3868 375{
ab53eb64 376 sfree(state->tiles);
4efb3868 377 sfree(state);
378}
379
df11cd4e 380static char *solve_game(game_state *state, game_state *currstate,
c566778e 381 char *aux, char **error)
2ac6d24e 382{
df11cd4e 383 return dupstr("S");
2ac6d24e 384}
385
fa3abef5 386static int game_can_format_as_text_now(game_params *params)
387{
388 return TRUE;
389}
390
9b4b03d3 391static char *game_text_format(game_state *state)
392{
af52394e 393 char *ret, *p, buf[80];
394 int x, y, col, maxlen;
395
396 /*
397 * First work out how many characters we need to display each
398 * number.
399 */
400 col = sprintf(buf, "%d", state->n-1);
401
402 /*
403 * Now we know the exact total size of the grid we're going to
404 * produce: it's got h rows, each containing w lots of col, w-1
405 * spaces and a trailing newline.
406 */
407 maxlen = state->h * state->w * (col+1);
408
48a10826 409 ret = snewn(maxlen+1, char);
af52394e 410 p = ret;
411
412 for (y = 0; y < state->h; y++) {
413 for (x = 0; x < state->w; x++) {
414 int v = state->tiles[state->w*y+x];
415 if (v == 0)
416 sprintf(buf, "%*s", col, "");
417 else
418 sprintf(buf, "%*d", col, v);
419 memcpy(p, buf, col);
420 p += col;
421 if (x+1 == state->w)
422 *p++ = '\n';
423 else
424 *p++ = ' ';
425 }
426 }
427
428 assert(p - ret == maxlen);
429 *p = '\0';
430 return ret;
9b4b03d3 431}
432
be8d5aa1 433static game_ui *new_ui(game_state *state)
74a4e547 434{
435 return NULL;
436}
437
be8d5aa1 438static void free_ui(game_ui *ui)
74a4e547 439{
440}
441
844f605f 442static char *encode_ui(game_ui *ui)
ae8290c6 443{
444 return NULL;
445}
446
844f605f 447static void decode_ui(game_ui *ui, char *encoding)
ae8290c6 448{
449}
450
07dfb697 451static void game_changed_state(game_ui *ui, game_state *oldstate,
452 game_state *newstate)
453{
454}
455
1e3e152d 456struct game_drawstate {
457 int started;
458 int w, h, bgcolour;
459 int *tiles;
460 int tilesize;
461};
462
df11cd4e 463static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
464 int x, int y, int button)
465{
466 int gx, gy, dx, dy;
467 char buf[80];
4efb3868 468
f0ee053c 469 button &= ~MOD_MASK;
470
df11cd4e 471 gx = X(state, state->gap_pos);
472 gy = Y(state, state->gap_pos);
4efb3868 473
474 if (button == CURSOR_RIGHT && gx > 0)
475 dx = gx - 1, dy = gy;
df11cd4e 476 else if (button == CURSOR_LEFT && gx < state->w-1)
4efb3868 477 dx = gx + 1, dy = gy;
478 else if (button == CURSOR_DOWN && gy > 0)
479 dy = gy - 1, dx = gx;
df11cd4e 480 else if (button == CURSOR_UP && gy < state->h-1)
4efb3868 481 dy = gy + 1, dx = gx;
482 else if (button == LEFT_BUTTON) {
483 dx = FROMCOORD(x);
484 dy = FROMCOORD(y);
df11cd4e 485 if (dx < 0 || dx >= state->w || dy < 0 || dy >= state->h)
4efb3868 486 return NULL; /* out of bounds */
487 /*
488 * Any click location should be equal to the gap location
489 * in _precisely_ one coordinate.
490 */
491 if ((dx == gx && dy == gy) || (dx != gx && dy != gy))
492 return NULL;
493 } else
494 return NULL; /* no move */
495
df11cd4e 496 sprintf(buf, "M%d,%d", dx, dy);
497 return dupstr(buf);
498}
499
500static game_state *execute_move(game_state *from, char *move)
501{
502 int gx, gy, dx, dy, ux, uy, up, p;
503 game_state *ret;
504
505 if (!strcmp(move, "S")) {
506 int i;
507
508 ret = dup_game(from);
509
510 /*
511 * Simply replace the grid with a solved one. For this game,
512 * this isn't a useful operation for actually telling the user
513 * what they should have done, but it is useful for
514 * conveniently being able to get hold of a clean state from
515 * which to practise manoeuvres.
516 */
517 for (i = 0; i < ret->n; i++)
518 ret->tiles[i] = (i+1) % ret->n;
519 ret->gap_pos = ret->n-1;
a440f184 520 ret->used_solve = TRUE;
df11cd4e 521 ret->completed = ret->movecount = 1;
522
523 return ret;
524 }
525
526 gx = X(from, from->gap_pos);
527 gy = Y(from, from->gap_pos);
528
529 if (move[0] != 'M' ||
530 sscanf(move+1, "%d,%d", &dx, &dy) != 2 ||
531 (dx == gx && dy == gy) || (dx != gx && dy != gy) ||
532 dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
533 return NULL;
534
4efb3868 535 /*
536 * Find the unit displacement from the original gap
537 * position towards this one.
538 */
539 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
540 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
541 up = C(from, ux, uy);
542
543 ret = dup_game(from);
544
545 ret->gap_pos = C(from, dx, dy);
546 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
547
548 ret->tiles[ret->gap_pos] = 0;
549
550 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
551 assert(p >= 0 && p < from->n);
552 ret->tiles[p] = from->tiles[p + up];
fd1a1a2b 553 ret->movecount++;
4efb3868 554 }
555
556 /*
557 * See if the game has been completed.
558 */
559 if (!ret->completed) {
fd1a1a2b 560 ret->completed = ret->movecount;
4efb3868 561 for (p = 0; p < ret->n; p++)
562 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
fd1a1a2b 563 ret->completed = 0;
4efb3868 564 }
565
566 return ret;
567}
568
569/* ----------------------------------------------------------------------
570 * Drawing routines.
571 */
572
1f3ee4ee 573static void game_compute_size(game_params *params, int tilesize,
574 int *x, int *y)
4efb3868 575{
1f3ee4ee 576 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
577 struct { int tilesize; } ads, *ds = &ads;
578 ads.tilesize = tilesize;
1e3e152d 579
4efb3868 580 *x = TILE_SIZE * params->w + 2 * BORDER;
581 *y = TILE_SIZE * params->h + 2 * BORDER;
582}
583
dafd6cf6 584static void game_set_size(drawing *dr, game_drawstate *ds,
585 game_params *params, int tilesize)
1f3ee4ee 586{
587 ds->tilesize = tilesize;
588}
589
8266f3fc 590static float *game_colours(frontend *fe, int *ncolours)
4efb3868 591{
592 float *ret = snewn(3 * NCOLOURS, float);
593 int i;
4efb3868 594
937a9eff 595 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
4efb3868 596
937a9eff 597 for (i = 0; i < 3; i++)
4efb3868 598 ret[COL_TEXT * 3 + i] = 0.0;
4efb3868 599
600 *ncolours = NCOLOURS;
601 return ret;
602}
603
dafd6cf6 604static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
4efb3868 605{
606 struct game_drawstate *ds = snew(struct game_drawstate);
607 int i;
608
609 ds->started = FALSE;
610 ds->w = state->w;
611 ds->h = state->h;
612 ds->bgcolour = COL_BACKGROUND;
613 ds->tiles = snewn(ds->w*ds->h, int);
1e3e152d 614 ds->tilesize = 0; /* haven't decided yet */
4efb3868 615 for (i = 0; i < ds->w*ds->h; i++)
616 ds->tiles[i] = -1;
617
618 return ds;
619}
620
dafd6cf6 621static void game_free_drawstate(drawing *dr, game_drawstate *ds)
4efb3868 622{
623 sfree(ds->tiles);
624 sfree(ds);
625}
626
dafd6cf6 627static void draw_tile(drawing *dr, game_drawstate *ds, game_state *state,
1e3e152d 628 int x, int y, int tile, int flash_colour)
4efb3868 629{
630 if (tile == 0) {
dafd6cf6 631 draw_rect(dr, x, y, TILE_SIZE, TILE_SIZE,
4efb3868 632 flash_colour);
633 } else {
634 int coords[6];
635 char str[40];
636
637 coords[0] = x + TILE_SIZE - 1;
638 coords[1] = y + TILE_SIZE - 1;
639 coords[2] = x + TILE_SIZE - 1;
640 coords[3] = y;
641 coords[4] = x;
642 coords[5] = y + TILE_SIZE - 1;
dafd6cf6 643 draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
4efb3868 644
645 coords[0] = x;
646 coords[1] = y;
dafd6cf6 647 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
4efb3868 648
dafd6cf6 649 draw_rect(dr, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
4efb3868 650 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
651 flash_colour);
652
653 sprintf(str, "%d", tile);
dafd6cf6 654 draw_text(dr, x + TILE_SIZE/2, y + TILE_SIZE/2,
4efb3868 655 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
656 COL_TEXT, str);
657 }
dafd6cf6 658 draw_update(dr, x, y, TILE_SIZE, TILE_SIZE);
4efb3868 659}
660
dafd6cf6 661static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
c822de4a 662 game_state *state, int dir, game_ui *ui,
74a4e547 663 float animtime, float flashtime)
4efb3868 664{
665 int i, pass, bgcolour;
666
667 if (flashtime > 0) {
668 int frame = (int)(flashtime / FLASH_FRAME);
669 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
670 } else
671 bgcolour = COL_BACKGROUND;
672
673 if (!ds->started) {
19f24306 674 int coords[10];
4efb3868 675
dafd6cf6 676 draw_rect(dr, 0, 0,
4efb3868 677 TILE_SIZE * state->w + 2 * BORDER,
678 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
dafd6cf6 679 draw_update(dr, 0, 0,
4efb3868 680 TILE_SIZE * state->w + 2 * BORDER,
681 TILE_SIZE * state->h + 2 * BORDER);
682
683 /*
684 * Recessed area containing the whole puzzle.
685 */
686 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
687 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
688 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
689 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
19f24306 690 coords[4] = coords[2] - TILE_SIZE;
691 coords[5] = coords[3] + TILE_SIZE;
692 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
693 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
694 coords[6] = coords[8] + TILE_SIZE;
695 coords[7] = coords[9] - TILE_SIZE;
dafd6cf6 696 draw_polygon(dr, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
4efb3868 697
698 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
699 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
dafd6cf6 700 draw_polygon(dr, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
4efb3868 701
702 ds->started = TRUE;
703 }
704
705 /*
706 * Now draw each tile. We do this in two passes to make
707 * animation easy.
708 */
709 for (pass = 0; pass < 2; pass++) {
710 for (i = 0; i < state->n; i++) {
711 int t, t0;
712 /*
713 * Figure out what should be displayed at this
714 * location. It's either a simple tile, or it's a
715 * transition between two tiles (in which case we say
716 * -1 because it must always be drawn).
717 */
718
719 if (oldstate && oldstate->tiles[i] != state->tiles[i])
720 t = -1;
721 else
722 t = state->tiles[i];
723
724 t0 = t;
725
726 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
727 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
728 int x, y;
729
730 /*
731 * Figure out what to _actually_ draw, and where to
732 * draw it.
733 */
734 if (t == -1) {
735 int x0, y0, x1, y1;
736 int j;
737
738 /*
739 * On the first pass, just blank the tile.
740 */
741 if (pass == 0) {
742 x = COORD(X(state, i));
743 y = COORD(Y(state, i));
744 t = 0;
745 } else {
746 float c;
747
748 t = state->tiles[i];
749
750 /*
751 * Don't bother moving the gap; just don't
752 * draw it.
753 */
754 if (t == 0)
755 continue;
756
757 /*
758 * Find the coordinates of this tile in the old and
759 * new states.
760 */
761 x1 = COORD(X(state, i));
762 y1 = COORD(Y(state, i));
763 for (j = 0; j < oldstate->n; j++)
764 if (oldstate->tiles[j] == state->tiles[i])
765 break;
766 assert(j < oldstate->n);
767 x0 = COORD(X(state, j));
768 y0 = COORD(Y(state, j));
769
770 c = (animtime / ANIM_TIME);
771 if (c < 0.0F) c = 0.0F;
772 if (c > 1.0F) c = 1.0F;
773
774 x = x0 + (int)(c * (x1 - x0));
775 y = y0 + (int)(c * (y1 - y0));
776 }
777
778 } else {
779 if (pass == 0)
780 continue;
781 x = COORD(X(state, i));
782 y = COORD(Y(state, i));
783 }
784
dafd6cf6 785 draw_tile(dr, ds, state, x, y, t, bgcolour);
4efb3868 786 }
787 ds->tiles[i] = t0;
788 }
789 }
790 ds->bgcolour = bgcolour;
fd1a1a2b 791
792 /*
793 * Update the status bar.
794 */
795 {
796 char statusbuf[256];
797
d108c342 798 /*
799 * Don't show the new status until we're also showing the
800 * new _state_ - after the game animation is complete.
801 */
802 if (oldstate)
803 state = oldstate;
804
2ac6d24e 805 if (state->used_solve)
806 sprintf(statusbuf, "Moves since auto-solve: %d",
807 state->movecount - state->completed);
808 else
809 sprintf(statusbuf, "%sMoves: %d",
810 (state->completed ? "COMPLETED! " : ""),
811 (state->completed ? state->completed : state->movecount));
fd1a1a2b 812
dafd6cf6 813 status_bar(dr, statusbuf);
fd1a1a2b 814 }
4efb3868 815}
816
be8d5aa1 817static float game_anim_length(game_state *oldstate,
e3f21163 818 game_state *newstate, int dir, game_ui *ui)
4efb3868 819{
a440f184 820 return ANIM_TIME;
4efb3868 821}
822
be8d5aa1 823static float game_flash_length(game_state *oldstate,
e3f21163 824 game_state *newstate, int dir, game_ui *ui)
4efb3868 825{
2ac6d24e 826 if (!oldstate->completed && newstate->completed &&
827 !oldstate->used_solve && !newstate->used_solve)
4efb3868 828 return 2 * FLASH_FRAME;
829 else
830 return 0.0F;
831}
fd1a1a2b 832
1cea529f 833static int game_status(game_state *state)
4496362f 834{
1cea529f 835 return state->completed ? +1 : 0;
4496362f 836}
837
4d08de49 838static int game_timing_state(game_state *state, game_ui *ui)
48dcdd62 839{
840 return TRUE;
841}
842
dafd6cf6 843static void game_print_size(game_params *params, float *x, float *y)
844{
845}
846
847static void game_print(drawing *dr, game_state *state, int tilesize)
848{
849}
850
be8d5aa1 851#ifdef COMBINED
852#define thegame fifteen
853#endif
854
855const struct game thegame = {
750037d7 856 "Fifteen", "games.fifteen", "fifteen",
be8d5aa1 857 default_params,
858 game_fetch_preset,
859 decode_params,
860 encode_params,
861 free_params,
862 dup_params,
1d228b10 863 TRUE, game_configure, custom_params,
be8d5aa1 864 validate_params,
1185e3c5 865 new_game_desc,
1185e3c5 866 validate_desc,
be8d5aa1 867 new_game,
868 dup_game,
869 free_game,
2ac6d24e 870 TRUE, solve_game,
fa3abef5 871 TRUE, game_can_format_as_text_now, game_text_format,
be8d5aa1 872 new_ui,
873 free_ui,
ae8290c6 874 encode_ui,
875 decode_ui,
07dfb697 876 game_changed_state,
df11cd4e 877 interpret_move,
878 execute_move,
1f3ee4ee 879 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
be8d5aa1 880 game_colours,
881 game_new_drawstate,
882 game_free_drawstate,
883 game_redraw,
884 game_anim_length,
885 game_flash_length,
1cea529f 886 game_status,
dafd6cf6 887 FALSE, FALSE, game_print_size, game_print,
ac9f41c4 888 TRUE, /* wants_statusbar */
48dcdd62 889 FALSE, game_timing_state,
2705d374 890 0, /* flags */
be8d5aa1 891};