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(*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
)
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 game_aux_info
**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 void game_free_aux_info(game_aux_info
*aux
)
272 assert(!"Shouldn't happen");
275 static char *validate_desc(game_params
*params
, char *desc
)
281 area
= params
->w
* params
->h
;
285 used
= snewn(area
, int);
286 for (i
= 0; i
< area
; i
++)
289 for (i
= 0; i
< area
; i
++) {
293 if (*p
< '0' || *p
> '9') {
294 err
= "Not enough numbers in string";
297 while (*p
>= '0' && *p
<= '9')
299 if (i
< area
-1 && *p
!= ',') {
300 err
= "Expected comma after number";
303 else if (i
== area
-1 && *p
) {
304 err
= "Excess junk at end of string";
308 if (n
< 0 || n
>= area
) {
309 err
= "Number out of range";
313 err
= "Number used twice";
318 if (*p
) p
++; /* eat comma */
326 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
328 game_state
*state
= snew(game_state
);
332 state
->w
= params
->w
;
333 state
->h
= params
->h
;
334 state
->n
= params
->w
* params
->h
;
335 state
->tiles
= snewn(state
->n
, int);
340 for (i
= 0; i
< state
->n
; i
++) {
342 state
->tiles
[i
] = atoi(p
);
343 if (state
->tiles
[i
] == 0)
345 while (*p
&& *p
!= ',')
347 if (*p
) p
++; /* eat comma */
350 assert(state
->tiles
[state
->gap_pos
] == 0);
352 state
->completed
= state
->movecount
= 0;
353 state
->used_solve
= state
->just_used_solve
= FALSE
;
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
;
370 ret
->used_solve
= state
->used_solve
;
371 ret
->just_used_solve
= state
->just_used_solve
;
376 static void free_game(game_state
*state
)
382 static char *solve_game(game_state
*state
, game_state
*currstate
,
383 game_aux_info
*aux
, char **error
)
388 static char *game_text_format(game_state
*state
)
390 char *ret
, *p
, buf
[80];
391 int x
, y
, col
, maxlen
;
394 * First work out how many characters we need to display each
397 col
= sprintf(buf
, "%d", state
->n
-1);
400 * Now we know the exact total size of the grid we're going to
401 * produce: it's got h rows, each containing w lots of col, w-1
402 * spaces and a trailing newline.
404 maxlen
= state
->h
* state
->w
* (col
+1);
406 ret
= snewn(maxlen
+1, char);
409 for (y
= 0; y
< state
->h
; y
++) {
410 for (x
= 0; x
< state
->w
; x
++) {
411 int v
= state
->tiles
[state
->w
*y
+x
];
413 sprintf(buf
, "%*s", col
, "");
415 sprintf(buf
, "%*d", col
, v
);
425 assert(p
- ret
== maxlen
);
430 static game_ui
*new_ui(game_state
*state
)
435 static void free_ui(game_ui
*ui
)
439 char *encode_ui(game_ui
*ui
)
444 void decode_ui(game_ui
*ui
, char *encoding
)
448 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
449 game_state
*newstate
)
453 struct game_drawstate
{
460 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
461 int x
, int y
, int button
)
468 gx
= X(state
, state
->gap_pos
);
469 gy
= Y(state
, state
->gap_pos
);
471 if (button
== CURSOR_RIGHT
&& gx
> 0)
472 dx
= gx
- 1, dy
= gy
;
473 else if (button
== CURSOR_LEFT
&& gx
< state
->w
-1)
474 dx
= gx
+ 1, dy
= gy
;
475 else if (button
== CURSOR_DOWN
&& gy
> 0)
476 dy
= gy
- 1, dx
= gx
;
477 else if (button
== CURSOR_UP
&& gy
< state
->h
-1)
478 dy
= gy
+ 1, dx
= gx
;
479 else if (button
== LEFT_BUTTON
) {
482 if (dx
< 0 || dx
>= state
->w
|| dy
< 0 || dy
>= state
->h
)
483 return NULL
; /* out of bounds */
485 * Any click location should be equal to the gap location
486 * in _precisely_ one coordinate.
488 if ((dx
== gx
&& dy
== gy
) || (dx
!= gx
&& dy
!= gy
))
491 return NULL
; /* no move */
493 sprintf(buf
, "M%d,%d", dx
, dy
);
497 static game_state
*execute_move(game_state
*from
, char *move
)
499 int gx
, gy
, dx
, dy
, ux
, uy
, up
, p
;
502 if (!strcmp(move
, "S")) {
505 ret
= dup_game(from
);
508 * Simply replace the grid with a solved one. For this game,
509 * this isn't a useful operation for actually telling the user
510 * what they should have done, but it is useful for
511 * conveniently being able to get hold of a clean state from
512 * which to practise manoeuvres.
514 for (i
= 0; i
< ret
->n
; i
++)
515 ret
->tiles
[i
] = (i
+1) % ret
->n
;
516 ret
->gap_pos
= ret
->n
-1;
517 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
518 ret
->completed
= ret
->movecount
= 1;
523 gx
= X(from
, from
->gap_pos
);
524 gy
= Y(from
, from
->gap_pos
);
526 if (move
[0] != 'M' ||
527 sscanf(move
+1, "%d,%d", &dx
, &dy
) != 2 ||
528 (dx
== gx
&& dy
== gy
) || (dx
!= gx
&& dy
!= gy
) ||
529 dx
< 0 || dx
>= from
->w
|| dy
< 0 || dy
>= from
->h
)
533 * Find the unit displacement from the original gap
534 * position towards this one.
536 ux
= (dx
< gx ?
-1 : dx
> gx ?
+1 : 0);
537 uy
= (dy
< gy ?
-1 : dy
> gy ?
+1 : 0);
538 up
= C(from
, ux
, uy
);
540 ret
= dup_game(from
);
541 ret
->just_used_solve
= FALSE
; /* zero this in a hurry */
543 ret
->gap_pos
= C(from
, dx
, dy
);
544 assert(ret
->gap_pos
>= 0 && ret
->gap_pos
< ret
->n
);
546 ret
->tiles
[ret
->gap_pos
] = 0;
548 for (p
= from
->gap_pos
; p
!= ret
->gap_pos
; p
+= up
) {
549 assert(p
>= 0 && p
< from
->n
);
550 ret
->tiles
[p
] = from
->tiles
[p
+ up
];
555 * See if the game has been completed.
557 if (!ret
->completed
) {
558 ret
->completed
= ret
->movecount
;
559 for (p
= 0; p
< ret
->n
; p
++)
560 if (ret
->tiles
[p
] != (p
< ret
->n
-1 ? p
+1 : 0))
567 /* ----------------------------------------------------------------------
571 static void game_size(game_params
*params
, game_drawstate
*ds
,
572 int *x
, int *y
, int expand
)
576 * Each window dimension equals the tile size times one more
577 * than the grid dimension (the border is half the width of the
580 tsx
= *x
/ (params
->w
+ 1);
581 tsy
= *y
/ (params
->h
+ 1);
586 ds
->tilesize
= min(ts
, PREFERRED_TILE_SIZE
);
588 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
589 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
592 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
594 float *ret
= snewn(3 * NCOLOURS
, float);
598 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
601 * Drop the background colour so that the highlight is
602 * noticeably brighter than it while still being under 1.
604 max
= ret
[COL_BACKGROUND
*3];
605 for (i
= 1; i
< 3; i
++)
606 if (ret
[COL_BACKGROUND
*3+i
] > max
)
607 max
= ret
[COL_BACKGROUND
*3+i
];
608 if (max
* 1.2F
> 1.0F
) {
609 for (i
= 0; i
< 3; i
++)
610 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
613 for (i
= 0; i
< 3; i
++) {
614 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
615 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
616 ret
[COL_TEXT
* 3 + i
] = 0.0;
619 *ncolours
= NCOLOURS
;
623 static game_drawstate
*game_new_drawstate(game_state
*state
)
625 struct game_drawstate
*ds
= snew(struct game_drawstate
);
631 ds
->bgcolour
= COL_BACKGROUND
;
632 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
633 ds
->tilesize
= 0; /* haven't decided yet */
634 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
640 static void game_free_drawstate(game_drawstate
*ds
)
646 static void draw_tile(frontend
*fe
, game_drawstate
*ds
, game_state
*state
,
647 int x
, int y
, int tile
, int flash_colour
)
650 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
656 coords
[0] = x
+ TILE_SIZE
- 1;
657 coords
[1] = y
+ TILE_SIZE
- 1;
658 coords
[2] = x
+ TILE_SIZE
- 1;
661 coords
[5] = y
+ TILE_SIZE
- 1;
662 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
663 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
667 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
668 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
670 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
671 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
674 sprintf(str
, "%d", tile
);
675 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
676 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
679 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
682 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
683 game_state
*state
, int dir
, game_ui
*ui
,
684 float animtime
, float flashtime
)
686 int i
, pass
, bgcolour
;
689 int frame
= (int)(flashtime
/ FLASH_FRAME
);
690 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
692 bgcolour
= COL_BACKGROUND
;
698 TILE_SIZE
* state
->w
+ 2 * BORDER
,
699 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
700 draw_update(fe
, 0, 0,
701 TILE_SIZE
* state
->w
+ 2 * BORDER
,
702 TILE_SIZE
* state
->h
+ 2 * BORDER
);
705 * Recessed area containing the whole puzzle.
707 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
708 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
709 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
710 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
711 coords
[4] = coords
[2] - TILE_SIZE
;
712 coords
[5] = coords
[3] + TILE_SIZE
;
713 coords
[8] = COORD(0) - HIGHLIGHT_WIDTH
;
714 coords
[9] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
715 coords
[6] = coords
[8] + TILE_SIZE
;
716 coords
[7] = coords
[9] - TILE_SIZE
;
717 draw_polygon(fe
, coords
, 5, TRUE
, COL_HIGHLIGHT
);
718 draw_polygon(fe
, coords
, 5, FALSE
, COL_HIGHLIGHT
);
720 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
721 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
722 draw_polygon(fe
, coords
, 5, TRUE
, COL_LOWLIGHT
);
723 draw_polygon(fe
, coords
, 5, FALSE
, COL_LOWLIGHT
);
729 * Now draw each tile. We do this in two passes to make
732 for (pass
= 0; pass
< 2; pass
++) {
733 for (i
= 0; i
< state
->n
; i
++) {
736 * Figure out what should be displayed at this
737 * location. It's either a simple tile, or it's a
738 * transition between two tiles (in which case we say
739 * -1 because it must always be drawn).
742 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
749 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
750 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
754 * Figure out what to _actually_ draw, and where to
762 * On the first pass, just blank the tile.
765 x
= COORD(X(state
, i
));
766 y
= COORD(Y(state
, i
));
774 * Don't bother moving the gap; just don't
781 * Find the coordinates of this tile in the old and
784 x1
= COORD(X(state
, i
));
785 y1
= COORD(Y(state
, i
));
786 for (j
= 0; j
< oldstate
->n
; j
++)
787 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
789 assert(j
< oldstate
->n
);
790 x0
= COORD(X(state
, j
));
791 y0
= COORD(Y(state
, j
));
793 c
= (animtime
/ ANIM_TIME
);
794 if (c
< 0.0F
) c
= 0.0F
;
795 if (c
> 1.0F
) c
= 1.0F
;
797 x
= x0
+ (int)(c
* (x1
- x0
));
798 y
= y0
+ (int)(c
* (y1
- y0
));
804 x
= COORD(X(state
, i
));
805 y
= COORD(Y(state
, i
));
808 draw_tile(fe
, ds
, state
, x
, y
, t
, bgcolour
);
813 ds
->bgcolour
= bgcolour
;
816 * Update the status bar.
822 * Don't show the new status until we're also showing the
823 * new _state_ - after the game animation is complete.
828 if (state
->used_solve
)
829 sprintf(statusbuf
, "Moves since auto-solve: %d",
830 state
->movecount
- state
->completed
);
832 sprintf(statusbuf
, "%sMoves: %d",
833 (state
->completed ?
"COMPLETED! " : ""),
834 (state
->completed ? state
->completed
: state
->movecount
));
836 status_bar(fe
, statusbuf
);
840 static float game_anim_length(game_state
*oldstate
,
841 game_state
*newstate
, int dir
, game_ui
*ui
)
843 if ((dir
> 0 && newstate
->just_used_solve
) ||
844 (dir
< 0 && oldstate
->just_used_solve
))
850 static float game_flash_length(game_state
*oldstate
,
851 game_state
*newstate
, int dir
, game_ui
*ui
)
853 if (!oldstate
->completed
&& newstate
->completed
&&
854 !oldstate
->used_solve
&& !newstate
->used_solve
)
855 return 2 * FLASH_FRAME
;
860 static int game_wants_statusbar(void)
865 static int game_timing_state(game_state
*state
)
871 #define thegame fifteen
874 const struct game thegame
= {
875 "Fifteen", "games.fifteen",
882 TRUE
, game_configure
, custom_params
,
891 TRUE
, game_text_format
,
906 game_wants_statusbar
,
907 FALSE
, game_timing_state
,
908 0, /* mouse_priorities */