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) )
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;
52 static game_params *default_params(void)
54 game_params *ret = snew(game_params);
62 static int game_fetch_preset(int i, char **name, game_params **params)
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;
77 sprintf(buf, "%dx%d", w, h);
79 *params = ret = snew(game_params);
86 static void free_params(game_params *params)
91 static game_params *dup_params(game_params *params)
93 game_params *ret = snew(game_params);
94 *ret = *params; /* structure copy */
98 static void decode_params(game_params *ret, char const *string)
100 ret->w = ret->h = atoi(string);
102 while (*string && isdigit(*string)) string++;
103 if (*string == 'x') {
105 ret->h = atoi(string);
106 while (*string && isdigit((unsigned char)*string))
109 if (*string == 'm') {
111 ret->movetarget = atoi(string);
112 while (*string && isdigit((unsigned char)*string))
117 static char *encode_params(game_params *params, int full)
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);
130 static config_item *game_configure(game_params *params)
135 ret = snewn(4, config_item);
137 ret[0].name = "Width";
138 ret[0].type = C_STRING;
139 sprintf(buf, "%d", params->w);
140 ret[0].sval = dupstr(buf);
143 ret[1].name = "Height";
144 ret[1].type = C_STRING;
145 sprintf(buf, "%d", params->h);
146 ret[1].sval = dupstr(buf);
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);
163 static game_params *custom_params(config_item *cfg)
165 game_params *ret = snew(game_params);
167 ret->w = atoi(cfg[0].sval);
168 ret->h = atoi(cfg[1].sval);
169 ret->movetarget = atoi(cfg[2].sval);
174 static char *validate_params(game_params *params)
176 if (params->w < 2 || params->h < 2)
177 return "Width and height must both be at least two";
182 static int perm_parity(int *perm, int n)
188 for (i = 0; i < n-1; i++)
189 for (j = i+1; j < n; j++)
190 if (perm[i] > perm[j])
196 static char *new_game_desc(game_params *params, random_state *rs,
197 game_aux_info **aux, int interactive)
205 n = params->w * params->h;
207 tiles = snewn(n, int);
209 if (params->movetarget) {
211 int max = (params->w > params->h ? params->w : params->h);
212 int *prevmoves = snewn(max, int);
215 * Shuffle the old-fashioned way, by making a series of
216 * single moves on the grid.
219 for (i = 0; i < n; i++)
222 for (i = 0; i < params->movetarget; i++) {
223 int start, offset, len, direction, index;
227 * Choose a move to make. We can choose from any row
231 j = random_upto(rs, params->w + params->h);
241 index = j - params->w;
242 start = index * params->w;
247 direction = -1 + 2 * random_upto(rs, 2);
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
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.
267 if (offset == prevoffset) {
268 tmp = prevmoves[index] + direction;
269 if (abs(2*tmp) > len || abs(tmp) < abs(prevmoves[index]))
273 /* If we didn't `continue', we've found an OK move to make. */
274 if (offset != prevoffset) {
276 for (i = 0; i < max; i++)
280 prevmoves[index] += direction;
288 start += (len-1) * offset;
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;
301 used = snewn(n, int);
303 for (i = 0; i < n; i++) {
309 * If both dimensions are odd, there is a parity
312 if (params->w & params->h & 1)
318 * Place everything except (possibly) the last two tiles.
320 for (x = 0, i = n; i > stop; i--) {
321 int k = i > 1 ? random_upto(rs, i) : 0;
324 for (j = 0; j < n; j++)
325 if (!used[j] && (k-- == 0))
328 assert(j < n && !used[j]);
331 while (tiles[x] >= 0)
339 * Find the last two locations, and the last two
342 while (tiles[x] >= 0)
347 while (tiles[x] >= 0)
352 for (i = 0; i < n; i++)
356 for (i = p1+1; i < n; i++)
362 * Try the last two tiles one way round. If that fails,
367 if (perm_parity(tiles, n) != 0) {
370 assert(perm_parity(tiles, n) == 0);
378 * Now construct the game description, by describing the tile
379 * array as a simple sequence of comma-separated integers.
383 for (i = 0; i < n; i++) {
387 k = sprintf(buf, "%d,", tiles[i]+1);
389 ret = sresize(ret, retlen + k + 1, char);
390 strcpy(ret + retlen, buf);
393 ret[retlen-1] = '\0'; /* delete last comma */
400 static void game_free_aux_info(game_aux_info *aux)
402 assert(!"Shouldn't happen");
406 static char *validate_desc(game_params *params, char *desc)
412 area = params->w * params->h;
416 used = snewn(area, int);
417 for (i = 0; i < area; i++)
420 for (i = 0; i < area; i++) {
424 if (*p < '0' || *p > '9') {
425 err = "Not enough numbers in string";
428 while (*p >= '0' && *p <= '9')
430 if (i < area-1 && *p != ',') {
431 err = "Expected comma after number";
434 else if (i == area-1 && *p) {
435 err = "Excess junk at end of string";
439 if (n < 1 || n > area) {
440 err = "Number out of range";
444 err = "Number used twice";
449 if (*p) p++; /* eat comma */
457 static game_state *new_game(midend_data *me, game_params *params, char *desc)
459 game_state *state = snew(game_state);
463 state->w = params->w;
464 state->h = params->h;
465 state->n = params->w * params->h;
466 state->tiles = snewn(state->n, int);
470 for (i = 0; i < state->n; i++) {
472 state->tiles[i] = atoi(p);
473 while (*p && *p != ',')
475 if (*p) p++; /* eat comma */
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;
487 static game_state *dup_game(game_state *state)
489 game_state *ret = snew(game_state);
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;
506 static void free_game(game_state *state)
512 static game_state *solve_game(game_state *state, game_aux_info *aux,
515 game_state *ret = dup_game(state);
519 * Simply replace the grid with a solved one. For this game,
520 * this isn't a useful operation for actually telling the user
521 * what they should have done, but it is useful for
522 * conveniently being able to get hold of a clean state from
523 * which to practise manoeuvres.
525 for (i = 0; i < ret->n; i++)
527 ret->used_solve = ret->just_used_solve = TRUE;
528 ret->completed = ret->movecount = 1;
533 static char *game_text_format(game_state *state)
535 char *ret, *p, buf[80];
536 int x, y, col, maxlen;
539 * First work out how many characters we need to display each
542 col = sprintf(buf, "%d", state->n);
545 * Now we know the exact total size of the grid we're going to
546 * produce: it's got h rows, each containing w lots of col, w-1
547 * spaces and a trailing newline.
549 maxlen = state->h * state->w * (col+1);
551 ret = snewn(maxlen+1, char);
554 for (y = 0; y < state->h; y++) {
555 for (x = 0; x < state->w; x++) {
556 int v = state->tiles[state->w*y+x];
557 sprintf(buf, "%*d", col, v);
567 assert(p - ret == maxlen);
572 static game_ui *new_ui(game_state *state)
577 static void free_ui(game_ui *ui)
581 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
582 int x, int y, int button) {
584 int dx, dy, tx, ty, n;
588 if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
593 if (cx == -1 && cy >= 0 && cy < from->h)
594 n = from->w, dx = +1, dy = 0;
595 else if (cx == from->w && cy >= 0 && cy < from->h)
596 n = from->w, dx = -1, dy = 0;
597 else if (cy == -1 && cx >= 0 && cx < from->w)
598 n = from->h, dy = +1, dx = 0;
599 else if (cy == from->h && cx >= 0 && cx < from->w)
600 n = from->h, dy = -1, dx = 0;
602 return NULL; /* invalid click location */
604 /* reverse direction if right hand button is pressed */
605 if (button == RIGHT_BUTTON)
607 dx = -dx; if (dx) cx = from->w - 1 - cx;
608 dy = -dy; if (dy) cy = from->h - 1 - cy;
611 ret = dup_game(from);
612 ret->just_used_solve = FALSE; /* zero this in a hurry */
617 tx = (cx + dx + from->w) % from->w;
618 ty = (cy + dy + from->h) % from->h;
619 ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
624 ret->last_movement_sense = -(dx+dy);
627 * See if the game has been completed.
629 if (!ret->completed) {
630 ret->completed = ret->movecount;
631 for (n = 0; n < ret->n; n++)
632 if (ret->tiles[n] != n+1)
633 ret->completed = FALSE;
639 /* ----------------------------------------------------------------------
643 struct game_drawstate {
649 static void game_size(game_params *params, int *x, int *y)
651 *x = TILE_SIZE * params->w + 2 * BORDER;
652 *y = TILE_SIZE * params->h + 2 * BORDER;
655 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
657 float *ret = snewn(3 * NCOLOURS, float);
661 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
664 * Drop the background colour so that the highlight is
665 * noticeably brighter than it while still being under 1.
667 max = ret[COL_BACKGROUND*3];
668 for (i = 1; i < 3; i++)
669 if (ret[COL_BACKGROUND*3+i] > max)
670 max = ret[COL_BACKGROUND*3+i];
671 if (max * 1.2F > 1.0F) {
672 for (i = 0; i < 3; i++)
673 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
676 for (i = 0; i < 3; i++) {
677 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
678 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
679 ret[COL_TEXT * 3 + i] = 0.0;
682 *ncolours = NCOLOURS;
686 static game_drawstate *game_new_drawstate(game_state *state)
688 struct game_drawstate *ds = snew(struct game_drawstate);
694 ds->bgcolour = COL_BACKGROUND;
695 ds->tiles = snewn(ds->w*ds->h, int);
696 for (i = 0; i < ds->w*ds->h; i++)
702 static void game_free_drawstate(game_drawstate *ds)
708 static void draw_tile(frontend *fe, game_state *state, int x, int y,
709 int tile, int flash_colour)
712 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
718 coords[0] = x + TILE_SIZE - 1;
719 coords[1] = y + TILE_SIZE - 1;
720 coords[2] = x + TILE_SIZE - 1;
723 coords[5] = y + TILE_SIZE - 1;
724 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
725 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
729 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
730 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
732 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
733 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
736 sprintf(str, "%d", tile);
737 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
738 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
741 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
744 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
747 int ydy = -xdx, ydx = xdy;
749 #define POINT(n, xx, yy) ( \
750 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
751 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
753 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
754 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
755 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
756 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
757 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
758 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
759 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
761 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
762 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
765 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
766 game_state *state, int dir, game_ui *ui,
767 float animtime, float flashtime)
772 int frame = (int)(flashtime / FLASH_FRAME);
773 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
775 bgcolour = COL_BACKGROUND;
781 TILE_SIZE * state->w + 2 * BORDER,
782 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
783 draw_update(fe, 0, 0,
784 TILE_SIZE * state->w + 2 * BORDER,
785 TILE_SIZE * state->h + 2 * BORDER);
788 * Recessed area containing the whole puzzle.
790 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
791 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
792 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
793 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
794 coords[4] = coords[2] - TILE_SIZE;
795 coords[5] = coords[3] + TILE_SIZE;
796 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
797 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
798 coords[6] = coords[8] + TILE_SIZE;
799 coords[7] = coords[9] - TILE_SIZE;
800 draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT);
801 draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT);
803 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
804 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
805 draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT);
806 draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT);
809 * Arrows for making moves.
811 for (i = 0; i < state->w; i++) {
812 draw_arrow(fe, COORD(i), COORD(0), +1, 0);
813 draw_arrow(fe, COORD(i+1), COORD(state->h), -1, 0);
815 for (i = 0; i < state->h; i++) {
816 draw_arrow(fe, COORD(state->w), COORD(i), 0, +1);
817 draw_arrow(fe, COORD(0), COORD(i+1), 0, -1);
824 * Now draw each tile.
827 clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
829 for (i = 0; i < state->n; i++) {
832 * Figure out what should be displayed at this
833 * location. It's either a simple tile, or it's a
834 * transition between two tiles (in which case we say
835 * -1 because it must always be drawn).
838 if (oldstate && oldstate->tiles[i] != state->tiles[i])
845 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
846 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
850 * Figure out what to _actually_ draw, and where to
854 int x0, y0, x1, y1, dx, dy;
861 sense = -oldstate->last_movement_sense;
863 sense = state->last_movement_sense;
869 * FIXME: must be prepared to draw a double
870 * tile in some situations.
874 * Find the coordinates of this tile in the old and
877 x1 = COORD(X(state, i));
878 y1 = COORD(Y(state, i));
879 for (j = 0; j < oldstate->n; j++)
880 if (oldstate->tiles[j] == state->tiles[i])
882 assert(j < oldstate->n);
883 x0 = COORD(X(state, j));
884 y0 = COORD(Y(state, j));
888 dx != TILE_SIZE * sense) {
889 dx = (dx < 0 ? dx + TILE_SIZE * state->w :
890 dx - TILE_SIZE * state->w);
891 assert(abs(dx) == TILE_SIZE);
895 dy != TILE_SIZE * sense) {
896 dy = (dy < 0 ? dy + TILE_SIZE * state->h :
897 dy - TILE_SIZE * state->h);
898 assert(abs(dy) == TILE_SIZE);
901 c = (animtime / ANIM_TIME);
902 if (c < 0.0F) c = 0.0F;
903 if (c > 1.0F) c = 1.0F;
905 x = x0 + (int)(c * dx);
906 y = y0 + (int)(c * dy);
907 x2 = x1 - dx + (int)(c * dx);
908 y2 = y1 - dy + (int)(c * dy);
910 x = COORD(X(state, i));
911 y = COORD(Y(state, i));
915 draw_tile(fe, state, x, y, t, bgcolour);
916 if (x2 != -1 || y2 != -1)
917 draw_tile(fe, state, x2, y2, t, bgcolour);
924 ds->bgcolour = bgcolour;
927 * Update the status bar.
933 * Don't show the new status until we're also showing the
934 * new _state_ - after the game animation is complete.
939 if (state->used_solve)
940 sprintf(statusbuf, "Moves since auto-solve: %d",
941 state->movecount - state->completed);
943 sprintf(statusbuf, "%sMoves: %d",
944 (state->completed ? "COMPLETED! " : ""),
945 (state->completed ? state->completed : state->movecount));
946 if (state->movetarget)
947 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
951 status_bar(fe, statusbuf);
955 static float game_anim_length(game_state *oldstate,
956 game_state *newstate, int dir, game_ui *ui)
958 if ((dir > 0 && newstate->just_used_solve) ||
959 (dir < 0 && oldstate->just_used_solve))
965 static float game_flash_length(game_state *oldstate,
966 game_state *newstate, int dir, game_ui *ui)
968 if (!oldstate->completed && newstate->completed &&
969 !oldstate->used_solve && !newstate->used_solve)
970 return 2 * FLASH_FRAME;
975 static int game_wants_statusbar(void)
980 static int game_timing_state(game_state *state)
986 #define thegame sixteen
989 const struct game thegame = {
990 "Sixteen", "games.sixteen",
997 TRUE, game_configure, custom_params,
1006 TRUE, game_text_format,
1013 game_free_drawstate,
1017 game_wants_statusbar,
1018 FALSE, game_timing_state,
1019 0, /* mouse_priorities */