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";
16 const int game_can_configure
= TRUE
;
19 #define BORDER TILE_SIZE /* big border to fill with arrows */
20 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
21 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
22 #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
24 #define ANIM_TIME 0.1F
25 #define FLASH_FRAME 0.1F
27 #define X(state, i) ( (i) % (state)->w )
28 #define Y(state, i) ( (i) / (state)->w )
29 #define C(state, x, y) ( (y) * (state)->w + (x) )
48 int last_movement_sense
;
51 game_params
*default_params(void)
53 game_params
*ret
= snew(game_params
);
60 int game_fetch_preset(int i
, char **name
, game_params
**params
)
67 case 0: w
= 3, h
= 3; break;
68 case 1: w
= 4, h
= 3; break;
69 case 2: w
= 4, h
= 4; break;
70 case 3: w
= 5, h
= 4; break;
71 case 4: w
= 5, h
= 5; break;
72 default: return FALSE
;
75 sprintf(buf
, "%dx%d", w
, h
);
77 *params
= ret
= snew(game_params
);
83 void free_params(game_params
*params
)
88 game_params
*dup_params(game_params
*params
)
90 game_params
*ret
= snew(game_params
);
91 *ret
= *params
; /* structure copy */
95 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 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 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 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 char *new_game_seed(game_params
*params
)
162 n
= params
->w
* params
->h
;
164 tiles
= snewn(n
, int);
165 used
= snewn(n
, int);
167 for (i
= 0; i
< n
; i
++) {
173 * If both dimensions are odd, there is a parity constraint.
175 if (params
->w
& params
->h
& 1)
181 * Place everything except (possibly) the last two tiles.
183 for (x
= 0, i
= n
; i
> stop
; i
--) {
184 int k
= i
> 1 ?
rand_upto(i
) : 0;
187 for (j
= 0; j
< n
; j
++)
188 if (!used
[j
] && (k
-- == 0))
191 assert(j
< n
&& !used
[j
]);
194 while (tiles
[x
] >= 0)
202 * Find the last two locations, and the last two pieces.
204 while (tiles
[x
] >= 0)
209 while (tiles
[x
] >= 0)
214 for (i
= 0; i
< n
; i
++)
218 for (i
= p1
+1; i
< n
; i
++)
224 * Try the last two tiles one way round. If that fails, swap
229 if (perm_parity(tiles
, n
) != 0) {
232 assert(perm_parity(tiles
, n
) == 0);
237 * Now construct the game seed, by describing the tile array as
238 * a simple sequence of comma-separated integers.
242 for (i
= 0; i
< n
; i
++) {
246 k
= sprintf(buf
, "%d,", tiles
[i
]+1);
248 ret
= sresize(ret
, retlen
+ k
+ 1, char);
249 strcpy(ret
+ retlen
, buf
);
252 ret
[retlen
-1] = '\0'; /* delete last comma */
260 game_state
*new_game(game_params
*params
, char *seed
)
262 game_state
*state
= snew(game_state
);
266 state
->w
= params
->w
;
267 state
->h
= params
->h
;
268 state
->n
= params
->w
* params
->h
;
269 state
->tiles
= snewn(state
->n
, int);
273 for (i
= 0; i
< state
->n
; i
++) {
275 state
->tiles
[i
] = atoi(p
);
276 while (*p
&& *p
!= ',')
278 if (*p
) p
++; /* eat comma */
282 state
->completed
= state
->movecount
= 0;
283 state
->last_movement_sense
= 0;
288 game_state
*dup_game(game_state
*state
)
290 game_state
*ret
= snew(game_state
);
295 ret
->tiles
= snewn(state
->w
* state
->h
, int);
296 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
297 ret
->completed
= state
->completed
;
298 ret
->movecount
= state
->movecount
;
299 ret
->last_movement_sense
= state
->last_movement_sense
;
304 void free_game(game_state
*state
)
309 game_state
*make_move(game_state
*from
, int x
, int y
, int button
)
312 int dx
, dy
, tx
, ty
, n
;
315 if (button
!= LEFT_BUTTON
)
320 if (cx
== -1 && cy
>= 0 && cy
< from
->h
)
321 n
= from
->w
, dx
= +1, dy
= 0;
322 else if (cx
== from
->w
&& cy
>= 0 && cy
< from
->h
)
323 n
= from
->w
, dx
= -1, dy
= 0;
324 else if (cy
== -1 && cx
>= 0 && cx
< from
->w
)
325 n
= from
->h
, dy
= +1, dx
= 0;
326 else if (cy
== from
->h
&& cx
>= 0 && cx
< from
->w
)
327 n
= from
->h
, dy
= -1, dx
= 0;
329 return NULL
; /* invalid click location */
331 ret
= dup_game(from
);
336 tx
= (cx
+ dx
+ from
->w
) % from
->w
;
337 ty
= (cy
+ dy
+ from
->h
) % from
->h
;
338 ret
->tiles
[C(ret
, cx
, cy
)] = from
->tiles
[C(from
, tx
, ty
)];
343 ret
->last_movement_sense
= -(dx
+dy
);
346 * See if the game has been completed.
348 if (!ret
->completed
) {
349 ret
->completed
= ret
->movecount
;
350 for (n
= 0; n
< ret
->n
; n
++)
351 if (ret
->tiles
[n
] != n
+1)
352 ret
->completed
= FALSE
;
358 /* ----------------------------------------------------------------------
362 struct game_drawstate
{
368 void game_size(game_params
*params
, int *x
, int *y
)
370 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
371 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
374 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
376 float *ret
= snewn(3 * NCOLOURS
, float);
380 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
383 * Drop the background colour so that the highlight is
384 * noticeably brighter than it while still being under 1.
386 max
= ret
[COL_BACKGROUND
*3];
387 for (i
= 1; i
< 3; i
++)
388 if (ret
[COL_BACKGROUND
*3+i
] > max
)
389 max
= ret
[COL_BACKGROUND
*3+i
];
390 if (max
* 1.2F
> 1.0F
) {
391 for (i
= 0; i
< 3; i
++)
392 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
395 for (i
= 0; i
< 3; i
++) {
396 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
397 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
398 ret
[COL_TEXT
* 3 + i
] = 0.0;
401 *ncolours
= NCOLOURS
;
405 game_drawstate
*game_new_drawstate(game_state
*state
)
407 struct game_drawstate
*ds
= snew(struct game_drawstate
);
413 ds
->bgcolour
= COL_BACKGROUND
;
414 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
415 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
421 void game_free_drawstate(game_drawstate
*ds
)
427 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
428 int tile
, int flash_colour
)
431 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
437 coords
[0] = x
+ TILE_SIZE
- 1;
438 coords
[1] = y
+ TILE_SIZE
- 1;
439 coords
[2] = x
+ TILE_SIZE
- 1;
442 coords
[5] = y
+ TILE_SIZE
- 1;
443 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
444 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
448 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
449 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
451 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
452 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
455 sprintf(str
, "%d", tile
);
456 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
457 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
460 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
463 static void draw_arrow(frontend
*fe
, int x
, int y
, int xdx
, int xdy
)
466 int ydy
= -xdx
, ydx
= xdy
;
468 #define POINT(n, xx, yy) ( \
469 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
470 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
472 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
473 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
474 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
475 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
476 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
477 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
478 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
480 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
481 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
484 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
485 game_state
*state
, float animtime
, float flashtime
)
487 int i
, pass
, bgcolour
;
490 int frame
= (int)(flashtime
/ FLASH_FRAME
);
491 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
493 bgcolour
= COL_BACKGROUND
;
499 TILE_SIZE
* state
->w
+ 2 * BORDER
,
500 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
501 draw_update(fe
, 0, 0,
502 TILE_SIZE
* state
->w
+ 2 * BORDER
,
503 TILE_SIZE
* state
->h
+ 2 * BORDER
);
506 * Recessed area containing the whole puzzle.
508 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
509 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
510 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
511 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
512 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
513 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
514 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
515 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
517 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
518 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
519 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
520 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
523 * Arrows for making moves.
525 for (i
= 0; i
< state
->w
; i
++) {
526 draw_arrow(fe
, COORD(i
), COORD(0), +1, 0);
527 draw_arrow(fe
, COORD(i
+1), COORD(state
->h
), -1, 0);
529 for (i
= 0; i
< state
->h
; i
++) {
530 draw_arrow(fe
, COORD(state
->w
), COORD(i
), 0, +1);
531 draw_arrow(fe
, COORD(0), COORD(i
+1), 0, -1);
538 * Now draw each tile. We do this in two passes to make
542 clip(fe
, COORD(0), COORD(0), TILE_SIZE
*state
->w
, TILE_SIZE
*state
->h
);
544 for (pass
= 0; pass
< 2; pass
++) {
545 for (i
= 0; i
< state
->n
; i
++) {
548 * Figure out what should be displayed at this
549 * location. It's either a simple tile, or it's a
550 * transition between two tiles (in which case we say
551 * -1 because it must always be drawn).
554 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
561 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
562 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
566 * Figure out what to _actually_ draw, and where to
570 int x0
, y0
, x1
, y1
, dx
, dy
;
574 * On the first pass, just blank the tile.
577 x
= COORD(X(state
, i
));
578 y
= COORD(Y(state
, i
));
587 * FIXME: must be prepared to draw a double
588 * tile in some situations.
592 * Find the coordinates of this tile in the old and
595 x1
= COORD(X(state
, i
));
596 y1
= COORD(Y(state
, i
));
597 for (j
= 0; j
< oldstate
->n
; j
++)
598 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
600 assert(j
< oldstate
->n
);
601 x0
= COORD(X(state
, j
));
602 y0
= COORD(Y(state
, j
));
606 dx
!= TILE_SIZE
* state
->last_movement_sense
) {
607 dx
= (dx
< 0 ? dx
+ TILE_SIZE
* state
->w
:
608 dx
- TILE_SIZE
* state
->w
);
609 assert(abs(dx
) == TILE_SIZE
);
613 dy
!= TILE_SIZE
* state
->last_movement_sense
) {
614 dy
= (dy
< 0 ? dy
+ TILE_SIZE
* state
->h
:
615 dy
- TILE_SIZE
* state
->h
);
616 assert(abs(dy
) == TILE_SIZE
);
619 c
= (animtime
/ ANIM_TIME
);
620 if (c
< 0.0F
) c
= 0.0F
;
621 if (c
> 1.0F
) c
= 1.0F
;
623 x
= x0
+ (int)(c
* dx
);
624 y
= y0
+ (int)(c
* dy
);
625 x2
= x1
- dx
+ (int)(c
* dx
);
626 y2
= y1
- dy
+ (int)(c
* dy
);
632 x
= COORD(X(state
, i
));
633 y
= COORD(Y(state
, i
));
637 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
638 if (x2
!= -1 || y2
!= -1)
639 draw_tile(fe
, state
, x2
, y2
, 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)