2 * fifteen.c: standard 15-puzzle.
14 #define PREFERRED_TILE_SIZE 48
15 #define TILE_SIZE (ds->tilesize)
16 #define BORDER (TILE_SIZE / 2)
17 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
18 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
19 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
21 #define ANIM_TIME 0.13F
22 #define FLASH_FRAME 0.13F
24 #define X(state, i) ( (i) % (state)->w )
25 #define Y(state, i) ( (i) / (state)->w )
26 #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 */
50 static game_params
*default_params(void)
52 game_params
*ret
= snew(game_params
);
59 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
64 static void free_params(game_params
*params
)
69 static game_params
*dup_params(game_params
*params
)
71 game_params
*ret
= snew(game_params
);
72 *ret
= *params
; /* structure copy */
76 static void decode_params(game_params
*ret
, char const *string
)
78 ret
->w
= ret
->h
= atoi(string
);
79 while (*string
&& isdigit((unsigned char)*string
)) string
++;
82 ret
->h
= atoi(string
);
86 static char *encode_params(game_params
*params
, int full
)
90 sprintf(data
, "%dx%d", params
->w
, params
->h
);
95 static config_item
*game_configure(game_params
*params
)
100 ret
= snewn(3, config_item
);
102 ret
[0].name
= "Width";
103 ret
[0].type
= C_STRING
;
104 sprintf(buf
, "%d", params
->w
);
105 ret
[0].sval
= dupstr(buf
);
108 ret
[1].name
= "Height";
109 ret
[1].type
= C_STRING
;
110 sprintf(buf
, "%d", params
->h
);
111 ret
[1].sval
= dupstr(buf
);
122 static game_params
*custom_params(config_item
*cfg
)
124 game_params
*ret
= snew(game_params
);
126 ret
->w
= atoi(cfg
[0].sval
);
127 ret
->h
= atoi(cfg
[1].sval
);
132 static char *validate_params(game_params
*params
, int full
)
134 if (params
->w
< 2 || params
->h
< 2)
135 return "Width and height must both be at least two";
140 static int perm_parity(int *perm
, int n
)
146 for (i
= 0; i
< n
-1; i
++)
147 for (j
= i
+1; j
< n
; j
++)
148 if (perm
[i
] > perm
[j
])
154 static char *new_game_desc(game_params
*params
, random_state
*rs
,
155 char **aux
, int interactive
)
158 int x1
, x2
, p1
, p2
, parity
;
163 n
= params
->w
* params
->h
;
165 tiles
= snewn(n
, int);
166 used
= snewn(n
, int);
168 for (i
= 0; i
< n
; i
++) {
173 gap
= random_upto(rs
, n
);
178 * Place everything else except the last two tiles.
180 for (x
= 0, i
= n
-1; i
> 2; i
--) {
181 int k
= random_upto(rs
, i
);
184 for (j
= 0; j
< n
; j
++)
185 if (!used
[j
] && (k
-- == 0))
188 assert(j
< n
&& !used
[j
]);
191 while (tiles
[x
] >= 0)
198 * Find the last two locations, and the last two pieces.
200 while (tiles
[x
] >= 0)
205 while (tiles
[x
] >= 0)
210 for (i
= 0; i
< n
; i
++)
214 for (i
= p1
+1; i
< n
; i
++)
220 * Determine the required parity of the overall permutation.
221 * This is the XOR of:
223 * - The chessboard parity ((x^y)&1) of the gap square. The
224 * bottom right counts as even.
226 * - The parity of n. (The target permutation is 1,...,n-1,0
227 * rather than 0,...,n-1; this is a cyclic permutation of
228 * the starting point and hence is odd iff n is even.)
230 parity
= ((X(params
, gap
) - (params
->w
-1)) ^
231 (Y(params
, gap
) - (params
->h
-1)) ^
235 * Try the last two tiles one way round. If that fails, swap
240 if (perm_parity(tiles
, n
) != parity
) {
243 assert(perm_parity(tiles
, n
) == parity
);
247 * Now construct the game description, by describing the tile
248 * array as a simple sequence of comma-separated integers.
252 for (i
= 0; i
< n
; i
++) {
256 k
= sprintf(buf
, "%d,", tiles
[i
]);
258 ret
= sresize(ret
, retlen
+ k
+ 1, char);
259 strcpy(ret
+ retlen
, buf
);
262 ret
[retlen
-1] = '\0'; /* delete last comma */
270 static char *validate_desc(game_params
*params
, char *desc
)
276 area
= params
->w
* params
->h
;
280 used
= snewn(area
, int);
281 for (i
= 0; i
< area
; i
++)
284 for (i
= 0; i
< area
; i
++) {
288 if (*p
< '0' || *p
> '9') {
289 err
= "Not enough numbers in string";
292 while (*p
>= '0' && *p
<= '9')
294 if (i
< area
-1 && *p
!= ',') {
295 err
= "Expected comma after number";
298 else if (i
== area
-1 && *p
) {
299 err
= "Excess junk at end of string";
303 if (n
< 0 || n
>= area
) {
304 err
= "Number out of range";
308 err
= "Number used twice";
313 if (*p
) p
++; /* eat comma */
321 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
323 game_state
*state
= snew(game_state
);
327 state
->w
= params
->w
;
328 state
->h
= params
->h
;
329 state
->n
= params
->w
* params
->h
;
330 state
->tiles
= snewn(state
->n
, int);
335 for (i
= 0; i
< state
->n
; i
++) {
337 state
->tiles
[i
] = atoi(p
);
338 if (state
->tiles
[i
] == 0)
340 while (*p
&& *p
!= ',')
342 if (*p
) p
++; /* eat comma */
345 assert(state
->tiles
[state
->gap_pos
] == 0);
347 state
->completed
= state
->movecount
= 0;
348 state
->used_solve
= state
->just_used_solve
= FALSE
;
353 static game_state
*dup_game(game_state
*state
)
355 game_state
*ret
= snew(game_state
);
360 ret
->tiles
= snewn(state
->w
* state
->h
, int);
361 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
362 ret
->gap_pos
= state
->gap_pos
;
363 ret
->completed
= state
->completed
;
364 ret
->movecount
= state
->movecount
;
365 ret
->used_solve
= state
->used_solve
;
366 ret
->just_used_solve
= state
->just_used_solve
;
371 static void free_game(game_state
*state
)
377 static char *solve_game(game_state
*state
, game_state
*currstate
,
378 char *aux
, char **error
)
383 static char *game_text_format(game_state
*state
)
385 char *ret
, *p
, buf
[80];
386 int x
, y
, col
, maxlen
;
389 * First work out how many characters we need to display each
392 col
= sprintf(buf
, "%d", state
->n
-1);
395 * Now we know the exact total size of the grid we're going to
396 * produce: it's got h rows, each containing w lots of col, w-1
397 * spaces and a trailing newline.
399 maxlen
= state
->h
* state
->w
* (col
+1);
401 ret
= snewn(maxlen
+1, char);
404 for (y
= 0; y
< state
->h
; y
++) {
405 for (x
= 0; x
< state
->w
; x
++) {
406 int v
= state
->tiles
[state
->w
*y
+x
];
408 sprintf(buf
, "%*s", col
, "");
410 sprintf(buf
, "%*d", col
, v
);
420 assert(p
- ret
== maxlen
);
425 static game_ui
*new_ui(game_state
*state
)
430 static void free_ui(game_ui
*ui
)
434 static char *encode_ui(game_ui
*ui
)
439 static void decode_ui(game_ui
*ui
, char *encoding
)
443 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
444 game_state
*newstate
)
448 struct game_drawstate
{
455 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
456 int x
, int y
, int button
)
463 gx
= X(state
, state
->gap_pos
);
464 gy
= Y(state
, state
->gap_pos
);
466 if (button
== CURSOR_RIGHT
&& gx
> 0)
467 dx
= gx
- 1, dy
= gy
;
468 else if (button
== CURSOR_LEFT
&& gx
< state
->w
-1)
469 dx
= gx
+ 1, dy
= gy
;
470 else if (button
== CURSOR_DOWN
&& gy
> 0)
471 dy
= gy
- 1, dx
= gx
;
472 else if (button
== CURSOR_UP
&& gy
< state
->h
-1)
473 dy
= gy
+ 1, dx
= gx
;
474 else if (button
== LEFT_BUTTON
) {
477 if (dx
< 0 || dx
>= state
->w
|| dy
< 0 || dy
>= state
->h
)
478 return NULL
; /* out of bounds */
480 * Any click location should be equal to the gap location
481 * in _precisely_ one coordinate.
483 if ((dx
== gx
&& dy
== gy
) || (dx
!= gx
&& dy
!= gy
))
486 return NULL
; /* no move */
488 sprintf(buf
, "M%d,%d", dx
, dy
);
492 static game_state
*execute_move(game_state
*from
, char *move
)
494 int gx
, gy
, dx
, dy
, ux
, uy
, up
, p
;
497 if (!strcmp(move
, "S")) {
500 ret
= dup_game(from
);
503 * Simply replace the grid with a solved one. For this game,
504 * this isn't a useful operation for actually telling the user
505 * what they should have done, but it is useful for
506 * conveniently being able to get hold of a clean state from
507 * which to practise manoeuvres.
509 for (i
= 0; i
< ret
->n
; i
++)
510 ret
->tiles
[i
] = (i
+1) % ret
->n
;
511 ret
->gap_pos
= ret
->n
-1;
512 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
513 ret
->completed
= ret
->movecount
= 1;
518 gx
= X(from
, from
->gap_pos
);
519 gy
= Y(from
, from
->gap_pos
);
521 if (move
[0] != 'M' ||
522 sscanf(move
+1, "%d,%d", &dx
, &dy
) != 2 ||
523 (dx
== gx
&& dy
== gy
) || (dx
!= gx
&& dy
!= gy
) ||
524 dx
< 0 || dx
>= from
->w
|| dy
< 0 || dy
>= from
->h
)
528 * Find the unit displacement from the original gap
529 * position towards this one.
531 ux
= (dx
< gx ?
-1 : dx
> gx ?
+1 : 0);
532 uy
= (dy
< gy ?
-1 : dy
> gy ?
+1 : 0);
533 up
= C(from
, ux
, uy
);
535 ret
= dup_game(from
);
536 ret
->just_used_solve
= FALSE
; /* zero this in a hurry */
538 ret
->gap_pos
= C(from
, dx
, dy
);
539 assert(ret
->gap_pos
>= 0 && ret
->gap_pos
< ret
->n
);
541 ret
->tiles
[ret
->gap_pos
] = 0;
543 for (p
= from
->gap_pos
; p
!= ret
->gap_pos
; p
+= up
) {
544 assert(p
>= 0 && p
< from
->n
);
545 ret
->tiles
[p
] = from
->tiles
[p
+ up
];
550 * See if the game has been completed.
552 if (!ret
->completed
) {
553 ret
->completed
= ret
->movecount
;
554 for (p
= 0; p
< ret
->n
; p
++)
555 if (ret
->tiles
[p
] != (p
< ret
->n
-1 ? p
+1 : 0))
562 /* ----------------------------------------------------------------------
566 static void game_compute_size(game_params
*params
, int tilesize
,
569 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
570 struct { int tilesize
; } ads
, *ds
= &ads
;
571 ads
.tilesize
= tilesize
;
573 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
574 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
577 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
578 game_params
*params
, int tilesize
)
580 ds
->tilesize
= tilesize
;
583 static float *game_colours(frontend
*fe
, int *ncolours
)
585 float *ret
= snewn(3 * NCOLOURS
, float);
588 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_HIGHLIGHT
, COL_LOWLIGHT
);
590 for (i
= 0; i
< 3; i
++)
591 ret
[COL_TEXT
* 3 + i
] = 0.0;
593 *ncolours
= NCOLOURS
;
597 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
599 struct game_drawstate
*ds
= snew(struct game_drawstate
);
605 ds
->bgcolour
= COL_BACKGROUND
;
606 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
607 ds
->tilesize
= 0; /* haven't decided yet */
608 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
614 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
620 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, game_state
*state
,
621 int x
, int y
, int tile
, int flash_colour
)
624 draw_rect(dr
, x
, y
, TILE_SIZE
, TILE_SIZE
,
630 coords
[0] = x
+ TILE_SIZE
- 1;
631 coords
[1] = y
+ TILE_SIZE
- 1;
632 coords
[2] = x
+ TILE_SIZE
- 1;
635 coords
[5] = y
+ TILE_SIZE
- 1;
636 draw_polygon(dr
, coords
, 3, COL_LOWLIGHT
, COL_LOWLIGHT
);
640 draw_polygon(dr
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
642 draw_rect(dr
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
643 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
646 sprintf(str
, "%d", tile
);
647 draw_text(dr
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
648 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
651 draw_update(dr
, x
, y
, TILE_SIZE
, TILE_SIZE
);
654 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
655 game_state
*state
, int dir
, game_ui
*ui
,
656 float animtime
, float flashtime
)
658 int i
, pass
, bgcolour
;
661 int frame
= (int)(flashtime
/ FLASH_FRAME
);
662 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
664 bgcolour
= COL_BACKGROUND
;
670 TILE_SIZE
* state
->w
+ 2 * BORDER
,
671 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
672 draw_update(dr
, 0, 0,
673 TILE_SIZE
* state
->w
+ 2 * BORDER
,
674 TILE_SIZE
* state
->h
+ 2 * BORDER
);
677 * Recessed area containing the whole puzzle.
679 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
680 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
681 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
682 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
683 coords
[4] = coords
[2] - TILE_SIZE
;
684 coords
[5] = coords
[3] + TILE_SIZE
;
685 coords
[8] = COORD(0) - HIGHLIGHT_WIDTH
;
686 coords
[9] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
687 coords
[6] = coords
[8] + TILE_SIZE
;
688 coords
[7] = coords
[9] - TILE_SIZE
;
689 draw_polygon(dr
, coords
, 5, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
691 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
692 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
693 draw_polygon(dr
, coords
, 5, COL_LOWLIGHT
, COL_LOWLIGHT
);
699 * Now draw each tile. We do this in two passes to make
702 for (pass
= 0; pass
< 2; pass
++) {
703 for (i
= 0; i
< state
->n
; i
++) {
706 * Figure out what should be displayed at this
707 * location. It's either a simple tile, or it's a
708 * transition between two tiles (in which case we say
709 * -1 because it must always be drawn).
712 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
719 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
720 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
724 * Figure out what to _actually_ draw, and where to
732 * On the first pass, just blank the tile.
735 x
= COORD(X(state
, i
));
736 y
= COORD(Y(state
, i
));
744 * Don't bother moving the gap; just don't
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
));
763 c
= (animtime
/ ANIM_TIME
);
764 if (c
< 0.0F
) c
= 0.0F
;
765 if (c
> 1.0F
) c
= 1.0F
;
767 x
= x0
+ (int)(c
* (x1
- x0
));
768 y
= y0
+ (int)(c
* (y1
- y0
));
774 x
= COORD(X(state
, i
));
775 y
= COORD(Y(state
, i
));
778 draw_tile(dr
, ds
, state
, x
, y
, t
, bgcolour
);
783 ds
->bgcolour
= bgcolour
;
786 * Update the status bar.
792 * Don't show the new status until we're also showing the
793 * new _state_ - after the game animation is complete.
798 if (state
->used_solve
)
799 sprintf(statusbuf
, "Moves since auto-solve: %d",
800 state
->movecount
- state
->completed
);
802 sprintf(statusbuf
, "%sMoves: %d",
803 (state
->completed ?
"COMPLETED! " : ""),
804 (state
->completed ? state
->completed
: state
->movecount
));
806 status_bar(dr
, statusbuf
);
810 static float game_anim_length(game_state
*oldstate
,
811 game_state
*newstate
, int dir
, game_ui
*ui
)
813 if ((dir
> 0 && newstate
->just_used_solve
) ||
814 (dir
< 0 && oldstate
->just_used_solve
))
820 static float game_flash_length(game_state
*oldstate
,
821 game_state
*newstate
, int dir
, game_ui
*ui
)
823 if (!oldstate
->completed
&& newstate
->completed
&&
824 !oldstate
->used_solve
&& !newstate
->used_solve
)
825 return 2 * FLASH_FRAME
;
830 static int game_timing_state(game_state
*state
, game_ui
*ui
)
835 static void game_print_size(game_params
*params
, float *x
, float *y
)
839 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
844 #define thegame fifteen
847 const struct game thegame
= {
848 "Fifteen", "games.fifteen",
855 TRUE
, game_configure
, custom_params
,
863 TRUE
, game_text_format
,
871 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
878 FALSE
, FALSE
, game_print_size
, game_print
,
879 TRUE
, /* wants_statusbar */
880 FALSE
, game_timing_state
,