2 * fifteen.c: standard 15-puzzle.
13 const char *const game_name
= "Fifteen";
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.1F
22 #define FLASH_FRAME 0.1F
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) )
48 game_params
*default_params(void)
50 game_params
*ret
= snew(game_params
);
57 int game_fetch_preset(int i
, char **name
, game_params
**params
)
62 void free_params(game_params
*params
)
67 game_params
*dup_params(game_params
*params
)
69 game_params
*ret
= snew(game_params
);
70 *ret
= *params
; /* structure copy */
74 int perm_parity(int *perm
, int n
)
80 for (i
= 0; i
< n
-1; i
++)
81 for (j
= i
+1; j
< n
; j
++)
82 if (perm
[i
] > perm
[j
])
88 char *new_game_seed(game_params
*params
)
91 int x1
, x2
, p1
, p2
, parity
;
96 n
= params
->w
* params
->h
;
98 tiles
= snewn(n
, int);
101 for (i
= 0; i
< n
; i
++) {
111 * Place everything else except the last two tiles.
113 for (x
= 0, i
= n
-1; i
> 2; i
--) {
114 int k
= rand_upto(i
);
117 for (j
= 0; j
< n
; j
++)
118 if (!used
[j
] && (k
-- == 0))
121 assert(j
< n
&& !used
[j
]);
124 while (tiles
[x
] >= 0)
131 * Find the last two locations, and the last two pieces.
133 while (tiles
[x
] >= 0)
138 while (tiles
[x
] >= 0)
143 for (i
= 0; i
< n
; i
++)
147 for (i
= p1
+1; i
< n
; i
++)
153 * Determine the required parity of the overall permutation.
154 * This is the XOR of:
156 * - The chessboard parity ((x^y)&1) of the gap square. The
157 * bottom right, and therefore also the top left, count as
160 * - The parity of n. (The target permutation is 1,...,n-1,0
161 * rather than 0,...,n-1; this is a cyclic permutation of
162 * the starting point and hence is odd iff n is even.)
164 parity
= (X(params
, gap
) ^ Y(params
, gap
) ^ (n
+1)) & 1;
167 * Try the last two tiles one way round. If that fails, swap
172 if (perm_parity(tiles
, n
) != parity
) {
175 assert(perm_parity(tiles
, n
) == parity
);
179 * Now construct the game seed, by describing the tile array as
180 * a simple sequence of comma-separated integers.
184 for (i
= 0; i
< n
; i
++) {
188 k
= sprintf(buf
, "%d,", tiles
[i
]);
190 ret
= sresize(ret
, retlen
+ k
+ 1, char);
191 strcpy(ret
+ retlen
, buf
);
194 ret
[retlen
-1] = '\0'; /* delete last comma */
202 game_state
*new_game(game_params
*params
, char *seed
)
204 game_state
*state
= snew(game_state
);
208 state
->w
= params
->w
;
209 state
->h
= params
->h
;
210 state
->n
= params
->w
* params
->h
;
211 state
->tiles
= snewn(state
->n
, int);
216 for (i
= 0; i
< state
->n
; i
++) {
218 state
->tiles
[i
] = atoi(p
);
219 if (state
->tiles
[i
] == 0)
221 while (*p
&& *p
!= ',')
223 if (*p
) p
++; /* eat comma */
226 assert(state
->tiles
[state
->gap_pos
] == 0);
228 state
->completed
= state
->movecount
= 0;
233 game_state
*dup_game(game_state
*state
)
235 game_state
*ret
= snew(game_state
);
240 ret
->tiles
= snewn(state
->w
* state
->h
, int);
241 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
242 ret
->gap_pos
= state
->gap_pos
;
243 ret
->completed
= state
->completed
;
244 ret
->movecount
= state
->movecount
;
249 void free_game(game_state
*state
)
254 game_state
*make_move(game_state
*from
, int x
, int y
, int button
)
256 int gx
, gy
, dx
, dy
, ux
, uy
, up
, p
;
259 gx
= X(from
, from
->gap_pos
);
260 gy
= Y(from
, from
->gap_pos
);
262 if (button
== CURSOR_RIGHT
&& gx
> 0)
263 dx
= gx
- 1, dy
= gy
;
264 else if (button
== CURSOR_LEFT
&& gx
< from
->w
-1)
265 dx
= gx
+ 1, dy
= gy
;
266 else if (button
== CURSOR_DOWN
&& gy
> 0)
267 dy
= gy
- 1, dx
= gx
;
268 else if (button
== CURSOR_UP
&& gy
< from
->h
-1)
269 dy
= gy
+ 1, dx
= gx
;
270 else if (button
== LEFT_BUTTON
) {
273 if (dx
< 0 || dx
>= from
->w
|| dy
< 0 || dy
>= from
->h
)
274 return NULL
; /* out of bounds */
276 * Any click location should be equal to the gap location
277 * in _precisely_ one coordinate.
279 if ((dx
== gx
&& dy
== gy
) || (dx
!= gx
&& dy
!= gy
))
282 return NULL
; /* no move */
285 * Find the unit displacement from the original gap
286 * position towards this one.
288 ux
= (dx
< gx ?
-1 : dx
> gx ?
+1 : 0);
289 uy
= (dy
< gy ?
-1 : dy
> gy ?
+1 : 0);
290 up
= C(from
, ux
, uy
);
292 ret
= dup_game(from
);
294 ret
->gap_pos
= C(from
, dx
, dy
);
295 assert(ret
->gap_pos
>= 0 && ret
->gap_pos
< ret
->n
);
297 ret
->tiles
[ret
->gap_pos
] = 0;
299 for (p
= from
->gap_pos
; p
!= ret
->gap_pos
; p
+= up
) {
300 assert(p
>= 0 && p
< from
->n
);
301 ret
->tiles
[p
] = from
->tiles
[p
+ up
];
306 * See if the game has been completed.
308 if (!ret
->completed
) {
309 ret
->completed
= ret
->movecount
;
310 for (p
= 0; p
< ret
->n
; p
++)
311 if (ret
->tiles
[p
] != (p
< ret
->n
-1 ? p
+1 : 0))
318 /* ----------------------------------------------------------------------
322 struct game_drawstate
{
328 void game_size(game_params
*params
, int *x
, int *y
)
330 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
331 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
334 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
336 float *ret
= snewn(3 * NCOLOURS
, float);
340 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
343 * Drop the background colour so that the highlight is
344 * noticeably brighter than it while still being under 1.
346 max
= ret
[COL_BACKGROUND
*3];
347 for (i
= 1; i
< 3; i
++)
348 if (ret
[COL_BACKGROUND
*3+i
] > max
)
349 max
= ret
[COL_BACKGROUND
*3+i
];
350 if (max
* 1.2F
> 1.0F
) {
351 for (i
= 0; i
< 3; i
++)
352 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
355 for (i
= 0; i
< 3; i
++) {
356 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
357 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
358 ret
[COL_TEXT
* 3 + i
] = 0.0;
361 *ncolours
= NCOLOURS
;
365 game_drawstate
*game_new_drawstate(game_state
*state
)
367 struct game_drawstate
*ds
= snew(struct game_drawstate
);
373 ds
->bgcolour
= COL_BACKGROUND
;
374 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
375 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
381 void game_free_drawstate(game_drawstate
*ds
)
387 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
388 int tile
, int flash_colour
)
391 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
397 coords
[0] = x
+ TILE_SIZE
- 1;
398 coords
[1] = y
+ TILE_SIZE
- 1;
399 coords
[2] = x
+ TILE_SIZE
- 1;
402 coords
[5] = y
+ TILE_SIZE
- 1;
403 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
404 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
408 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
409 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
411 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
412 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
415 sprintf(str
, "%d", tile
);
416 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
417 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
420 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
423 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
424 game_state
*state
, float animtime
, float flashtime
)
426 int i
, pass
, bgcolour
;
429 int frame
= (int)(flashtime
/ FLASH_FRAME
);
430 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
432 bgcolour
= COL_BACKGROUND
;
438 TILE_SIZE
* state
->w
+ 2 * BORDER
,
439 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
440 draw_update(fe
, 0, 0,
441 TILE_SIZE
* state
->w
+ 2 * BORDER
,
442 TILE_SIZE
* state
->h
+ 2 * BORDER
);
445 * Recessed area containing the whole puzzle.
447 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
448 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
449 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
450 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
451 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
452 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
453 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
454 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
456 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
457 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
458 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
459 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
465 * Now draw each tile. We do this in two passes to make
468 for (pass
= 0; pass
< 2; pass
++) {
469 for (i
= 0; i
< state
->n
; i
++) {
472 * Figure out what should be displayed at this
473 * location. It's either a simple tile, or it's a
474 * transition between two tiles (in which case we say
475 * -1 because it must always be drawn).
478 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
485 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
486 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
490 * Figure out what to _actually_ draw, and where to
498 * On the first pass, just blank the tile.
501 x
= COORD(X(state
, i
));
502 y
= COORD(Y(state
, i
));
510 * Don't bother moving the gap; just don't
517 * Find the coordinates of this tile in the old and
520 x1
= COORD(X(state
, i
));
521 y1
= COORD(Y(state
, i
));
522 for (j
= 0; j
< oldstate
->n
; j
++)
523 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
525 assert(j
< oldstate
->n
);
526 x0
= COORD(X(state
, j
));
527 y0
= COORD(Y(state
, j
));
529 c
= (animtime
/ ANIM_TIME
);
530 if (c
< 0.0F
) c
= 0.0F
;
531 if (c
> 1.0F
) c
= 1.0F
;
533 x
= x0
+ (int)(c
* (x1
- x0
));
534 y
= y0
+ (int)(c
* (y1
- y0
));
540 x
= COORD(X(state
, i
));
541 y
= COORD(Y(state
, i
));
544 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
549 ds
->bgcolour
= bgcolour
;
552 * Update the status bar.
557 sprintf(statusbuf
, "%sMoves: %d",
558 (state
->completed ?
"COMPLETED! " : ""),
559 (state
->completed ? state
->completed
: state
->movecount
));
561 status_bar(fe
, statusbuf
);
565 float game_anim_length(game_state
*oldstate
, game_state
*newstate
)
570 float game_flash_length(game_state
*oldstate
, game_state
*newstate
)
572 if (!oldstate
->completed
&& newstate
->completed
)
573 return 2 * FLASH_FRAME
;
578 int game_wants_statusbar(void)