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
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 )
22 #define ANIM_TIME 0.13F
23 #define FLASH_FRAME 0.13F
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) )
45 int just_used_solve; /* used to suppress undo animation */
46 int used_solve; /* used to suppress completion flash */
48 int last_movement_sense;
51 static game_params *default_params(void)
53 game_params *ret = snew(game_params);
60 static int game_fetch_preset(int i, char **name, game_params **params)
67 case 0: w = 3, h = 3; break;
68 case 1: w = 4, h = 3; break;
69 case 2: w = 4, h = 4; break;
70 case 3: w = 5, h = 4; break;
71 case 4: w = 5, h = 5; break;
72 default: return FALSE;
75 sprintf(buf, "%dx%d", w, h);
77 *params = ret = snew(game_params);
83 static void free_params(game_params *params)
88 static game_params *dup_params(game_params *params)
90 game_params *ret = snew(game_params);
91 *ret = *params; /* structure copy */
95 static game_params *decode_params(char const *string)
97 game_params *ret = default_params();
99 ret->w = ret->h = atoi(string);
100 while (*string && isdigit(*string)) string++;
101 if (*string == 'x') {
103 ret->h = atoi(string);
109 static char *encode_params(game_params *params)
113 sprintf(data, "%dx%d", params->w, params->h);
118 static config_item *game_configure(game_params *params)
123 ret = snewn(3, config_item);
125 ret[0].name = "Width";
126 ret[0].type = C_STRING;
127 sprintf(buf, "%d", params->w);
128 ret[0].sval = dupstr(buf);
131 ret[1].name = "Height";
132 ret[1].type = C_STRING;
133 sprintf(buf, "%d", params->h);
134 ret[1].sval = dupstr(buf);
145 static game_params *custom_params(config_item *cfg)
147 game_params *ret = snew(game_params);
149 ret->w = atoi(cfg[0].sval);
150 ret->h = atoi(cfg[1].sval);
155 static char *validate_params(game_params *params)
157 if (params->w < 2 && params->h < 2)
158 return "Width and height must both be at least two";
163 static int perm_parity(int *perm, int n)
169 for (i = 0; i < n-1; i++)
170 for (j = i+1; j < n; j++)
171 if (perm[i] > perm[j])
177 static char *new_game_seed(game_params *params, random_state *rs,
186 n = params->w * params->h;
188 tiles = snewn(n, int);
189 used = snewn(n, int);
191 for (i = 0; i < n; i++) {
197 * If both dimensions are odd, there is a parity constraint.
199 if (params->w & params->h & 1)
205 * Place everything except (possibly) the last two tiles.
207 for (x = 0, i = n; i > stop; i--) {
208 int k = i > 1 ? random_upto(rs, i) : 0;
211 for (j = 0; j < n; j++)
212 if (!used[j] && (k-- == 0))
215 assert(j < n && !used[j]);
218 while (tiles[x] >= 0)
226 * Find the last two locations, and the last two pieces.
228 while (tiles[x] >= 0)
233 while (tiles[x] >= 0)
238 for (i = 0; i < n; i++)
242 for (i = p1+1; i < n; i++)
248 * Try the last two tiles one way round. If that fails, swap
253 if (perm_parity(tiles, n) != 0) {
256 assert(perm_parity(tiles, n) == 0);
261 * Now construct the game seed, by describing the tile array as
262 * a simple sequence of comma-separated integers.
266 for (i = 0; i < n; i++) {
270 k = sprintf(buf, "%d,", tiles[i]+1);
272 ret = sresize(ret, retlen + k + 1, char);
273 strcpy(ret + retlen, buf);
276 ret[retlen-1] = '\0'; /* delete last comma */
284 static void game_free_aux_info(game_aux_info *aux)
286 assert(!"Shouldn't happen");
290 static char *validate_seed(game_params *params, char *seed)
296 area = params->w * params->h;
300 used = snewn(area, int);
301 for (i = 0; i < area; i++)
304 for (i = 0; i < area; i++) {
308 if (*p < '0' || *p > '9') {
309 err = "Not enough numbers in string";
312 while (*p >= '0' && *p <= '9')
314 if (i < area-1 && *p != ',') {
315 err = "Expected comma after number";
318 else if (i == area-1 && *p) {
319 err = "Excess junk at end of string";
323 if (n < 1 || n > area) {
324 err = "Number out of range";
328 err = "Number used twice";
333 if (*p) p++; /* eat comma */
341 static game_state *new_game(game_params *params, char *seed)
343 game_state *state = snew(game_state);
347 state->w = params->w;
348 state->h = params->h;
349 state->n = params->w * params->h;
350 state->tiles = snewn(state->n, int);
354 for (i = 0; i < state->n; i++) {
356 state->tiles[i] = atoi(p);
357 while (*p && *p != ',')
359 if (*p) p++; /* eat comma */
363 state->completed = state->movecount = 0;
364 state->used_solve = state->just_used_solve = FALSE;
365 state->last_movement_sense = 0;
370 static game_state *dup_game(game_state *state)
372 game_state *ret = snew(game_state);
377 ret->tiles = snewn(state->w * state->h, int);
378 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
379 ret->completed = state->completed;
380 ret->movecount = state->movecount;
381 ret->used_solve = state->used_solve;
382 ret->just_used_solve = state->just_used_solve;
383 ret->last_movement_sense = state->last_movement_sense;
388 static void free_game(game_state *state)
393 static game_state *solve_game(game_state *state, game_aux_info *aux,
396 game_state *ret = dup_game(state);
400 * Simply replace the grid with a solved one. For this game,
401 * this isn't a useful operation for actually telling the user
402 * what they should have done, but it is useful for
403 * conveniently being able to get hold of a clean state from
404 * which to practise manoeuvres.
406 for (i = 0; i < ret->n; i++)
408 ret->used_solve = ret->just_used_solve = TRUE;
409 ret->completed = ret->movecount;
414 static char *game_text_format(game_state *state)
416 char *ret, *p, buf[80];
417 int x, y, col, maxlen;
420 * First work out how many characters we need to display each
423 col = sprintf(buf, "%d", state->n);
426 * Now we know the exact total size of the grid we're going to
427 * produce: it's got h rows, each containing w lots of col, w-1
428 * spaces and a trailing newline.
430 maxlen = state->h * state->w * (col+1);
432 ret = snewn(maxlen, char);
435 for (y = 0; y < state->h; y++) {
436 for (x = 0; x < state->w; x++) {
437 int v = state->tiles[state->w*y+x];
438 sprintf(buf, "%*d", col, v);
448 assert(p - ret == maxlen);
453 static game_ui *new_ui(game_state *state)
458 static void free_ui(game_ui *ui)
462 static game_state *make_move(game_state *from, game_ui *ui,
463 int x, int y, int button)
466 int dx, dy, tx, ty, n;
469 if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
474 if (cx == -1 && cy >= 0 && cy < from->h)
475 n = from->w, dx = +1, dy = 0;
476 else if (cx == from->w && cy >= 0 && cy < from->h)
477 n = from->w, dx = -1, dy = 0;
478 else if (cy == -1 && cx >= 0 && cx < from->w)
479 n = from->h, dy = +1, dx = 0;
480 else if (cy == from->h && cx >= 0 && cx < from->w)
481 n = from->h, dy = -1, dx = 0;
483 return NULL; /* invalid click location */
485 /* reverse direction if right hand button is pressed */
486 if (button == RIGHT_BUTTON)
488 dx = -dx; if (dx) cx = from->w - 1 - cx;
489 dy = -dy; if (dy) cy = from->h - 1 - cy;
492 ret = dup_game(from);
493 ret->just_used_solve = FALSE; /* zero this in a hurry */
498 tx = (cx + dx + from->w) % from->w;
499 ty = (cy + dy + from->h) % from->h;
500 ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
505 ret->last_movement_sense = -(dx+dy);
508 * See if the game has been completed.
510 if (!ret->completed) {
511 ret->completed = ret->movecount;
512 for (n = 0; n < ret->n; n++)
513 if (ret->tiles[n] != n+1)
514 ret->completed = FALSE;
520 /* ----------------------------------------------------------------------
524 struct game_drawstate {
530 static void game_size(game_params *params, int *x, int *y)
532 *x = TILE_SIZE * params->w + 2 * BORDER;
533 *y = TILE_SIZE * params->h + 2 * BORDER;
536 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
538 float *ret = snewn(3 * NCOLOURS, float);
542 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
545 * Drop the background colour so that the highlight is
546 * noticeably brighter than it while still being under 1.
548 max = ret[COL_BACKGROUND*3];
549 for (i = 1; i < 3; i++)
550 if (ret[COL_BACKGROUND*3+i] > max)
551 max = ret[COL_BACKGROUND*3+i];
552 if (max * 1.2F > 1.0F) {
553 for (i = 0; i < 3; i++)
554 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
557 for (i = 0; i < 3; i++) {
558 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
559 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
560 ret[COL_TEXT * 3 + i] = 0.0;
563 *ncolours = NCOLOURS;
567 static game_drawstate *game_new_drawstate(game_state *state)
569 struct game_drawstate *ds = snew(struct game_drawstate);
575 ds->bgcolour = COL_BACKGROUND;
576 ds->tiles = snewn(ds->w*ds->h, int);
577 for (i = 0; i < ds->w*ds->h; i++)
583 static void game_free_drawstate(game_drawstate *ds)
589 static void draw_tile(frontend *fe, game_state *state, int x, int y,
590 int tile, int flash_colour)
593 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
599 coords[0] = x + TILE_SIZE - 1;
600 coords[1] = y + TILE_SIZE - 1;
601 coords[2] = x + TILE_SIZE - 1;
604 coords[5] = y + TILE_SIZE - 1;
605 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
606 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
610 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
611 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
613 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
614 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
617 sprintf(str, "%d", tile);
618 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
619 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
622 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
625 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
628 int ydy = -xdx, ydx = xdy;
630 #define POINT(n, xx, yy) ( \
631 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
632 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
634 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
635 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
636 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
637 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
638 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
639 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
640 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
642 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
643 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
646 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
647 game_state *state, int dir, game_ui *ui,
648 float animtime, float flashtime)
653 int frame = (int)(flashtime / FLASH_FRAME);
654 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
656 bgcolour = COL_BACKGROUND;
662 TILE_SIZE * state->w + 2 * BORDER,
663 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
664 draw_update(fe, 0, 0,
665 TILE_SIZE * state->w + 2 * BORDER,
666 TILE_SIZE * state->h + 2 * BORDER);
669 * Recessed area containing the whole puzzle.
671 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
672 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
673 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
674 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
675 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
676 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
677 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
678 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
680 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
681 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
682 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
683 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
686 * Arrows for making moves.
688 for (i = 0; i < state->w; i++) {
689 draw_arrow(fe, COORD(i), COORD(0), +1, 0);
690 draw_arrow(fe, COORD(i+1), COORD(state->h), -1, 0);
692 for (i = 0; i < state->h; i++) {
693 draw_arrow(fe, COORD(state->w), COORD(i), 0, +1);
694 draw_arrow(fe, COORD(0), COORD(i+1), 0, -1);
701 * Now draw each tile.
704 clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
706 for (i = 0; i < state->n; i++) {
709 * Figure out what should be displayed at this
710 * location. It's either a simple tile, or it's a
711 * transition between two tiles (in which case we say
712 * -1 because it must always be drawn).
715 if (oldstate && oldstate->tiles[i] != state->tiles[i])
722 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
723 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
727 * Figure out what to _actually_ draw, and where to
731 int x0, y0, x1, y1, dx, dy;
738 sense = -oldstate->last_movement_sense;
740 sense = state->last_movement_sense;
746 * FIXME: must be prepared to draw a double
747 * tile in some situations.
751 * Find the coordinates of this tile in the old and
754 x1 = COORD(X(state, i));
755 y1 = COORD(Y(state, i));
756 for (j = 0; j < oldstate->n; j++)
757 if (oldstate->tiles[j] == state->tiles[i])
759 assert(j < oldstate->n);
760 x0 = COORD(X(state, j));
761 y0 = COORD(Y(state, j));
765 dx != TILE_SIZE * sense) {
766 dx = (dx < 0 ? dx + TILE_SIZE * state->w :
767 dx - TILE_SIZE * state->w);
768 assert(abs(dx) == TILE_SIZE);
772 dy != TILE_SIZE * sense) {
773 dy = (dy < 0 ? dy + TILE_SIZE * state->h :
774 dy - TILE_SIZE * state->h);
775 assert(abs(dy) == TILE_SIZE);
778 c = (animtime / ANIM_TIME);
779 if (c < 0.0F) c = 0.0F;
780 if (c > 1.0F) c = 1.0F;
782 x = x0 + (int)(c * dx);
783 y = y0 + (int)(c * dy);
784 x2 = x1 - dx + (int)(c * dx);
785 y2 = y1 - dy + (int)(c * dy);
787 x = COORD(X(state, i));
788 y = COORD(Y(state, i));
792 draw_tile(fe, state, x, y, t, bgcolour);
793 if (x2 != -1 || y2 != -1)
794 draw_tile(fe, state, x2, y2, t, bgcolour);
801 ds->bgcolour = bgcolour;
804 * Update the status bar.
810 * Don't show the new status until we're also showing the
811 * new _state_ - after the game animation is complete.
816 if (state->used_solve)
817 sprintf(statusbuf, "Moves since auto-solve: %d",
818 state->movecount - state->completed);
820 sprintf(statusbuf, "%sMoves: %d",
821 (state->completed ? "COMPLETED! " : ""),
822 (state->completed ? state->completed : state->movecount));
824 status_bar(fe, statusbuf);
828 static float game_anim_length(game_state *oldstate,
829 game_state *newstate, int dir)
831 if ((dir > 0 && newstate->just_used_solve) ||
832 (dir < 0 && oldstate->just_used_solve))
838 static float game_flash_length(game_state *oldstate,
839 game_state *newstate, int dir)
841 if (!oldstate->completed && newstate->completed &&
842 !oldstate->used_solve && !newstate->used_solve)
843 return 2 * FLASH_FRAME;
848 static int game_wants_statusbar(void)
854 #define thegame sixteen
857 const struct game thegame = {
858 "Sixteen", "games.sixteen",
865 TRUE, game_configure, custom_params,
874 TRUE, game_text_format,
885 game_wants_statusbar,