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) )
47 static game_params *default_params(void)
49 game_params *ret = snew(game_params);
56 static int game_fetch_preset(int i, char **name, game_params **params)
61 static void free_params(game_params *params)
66 static game_params *dup_params(game_params *params)
68 game_params *ret = snew(game_params);
69 *ret = *params; /* structure copy */
73 static game_params *decode_params(char const *string)
75 game_params *ret = default_params();
77 ret->w = ret->h = atoi(string);
78 while (*string && isdigit(*string)) string++;
81 ret->h = atoi(string);
87 static char *encode_params(game_params *params)
91 sprintf(data, "%dx%d", params->w, params->h);
96 static config_item *game_configure(game_params *params)
101 ret = snewn(3, config_item);
103 ret[0].name = "Width";
104 ret[0].type = C_STRING;
105 sprintf(buf, "%d", params->w);
106 ret[0].sval = dupstr(buf);
109 ret[1].name = "Height";
110 ret[1].type = C_STRING;
111 sprintf(buf, "%d", params->h);
112 ret[1].sval = dupstr(buf);
123 static game_params *custom_params(config_item *cfg)
125 game_params *ret = snew(game_params);
127 ret->w = atoi(cfg[0].sval);
128 ret->h = atoi(cfg[1].sval);
133 static char *validate_params(game_params *params)
135 if (params->w < 2 && params->h < 2)
136 return "Width and height must both be at least two";
141 static int perm_parity(int *perm, int n)
147 for (i = 0; i < n-1; i++)
148 for (j = i+1; j < n; j++)
149 if (perm[i] > perm[j])
155 static char *new_game_seed(game_params *params, random_state *rs,
159 int x1, x2, p1, p2, parity;
164 n = params->w * params->h;
166 tiles = snewn(n, int);
167 used = snewn(n, int);
169 for (i = 0; i < n; i++) {
174 gap = random_upto(rs, n);
179 * Place everything else except the last two tiles.
181 for (x = 0, i = n-1; i > 2; i--) {
182 int k = random_upto(rs, i);
185 for (j = 0; j < n; j++)
186 if (!used[j] && (k-- == 0))
189 assert(j < n && !used[j]);
192 while (tiles[x] >= 0)
199 * Find the last two locations, and the last two pieces.
201 while (tiles[x] >= 0)
206 while (tiles[x] >= 0)
211 for (i = 0; i < n; i++)
215 for (i = p1+1; i < n; i++)
221 * Determine the required parity of the overall permutation.
222 * This is the XOR of:
224 * - The chessboard parity ((x^y)&1) of the gap square. The
225 * bottom right counts as even.
227 * - The parity of n. (The target permutation is 1,...,n-1,0
228 * rather than 0,...,n-1; this is a cyclic permutation of
229 * the starting point and hence is odd iff n is even.)
231 parity = ((X(params, gap) - (params->w-1)) ^
232 (Y(params, gap) - (params->h-1)) ^
236 * Try the last two tiles one way round. If that fails, swap
241 if (perm_parity(tiles, n) != parity) {
244 assert(perm_parity(tiles, n) == parity);
248 * Now construct the game seed, by describing the tile array as
249 * a simple sequence of comma-separated integers.
253 for (i = 0; i < n; i++) {
257 k = sprintf(buf, "%d,", tiles[i]);
259 ret = sresize(ret, retlen + k + 1, char);
260 strcpy(ret + retlen, buf);
263 ret[retlen-1] = '\0'; /* delete last comma */
271 void game_free_aux_info(game_aux_info *aux)
273 assert(!"Shouldn't happen");
276 static char *validate_seed(game_params *params, char *seed)
282 area = params->w * params->h;
286 used = snewn(area, int);
287 for (i = 0; i < area; i++)
290 for (i = 0; i < area; i++) {
294 if (*p < '0' || *p > '9') {
295 err = "Not enough numbers in string";
298 while (*p >= '0' && *p <= '9')
300 if (i < area-1 && *p != ',') {
301 err = "Expected comma after number";
304 else if (i == area-1 && *p) {
305 err = "Excess junk at end of string";
309 if (n < 0 || n >= area) {
310 err = "Number out of range";
314 err = "Number used twice";
319 if (*p) p++; /* eat comma */
327 static game_state *new_game(game_params *params, char *seed)
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;
358 static game_state *dup_game(game_state *state)
360 game_state *ret = snew(game_state);
365 ret->tiles = snewn(state->w * state->h, int);
366 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
367 ret->gap_pos = state->gap_pos;
368 ret->completed = state->completed;
369 ret->movecount = state->movecount;
374 static void free_game(game_state *state)
379 static char *game_text_format(game_state *state)
381 char *ret, *p, buf[80];
382 int x, y, col, maxlen;
385 * First work out how many characters we need to display each
388 col = sprintf(buf, "%d", state->n-1);
391 * Now we know the exact total size of the grid we're going to
392 * produce: it's got h rows, each containing w lots of col, w-1
393 * spaces and a trailing newline.
395 maxlen = state->h * state->w * (col+1);
397 ret = snewn(maxlen, char);
400 for (y = 0; y < state->h; y++) {
401 for (x = 0; x < state->w; x++) {
402 int v = state->tiles[state->w*y+x];
404 sprintf(buf, "%*s", col, "");
406 sprintf(buf, "%*d", col, v);
416 assert(p - ret == maxlen);
421 static game_ui *new_ui(game_state *state)
426 static void free_ui(game_ui *ui)
430 static game_state *make_move(game_state *from, game_ui *ui,
431 int x, int y, int button)
433 int gx, gy, dx, dy, ux, uy, up, p;
436 gx = X(from, from->gap_pos);
437 gy = Y(from, from->gap_pos);
439 if (button == CURSOR_RIGHT && gx > 0)
440 dx = gx - 1, dy = gy;
441 else if (button == CURSOR_LEFT && gx < from->w-1)
442 dx = gx + 1, dy = gy;
443 else if (button == CURSOR_DOWN && gy > 0)
444 dy = gy - 1, dx = gx;
445 else if (button == CURSOR_UP && gy < from->h-1)
446 dy = gy + 1, dx = gx;
447 else if (button == LEFT_BUTTON) {
450 if (dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
451 return NULL; /* out of bounds */
453 * Any click location should be equal to the gap location
454 * in _precisely_ one coordinate.
456 if ((dx == gx && dy == gy) || (dx != gx && dy != gy))
459 return NULL; /* no move */
462 * Find the unit displacement from the original gap
463 * position towards this one.
465 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
466 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
467 up = C(from, ux, uy);
469 ret = dup_game(from);
471 ret->gap_pos = C(from, dx, dy);
472 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
474 ret->tiles[ret->gap_pos] = 0;
476 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
477 assert(p >= 0 && p < from->n);
478 ret->tiles[p] = from->tiles[p + up];
483 * See if the game has been completed.
485 if (!ret->completed) {
486 ret->completed = ret->movecount;
487 for (p = 0; p < ret->n; p++)
488 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
495 /* ----------------------------------------------------------------------
499 struct game_drawstate {
505 static void game_size(game_params *params, int *x, int *y)
507 *x = TILE_SIZE * params->w + 2 * BORDER;
508 *y = TILE_SIZE * params->h + 2 * BORDER;
511 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
513 float *ret = snewn(3 * NCOLOURS, float);
517 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
520 * Drop the background colour so that the highlight is
521 * noticeably brighter than it while still being under 1.
523 max = ret[COL_BACKGROUND*3];
524 for (i = 1; i < 3; i++)
525 if (ret[COL_BACKGROUND*3+i] > max)
526 max = ret[COL_BACKGROUND*3+i];
527 if (max * 1.2F > 1.0F) {
528 for (i = 0; i < 3; i++)
529 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
532 for (i = 0; i < 3; i++) {
533 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
534 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
535 ret[COL_TEXT * 3 + i] = 0.0;
538 *ncolours = NCOLOURS;
542 static game_drawstate *game_new_drawstate(game_state *state)
544 struct game_drawstate *ds = snew(struct game_drawstate);
550 ds->bgcolour = COL_BACKGROUND;
551 ds->tiles = snewn(ds->w*ds->h, int);
552 for (i = 0; i < ds->w*ds->h; i++)
558 static void game_free_drawstate(game_drawstate *ds)
564 static void draw_tile(frontend *fe, game_state *state, int x, int y,
565 int tile, int flash_colour)
568 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
574 coords[0] = x + TILE_SIZE - 1;
575 coords[1] = y + TILE_SIZE - 1;
576 coords[2] = x + TILE_SIZE - 1;
579 coords[5] = y + TILE_SIZE - 1;
580 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
581 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
585 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
586 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
588 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
589 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
592 sprintf(str, "%d", tile);
593 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
594 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
597 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
600 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
601 game_state *state, int dir, game_ui *ui,
602 float animtime, float flashtime)
604 int i, pass, bgcolour;
607 int frame = (int)(flashtime / FLASH_FRAME);
608 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
610 bgcolour = COL_BACKGROUND;
616 TILE_SIZE * state->w + 2 * BORDER,
617 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
618 draw_update(fe, 0, 0,
619 TILE_SIZE * state->w + 2 * BORDER,
620 TILE_SIZE * state->h + 2 * BORDER);
623 * Recessed area containing the whole puzzle.
625 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
626 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
627 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
628 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
629 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
630 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
631 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
632 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
634 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
635 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
636 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
637 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
643 * Now draw each tile. We do this in two passes to make
646 for (pass = 0; pass < 2; pass++) {
647 for (i = 0; i < state->n; i++) {
650 * Figure out what should be displayed at this
651 * location. It's either a simple tile, or it's a
652 * transition between two tiles (in which case we say
653 * -1 because it must always be drawn).
656 if (oldstate && oldstate->tiles[i] != state->tiles[i])
663 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
664 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
668 * Figure out what to _actually_ draw, and where to
676 * On the first pass, just blank the tile.
679 x = COORD(X(state, i));
680 y = COORD(Y(state, i));
688 * Don't bother moving the gap; just don't
695 * Find the coordinates of this tile in the old and
698 x1 = COORD(X(state, i));
699 y1 = COORD(Y(state, i));
700 for (j = 0; j < oldstate->n; j++)
701 if (oldstate->tiles[j] == state->tiles[i])
703 assert(j < oldstate->n);
704 x0 = COORD(X(state, j));
705 y0 = COORD(Y(state, j));
707 c = (animtime / ANIM_TIME);
708 if (c < 0.0F) c = 0.0F;
709 if (c > 1.0F) c = 1.0F;
711 x = x0 + (int)(c * (x1 - x0));
712 y = y0 + (int)(c * (y1 - y0));
718 x = COORD(X(state, i));
719 y = COORD(Y(state, i));
722 draw_tile(fe, state, x, y, t, bgcolour);
727 ds->bgcolour = bgcolour;
730 * Update the status bar.
736 * Don't show the new status until we're also showing the
737 * new _state_ - after the game animation is complete.
742 sprintf(statusbuf, "%sMoves: %d",
743 (state->completed ? "COMPLETED! " : ""),
744 (state->completed ? state->completed : state->movecount));
746 status_bar(fe, statusbuf);
750 static float game_anim_length(game_state *oldstate,
751 game_state *newstate, int dir)
756 static float game_flash_length(game_state *oldstate,
757 game_state *newstate, int dir)
759 if (!oldstate->completed && newstate->completed)
760 return 2 * FLASH_FRAME;
765 static int game_wants_statusbar(void)
771 #define thegame fifteen
774 const struct game thegame = {
775 "Fifteen", "games.fifteen",
782 TRUE, game_configure, custom_params,
790 TRUE, game_text_format,
801 game_wants_statusbar,