2 * fifteen.c: standard 15-puzzle.
14 #define PREFERRED_TILE_SIZE 48
15 #define TILE_SIZE (ds->tilesize)
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 )
21 #define ANIM_TIME 0.13F
22 #define FLASH_FRAME 0.13F
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) )
45 int used_solve; /* used to suppress completion flash */
49 static game_params *default_params(void)
51 game_params *ret = snew(game_params);
58 static int game_fetch_preset(int i, char **name, game_params **params)
61 *params = default_params();
62 *name = dupstr("4x4");
68 static void free_params(game_params *params)
73 static game_params *dup_params(const game_params *params)
75 game_params *ret = snew(game_params);
76 *ret = *params; /* structure copy */
80 static void decode_params(game_params *ret, char const *string)
82 ret->w = ret->h = atoi(string);
83 while (*string && isdigit((unsigned char)*string)) string++;
86 ret->h = atoi(string);
90 static char *encode_params(const game_params *params, int full)
94 sprintf(data, "%dx%d", params->w, params->h);
99 static config_item *game_configure(const game_params *params)
104 ret = snewn(3, config_item);
106 ret[0].name = "Width";
107 ret[0].type = C_STRING;
108 sprintf(buf, "%d", params->w);
109 ret[0].sval = dupstr(buf);
112 ret[1].name = "Height";
113 ret[1].type = C_STRING;
114 sprintf(buf, "%d", params->h);
115 ret[1].sval = dupstr(buf);
126 static game_params *custom_params(const config_item *cfg)
128 game_params *ret = snew(game_params);
130 ret->w = atoi(cfg[0].sval);
131 ret->h = atoi(cfg[1].sval);
136 static char *validate_params(const game_params *params, int full)
138 if (params->w < 2 || params->h < 2)
139 return "Width and height must both be at least two";
144 static int perm_parity(int *perm, int n)
150 for (i = 0; i < n-1; i++)
151 for (j = i+1; j < n; j++)
152 if (perm[i] > perm[j])
158 static char *new_game_desc(const game_params *params, random_state *rs,
159 char **aux, int interactive)
162 int x1, x2, p1, p2, parity;
167 n = params->w * params->h;
169 tiles = snewn(n, int);
170 used = snewn(n, int);
172 for (i = 0; i < n; i++) {
177 gap = random_upto(rs, n);
182 * Place everything else except the last two tiles.
184 for (x = 0, i = n-1; i > 2; i--) {
185 int k = random_upto(rs, i);
188 for (j = 0; j < n; j++)
189 if (!used[j] && (k-- == 0))
192 assert(j < n && !used[j]);
195 while (tiles[x] >= 0)
202 * Find the last two locations, and the last two pieces.
204 while (tiles[x] >= 0)
209 while (tiles[x] >= 0)
214 for (i = 0; i < n; i++)
218 for (i = p1+1; i < n; i++)
224 * Determine the required parity of the overall permutation.
225 * This is the XOR of:
227 * - The chessboard parity ((x^y)&1) of the gap square. The
228 * bottom right counts as even.
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.)
234 parity = ((X(params, gap) - (params->w-1)) ^
235 (Y(params, gap) - (params->h-1)) ^
239 * Try the last two tiles one way round. If that fails, swap
244 if (perm_parity(tiles, n) != parity) {
247 assert(perm_parity(tiles, n) == parity);
251 * Now construct the game description, by describing the tile
252 * array as a simple sequence of comma-separated integers.
256 for (i = 0; i < n; i++) {
260 k = sprintf(buf, "%d,", tiles[i]);
262 ret = sresize(ret, retlen + k + 1, char);
263 strcpy(ret + retlen, buf);
266 ret[retlen-1] = '\0'; /* delete last comma */
274 static char *validate_desc(const game_params *params, const char *desc)
281 area = params->w * params->h;
285 used = snewn(area, int);
286 for (i = 0; i < area; i++)
289 for (i = 0; i < area; i++) {
293 if (*p < '0' || *p > '9') {
294 err = "Not enough numbers in string";
297 while (*p >= '0' && *p <= '9')
299 if (i < area-1 && *p != ',') {
300 err = "Expected comma after number";
303 else if (i == area-1 && *p) {
304 err = "Excess junk at end of string";
308 if (n < 0 || n >= area) {
309 err = "Number out of range";
313 err = "Number used twice";
318 if (*p) p++; /* eat comma */
326 static game_state *new_game(midend *me, const game_params *params,
329 game_state *state = snew(game_state);
333 state->w = params->w;
334 state->h = params->h;
335 state->n = params->w * params->h;
336 state->tiles = snewn(state->n, int);
341 for (i = 0; i < state->n; i++) {
343 state->tiles[i] = atoi(p);
344 if (state->tiles[i] == 0)
346 while (*p && *p != ',')
348 if (*p) p++; /* eat comma */
351 assert(state->tiles[state->gap_pos] == 0);
353 state->completed = state->movecount = 0;
354 state->used_solve = FALSE;
359 static game_state *dup_game(const game_state *state)
361 game_state *ret = snew(game_state);
366 ret->tiles = snewn(state->w * state->h, int);
367 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
368 ret->gap_pos = state->gap_pos;
369 ret->completed = state->completed;
370 ret->movecount = state->movecount;
371 ret->used_solve = state->used_solve;
376 static void free_game(game_state *state)
382 static char *solve_game(const game_state *state, const game_state *currstate,
383 const char *aux, char **error)
388 static int game_can_format_as_text_now(const game_params *params)
393 static char *game_text_format(const game_state *state)
395 char *ret, *p, buf[80];
396 int x, y, col, maxlen;
399 * First work out how many characters we need to display each
402 col = sprintf(buf, "%d", state->n-1);
405 * Now we know the exact total size of the grid we're going to
406 * produce: it's got h rows, each containing w lots of col, w-1
407 * spaces and a trailing newline.
409 maxlen = state->h * state->w * (col+1);
411 ret = snewn(maxlen+1, char);
414 for (y = 0; y < state->h; y++) {
415 for (x = 0; x < state->w; x++) {
416 int v = state->tiles[state->w*y+x];
418 sprintf(buf, "%*s", col, "");
420 sprintf(buf, "%*d", col, v);
430 assert(p - ret == maxlen);
435 static game_ui *new_ui(const game_state *state)
440 static void free_ui(game_ui *ui)
444 static char *encode_ui(const game_ui *ui)
449 static void decode_ui(game_ui *ui, const char *encoding)
453 static void game_changed_state(game_ui *ui, const game_state *oldstate,
454 const game_state *newstate)
458 struct game_drawstate {
465 static char *interpret_move(const game_state *state, game_ui *ui,
466 const game_drawstate *ds,
467 int x, int y, int button)
474 gx = X(state, state->gap_pos);
475 gy = Y(state, state->gap_pos);
477 if (button == CURSOR_RIGHT && gx > 0)
478 dx = gx - 1, dy = gy;
479 else if (button == CURSOR_LEFT && gx < state->w-1)
480 dx = gx + 1, dy = gy;
481 else if (button == CURSOR_DOWN && gy > 0)
482 dy = gy - 1, dx = gx;
483 else if (button == CURSOR_UP && gy < state->h-1)
484 dy = gy + 1, dx = gx;
485 else if (button == LEFT_BUTTON) {
488 if (dx < 0 || dx >= state->w || dy < 0 || dy >= state->h)
489 return NULL; /* out of bounds */
491 * Any click location should be equal to the gap location
492 * in _precisely_ one coordinate.
494 if ((dx == gx && dy == gy) || (dx != gx && dy != gy))
497 return NULL; /* no move */
499 sprintf(buf, "M%d,%d", dx, dy);
503 static game_state *execute_move(const game_state *from, const char *move)
505 int gx, gy, dx, dy, ux, uy, up, p;
508 if (!strcmp(move, "S")) {
511 ret = dup_game(from);
514 * Simply replace the grid with a solved one. For this game,
515 * this isn't a useful operation for actually telling the user
516 * what they should have done, but it is useful for
517 * conveniently being able to get hold of a clean state from
518 * which to practise manoeuvres.
520 for (i = 0; i < ret->n; i++)
521 ret->tiles[i] = (i+1) % ret->n;
522 ret->gap_pos = ret->n-1;
523 ret->used_solve = TRUE;
524 ret->completed = ret->movecount = 1;
529 gx = X(from, from->gap_pos);
530 gy = Y(from, from->gap_pos);
532 if (move[0] != 'M' ||
533 sscanf(move+1, "%d,%d", &dx, &dy) != 2 ||
534 (dx == gx && dy == gy) || (dx != gx && dy != gy) ||
535 dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
539 * Find the unit displacement from the original gap
540 * position towards this one.
542 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
543 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
544 up = C(from, ux, uy);
546 ret = dup_game(from);
548 ret->gap_pos = C(from, dx, dy);
549 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
551 ret->tiles[ret->gap_pos] = 0;
553 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
554 assert(p >= 0 && p < from->n);
555 ret->tiles[p] = from->tiles[p + up];
560 * See if the game has been completed.
562 if (!ret->completed) {
563 ret->completed = ret->movecount;
564 for (p = 0; p < ret->n; p++)
565 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
572 /* ----------------------------------------------------------------------
576 static void game_compute_size(const game_params *params, int tilesize,
579 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
580 struct { int tilesize; } ads, *ds = &ads;
581 ads.tilesize = tilesize;
583 *x = TILE_SIZE * params->w + 2 * BORDER;
584 *y = TILE_SIZE * params->h + 2 * BORDER;
587 static void game_set_size(drawing *dr, game_drawstate *ds,
588 const game_params *params, int tilesize)
590 ds->tilesize = tilesize;
593 static float *game_colours(frontend *fe, int *ncolours)
595 float *ret = snewn(3 * NCOLOURS, float);
598 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
600 for (i = 0; i < 3; i++)
601 ret[COL_TEXT * 3 + i] = 0.0;
603 *ncolours = NCOLOURS;
607 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
609 struct game_drawstate *ds = snew(struct game_drawstate);
615 ds->bgcolour = COL_BACKGROUND;
616 ds->tiles = snewn(ds->w*ds->h, int);
617 ds->tilesize = 0; /* haven't decided yet */
618 for (i = 0; i < ds->w*ds->h; i++)
624 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
630 static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state,
631 int x, int y, int tile, int flash_colour)
634 draw_rect(dr, x, y, TILE_SIZE, TILE_SIZE,
640 coords[0] = x + TILE_SIZE - 1;
641 coords[1] = y + TILE_SIZE - 1;
642 coords[2] = x + TILE_SIZE - 1;
645 coords[5] = y + TILE_SIZE - 1;
646 draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
650 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
652 draw_rect(dr, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
653 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
656 sprintf(str, "%d", tile);
657 draw_text(dr, x + TILE_SIZE/2, y + TILE_SIZE/2,
658 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
661 draw_update(dr, x, y, TILE_SIZE, TILE_SIZE);
664 static void game_redraw(drawing *dr, game_drawstate *ds,
665 const game_state *oldstate, const game_state *state,
666 int dir, const game_ui *ui,
667 float animtime, float flashtime)
669 int i, pass, bgcolour;
672 int frame = (int)(flashtime / FLASH_FRAME);
673 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
675 bgcolour = COL_BACKGROUND;
681 TILE_SIZE * state->w + 2 * BORDER,
682 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
683 draw_update(dr, 0, 0,
684 TILE_SIZE * state->w + 2 * BORDER,
685 TILE_SIZE * state->h + 2 * BORDER);
688 * Recessed area containing the whole puzzle.
690 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
691 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
692 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
693 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
694 coords[4] = coords[2] - TILE_SIZE;
695 coords[5] = coords[3] + TILE_SIZE;
696 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
697 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
698 coords[6] = coords[8] + TILE_SIZE;
699 coords[7] = coords[9] - TILE_SIZE;
700 draw_polygon(dr, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
702 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
703 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
704 draw_polygon(dr, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
710 * Now draw each tile. We do this in two passes to make
713 for (pass = 0; pass < 2; pass++) {
714 for (i = 0; i < state->n; i++) {
717 * Figure out what should be displayed at this
718 * location. It's either a simple tile, or it's a
719 * transition between two tiles (in which case we say
720 * -1 because it must always be drawn).
723 if (oldstate && oldstate->tiles[i] != state->tiles[i])
730 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
731 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
735 * Figure out what to _actually_ draw, and where to
743 * On the first pass, just blank the tile.
746 x = COORD(X(state, i));
747 y = COORD(Y(state, i));
755 * Don't bother moving the gap; just don't
762 * Find the coordinates of this tile in the old and
765 x1 = COORD(X(state, i));
766 y1 = COORD(Y(state, i));
767 for (j = 0; j < oldstate->n; j++)
768 if (oldstate->tiles[j] == state->tiles[i])
770 assert(j < oldstate->n);
771 x0 = COORD(X(state, j));
772 y0 = COORD(Y(state, j));
774 c = (animtime / ANIM_TIME);
775 if (c < 0.0F) c = 0.0F;
776 if (c > 1.0F) c = 1.0F;
778 x = x0 + (int)(c * (x1 - x0));
779 y = y0 + (int)(c * (y1 - y0));
785 x = COORD(X(state, i));
786 y = COORD(Y(state, i));
789 draw_tile(dr, ds, state, x, y, t, bgcolour);
794 ds->bgcolour = bgcolour;
797 * Update the status bar.
803 * Don't show the new status until we're also showing the
804 * new _state_ - after the game animation is complete.
809 if (state->used_solve)
810 sprintf(statusbuf, "Moves since auto-solve: %d",
811 state->movecount - state->completed);
813 sprintf(statusbuf, "%sMoves: %d",
814 (state->completed ? "COMPLETED! " : ""),
815 (state->completed ? state->completed : state->movecount));
817 status_bar(dr, statusbuf);
821 static float game_anim_length(const game_state *oldstate,
822 const game_state *newstate, int dir, game_ui *ui)
827 static float game_flash_length(const game_state *oldstate,
828 const game_state *newstate, int dir, game_ui *ui)
830 if (!oldstate->completed && newstate->completed &&
831 !oldstate->used_solve && !newstate->used_solve)
832 return 2 * FLASH_FRAME;
837 static int game_status(const game_state *state)
839 return state->completed ? +1 : 0;
842 static int game_timing_state(const game_state *state, game_ui *ui)
847 static void game_print_size(const game_params *params, float *x, float *y)
851 static void game_print(drawing *dr, const game_state *state, int tilesize)
856 #define thegame fifteen
859 const struct game thegame = {
860 "Fifteen", "games.fifteen", "fifteen",
867 TRUE, game_configure, custom_params,
875 TRUE, game_can_format_as_text_now, game_text_format,
883 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
891 FALSE, FALSE, game_print_size, game_print,
892 TRUE, /* wants_statusbar */
893 FALSE, game_timing_state,