2 * sixteen.c: `16-puzzle', a sliding-tiles jigsaw which differs
3 * from the 15-puzzle in that you toroidally rotate a row or column
15 const char *const game_name
= "Sixteen";
18 #define BORDER TILE_SIZE /* big border to fill with arrows */
19 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
20 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
21 #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
23 #define ANIM_TIME 0.1F
24 #define FLASH_FRAME 0.1F
26 #define X(state, i) ( (i) % (state)->w )
27 #define Y(state, i) ( (i) / (state)->w )
28 #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
)
64 case 0: w
= 3, h
= 3; break;
65 case 1: w
= 4, h
= 3; break;
66 case 2: w
= 4, h
= 4; break;
67 case 3: w
= 5, h
= 4; break;
68 case 4: w
= 5, h
= 5; break;
69 default: return FALSE
;
72 sprintf(buf
, "%dx%d", w
, h
);
74 *params
= ret
= snew(game_params
);
80 void free_params(game_params
*params
)
85 game_params
*dup_params(game_params
*params
)
87 game_params
*ret
= snew(game_params
);
88 *ret
= *params
; /* structure copy */
92 int perm_parity(int *perm
, int n
)
98 for (i
= 0; i
< n
-1; i
++)
99 for (j
= i
+1; j
< n
; j
++)
100 if (perm
[i
] > perm
[j
])
106 char *new_game_seed(game_params
*params
)
114 n
= params
->w
* params
->h
;
116 tiles
= snewn(n
, int);
117 used
= snewn(n
, int);
119 for (i
= 0; i
< n
; i
++) {
125 * If both dimensions are odd, there is a parity constraint.
127 if (params
->w
& params
->h
& 1)
133 * Place everything except (possibly) the last two tiles.
135 for (x
= 0, i
= n
; i
> stop
; i
--) {
136 int k
= i
> 1 ?
rand_upto(i
) : 0;
139 for (j
= 0; j
< n
; j
++)
140 if (!used
[j
] && (k
-- == 0))
143 assert(j
< n
&& !used
[j
]);
146 while (tiles
[x
] >= 0)
154 * Find the last two locations, and the last two pieces.
156 while (tiles
[x
] >= 0)
161 while (tiles
[x
] >= 0)
166 for (i
= 0; i
< n
; i
++)
170 for (i
= p1
+1; i
< n
; i
++)
176 * Try the last two tiles one way round. If that fails, swap
181 if (perm_parity(tiles
, n
) != 0) {
184 assert(perm_parity(tiles
, n
) == 0);
189 * Now construct the game seed, by describing the tile array as
190 * a simple sequence of comma-separated integers.
194 for (i
= 0; i
< n
; i
++) {
198 k
= sprintf(buf
, "%d,", tiles
[i
]+1);
200 ret
= sresize(ret
, retlen
+ k
+ 1, char);
201 strcpy(ret
+ retlen
, buf
);
204 ret
[retlen
-1] = '\0'; /* delete last comma */
212 game_state
*new_game(game_params
*params
, char *seed
)
214 game_state
*state
= snew(game_state
);
218 state
->w
= params
->w
;
219 state
->h
= params
->h
;
220 state
->n
= params
->w
* params
->h
;
221 state
->tiles
= snewn(state
->n
, int);
225 for (i
= 0; i
< state
->n
; i
++) {
227 state
->tiles
[i
] = atoi(p
);
228 while (*p
&& *p
!= ',')
230 if (*p
) p
++; /* eat comma */
234 state
->completed
= FALSE
;
239 game_state
*dup_game(game_state
*state
)
241 game_state
*ret
= snew(game_state
);
246 ret
->tiles
= snewn(state
->w
* state
->h
, int);
247 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
248 ret
->completed
= state
->completed
;
253 void free_game(game_state
*state
)
258 game_state
*make_move(game_state
*from
, int x
, int y
, int button
)
261 int dx
, dy
, tx
, ty
, n
;
264 if (button
!= LEFT_BUTTON
)
269 if (cx
== -1 && cy
>= 0 && cy
< from
->h
)
270 n
= from
->w
, dx
= +1, dy
= 0;
271 else if (cx
== from
->w
&& cy
>= 0 && cy
< from
->h
)
272 n
= from
->w
, dx
= -1, dy
= 0;
273 else if (cy
== -1 && cx
>= 0 && cx
< from
->w
)
274 n
= from
->h
, dy
= +1, dx
= 0;
275 else if (cy
== from
->h
&& cx
>= 0 && cx
< from
->w
)
276 n
= from
->h
, dy
= -1, dx
= 0;
278 return NULL
; /* invalid click location */
280 ret
= dup_game(from
);
285 tx
= (cx
+ dx
+ from
->w
) % from
->w
;
286 ty
= (cy
+ dy
+ from
->h
) % from
->h
;
287 ret
->tiles
[C(ret
, cx
, cy
)] = from
->tiles
[C(from
, tx
, ty
)];
291 * See if the game has been completed.
293 if (!ret
->completed
) {
294 ret
->completed
= TRUE
;
295 for (n
= 0; n
< ret
->n
; n
++)
296 if (ret
->tiles
[n
] != n
+1)
297 ret
->completed
= FALSE
;
303 /* ----------------------------------------------------------------------
307 struct game_drawstate
{
313 void game_size(game_params
*params
, int *x
, int *y
)
315 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
316 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
319 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
321 float *ret
= snewn(3 * NCOLOURS
, float);
325 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
328 * Drop the background colour so that the highlight is
329 * noticeably brighter than it while still being under 1.
331 max
= ret
[COL_BACKGROUND
*3];
332 for (i
= 1; i
< 3; i
++)
333 if (ret
[COL_BACKGROUND
*3+i
] > max
)
334 max
= ret
[COL_BACKGROUND
*3+i
];
335 if (max
* 1.2F
> 1.0F
) {
336 for (i
= 0; i
< 3; i
++)
337 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
340 for (i
= 0; i
< 3; i
++) {
341 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
342 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
343 ret
[COL_TEXT
* 3 + i
] = 0.0;
346 *ncolours
= NCOLOURS
;
350 game_drawstate
*game_new_drawstate(game_state
*state
)
352 struct game_drawstate
*ds
= snew(struct game_drawstate
);
358 ds
->bgcolour
= COL_BACKGROUND
;
359 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
360 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
366 void game_free_drawstate(game_drawstate
*ds
)
372 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
373 int tile
, int flash_colour
)
376 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
382 coords
[0] = x
+ TILE_SIZE
- 1;
383 coords
[1] = y
+ TILE_SIZE
- 1;
384 coords
[2] = x
+ TILE_SIZE
- 1;
387 coords
[5] = y
+ TILE_SIZE
- 1;
388 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
389 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
393 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
394 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
396 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
397 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
400 sprintf(str
, "%d", tile
);
401 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
402 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
405 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
408 static void draw_arrow(frontend
*fe
, int x
, int y
, int xdx
, int xdy
)
411 int ydy
= -xdx
, ydx
= xdy
;
413 #define POINT(n, xx, yy) ( \
414 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
415 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
417 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
418 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
419 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
420 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
421 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
422 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
423 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
425 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
426 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
429 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
430 game_state
*state
, float animtime
, float flashtime
)
432 int i
, pass
, bgcolour
;
435 int frame
= (int)(flashtime
/ FLASH_FRAME
);
436 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
438 bgcolour
= COL_BACKGROUND
;
444 TILE_SIZE
* state
->w
+ 2 * BORDER
,
445 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
446 draw_update(fe
, 0, 0,
447 TILE_SIZE
* state
->w
+ 2 * BORDER
,
448 TILE_SIZE
* state
->h
+ 2 * BORDER
);
451 * Recessed area containing the whole puzzle.
453 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
454 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
455 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
456 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
457 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
458 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
459 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
460 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
462 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
463 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
464 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
465 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
468 * Arrows for making moves.
470 for (i
= 0; i
< state
->w
; i
++) {
471 draw_arrow(fe
, COORD(i
), COORD(0), +1, 0);
472 draw_arrow(fe
, COORD(i
+1), COORD(state
->h
), -1, 0);
474 for (i
= 0; i
< state
->h
; i
++) {
475 draw_arrow(fe
, COORD(state
->w
), COORD(i
), 0, +1);
476 draw_arrow(fe
, COORD(0), COORD(i
+1), 0, -1);
483 * Now draw each tile. We do this in two passes to make
487 clip(fe
, COORD(0), COORD(0), TILE_SIZE
*state
->w
, TILE_SIZE
*state
->h
);
489 for (pass
= 0; pass
< 2; pass
++) {
490 for (i
= 0; i
< state
->n
; i
++) {
493 * Figure out what should be displayed at this
494 * location. It's either a simple tile, or it's a
495 * transition between two tiles (in which case we say
496 * -1 because it must always be drawn).
499 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
506 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
507 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
511 * Figure out what to _actually_ draw, and where to
515 int x0
, y0
, x1
, y1
, dx
, dy
;
519 * On the first pass, just blank the tile.
522 x
= COORD(X(state
, i
));
523 y
= COORD(Y(state
, i
));
532 * FIXME: must be prepared to draw a double
533 * tile in some situations.
537 * Find the coordinates of this tile in the old and
540 x1
= COORD(X(state
, i
));
541 y1
= COORD(Y(state
, i
));
542 for (j
= 0; j
< oldstate
->n
; j
++)
543 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
545 assert(j
< oldstate
->n
);
546 x0
= COORD(X(state
, j
));
547 y0
= COORD(Y(state
, j
));
550 if (abs(dx
) > TILE_SIZE
) {
551 dx
= (dx
< 0 ? dx
+ TILE_SIZE
* state
->w
:
552 dx
- TILE_SIZE
* state
->w
);
553 assert(abs(dx
) == TILE_SIZE
);
556 if (abs(dy
) > TILE_SIZE
) {
557 dy
= (dy
< 0 ? dy
+ TILE_SIZE
* state
->h
:
558 dy
- TILE_SIZE
* state
->h
);
559 assert(abs(dy
) == TILE_SIZE
);
562 c
= (animtime
/ ANIM_TIME
);
563 if (c
< 0.0F
) c
= 0.0F
;
564 if (c
> 1.0F
) c
= 1.0F
;
566 x
= x0
+ (int)(c
* dx
);
567 y
= y0
+ (int)(c
* dy
);
568 x2
= x1
- dx
+ (int)(c
* dx
);
569 y2
= y1
- dy
+ (int)(c
* dy
);
575 x
= COORD(X(state
, i
));
576 y
= COORD(Y(state
, i
));
580 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
581 if (x2
!= -1 || y2
!= -1)
582 draw_tile(fe
, state
, x2
, y2
, t
, bgcolour
);
590 ds
->bgcolour
= bgcolour
;
593 float game_anim_length(game_state
*oldstate
, game_state
*newstate
)
598 float game_flash_length(game_state
*oldstate
, game_state
*newstate
)
600 if (!oldstate
->completed
&& newstate
->completed
)
601 return 2 * FLASH_FRAME
;