2 * fifteen.c: standard 15-puzzle.
14 const char *const game_name = "Fifteen";
15 const char *const game_winhelp_topic = "games.fifteen";
16 const int game_can_configure = TRUE;
19 #define BORDER (TILE_SIZE / 2)
20 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
21 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
22 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
24 #define ANIM_TIME 0.13F
25 #define FLASH_FRAME 0.13F
27 #define X(state, i) ( (i) % (state)->w )
28 #define Y(state, i) ( (i) / (state)->w )
29 #define C(state, x, y) ( (y) * (state)->w + (x) )
51 game_params *default_params(void)
53 game_params *ret = snew(game_params);
60 int game_fetch_preset(int i, char **name, game_params **params)
65 void free_params(game_params *params)
70 game_params *dup_params(game_params *params)
72 game_params *ret = snew(game_params);
73 *ret = *params; /* structure copy */
77 game_params *decode_params(char const *string)
79 game_params *ret = default_params();
81 ret->w = ret->h = atoi(string);
82 while (*string && isdigit(*string)) string++;
85 ret->h = atoi(string);
91 char *encode_params(game_params *params)
95 sprintf(data, "%dx%d", params->w, params->h);
100 config_item *game_configure(game_params *params)
105 ret = snewn(3, config_item);
107 ret[0].name = "Width";
108 ret[0].type = C_STRING;
109 sprintf(buf, "%d", params->w);
110 ret[0].sval = dupstr(buf);
113 ret[1].name = "Height";
114 ret[1].type = C_STRING;
115 sprintf(buf, "%d", params->h);
116 ret[1].sval = dupstr(buf);
127 game_params *custom_params(config_item *cfg)
129 game_params *ret = snew(game_params);
131 ret->w = atoi(cfg[0].sval);
132 ret->h = atoi(cfg[1].sval);
137 char *validate_params(game_params *params)
139 if (params->w < 2 && params->h < 2)
140 return "Width and height must both be at least two";
145 int perm_parity(int *perm, int n)
151 for (i = 0; i < n-1; i++)
152 for (j = i+1; j < n; j++)
153 if (perm[i] > perm[j])
159 char *new_game_seed(game_params *params, random_state *rs)
162 int x1, x2, p1, p2, parity;
167 n = params->w * params->h;
169 tiles = snewn(n, int);
170 used = snewn(n, int);
172 for (i = 0; i < n; i++) {
177 gap = random_upto(rs, n);
182 * Place everything else except the last two tiles.
184 for (x = 0, i = n-1; i > 2; i--) {
185 int k = random_upto(rs, i);
188 for (j = 0; j < n; j++)
189 if (!used[j] && (k-- == 0))
192 assert(j < n && !used[j]);
195 while (tiles[x] >= 0)
202 * Find the last two locations, and the last two pieces.
204 while (tiles[x] >= 0)
209 while (tiles[x] >= 0)
214 for (i = 0; i < n; i++)
218 for (i = p1+1; i < n; i++)
224 * Determine the required parity of the overall permutation.
225 * This is the XOR of:
227 * - The chessboard parity ((x^y)&1) of the gap square. The
228 * bottom right counts as even.
230 * - The parity of n. (The target permutation is 1,...,n-1,0
231 * rather than 0,...,n-1; this is a cyclic permutation of
232 * the starting point and hence is odd iff n is even.)
234 parity = ((X(params, gap) - (params->w-1)) ^
235 (Y(params, gap) - (params->h-1)) ^
239 * Try the last two tiles one way round. If that fails, swap
244 if (perm_parity(tiles, n) != parity) {
247 assert(perm_parity(tiles, n) == parity);
251 * Now construct the game seed, by describing the tile array as
252 * a simple sequence of comma-separated integers.
256 for (i = 0; i < n; i++) {
260 k = sprintf(buf, "%d,", tiles[i]);
262 ret = sresize(ret, retlen + k + 1, char);
263 strcpy(ret + retlen, buf);
266 ret[retlen-1] = '\0'; /* delete last comma */
274 char *validate_seed(game_params *params, char *seed)
280 area = params->w * params->h;
284 used = snewn(area, int);
285 for (i = 0; i < area; i++)
288 for (i = 0; i < area; i++) {
292 if (*p < '0' || *p > '9') {
293 err = "Not enough numbers in string";
296 while (*p >= '0' && *p <= '9')
298 if (i < area-1 && *p != ',') {
299 err = "Expected comma after number";
302 else if (i == area-1 && *p) {
303 err = "Excess junk at end of string";
307 if (n < 0 || n >= area) {
308 err = "Number out of range";
312 err = "Number used twice";
317 if (*p) p++; /* eat comma */
325 game_state *new_game(game_params *params, char *seed)
327 game_state *state = snew(game_state);
331 state->w = params->w;
332 state->h = params->h;
333 state->n = params->w * params->h;
334 state->tiles = snewn(state->n, int);
339 for (i = 0; i < state->n; i++) {
341 state->tiles[i] = atoi(p);
342 if (state->tiles[i] == 0)
344 while (*p && *p != ',')
346 if (*p) p++; /* eat comma */
349 assert(state->tiles[state->gap_pos] == 0);
351 state->completed = state->movecount = 0;
356 game_state *dup_game(game_state *state)
358 game_state *ret = snew(game_state);
363 ret->tiles = snewn(state->w * state->h, int);
364 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
365 ret->gap_pos = state->gap_pos;
366 ret->completed = state->completed;
367 ret->movecount = state->movecount;
372 void free_game(game_state *state)
377 game_ui *new_ui(game_state *state)
382 void free_ui(game_ui *ui)
386 game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
388 int gx, gy, dx, dy, ux, uy, up, p;
391 gx = X(from, from->gap_pos);
392 gy = Y(from, from->gap_pos);
394 if (button == CURSOR_RIGHT && gx > 0)
395 dx = gx - 1, dy = gy;
396 else if (button == CURSOR_LEFT && gx < from->w-1)
397 dx = gx + 1, dy = gy;
398 else if (button == CURSOR_DOWN && gy > 0)
399 dy = gy - 1, dx = gx;
400 else if (button == CURSOR_UP && gy < from->h-1)
401 dy = gy + 1, dx = gx;
402 else if (button == LEFT_BUTTON) {
405 if (dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
406 return NULL; /* out of bounds */
408 * Any click location should be equal to the gap location
409 * in _precisely_ one coordinate.
411 if ((dx == gx && dy == gy) || (dx != gx && dy != gy))
414 return NULL; /* no move */
417 * Find the unit displacement from the original gap
418 * position towards this one.
420 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
421 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
422 up = C(from, ux, uy);
424 ret = dup_game(from);
426 ret->gap_pos = C(from, dx, dy);
427 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
429 ret->tiles[ret->gap_pos] = 0;
431 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
432 assert(p >= 0 && p < from->n);
433 ret->tiles[p] = from->tiles[p + up];
438 * See if the game has been completed.
440 if (!ret->completed) {
441 ret->completed = ret->movecount;
442 for (p = 0; p < ret->n; p++)
443 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
450 /* ----------------------------------------------------------------------
454 struct game_drawstate {
460 void game_size(game_params *params, int *x, int *y)
462 *x = TILE_SIZE * params->w + 2 * BORDER;
463 *y = TILE_SIZE * params->h + 2 * BORDER;
466 float *game_colours(frontend *fe, game_state *state, int *ncolours)
468 float *ret = snewn(3 * NCOLOURS, float);
472 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
475 * Drop the background colour so that the highlight is
476 * noticeably brighter than it while still being under 1.
478 max = ret[COL_BACKGROUND*3];
479 for (i = 1; i < 3; i++)
480 if (ret[COL_BACKGROUND*3+i] > max)
481 max = ret[COL_BACKGROUND*3+i];
482 if (max * 1.2F > 1.0F) {
483 for (i = 0; i < 3; i++)
484 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
487 for (i = 0; i < 3; i++) {
488 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
489 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
490 ret[COL_TEXT * 3 + i] = 0.0;
493 *ncolours = NCOLOURS;
497 game_drawstate *game_new_drawstate(game_state *state)
499 struct game_drawstate *ds = snew(struct game_drawstate);
505 ds->bgcolour = COL_BACKGROUND;
506 ds->tiles = snewn(ds->w*ds->h, int);
507 for (i = 0; i < ds->w*ds->h; i++)
513 void game_free_drawstate(game_drawstate *ds)
519 static void draw_tile(frontend *fe, game_state *state, int x, int y,
520 int tile, int flash_colour)
523 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
529 coords[0] = x + TILE_SIZE - 1;
530 coords[1] = y + TILE_SIZE - 1;
531 coords[2] = x + TILE_SIZE - 1;
534 coords[5] = y + TILE_SIZE - 1;
535 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
536 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
540 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
541 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
543 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
544 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
547 sprintf(str, "%d", tile);
548 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
549 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
552 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
555 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
556 game_state *state, game_ui *ui,
557 float animtime, float flashtime)
559 int i, pass, bgcolour;
562 int frame = (int)(flashtime / FLASH_FRAME);
563 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
565 bgcolour = COL_BACKGROUND;
571 TILE_SIZE * state->w + 2 * BORDER,
572 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
573 draw_update(fe, 0, 0,
574 TILE_SIZE * state->w + 2 * BORDER,
575 TILE_SIZE * state->h + 2 * BORDER);
578 * Recessed area containing the whole puzzle.
580 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
581 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
582 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
583 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
584 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
585 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
586 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
587 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
589 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
590 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
591 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
592 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
598 * Now draw each tile. We do this in two passes to make
601 for (pass = 0; pass < 2; pass++) {
602 for (i = 0; i < state->n; i++) {
605 * Figure out what should be displayed at this
606 * location. It's either a simple tile, or it's a
607 * transition between two tiles (in which case we say
608 * -1 because it must always be drawn).
611 if (oldstate && oldstate->tiles[i] != state->tiles[i])
618 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
619 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
623 * Figure out what to _actually_ draw, and where to
631 * On the first pass, just blank the tile.
634 x = COORD(X(state, i));
635 y = COORD(Y(state, i));
643 * Don't bother moving the gap; just don't
650 * Find the coordinates of this tile in the old and
653 x1 = COORD(X(state, i));
654 y1 = COORD(Y(state, i));
655 for (j = 0; j < oldstate->n; j++)
656 if (oldstate->tiles[j] == state->tiles[i])
658 assert(j < oldstate->n);
659 x0 = COORD(X(state, j));
660 y0 = COORD(Y(state, j));
662 c = (animtime / ANIM_TIME);
663 if (c < 0.0F) c = 0.0F;
664 if (c > 1.0F) c = 1.0F;
666 x = x0 + (int)(c * (x1 - x0));
667 y = y0 + (int)(c * (y1 - y0));
673 x = COORD(X(state, i));
674 y = COORD(Y(state, i));
677 draw_tile(fe, state, x, y, t, bgcolour);
682 ds->bgcolour = bgcolour;
685 * Update the status bar.
691 * Don't show the new status until we're also showing the
692 * new _state_ - after the game animation is complete.
697 sprintf(statusbuf, "%sMoves: %d",
698 (state->completed ? "COMPLETED! " : ""),
699 (state->completed ? state->completed : state->movecount));
701 status_bar(fe, statusbuf);
705 float game_anim_length(game_state *oldstate, game_state *newstate)
710 float game_flash_length(game_state *oldstate, game_state *newstate)
712 if (!oldstate->completed && newstate->completed)
713 return 2 * FLASH_FRAME;
718 int game_wants_statusbar(void)