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) )
44 int just_used_solve
; /* used to suppress undo animation */
45 int used_solve
; /* used to suppress completion flash */
49 static game_params
*default_params(void)
51 game_params
*ret
= snew(game_params
);
58 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
63 static void free_params(game_params
*params
)
68 static game_params
*dup_params(game_params
*params
)
70 game_params
*ret
= snew(game_params
);
71 *ret
= *params
; /* structure copy */
75 static void decode_params(game_params
*ret
, char const *string
)
77 ret
->w
= ret
->h
= atoi(string
);
78 while (*string
&& isdigit(*string
)) string
++;
81 ret
->h
= atoi(string
);
85 static char *encode_params(game_params
*params
, int full
)
89 sprintf(data
, "%dx%d", params
->w
, params
->h
);
94 static config_item
*game_configure(game_params
*params
)
99 ret
= snewn(3, config_item
);
101 ret
[0].name
= "Width";
102 ret
[0].type
= C_STRING
;
103 sprintf(buf
, "%d", params
->w
);
104 ret
[0].sval
= dupstr(buf
);
107 ret
[1].name
= "Height";
108 ret
[1].type
= C_STRING
;
109 sprintf(buf
, "%d", params
->h
);
110 ret
[1].sval
= dupstr(buf
);
121 static game_params
*custom_params(config_item
*cfg
)
123 game_params
*ret
= snew(game_params
);
125 ret
->w
= atoi(cfg
[0].sval
);
126 ret
->h
= atoi(cfg
[1].sval
);
131 static char *validate_params(game_params
*params
)
133 if (params
->w
< 2 && params
->h
< 2)
134 return "Width and height must both be at least two";
139 static int perm_parity(int *perm
, int n
)
145 for (i
= 0; i
< n
-1; i
++)
146 for (j
= i
+1; j
< n
; j
++)
147 if (perm
[i
] > perm
[j
])
153 static char *new_game_desc(game_params
*params
, random_state
*rs
,
157 int x1
, x2
, p1
, p2
, parity
;
162 n
= params
->w
* params
->h
;
164 tiles
= snewn(n
, int);
165 used
= snewn(n
, int);
167 for (i
= 0; i
< n
; i
++) {
172 gap
= random_upto(rs
, n
);
177 * Place everything else except the last two tiles.
179 for (x
= 0, i
= n
-1; i
> 2; i
--) {
180 int k
= random_upto(rs
, i
);
183 for (j
= 0; j
< n
; j
++)
184 if (!used
[j
] && (k
-- == 0))
187 assert(j
< n
&& !used
[j
]);
190 while (tiles
[x
] >= 0)
197 * Find the last two locations, and the last two pieces.
199 while (tiles
[x
] >= 0)
204 while (tiles
[x
] >= 0)
209 for (i
= 0; i
< n
; i
++)
213 for (i
= p1
+1; i
< n
; i
++)
219 * Determine the required parity of the overall permutation.
220 * This is the XOR of:
222 * - The chessboard parity ((x^y)&1) of the gap square. The
223 * bottom right counts as even.
225 * - The parity of n. (The target permutation is 1,...,n-1,0
226 * rather than 0,...,n-1; this is a cyclic permutation of
227 * the starting point and hence is odd iff n is even.)
229 parity
= ((X(params
, gap
) - (params
->w
-1)) ^
230 (Y(params
, gap
) - (params
->h
-1)) ^
234 * Try the last two tiles one way round. If that fails, swap
239 if (perm_parity(tiles
, n
) != parity
) {
242 assert(perm_parity(tiles
, n
) == parity
);
246 * Now construct the game description, by describing the tile
247 * array as a simple sequence of comma-separated integers.
251 for (i
= 0; i
< n
; i
++) {
255 k
= sprintf(buf
, "%d,", tiles
[i
]);
257 ret
= sresize(ret
, retlen
+ k
+ 1, char);
258 strcpy(ret
+ retlen
, buf
);
261 ret
[retlen
-1] = '\0'; /* delete last comma */
269 static void game_free_aux_info(game_aux_info
*aux
)
271 assert(!"Shouldn't happen");
274 static char *validate_desc(game_params
*params
, char *desc
)
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 static game_state
*new_game(game_params
*params
, char *desc
)
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;
352 state
->used_solve
= state
->just_used_solve
= FALSE
;
357 static game_state
*dup_game(game_state
*state
)
359 game_state
*ret
= snew(game_state
);
364 ret
->tiles
= snewn(state
->w
* state
->h
, int);
365 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
366 ret
->gap_pos
= state
->gap_pos
;
367 ret
->completed
= state
->completed
;
368 ret
->movecount
= state
->movecount
;
369 ret
->used_solve
= state
->used_solve
;
370 ret
->just_used_solve
= state
->just_used_solve
;
375 static void free_game(game_state
*state
)
380 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
383 game_state
*ret
= dup_game(state
);
387 * Simply replace the grid with a solved one. For this game,
388 * this isn't a useful operation for actually telling the user
389 * what they should have done, but it is useful for
390 * conveniently being able to get hold of a clean state from
391 * which to practise manoeuvres.
393 for (i
= 0; i
< ret
->n
; i
++)
394 ret
->tiles
[i
] = (i
+1) % ret
->n
;
395 ret
->gap_pos
= ret
->n
-1;
396 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
397 ret
->completed
= ret
->movecount
= 1;
402 static char *game_text_format(game_state
*state
)
404 char *ret
, *p
, buf
[80];
405 int x
, y
, col
, maxlen
;
408 * First work out how many characters we need to display each
411 col
= sprintf(buf
, "%d", state
->n
-1);
414 * Now we know the exact total size of the grid we're going to
415 * produce: it's got h rows, each containing w lots of col, w-1
416 * spaces and a trailing newline.
418 maxlen
= state
->h
* state
->w
* (col
+1);
420 ret
= snewn(maxlen
+1, char);
423 for (y
= 0; y
< state
->h
; y
++) {
424 for (x
= 0; x
< state
->w
; x
++) {
425 int v
= state
->tiles
[state
->w
*y
+x
];
427 sprintf(buf
, "%*s", col
, "");
429 sprintf(buf
, "%*d", col
, v
);
439 assert(p
- ret
== maxlen
);
444 static game_ui
*new_ui(game_state
*state
)
449 static void free_ui(game_ui
*ui
)
453 static game_state
*make_move(game_state
*from
, game_ui
*ui
,
454 int x
, int y
, int button
)
456 int gx
, gy
, dx
, dy
, ux
, uy
, up
, p
;
459 gx
= X(from
, from
->gap_pos
);
460 gy
= Y(from
, from
->gap_pos
);
462 if (button
== CURSOR_RIGHT
&& gx
> 0)
463 dx
= gx
- 1, dy
= gy
;
464 else if (button
== CURSOR_LEFT
&& gx
< from
->w
-1)
465 dx
= gx
+ 1, dy
= gy
;
466 else if (button
== CURSOR_DOWN
&& gy
> 0)
467 dy
= gy
- 1, dx
= gx
;
468 else if (button
== CURSOR_UP
&& gy
< from
->h
-1)
469 dy
= gy
+ 1, dx
= gx
;
470 else if (button
== LEFT_BUTTON
) {
473 if (dx
< 0 || dx
>= from
->w
|| dy
< 0 || dy
>= from
->h
)
474 return NULL
; /* out of bounds */
476 * Any click location should be equal to the gap location
477 * in _precisely_ one coordinate.
479 if ((dx
== gx
&& dy
== gy
) || (dx
!= gx
&& dy
!= gy
))
482 return NULL
; /* no move */
485 * Find the unit displacement from the original gap
486 * position towards this one.
488 ux
= (dx
< gx ?
-1 : dx
> gx ?
+1 : 0);
489 uy
= (dy
< gy ?
-1 : dy
> gy ?
+1 : 0);
490 up
= C(from
, ux
, uy
);
492 ret
= dup_game(from
);
493 ret
->just_used_solve
= FALSE
; /* zero this in a hurry */
495 ret
->gap_pos
= C(from
, dx
, dy
);
496 assert(ret
->gap_pos
>= 0 && ret
->gap_pos
< ret
->n
);
498 ret
->tiles
[ret
->gap_pos
] = 0;
500 for (p
= from
->gap_pos
; p
!= ret
->gap_pos
; p
+= up
) {
501 assert(p
>= 0 && p
< from
->n
);
502 ret
->tiles
[p
] = from
->tiles
[p
+ up
];
507 * See if the game has been completed.
509 if (!ret
->completed
) {
510 ret
->completed
= ret
->movecount
;
511 for (p
= 0; p
< ret
->n
; p
++)
512 if (ret
->tiles
[p
] != (p
< ret
->n
-1 ? p
+1 : 0))
519 /* ----------------------------------------------------------------------
523 struct game_drawstate
{
529 static void game_size(game_params
*params
, int *x
, int *y
)
531 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
532 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
535 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
537 float *ret
= snewn(3 * NCOLOURS
, float);
541 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
544 * Drop the background colour so that the highlight is
545 * noticeably brighter than it while still being under 1.
547 max
= ret
[COL_BACKGROUND
*3];
548 for (i
= 1; i
< 3; i
++)
549 if (ret
[COL_BACKGROUND
*3+i
] > max
)
550 max
= ret
[COL_BACKGROUND
*3+i
];
551 if (max
* 1.2F
> 1.0F
) {
552 for (i
= 0; i
< 3; i
++)
553 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
556 for (i
= 0; i
< 3; i
++) {
557 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
558 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
559 ret
[COL_TEXT
* 3 + i
] = 0.0;
562 *ncolours
= NCOLOURS
;
566 static game_drawstate
*game_new_drawstate(game_state
*state
)
568 struct game_drawstate
*ds
= snew(struct game_drawstate
);
574 ds
->bgcolour
= COL_BACKGROUND
;
575 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
576 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
582 static void game_free_drawstate(game_drawstate
*ds
)
588 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
589 int tile
, int flash_colour
)
592 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
598 coords
[0] = x
+ TILE_SIZE
- 1;
599 coords
[1] = y
+ TILE_SIZE
- 1;
600 coords
[2] = x
+ TILE_SIZE
- 1;
603 coords
[5] = y
+ TILE_SIZE
- 1;
604 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
605 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
609 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
610 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
612 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
613 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
616 sprintf(str
, "%d", tile
);
617 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
618 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
621 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
624 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
625 game_state
*state
, int dir
, game_ui
*ui
,
626 float animtime
, float flashtime
)
628 int i
, pass
, bgcolour
;
631 int frame
= (int)(flashtime
/ FLASH_FRAME
);
632 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
634 bgcolour
= COL_BACKGROUND
;
640 TILE_SIZE
* state
->w
+ 2 * BORDER
,
641 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
642 draw_update(fe
, 0, 0,
643 TILE_SIZE
* state
->w
+ 2 * BORDER
,
644 TILE_SIZE
* state
->h
+ 2 * BORDER
);
647 * Recessed area containing the whole puzzle.
649 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
650 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
651 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
652 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
653 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
654 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
655 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
656 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
658 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
659 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
660 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
661 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
667 * Now draw each tile. We do this in two passes to make
670 for (pass
= 0; pass
< 2; pass
++) {
671 for (i
= 0; i
< state
->n
; i
++) {
674 * Figure out what should be displayed at this
675 * location. It's either a simple tile, or it's a
676 * transition between two tiles (in which case we say
677 * -1 because it must always be drawn).
680 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
687 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
688 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
692 * Figure out what to _actually_ draw, and where to
700 * On the first pass, just blank the tile.
703 x
= COORD(X(state
, i
));
704 y
= COORD(Y(state
, i
));
712 * Don't bother moving the gap; just don't
719 * Find the coordinates of this tile in the old and
722 x1
= COORD(X(state
, i
));
723 y1
= COORD(Y(state
, i
));
724 for (j
= 0; j
< oldstate
->n
; j
++)
725 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
727 assert(j
< oldstate
->n
);
728 x0
= COORD(X(state
, j
));
729 y0
= COORD(Y(state
, j
));
731 c
= (animtime
/ ANIM_TIME
);
732 if (c
< 0.0F
) c
= 0.0F
;
733 if (c
> 1.0F
) c
= 1.0F
;
735 x
= x0
+ (int)(c
* (x1
- x0
));
736 y
= y0
+ (int)(c
* (y1
- y0
));
742 x
= COORD(X(state
, i
));
743 y
= COORD(Y(state
, i
));
746 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
751 ds
->bgcolour
= bgcolour
;
754 * Update the status bar.
760 * Don't show the new status until we're also showing the
761 * new _state_ - after the game animation is complete.
766 if (state
->used_solve
)
767 sprintf(statusbuf
, "Moves since auto-solve: %d",
768 state
->movecount
- state
->completed
);
770 sprintf(statusbuf
, "%sMoves: %d",
771 (state
->completed ?
"COMPLETED! " : ""),
772 (state
->completed ? state
->completed
: state
->movecount
));
774 status_bar(fe
, statusbuf
);
778 static float game_anim_length(game_state
*oldstate
,
779 game_state
*newstate
, int dir
)
781 if ((dir
> 0 && newstate
->just_used_solve
) ||
782 (dir
< 0 && oldstate
->just_used_solve
))
788 static float game_flash_length(game_state
*oldstate
,
789 game_state
*newstate
, int dir
)
791 if (!oldstate
->completed
&& newstate
->completed
&&
792 !oldstate
->used_solve
&& !newstate
->used_solve
)
793 return 2 * FLASH_FRAME
;
798 static int game_wants_statusbar(void)
804 #define thegame fifteen
807 const struct game thegame
= {
808 "Fifteen", "games.fifteen",
815 TRUE
, game_configure
, custom_params
,
824 TRUE
, game_text_format
,
835 game_wants_statusbar
,