2 * fifteen.c: standard 15-puzzle.
13 const char *const game_name
= "Fifteen";
14 const int game_can_configure
= TRUE
;
17 #define BORDER (TILE_SIZE / 2)
18 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
19 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
20 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
22 #define ANIM_TIME 0.13F
23 #define FLASH_FRAME 0.13F
25 #define X(state, i) ( (i) % (state)->w )
26 #define Y(state, i) ( (i) / (state)->w )
27 #define C(state, x, y) ( (y) * (state)->w + (x) )
49 game_params
*default_params(void)
51 game_params
*ret
= snew(game_params
);
58 int game_fetch_preset(int i
, char **name
, game_params
**params
)
63 void free_params(game_params
*params
)
68 game_params
*dup_params(game_params
*params
)
70 game_params
*ret
= snew(game_params
);
71 *ret
= *params
; /* structure copy */
75 config_item
*game_configure(game_params
*params
)
80 ret
= snewn(3, config_item
);
82 ret
[0].name
= "Width";
83 ret
[0].type
= C_STRING
;
84 sprintf(buf
, "%d", params
->w
);
85 ret
[0].sval
= dupstr(buf
);
88 ret
[1].name
= "Height";
89 ret
[1].type
= C_STRING
;
90 sprintf(buf
, "%d", params
->h
);
91 ret
[1].sval
= dupstr(buf
);
102 game_params
*custom_params(config_item
*cfg
)
104 game_params
*ret
= snew(game_params
);
106 ret
->w
= atoi(cfg
[0].sval
);
107 ret
->h
= atoi(cfg
[1].sval
);
112 char *validate_params(game_params
*params
)
114 if (params
->w
< 2 && params
->h
< 2)
115 return "Width and height must both be at least two";
120 int perm_parity(int *perm
, int n
)
126 for (i
= 0; i
< n
-1; i
++)
127 for (j
= i
+1; j
< n
; j
++)
128 if (perm
[i
] > perm
[j
])
134 char *new_game_seed(game_params
*params
, random_state
*rs
)
137 int x1
, x2
, p1
, p2
, parity
;
142 n
= params
->w
* params
->h
;
144 tiles
= snewn(n
, int);
145 used
= snewn(n
, int);
147 for (i
= 0; i
< n
; i
++) {
152 gap
= random_upto(rs
, n
);
157 * Place everything else except the last two tiles.
159 for (x
= 0, i
= n
-1; i
> 2; i
--) {
160 int k
= random_upto(rs
, i
);
163 for (j
= 0; j
< n
; j
++)
164 if (!used
[j
] && (k
-- == 0))
167 assert(j
< n
&& !used
[j
]);
170 while (tiles
[x
] >= 0)
177 * Find the last two locations, and the last two pieces.
179 while (tiles
[x
] >= 0)
184 while (tiles
[x
] >= 0)
189 for (i
= 0; i
< n
; i
++)
193 for (i
= p1
+1; i
< n
; i
++)
199 * Determine the required parity of the overall permutation.
200 * This is the XOR of:
202 * - The chessboard parity ((x^y)&1) of the gap square. The
203 * bottom right counts as even.
205 * - The parity of n. (The target permutation is 1,...,n-1,0
206 * rather than 0,...,n-1; this is a cyclic permutation of
207 * the starting point and hence is odd iff n is even.)
209 parity
= ((X(params
, gap
) - (params
->w
-1)) ^
210 (Y(params
, gap
) - (params
->h
-1)) ^
214 * Try the last two tiles one way round. If that fails, swap
219 if (perm_parity(tiles
, n
) != parity
) {
222 assert(perm_parity(tiles
, n
) == parity
);
226 * Now construct the game seed, by describing the tile array as
227 * a simple sequence of comma-separated integers.
231 for (i
= 0; i
< n
; i
++) {
235 k
= sprintf(buf
, "%d,", tiles
[i
]);
237 ret
= sresize(ret
, retlen
+ k
+ 1, char);
238 strcpy(ret
+ retlen
, buf
);
241 ret
[retlen
-1] = '\0'; /* delete last comma */
249 char *validate_seed(game_params
*params
, char *seed
)
255 area
= params
->w
* params
->h
;
259 used
= snewn(area
, int);
260 for (i
= 0; i
< area
; i
++)
263 for (i
= 0; i
< area
; i
++) {
267 if (*p
< '0' || *p
> '9') {
268 err
= "Not enough numbers in string";
271 while (*p
>= '0' && *p
<= '9')
273 if (i
< area
-1 && *p
!= ',') {
274 err
= "Expected comma after number";
277 else if (i
== area
-1 && *p
) {
278 err
= "Excess junk at end of string";
282 if (n
< 0 || n
>= area
) {
283 err
= "Number out of range";
287 err
= "Number used twice";
292 if (*p
) p
++; /* eat comma */
300 game_state
*new_game(game_params
*params
, char *seed
)
302 game_state
*state
= snew(game_state
);
306 state
->w
= params
->w
;
307 state
->h
= params
->h
;
308 state
->n
= params
->w
* params
->h
;
309 state
->tiles
= snewn(state
->n
, int);
314 for (i
= 0; i
< state
->n
; i
++) {
316 state
->tiles
[i
] = atoi(p
);
317 if (state
->tiles
[i
] == 0)
319 while (*p
&& *p
!= ',')
321 if (*p
) p
++; /* eat comma */
324 assert(state
->tiles
[state
->gap_pos
] == 0);
326 state
->completed
= state
->movecount
= 0;
331 game_state
*dup_game(game_state
*state
)
333 game_state
*ret
= snew(game_state
);
338 ret
->tiles
= snewn(state
->w
* state
->h
, int);
339 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
340 ret
->gap_pos
= state
->gap_pos
;
341 ret
->completed
= state
->completed
;
342 ret
->movecount
= state
->movecount
;
347 void free_game(game_state
*state
)
352 game_state
*make_move(game_state
*from
, int x
, int y
, int button
)
354 int gx
, gy
, dx
, dy
, ux
, uy
, up
, p
;
357 gx
= X(from
, from
->gap_pos
);
358 gy
= Y(from
, from
->gap_pos
);
360 if (button
== CURSOR_RIGHT
&& gx
> 0)
361 dx
= gx
- 1, dy
= gy
;
362 else if (button
== CURSOR_LEFT
&& gx
< from
->w
-1)
363 dx
= gx
+ 1, dy
= gy
;
364 else if (button
== CURSOR_DOWN
&& gy
> 0)
365 dy
= gy
- 1, dx
= gx
;
366 else if (button
== CURSOR_UP
&& gy
< from
->h
-1)
367 dy
= gy
+ 1, dx
= gx
;
368 else if (button
== LEFT_BUTTON
) {
371 if (dx
< 0 || dx
>= from
->w
|| dy
< 0 || dy
>= from
->h
)
372 return NULL
; /* out of bounds */
374 * Any click location should be equal to the gap location
375 * in _precisely_ one coordinate.
377 if ((dx
== gx
&& dy
== gy
) || (dx
!= gx
&& dy
!= gy
))
380 return NULL
; /* no move */
383 * Find the unit displacement from the original gap
384 * position towards this one.
386 ux
= (dx
< gx ?
-1 : dx
> gx ?
+1 : 0);
387 uy
= (dy
< gy ?
-1 : dy
> gy ?
+1 : 0);
388 up
= C(from
, ux
, uy
);
390 ret
= dup_game(from
);
392 ret
->gap_pos
= C(from
, dx
, dy
);
393 assert(ret
->gap_pos
>= 0 && ret
->gap_pos
< ret
->n
);
395 ret
->tiles
[ret
->gap_pos
] = 0;
397 for (p
= from
->gap_pos
; p
!= ret
->gap_pos
; p
+= up
) {
398 assert(p
>= 0 && p
< from
->n
);
399 ret
->tiles
[p
] = from
->tiles
[p
+ up
];
404 * See if the game has been completed.
406 if (!ret
->completed
) {
407 ret
->completed
= ret
->movecount
;
408 for (p
= 0; p
< ret
->n
; p
++)
409 if (ret
->tiles
[p
] != (p
< ret
->n
-1 ? p
+1 : 0))
416 /* ----------------------------------------------------------------------
420 struct game_drawstate
{
426 void game_size(game_params
*params
, int *x
, int *y
)
428 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
429 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
432 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
434 float *ret
= snewn(3 * NCOLOURS
, float);
438 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
441 * Drop the background colour so that the highlight is
442 * noticeably brighter than it while still being under 1.
444 max
= ret
[COL_BACKGROUND
*3];
445 for (i
= 1; i
< 3; i
++)
446 if (ret
[COL_BACKGROUND
*3+i
] > max
)
447 max
= ret
[COL_BACKGROUND
*3+i
];
448 if (max
* 1.2F
> 1.0F
) {
449 for (i
= 0; i
< 3; i
++)
450 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
453 for (i
= 0; i
< 3; i
++) {
454 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
455 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
456 ret
[COL_TEXT
* 3 + i
] = 0.0;
459 *ncolours
= NCOLOURS
;
463 game_drawstate
*game_new_drawstate(game_state
*state
)
465 struct game_drawstate
*ds
= snew(struct game_drawstate
);
471 ds
->bgcolour
= COL_BACKGROUND
;
472 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
473 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
479 void game_free_drawstate(game_drawstate
*ds
)
485 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
486 int tile
, int flash_colour
)
489 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
495 coords
[0] = x
+ TILE_SIZE
- 1;
496 coords
[1] = y
+ TILE_SIZE
- 1;
497 coords
[2] = x
+ TILE_SIZE
- 1;
500 coords
[5] = y
+ TILE_SIZE
- 1;
501 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
502 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
506 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
507 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
509 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
510 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
513 sprintf(str
, "%d", tile
);
514 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
515 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
518 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
521 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
522 game_state
*state
, float animtime
, float flashtime
)
524 int i
, pass
, bgcolour
;
527 int frame
= (int)(flashtime
/ FLASH_FRAME
);
528 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
530 bgcolour
= COL_BACKGROUND
;
536 TILE_SIZE
* state
->w
+ 2 * BORDER
,
537 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
538 draw_update(fe
, 0, 0,
539 TILE_SIZE
* state
->w
+ 2 * BORDER
,
540 TILE_SIZE
* state
->h
+ 2 * BORDER
);
543 * Recessed area containing the whole puzzle.
545 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
546 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
547 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
548 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
549 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
550 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
551 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
552 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
554 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
555 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
556 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
557 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
563 * Now draw each tile. We do this in two passes to make
566 for (pass
= 0; pass
< 2; pass
++) {
567 for (i
= 0; i
< state
->n
; i
++) {
570 * Figure out what should be displayed at this
571 * location. It's either a simple tile, or it's a
572 * transition between two tiles (in which case we say
573 * -1 because it must always be drawn).
576 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
583 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
584 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
588 * Figure out what to _actually_ draw, and where to
596 * On the first pass, just blank the tile.
599 x
= COORD(X(state
, i
));
600 y
= COORD(Y(state
, i
));
608 * Don't bother moving the gap; just don't
615 * Find the coordinates of this tile in the old and
618 x1
= COORD(X(state
, i
));
619 y1
= COORD(Y(state
, i
));
620 for (j
= 0; j
< oldstate
->n
; j
++)
621 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
623 assert(j
< oldstate
->n
);
624 x0
= COORD(X(state
, j
));
625 y0
= COORD(Y(state
, j
));
627 c
= (animtime
/ ANIM_TIME
);
628 if (c
< 0.0F
) c
= 0.0F
;
629 if (c
> 1.0F
) c
= 1.0F
;
631 x
= x0
+ (int)(c
* (x1
- x0
));
632 y
= y0
+ (int)(c
* (y1
- y0
));
638 x
= COORD(X(state
, i
));
639 y
= COORD(Y(state
, i
));
642 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
647 ds
->bgcolour
= bgcolour
;
650 * Update the status bar.
656 * Don't show the new status until we're also showing the
657 * new _state_ - after the game animation is complete.
662 sprintf(statusbuf
, "%sMoves: %d",
663 (state
->completed ?
"COMPLETED! " : ""),
664 (state
->completed ? state
->completed
: state
->movecount
));
666 status_bar(fe
, statusbuf
);
670 float game_anim_length(game_state
*oldstate
, game_state
*newstate
)
675 float game_flash_length(game_state
*oldstate
, game_state
*newstate
)
677 if (!oldstate
->completed
&& newstate
->completed
)
678 return 2 * FLASH_FRAME
;
683 int game_wants_statusbar(void)