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
16 const char *const game_name = "Sixteen";
17 const int game_can_configure = TRUE;
20 #define BORDER TILE_SIZE /* big border to fill with arrows */
21 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
22 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
23 #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
25 #define ANIM_TIME 0.13F
26 #define FLASH_FRAME 0.13F
28 #define X(state, i) ( (i) % (state)->w )
29 #define Y(state, i) ( (i) / (state)->w )
30 #define C(state, x, y) ( (y) * (state)->w + (x) )
49 int last_movement_sense;
52 game_params *default_params(void)
54 game_params *ret = snew(game_params);
61 int game_fetch_preset(int i, char **name, game_params **params)
68 case 0: w = 3, h = 3; break;
69 case 1: w = 4, h = 3; break;
70 case 2: w = 4, h = 4; break;
71 case 3: w = 5, h = 4; break;
72 case 4: w = 5, h = 5; break;
73 default: return FALSE;
76 sprintf(buf, "%dx%d", w, h);
78 *params = ret = snew(game_params);
84 void free_params(game_params *params)
89 game_params *dup_params(game_params *params)
91 game_params *ret = snew(game_params);
92 *ret = *params; /* structure copy */
96 game_params *decode_params(char const *string)
98 game_params *ret = default_params();
100 ret->w = ret->h = atoi(string);
101 while (*string && isdigit(*string)) string++;
102 if (*string == 'x') {
104 ret->h = atoi(string);
110 char *encode_params(game_params *params)
114 sprintf(data, "%dx%d", params->w, params->h);
119 config_item *game_configure(game_params *params)
124 ret = snewn(3, config_item);
126 ret[0].name = "Width";
127 ret[0].type = C_STRING;
128 sprintf(buf, "%d", params->w);
129 ret[0].sval = dupstr(buf);
132 ret[1].name = "Height";
133 ret[1].type = C_STRING;
134 sprintf(buf, "%d", params->h);
135 ret[1].sval = dupstr(buf);
146 game_params *custom_params(config_item *cfg)
148 game_params *ret = snew(game_params);
150 ret->w = atoi(cfg[0].sval);
151 ret->h = atoi(cfg[1].sval);
156 char *validate_params(game_params *params)
158 if (params->w < 2 && params->h < 2)
159 return "Width and height must both be at least two";
164 int perm_parity(int *perm, int n)
170 for (i = 0; i < n-1; i++)
171 for (j = i+1; j < n; j++)
172 if (perm[i] > perm[j])
178 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 */
285 char *validate_seed(game_params *params, char *seed)
291 area = params->w * params->h;
295 used = snewn(area, int);
296 for (i = 0; i < area; i++)
299 for (i = 0; i < area; i++) {
303 if (*p < '0' || *p > '9') {
304 err = "Not enough numbers in string";
307 while (*p >= '0' && *p <= '9')
309 if (i < area-1 && *p != ',') {
310 err = "Expected comma after number";
313 else if (i == area-1 && *p) {
314 err = "Excess junk at end of string";
318 if (n < 1 || n > area) {
319 err = "Number out of range";
323 err = "Number used twice";
328 if (*p) p++; /* eat comma */
336 game_state *new_game(game_params *params, char *seed)
338 game_state *state = snew(game_state);
342 state->w = params->w;
343 state->h = params->h;
344 state->n = params->w * params->h;
345 state->tiles = snewn(state->n, int);
349 for (i = 0; i < state->n; i++) {
351 state->tiles[i] = atoi(p);
352 while (*p && *p != ',')
354 if (*p) p++; /* eat comma */
358 state->completed = state->movecount = 0;
359 state->last_movement_sense = 0;
364 game_state *dup_game(game_state *state)
366 game_state *ret = snew(game_state);
371 ret->tiles = snewn(state->w * state->h, int);
372 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
373 ret->completed = state->completed;
374 ret->movecount = state->movecount;
375 ret->last_movement_sense = state->last_movement_sense;
380 void free_game(game_state *state)
385 game_ui *new_ui(game_state *state)
390 void free_ui(game_ui *ui)
394 game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
397 int dx, dy, tx, ty, n;
400 if (button != LEFT_BUTTON)
405 if (cx == -1 && cy >= 0 && cy < from->h)
406 n = from->w, dx = +1, dy = 0;
407 else if (cx == from->w && cy >= 0 && cy < from->h)
408 n = from->w, dx = -1, dy = 0;
409 else if (cy == -1 && cx >= 0 && cx < from->w)
410 n = from->h, dy = +1, dx = 0;
411 else if (cy == from->h && cx >= 0 && cx < from->w)
412 n = from->h, dy = -1, dx = 0;
414 return NULL; /* invalid click location */
416 ret = dup_game(from);
421 tx = (cx + dx + from->w) % from->w;
422 ty = (cy + dy + from->h) % from->h;
423 ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
428 ret->last_movement_sense = -(dx+dy);
431 * See if the game has been completed.
433 if (!ret->completed) {
434 ret->completed = ret->movecount;
435 for (n = 0; n < ret->n; n++)
436 if (ret->tiles[n] != n+1)
437 ret->completed = FALSE;
443 /* ----------------------------------------------------------------------
447 struct game_drawstate {
453 void game_size(game_params *params, int *x, int *y)
455 *x = TILE_SIZE * params->w + 2 * BORDER;
456 *y = TILE_SIZE * params->h + 2 * BORDER;
459 float *game_colours(frontend *fe, game_state *state, int *ncolours)
461 float *ret = snewn(3 * NCOLOURS, float);
465 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
468 * Drop the background colour so that the highlight is
469 * noticeably brighter than it while still being under 1.
471 max = ret[COL_BACKGROUND*3];
472 for (i = 1; i < 3; i++)
473 if (ret[COL_BACKGROUND*3+i] > max)
474 max = ret[COL_BACKGROUND*3+i];
475 if (max * 1.2F > 1.0F) {
476 for (i = 0; i < 3; i++)
477 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
480 for (i = 0; i < 3; i++) {
481 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
482 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
483 ret[COL_TEXT * 3 + i] = 0.0;
486 *ncolours = NCOLOURS;
490 game_drawstate *game_new_drawstate(game_state *state)
492 struct game_drawstate *ds = snew(struct game_drawstate);
498 ds->bgcolour = COL_BACKGROUND;
499 ds->tiles = snewn(ds->w*ds->h, int);
500 for (i = 0; i < ds->w*ds->h; i++)
506 void game_free_drawstate(game_drawstate *ds)
512 static void draw_tile(frontend *fe, game_state *state, int x, int y,
513 int tile, int flash_colour)
516 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
522 coords[0] = x + TILE_SIZE - 1;
523 coords[1] = y + TILE_SIZE - 1;
524 coords[2] = x + TILE_SIZE - 1;
527 coords[5] = y + TILE_SIZE - 1;
528 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
529 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
533 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
534 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
536 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
537 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
540 sprintf(str, "%d", tile);
541 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
542 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
545 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
548 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
551 int ydy = -xdx, ydx = xdy;
553 #define POINT(n, xx, yy) ( \
554 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
555 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
557 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
558 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
559 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
560 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
561 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
562 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
563 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
565 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
566 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
569 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
570 game_state *state, game_ui *ui,
571 float animtime, float flashtime)
576 int frame = (int)(flashtime / FLASH_FRAME);
577 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
579 bgcolour = COL_BACKGROUND;
585 TILE_SIZE * state->w + 2 * BORDER,
586 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
587 draw_update(fe, 0, 0,
588 TILE_SIZE * state->w + 2 * BORDER,
589 TILE_SIZE * state->h + 2 * BORDER);
592 * Recessed area containing the whole puzzle.
594 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
595 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
596 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
597 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
598 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
599 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
600 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
601 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
603 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
604 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
605 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
606 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
609 * Arrows for making moves.
611 for (i = 0; i < state->w; i++) {
612 draw_arrow(fe, COORD(i), COORD(0), +1, 0);
613 draw_arrow(fe, COORD(i+1), COORD(state->h), -1, 0);
615 for (i = 0; i < state->h; i++) {
616 draw_arrow(fe, COORD(state->w), COORD(i), 0, +1);
617 draw_arrow(fe, COORD(0), COORD(i+1), 0, -1);
624 * Now draw each tile.
627 clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
629 for (i = 0; i < state->n; i++) {
632 * Figure out what should be displayed at this
633 * location. It's either a simple tile, or it's a
634 * transition between two tiles (in which case we say
635 * -1 because it must always be drawn).
638 if (oldstate && oldstate->tiles[i] != state->tiles[i])
645 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
646 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
650 * Figure out what to _actually_ draw, and where to
654 int x0, y0, x1, y1, dx, dy;
659 if (oldstate && state->movecount < oldstate->movecount)
660 sense = -oldstate->last_movement_sense;
662 sense = state->last_movement_sense;
667 * FIXME: must be prepared to draw a double
668 * tile in some situations.
672 * Find the coordinates of this tile in the old and
675 x1 = COORD(X(state, i));
676 y1 = COORD(Y(state, i));
677 for (j = 0; j < oldstate->n; j++)
678 if (oldstate->tiles[j] == state->tiles[i])
680 assert(j < oldstate->n);
681 x0 = COORD(X(state, j));
682 y0 = COORD(Y(state, j));
686 dx != TILE_SIZE * sense) {
687 dx = (dx < 0 ? dx + TILE_SIZE * state->w :
688 dx - TILE_SIZE * state->w);
689 assert(abs(dx) == TILE_SIZE);
693 dy != TILE_SIZE * sense) {
694 dy = (dy < 0 ? dy + TILE_SIZE * state->h :
695 dy - TILE_SIZE * state->h);
696 assert(abs(dy) == TILE_SIZE);
699 c = (animtime / ANIM_TIME);
700 if (c < 0.0F) c = 0.0F;
701 if (c > 1.0F) c = 1.0F;
703 x = x0 + (int)(c * dx);
704 y = y0 + (int)(c * dy);
705 x2 = x1 - dx + (int)(c * dx);
706 y2 = y1 - dy + (int)(c * dy);
708 x = COORD(X(state, i));
709 y = COORD(Y(state, i));
713 draw_tile(fe, state, x, y, t, bgcolour);
714 if (x2 != -1 || y2 != -1)
715 draw_tile(fe, state, x2, y2, t, bgcolour);
722 ds->bgcolour = bgcolour;
725 * Update the status bar.
731 * Don't show the new status until we're also showing the
732 * new _state_ - after the game animation is complete.
737 sprintf(statusbuf, "%sMoves: %d",
738 (state->completed ? "COMPLETED! " : ""),
739 (state->completed ? state->completed : state->movecount));
741 status_bar(fe, statusbuf);
745 float game_anim_length(game_state *oldstate, game_state *newstate)
750 float game_flash_length(game_state *oldstate, game_state *newstate)
752 if (!oldstate->completed && newstate->completed)
753 return 2 * FLASH_FRAME;
758 int game_wants_statusbar(void)