2 * fifteen.c: standard 15-puzzle.
15 #define BORDER (TILE_SIZE / 2)
16 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
17 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
18 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
20 #define ANIM_TIME 0.13F
21 #define FLASH_FRAME 0.13F
23 #define X(state, i) ( (i) % (state)->w )
24 #define Y(state, i) ( (i) / (state)->w )
25 #define C(state, x, y) ( (y) * (state)->w + (x) )
44 int just_used_solve; /* used to suppress undo animation */
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)
63 static void free_params(game_params *params)
68 static game_params *dup_params(game_params *params)
70 game_params *ret = snew(game_params);
71 *ret = *params; /* structure copy */
75 static game_params *decode_params(char const *string)
77 game_params *ret = default_params();
79 ret->w = ret->h = atoi(string);
80 while (*string && isdigit(*string)) string++;
83 ret->h = atoi(string);
89 static char *encode_params(game_params *params)
93 sprintf(data, "%dx%d", params->w, params->h);
98 static config_item *game_configure(game_params *params)
103 ret = snewn(3, config_item);
105 ret[0].name = "Width";
106 ret[0].type = C_STRING;
107 sprintf(buf, "%d", params->w);
108 ret[0].sval = dupstr(buf);
111 ret[1].name = "Height";
112 ret[1].type = C_STRING;
113 sprintf(buf, "%d", params->h);
114 ret[1].sval = dupstr(buf);
125 static game_params *custom_params(config_item *cfg)
127 game_params *ret = snew(game_params);
129 ret->w = atoi(cfg[0].sval);
130 ret->h = atoi(cfg[1].sval);
135 static char *validate_params(game_params *params)
137 if (params->w < 2 && params->h < 2)
138 return "Width and height must both be at least two";
143 static int perm_parity(int *perm, int n)
149 for (i = 0; i < n-1; i++)
150 for (j = i+1; j < n; j++)
151 if (perm[i] > perm[j])
157 static char *new_game_seed(game_params *params, random_state *rs,
161 int x1, x2, p1, p2, parity;
166 n = params->w * params->h;
168 tiles = snewn(n, int);
169 used = snewn(n, int);
171 for (i = 0; i < n; i++) {
176 gap = random_upto(rs, n);
181 * Place everything else except the last two tiles.
183 for (x = 0, i = n-1; i > 2; i--) {
184 int k = random_upto(rs, i);
187 for (j = 0; j < n; j++)
188 if (!used[j] && (k-- == 0))
191 assert(j < n && !used[j]);
194 while (tiles[x] >= 0)
201 * Find the last two locations, and the last two pieces.
203 while (tiles[x] >= 0)
208 while (tiles[x] >= 0)
213 for (i = 0; i < n; i++)
217 for (i = p1+1; i < n; i++)
223 * Determine the required parity of the overall permutation.
224 * This is the XOR of:
226 * - The chessboard parity ((x^y)&1) of the gap square. The
227 * bottom right counts as even.
229 * - The parity of n. (The target permutation is 1,...,n-1,0
230 * rather than 0,...,n-1; this is a cyclic permutation of
231 * the starting point and hence is odd iff n is even.)
233 parity = ((X(params, gap) - (params->w-1)) ^
234 (Y(params, gap) - (params->h-1)) ^
238 * Try the last two tiles one way round. If that fails, swap
243 if (perm_parity(tiles, n) != parity) {
246 assert(perm_parity(tiles, n) == parity);
250 * Now construct the game seed, by describing the tile array as
251 * a simple sequence of comma-separated integers.
255 for (i = 0; i < n; i++) {
259 k = sprintf(buf, "%d,", tiles[i]);
261 ret = sresize(ret, retlen + k + 1, char);
262 strcpy(ret + retlen, buf);
265 ret[retlen-1] = '\0'; /* delete last comma */
273 static void game_free_aux_info(game_aux_info *aux)
275 assert(!"Shouldn't happen");
278 static char *validate_seed(game_params *params, char *seed)
284 area = params->w * params->h;
288 used = snewn(area, int);
289 for (i = 0; i < area; i++)
292 for (i = 0; i < area; i++) {
296 if (*p < '0' || *p > '9') {
297 err = "Not enough numbers in string";
300 while (*p >= '0' && *p <= '9')
302 if (i < area-1 && *p != ',') {
303 err = "Expected comma after number";
306 else if (i == area-1 && *p) {
307 err = "Excess junk at end of string";
311 if (n < 0 || n >= area) {
312 err = "Number out of range";
316 err = "Number used twice";
321 if (*p) p++; /* eat comma */
329 static game_state *new_game(game_params *params, char *seed)
331 game_state *state = snew(game_state);
335 state->w = params->w;
336 state->h = params->h;
337 state->n = params->w * params->h;
338 state->tiles = snewn(state->n, int);
343 for (i = 0; i < state->n; i++) {
345 state->tiles[i] = atoi(p);
346 if (state->tiles[i] == 0)
348 while (*p && *p != ',')
350 if (*p) p++; /* eat comma */
353 assert(state->tiles[state->gap_pos] == 0);
355 state->completed = state->movecount = 0;
356 state->used_solve = state->just_used_solve = FALSE;
361 static game_state *dup_game(game_state *state)
363 game_state *ret = snew(game_state);
368 ret->tiles = snewn(state->w * state->h, int);
369 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
370 ret->gap_pos = state->gap_pos;
371 ret->completed = state->completed;
372 ret->movecount = state->movecount;
373 ret->used_solve = state->used_solve;
374 ret->just_used_solve = state->just_used_solve;
379 static void free_game(game_state *state)
384 static game_state *solve_game(game_state *state, game_aux_info *aux,
387 game_state *ret = dup_game(state);
391 * Simply replace the grid with a solved one. For this game,
392 * this isn't a useful operation for actually telling the user
393 * what they should have done, but it is useful for
394 * conveniently being able to get hold of a clean state from
395 * which to practise manoeuvres.
397 for (i = 0; i < ret->n; i++)
398 ret->tiles[i] = (i+1) % ret->n;
399 ret->gap_pos = ret->n-1;
400 ret->used_solve = ret->just_used_solve = TRUE;
401 ret->completed = ret->movecount;
406 static char *game_text_format(game_state *state)
408 char *ret, *p, buf[80];
409 int x, y, col, maxlen;
412 * First work out how many characters we need to display each
415 col = sprintf(buf, "%d", state->n-1);
418 * Now we know the exact total size of the grid we're going to
419 * produce: it's got h rows, each containing w lots of col, w-1
420 * spaces and a trailing newline.
422 maxlen = state->h * state->w * (col+1);
424 ret = snewn(maxlen+1, char);
427 for (y = 0; y < state->h; y++) {
428 for (x = 0; x < state->w; x++) {
429 int v = state->tiles[state->w*y+x];
431 sprintf(buf, "%*s", col, "");
433 sprintf(buf, "%*d", col, v);
443 assert(p - ret == maxlen);
448 static game_ui *new_ui(game_state *state)
453 static void free_ui(game_ui *ui)
457 static game_state *make_move(game_state *from, game_ui *ui,
458 int x, int y, int button)
460 int gx, gy, dx, dy, ux, uy, up, p;
463 gx = X(from, from->gap_pos);
464 gy = Y(from, from->gap_pos);
466 if (button == CURSOR_RIGHT && gx > 0)
467 dx = gx - 1, dy = gy;
468 else if (button == CURSOR_LEFT && gx < from->w-1)
469 dx = gx + 1, dy = gy;
470 else if (button == CURSOR_DOWN && gy > 0)
471 dy = gy - 1, dx = gx;
472 else if (button == CURSOR_UP && gy < from->h-1)
473 dy = gy + 1, dx = gx;
474 else if (button == LEFT_BUTTON) {
477 if (dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
478 return NULL; /* out of bounds */
480 * Any click location should be equal to the gap location
481 * in _precisely_ one coordinate.
483 if ((dx == gx && dy == gy) || (dx != gx && dy != gy))
486 return NULL; /* no move */
489 * Find the unit displacement from the original gap
490 * position towards this one.
492 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
493 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
494 up = C(from, ux, uy);
496 ret = dup_game(from);
497 ret->just_used_solve = FALSE; /* zero this in a hurry */
499 ret->gap_pos = C(from, dx, dy);
500 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
502 ret->tiles[ret->gap_pos] = 0;
504 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
505 assert(p >= 0 && p < from->n);
506 ret->tiles[p] = from->tiles[p + up];
511 * See if the game has been completed.
513 if (!ret->completed) {
514 ret->completed = ret->movecount;
515 for (p = 0; p < ret->n; p++)
516 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
523 /* ----------------------------------------------------------------------
527 struct game_drawstate {
533 static void game_size(game_params *params, int *x, int *y)
535 *x = TILE_SIZE * params->w + 2 * BORDER;
536 *y = TILE_SIZE * params->h + 2 * BORDER;
539 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
541 float *ret = snewn(3 * NCOLOURS, float);
545 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
548 * Drop the background colour so that the highlight is
549 * noticeably brighter than it while still being under 1.
551 max = ret[COL_BACKGROUND*3];
552 for (i = 1; i < 3; i++)
553 if (ret[COL_BACKGROUND*3+i] > max)
554 max = ret[COL_BACKGROUND*3+i];
555 if (max * 1.2F > 1.0F) {
556 for (i = 0; i < 3; i++)
557 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
560 for (i = 0; i < 3; i++) {
561 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
562 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
563 ret[COL_TEXT * 3 + i] = 0.0;
566 *ncolours = NCOLOURS;
570 static game_drawstate *game_new_drawstate(game_state *state)
572 struct game_drawstate *ds = snew(struct game_drawstate);
578 ds->bgcolour = COL_BACKGROUND;
579 ds->tiles = snewn(ds->w*ds->h, int);
580 for (i = 0; i < ds->w*ds->h; i++)
586 static void game_free_drawstate(game_drawstate *ds)
592 static void draw_tile(frontend *fe, game_state *state, int x, int y,
593 int tile, int flash_colour)
596 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
602 coords[0] = x + TILE_SIZE - 1;
603 coords[1] = y + TILE_SIZE - 1;
604 coords[2] = x + TILE_SIZE - 1;
607 coords[5] = y + TILE_SIZE - 1;
608 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
609 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
613 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
614 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
616 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
617 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
620 sprintf(str, "%d", tile);
621 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
622 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
625 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
628 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
629 game_state *state, int dir, game_ui *ui,
630 float animtime, float flashtime)
632 int i, pass, bgcolour;
635 int frame = (int)(flashtime / FLASH_FRAME);
636 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
638 bgcolour = COL_BACKGROUND;
644 TILE_SIZE * state->w + 2 * BORDER,
645 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
646 draw_update(fe, 0, 0,
647 TILE_SIZE * state->w + 2 * BORDER,
648 TILE_SIZE * state->h + 2 * BORDER);
651 * Recessed area containing the whole puzzle.
653 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
654 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
655 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
656 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
657 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
658 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
659 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
660 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
662 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
663 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
664 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
665 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
671 * Now draw each tile. We do this in two passes to make
674 for (pass = 0; pass < 2; pass++) {
675 for (i = 0; i < state->n; i++) {
678 * Figure out what should be displayed at this
679 * location. It's either a simple tile, or it's a
680 * transition between two tiles (in which case we say
681 * -1 because it must always be drawn).
684 if (oldstate && oldstate->tiles[i] != state->tiles[i])
691 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
692 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
696 * Figure out what to _actually_ draw, and where to
704 * On the first pass, just blank the tile.
707 x = COORD(X(state, i));
708 y = COORD(Y(state, i));
716 * Don't bother moving the gap; just don't
723 * Find the coordinates of this tile in the old and
726 x1 = COORD(X(state, i));
727 y1 = COORD(Y(state, i));
728 for (j = 0; j < oldstate->n; j++)
729 if (oldstate->tiles[j] == state->tiles[i])
731 assert(j < oldstate->n);
732 x0 = COORD(X(state, j));
733 y0 = COORD(Y(state, j));
735 c = (animtime / ANIM_TIME);
736 if (c < 0.0F) c = 0.0F;
737 if (c > 1.0F) c = 1.0F;
739 x = x0 + (int)(c * (x1 - x0));
740 y = y0 + (int)(c * (y1 - y0));
746 x = COORD(X(state, i));
747 y = COORD(Y(state, i));
750 draw_tile(fe, state, x, y, t, bgcolour);
755 ds->bgcolour = bgcolour;
758 * Update the status bar.
764 * Don't show the new status until we're also showing the
765 * new _state_ - after the game animation is complete.
770 if (state->used_solve)
771 sprintf(statusbuf, "Moves since auto-solve: %d",
772 state->movecount - state->completed);
774 sprintf(statusbuf, "%sMoves: %d",
775 (state->completed ? "COMPLETED! " : ""),
776 (state->completed ? state->completed : state->movecount));
778 status_bar(fe, statusbuf);
782 static float game_anim_length(game_state *oldstate,
783 game_state *newstate, int dir)
785 if ((dir > 0 && newstate->just_used_solve) ||
786 (dir < 0 && oldstate->just_used_solve))
792 static float game_flash_length(game_state *oldstate,
793 game_state *newstate, int dir)
795 if (!oldstate->completed && newstate->completed &&
796 !oldstate->used_solve && !newstate->used_solve)
797 return 2 * FLASH_FRAME;
802 static int game_wants_statusbar(void)
808 #define thegame fifteen
811 const struct game thegame = {
812 "Fifteen", "games.fifteen",
819 TRUE, game_configure, custom_params,
828 TRUE, game_text_format,
839 game_wants_statusbar,