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
16 const char *const game_name
= "Sixteen";
17 const char *const game_winhelp_topic
= "games.sixteen";
18 const int game_can_configure
= TRUE
;
21 #define BORDER TILE_SIZE /* big border to fill with arrows */
22 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
23 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
24 #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
26 #define ANIM_TIME 0.13F
27 #define FLASH_FRAME 0.13F
29 #define X(state, i) ( (i) % (state)->w )
30 #define Y(state, i) ( (i) / (state)->w )
31 #define C(state, x, y) ( (y) * (state)->w + (x) )
50 int last_movement_sense
;
53 game_params
*default_params(void)
55 game_params
*ret
= snew(game_params
);
62 int game_fetch_preset(int i
, char **name
, game_params
**params
)
69 case 0: w
= 3, h
= 3; break;
70 case 1: w
= 4, h
= 3; break;
71 case 2: w
= 4, h
= 4; break;
72 case 3: w
= 5, h
= 4; break;
73 case 4: w
= 5, h
= 5; break;
74 default: return FALSE
;
77 sprintf(buf
, "%dx%d", w
, h
);
79 *params
= ret
= snew(game_params
);
85 void free_params(game_params
*params
)
90 game_params
*dup_params(game_params
*params
)
92 game_params
*ret
= snew(game_params
);
93 *ret
= *params
; /* structure copy */
97 game_params
*decode_params(char const *string
)
99 game_params
*ret
= default_params();
101 ret
->w
= ret
->h
= atoi(string
);
102 while (*string
&& isdigit(*string
)) string
++;
103 if (*string
== 'x') {
105 ret
->h
= atoi(string
);
111 char *encode_params(game_params
*params
)
115 sprintf(data
, "%dx%d", params
->w
, params
->h
);
120 config_item
*game_configure(game_params
*params
)
125 ret
= snewn(3, config_item
);
127 ret
[0].name
= "Width";
128 ret
[0].type
= C_STRING
;
129 sprintf(buf
, "%d", params
->w
);
130 ret
[0].sval
= dupstr(buf
);
133 ret
[1].name
= "Height";
134 ret
[1].type
= C_STRING
;
135 sprintf(buf
, "%d", params
->h
);
136 ret
[1].sval
= dupstr(buf
);
147 game_params
*custom_params(config_item
*cfg
)
149 game_params
*ret
= snew(game_params
);
151 ret
->w
= atoi(cfg
[0].sval
);
152 ret
->h
= atoi(cfg
[1].sval
);
157 char *validate_params(game_params
*params
)
159 if (params
->w
< 2 && params
->h
< 2)
160 return "Width and height must both be at least two";
165 int perm_parity(int *perm
, int n
)
171 for (i
= 0; i
< n
-1; i
++)
172 for (j
= i
+1; j
< n
; j
++)
173 if (perm
[i
] > perm
[j
])
179 char *new_game_seed(game_params
*params
, random_state
*rs
)
187 n
= params
->w
* params
->h
;
189 tiles
= snewn(n
, int);
190 used
= snewn(n
, int);
192 for (i
= 0; i
< n
; i
++) {
198 * If both dimensions are odd, there is a parity constraint.
200 if (params
->w
& params
->h
& 1)
206 * Place everything except (possibly) the last two tiles.
208 for (x
= 0, i
= n
; i
> stop
; i
--) {
209 int k
= i
> 1 ?
random_upto(rs
, i
) : 0;
212 for (j
= 0; j
< n
; j
++)
213 if (!used
[j
] && (k
-- == 0))
216 assert(j
< n
&& !used
[j
]);
219 while (tiles
[x
] >= 0)
227 * Find the last two locations, and the last two pieces.
229 while (tiles
[x
] >= 0)
234 while (tiles
[x
] >= 0)
239 for (i
= 0; i
< n
; i
++)
243 for (i
= p1
+1; i
< n
; i
++)
249 * Try the last two tiles one way round. If that fails, swap
254 if (perm_parity(tiles
, n
) != 0) {
257 assert(perm_parity(tiles
, n
) == 0);
262 * Now construct the game seed, by describing the tile array as
263 * a simple sequence of comma-separated integers.
267 for (i
= 0; i
< n
; i
++) {
271 k
= sprintf(buf
, "%d,", tiles
[i
]+1);
273 ret
= sresize(ret
, retlen
+ k
+ 1, char);
274 strcpy(ret
+ retlen
, buf
);
277 ret
[retlen
-1] = '\0'; /* delete last comma */
286 char *validate_seed(game_params
*params
, char *seed
)
292 area
= params
->w
* params
->h
;
296 used
= snewn(area
, int);
297 for (i
= 0; i
< area
; i
++)
300 for (i
= 0; i
< area
; i
++) {
304 if (*p
< '0' || *p
> '9') {
305 err
= "Not enough numbers in string";
308 while (*p
>= '0' && *p
<= '9')
310 if (i
< area
-1 && *p
!= ',') {
311 err
= "Expected comma after number";
314 else if (i
== area
-1 && *p
) {
315 err
= "Excess junk at end of string";
319 if (n
< 1 || n
> area
) {
320 err
= "Number out of range";
324 err
= "Number used twice";
329 if (*p
) p
++; /* eat comma */
337 game_state
*new_game(game_params
*params
, char *seed
)
339 game_state
*state
= snew(game_state
);
343 state
->w
= params
->w
;
344 state
->h
= params
->h
;
345 state
->n
= params
->w
* params
->h
;
346 state
->tiles
= snewn(state
->n
, int);
350 for (i
= 0; i
< state
->n
; i
++) {
352 state
->tiles
[i
] = atoi(p
);
353 while (*p
&& *p
!= ',')
355 if (*p
) p
++; /* eat comma */
359 state
->completed
= state
->movecount
= 0;
360 state
->last_movement_sense
= 0;
365 game_state
*dup_game(game_state
*state
)
367 game_state
*ret
= snew(game_state
);
372 ret
->tiles
= snewn(state
->w
* state
->h
, int);
373 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
374 ret
->completed
= state
->completed
;
375 ret
->movecount
= state
->movecount
;
376 ret
->last_movement_sense
= state
->last_movement_sense
;
381 void free_game(game_state
*state
)
386 game_ui
*new_ui(game_state
*state
)
391 void free_ui(game_ui
*ui
)
395 game_state
*make_move(game_state
*from
, game_ui
*ui
, int x
, int y
, int button
)
398 int dx
, dy
, tx
, ty
, n
;
401 if (button
!= LEFT_BUTTON
&& button
!= RIGHT_BUTTON
)
406 if (cx
== -1 && cy
>= 0 && cy
< from
->h
)
407 n
= from
->w
, dx
= +1, dy
= 0;
408 else if (cx
== from
->w
&& cy
>= 0 && cy
< from
->h
)
409 n
= from
->w
, dx
= -1, dy
= 0;
410 else if (cy
== -1 && cx
>= 0 && cx
< from
->w
)
411 n
= from
->h
, dy
= +1, dx
= 0;
412 else if (cy
== from
->h
&& cx
>= 0 && cx
< from
->w
)
413 n
= from
->h
, dy
= -1, dx
= 0;
415 return NULL
; /* invalid click location */
417 /* reverse direction if right hand button is pressed */
418 if (button
== RIGHT_BUTTON
)
420 dx
= -dx
; if (dx
) cx
= from
->w
- 1 - cx
;
421 dy
= -dy
; if (dy
) cy
= from
->h
- 1 - cy
;
424 ret
= dup_game(from
);
429 tx
= (cx
+ dx
+ from
->w
) % from
->w
;
430 ty
= (cy
+ dy
+ from
->h
) % from
->h
;
431 ret
->tiles
[C(ret
, cx
, cy
)] = from
->tiles
[C(from
, tx
, ty
)];
436 ret
->last_movement_sense
= -(dx
+dy
);
439 * See if the game has been completed.
441 if (!ret
->completed
) {
442 ret
->completed
= ret
->movecount
;
443 for (n
= 0; n
< ret
->n
; n
++)
444 if (ret
->tiles
[n
] != n
+1)
445 ret
->completed
= FALSE
;
451 /* ----------------------------------------------------------------------
455 struct game_drawstate
{
461 void game_size(game_params
*params
, int *x
, int *y
)
463 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
464 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
467 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
469 float *ret
= snewn(3 * NCOLOURS
, float);
473 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
476 * Drop the background colour so that the highlight is
477 * noticeably brighter than it while still being under 1.
479 max
= ret
[COL_BACKGROUND
*3];
480 for (i
= 1; i
< 3; i
++)
481 if (ret
[COL_BACKGROUND
*3+i
] > max
)
482 max
= ret
[COL_BACKGROUND
*3+i
];
483 if (max
* 1.2F
> 1.0F
) {
484 for (i
= 0; i
< 3; i
++)
485 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
488 for (i
= 0; i
< 3; i
++) {
489 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
490 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
491 ret
[COL_TEXT
* 3 + i
] = 0.0;
494 *ncolours
= NCOLOURS
;
498 game_drawstate
*game_new_drawstate(game_state
*state
)
500 struct game_drawstate
*ds
= snew(struct game_drawstate
);
506 ds
->bgcolour
= COL_BACKGROUND
;
507 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
508 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
514 void game_free_drawstate(game_drawstate
*ds
)
520 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
521 int tile
, int flash_colour
)
524 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
530 coords
[0] = x
+ TILE_SIZE
- 1;
531 coords
[1] = y
+ TILE_SIZE
- 1;
532 coords
[2] = x
+ TILE_SIZE
- 1;
535 coords
[5] = y
+ TILE_SIZE
- 1;
536 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
537 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
541 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
542 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
544 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
545 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
548 sprintf(str
, "%d", tile
);
549 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
550 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
553 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
556 static void draw_arrow(frontend
*fe
, int x
, int y
, int xdx
, int xdy
)
559 int ydy
= -xdx
, ydx
= xdy
;
561 #define POINT(n, xx, yy) ( \
562 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
563 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
565 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
566 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
567 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
568 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
569 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
570 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
571 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
573 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
574 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
577 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
578 game_state
*state
, int dir
, game_ui
*ui
,
579 float animtime
, float flashtime
)
584 int frame
= (int)(flashtime
/ FLASH_FRAME
);
585 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
587 bgcolour
= COL_BACKGROUND
;
593 TILE_SIZE
* state
->w
+ 2 * BORDER
,
594 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
595 draw_update(fe
, 0, 0,
596 TILE_SIZE
* state
->w
+ 2 * BORDER
,
597 TILE_SIZE
* state
->h
+ 2 * BORDER
);
600 * Recessed area containing the whole puzzle.
602 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
603 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
604 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
605 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
606 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
607 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
608 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
609 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
611 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
612 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
613 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
614 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
617 * Arrows for making moves.
619 for (i
= 0; i
< state
->w
; i
++) {
620 draw_arrow(fe
, COORD(i
), COORD(0), +1, 0);
621 draw_arrow(fe
, COORD(i
+1), COORD(state
->h
), -1, 0);
623 for (i
= 0; i
< state
->h
; i
++) {
624 draw_arrow(fe
, COORD(state
->w
), COORD(i
), 0, +1);
625 draw_arrow(fe
, COORD(0), COORD(i
+1), 0, -1);
632 * Now draw each tile.
635 clip(fe
, COORD(0), COORD(0), TILE_SIZE
*state
->w
, TILE_SIZE
*state
->h
);
637 for (i
= 0; i
< state
->n
; i
++) {
640 * Figure out what should be displayed at this
641 * location. It's either a simple tile, or it's a
642 * transition between two tiles (in which case we say
643 * -1 because it must always be drawn).
646 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
653 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
654 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
658 * Figure out what to _actually_ draw, and where to
662 int x0
, y0
, x1
, y1
, dx
, dy
;
669 sense
= -oldstate
->last_movement_sense
;
671 sense
= state
->last_movement_sense
;
677 * FIXME: must be prepared to draw a double
678 * tile in some situations.
682 * Find the coordinates of this tile in the old and
685 x1
= COORD(X(state
, i
));
686 y1
= COORD(Y(state
, i
));
687 for (j
= 0; j
< oldstate
->n
; j
++)
688 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
690 assert(j
< oldstate
->n
);
691 x0
= COORD(X(state
, j
));
692 y0
= COORD(Y(state
, j
));
696 dx
!= TILE_SIZE
* sense
) {
697 dx
= (dx
< 0 ? dx
+ TILE_SIZE
* state
->w
:
698 dx
- TILE_SIZE
* state
->w
);
699 assert(abs(dx
) == TILE_SIZE
);
703 dy
!= TILE_SIZE
* sense
) {
704 dy
= (dy
< 0 ? dy
+ TILE_SIZE
* state
->h
:
705 dy
- TILE_SIZE
* state
->h
);
706 assert(abs(dy
) == TILE_SIZE
);
709 c
= (animtime
/ ANIM_TIME
);
710 if (c
< 0.0F
) c
= 0.0F
;
711 if (c
> 1.0F
) c
= 1.0F
;
713 x
= x0
+ (int)(c
* dx
);
714 y
= y0
+ (int)(c
* dy
);
715 x2
= x1
- dx
+ (int)(c
* dx
);
716 y2
= y1
- dy
+ (int)(c
* dy
);
718 x
= COORD(X(state
, i
));
719 y
= COORD(Y(state
, i
));
723 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
724 if (x2
!= -1 || y2
!= -1)
725 draw_tile(fe
, state
, x2
, y2
, t
, bgcolour
);
732 ds
->bgcolour
= bgcolour
;
735 * Update the status bar.
741 * Don't show the new status until we're also showing the
742 * new _state_ - after the game animation is complete.
747 sprintf(statusbuf
, "%sMoves: %d",
748 (state
->completed ?
"COMPLETED! " : ""),
749 (state
->completed ? state
->completed
: state
->movecount
));
751 status_bar(fe
, statusbuf
);
755 float game_anim_length(game_state
*oldstate
, game_state
*newstate
, int dir
)
760 float game_flash_length(game_state
*oldstate
, game_state
*newstate
, int dir
)
762 if (!oldstate
->completed
&& newstate
->completed
)
763 return 2 * FLASH_FRAME
;
768 int game_wants_statusbar(void)