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
17 #define BORDER TILE_SIZE /* big border to fill with arrows */
18 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
19 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
20 #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 )
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) )
46 int last_movement_sense
;
49 static game_params
*default_params(void)
51 game_params
*ret
= snew(game_params
);
58 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
65 case 0: w
= 3, h
= 3; break;
66 case 1: w
= 4, h
= 3; break;
67 case 2: w
= 4, h
= 4; break;
68 case 3: w
= 5, h
= 4; break;
69 case 4: w
= 5, h
= 5; break;
70 default: return FALSE
;
73 sprintf(buf
, "%dx%d", w
, h
);
75 *params
= ret
= snew(game_params
);
81 static void free_params(game_params
*params
)
86 static game_params
*dup_params(game_params
*params
)
88 game_params
*ret
= snew(game_params
);
89 *ret
= *params
; /* structure copy */
93 static game_params
*decode_params(char const *string
)
95 game_params
*ret
= default_params();
97 ret
->w
= ret
->h
= atoi(string
);
98 while (*string
&& isdigit(*string
)) string
++;
101 ret
->h
= atoi(string
);
107 static char *encode_params(game_params
*params
)
111 sprintf(data
, "%dx%d", params
->w
, params
->h
);
116 static config_item
*game_configure(game_params
*params
)
121 ret
= snewn(3, config_item
);
123 ret
[0].name
= "Width";
124 ret
[0].type
= C_STRING
;
125 sprintf(buf
, "%d", params
->w
);
126 ret
[0].sval
= dupstr(buf
);
129 ret
[1].name
= "Height";
130 ret
[1].type
= C_STRING
;
131 sprintf(buf
, "%d", params
->h
);
132 ret
[1].sval
= dupstr(buf
);
143 static game_params
*custom_params(config_item
*cfg
)
145 game_params
*ret
= snew(game_params
);
147 ret
->w
= atoi(cfg
[0].sval
);
148 ret
->h
= atoi(cfg
[1].sval
);
153 static char *validate_params(game_params
*params
)
155 if (params
->w
< 2 && params
->h
< 2)
156 return "Width and height must both be at least two";
161 static int perm_parity(int *perm
, int n
)
167 for (i
= 0; i
< n
-1; i
++)
168 for (j
= i
+1; j
< n
; j
++)
169 if (perm
[i
] > perm
[j
])
175 static char *new_game_seed(game_params
*params
, random_state
*rs
)
183 n
= params
->w
* params
->h
;
185 tiles
= snewn(n
, int);
186 used
= snewn(n
, int);
188 for (i
= 0; i
< n
; i
++) {
194 * If both dimensions are odd, there is a parity constraint.
196 if (params
->w
& params
->h
& 1)
202 * Place everything except (possibly) the last two tiles.
204 for (x
= 0, i
= n
; i
> stop
; i
--) {
205 int k
= i
> 1 ?
random_upto(rs
, i
) : 0;
208 for (j
= 0; j
< n
; j
++)
209 if (!used
[j
] && (k
-- == 0))
212 assert(j
< n
&& !used
[j
]);
215 while (tiles
[x
] >= 0)
223 * Find the last two locations, and the last two pieces.
225 while (tiles
[x
] >= 0)
230 while (tiles
[x
] >= 0)
235 for (i
= 0; i
< n
; i
++)
239 for (i
= p1
+1; i
< n
; i
++)
245 * Try the last two tiles one way round. If that fails, swap
250 if (perm_parity(tiles
, n
) != 0) {
253 assert(perm_parity(tiles
, n
) == 0);
258 * Now construct the game seed, by describing the tile array as
259 * a simple sequence of comma-separated integers.
263 for (i
= 0; i
< n
; i
++) {
267 k
= sprintf(buf
, "%d,", tiles
[i
]+1);
269 ret
= sresize(ret
, retlen
+ k
+ 1, char);
270 strcpy(ret
+ retlen
, buf
);
273 ret
[retlen
-1] = '\0'; /* delete last comma */
282 static char *validate_seed(game_params
*params
, char *seed
)
288 area
= params
->w
* params
->h
;
292 used
= snewn(area
, int);
293 for (i
= 0; i
< area
; i
++)
296 for (i
= 0; i
< area
; i
++) {
300 if (*p
< '0' || *p
> '9') {
301 err
= "Not enough numbers in string";
304 while (*p
>= '0' && *p
<= '9')
306 if (i
< area
-1 && *p
!= ',') {
307 err
= "Expected comma after number";
310 else if (i
== area
-1 && *p
) {
311 err
= "Excess junk at end of string";
315 if (n
< 1 || n
> area
) {
316 err
= "Number out of range";
320 err
= "Number used twice";
325 if (*p
) p
++; /* eat comma */
333 static game_state
*new_game(game_params
*params
, char *seed
)
335 game_state
*state
= snew(game_state
);
339 state
->w
= params
->w
;
340 state
->h
= params
->h
;
341 state
->n
= params
->w
* params
->h
;
342 state
->tiles
= snewn(state
->n
, int);
346 for (i
= 0; i
< state
->n
; i
++) {
348 state
->tiles
[i
] = atoi(p
);
349 while (*p
&& *p
!= ',')
351 if (*p
) p
++; /* eat comma */
355 state
->completed
= state
->movecount
= 0;
356 state
->last_movement_sense
= 0;
361 static game_state
*dup_game(game_state
*state
)
363 game_state
*ret
= snew(game_state
);
368 ret
->tiles
= snewn(state
->w
* state
->h
, int);
369 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
370 ret
->completed
= state
->completed
;
371 ret
->movecount
= state
->movecount
;
372 ret
->last_movement_sense
= state
->last_movement_sense
;
377 static void free_game(game_state
*state
)
382 static char *game_text_format(game_state
*state
)
387 static game_ui
*new_ui(game_state
*state
)
392 static void free_ui(game_ui
*ui
)
396 static game_state
*make_move(game_state
*from
, game_ui
*ui
,
397 int x
, int y
, int button
)
400 int dx
, dy
, tx
, ty
, n
;
403 if (button
!= LEFT_BUTTON
&& button
!= RIGHT_BUTTON
)
408 if (cx
== -1 && cy
>= 0 && cy
< from
->h
)
409 n
= from
->w
, dx
= +1, dy
= 0;
410 else if (cx
== from
->w
&& cy
>= 0 && cy
< from
->h
)
411 n
= from
->w
, dx
= -1, dy
= 0;
412 else if (cy
== -1 && cx
>= 0 && cx
< from
->w
)
413 n
= from
->h
, dy
= +1, dx
= 0;
414 else if (cy
== from
->h
&& cx
>= 0 && cx
< from
->w
)
415 n
= from
->h
, dy
= -1, dx
= 0;
417 return NULL
; /* invalid click location */
419 /* reverse direction if right hand button is pressed */
420 if (button
== RIGHT_BUTTON
)
422 dx
= -dx
; if (dx
) cx
= from
->w
- 1 - cx
;
423 dy
= -dy
; if (dy
) cy
= from
->h
- 1 - cy
;
426 ret
= dup_game(from
);
431 tx
= (cx
+ dx
+ from
->w
) % from
->w
;
432 ty
= (cy
+ dy
+ from
->h
) % from
->h
;
433 ret
->tiles
[C(ret
, cx
, cy
)] = from
->tiles
[C(from
, tx
, ty
)];
438 ret
->last_movement_sense
= -(dx
+dy
);
441 * See if the game has been completed.
443 if (!ret
->completed
) {
444 ret
->completed
= ret
->movecount
;
445 for (n
= 0; n
< ret
->n
; n
++)
446 if (ret
->tiles
[n
] != n
+1)
447 ret
->completed
= FALSE
;
453 /* ----------------------------------------------------------------------
457 struct game_drawstate
{
463 static void game_size(game_params
*params
, int *x
, int *y
)
465 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
466 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
469 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
471 float *ret
= snewn(3 * NCOLOURS
, float);
475 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
478 * Drop the background colour so that the highlight is
479 * noticeably brighter than it while still being under 1.
481 max
= ret
[COL_BACKGROUND
*3];
482 for (i
= 1; i
< 3; i
++)
483 if (ret
[COL_BACKGROUND
*3+i
] > max
)
484 max
= ret
[COL_BACKGROUND
*3+i
];
485 if (max
* 1.2F
> 1.0F
) {
486 for (i
= 0; i
< 3; i
++)
487 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
490 for (i
= 0; i
< 3; i
++) {
491 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
492 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
493 ret
[COL_TEXT
* 3 + i
] = 0.0;
496 *ncolours
= NCOLOURS
;
500 static game_drawstate
*game_new_drawstate(game_state
*state
)
502 struct game_drawstate
*ds
= snew(struct game_drawstate
);
508 ds
->bgcolour
= COL_BACKGROUND
;
509 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
510 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
516 static void game_free_drawstate(game_drawstate
*ds
)
522 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
523 int tile
, int flash_colour
)
526 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
532 coords
[0] = x
+ TILE_SIZE
- 1;
533 coords
[1] = y
+ TILE_SIZE
- 1;
534 coords
[2] = x
+ TILE_SIZE
- 1;
537 coords
[5] = y
+ TILE_SIZE
- 1;
538 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
539 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
543 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
544 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
546 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
547 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
550 sprintf(str
, "%d", tile
);
551 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
552 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
555 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
558 static void draw_arrow(frontend
*fe
, int x
, int y
, int xdx
, int xdy
)
561 int ydy
= -xdx
, ydx
= xdy
;
563 #define POINT(n, xx, yy) ( \
564 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
565 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
567 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
568 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
569 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
570 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
571 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
572 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
573 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
575 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
576 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
579 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
580 game_state
*state
, int dir
, game_ui
*ui
,
581 float animtime
, float flashtime
)
586 int frame
= (int)(flashtime
/ FLASH_FRAME
);
587 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
589 bgcolour
= COL_BACKGROUND
;
595 TILE_SIZE
* state
->w
+ 2 * BORDER
,
596 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
597 draw_update(fe
, 0, 0,
598 TILE_SIZE
* state
->w
+ 2 * BORDER
,
599 TILE_SIZE
* state
->h
+ 2 * BORDER
);
602 * Recessed area containing the whole puzzle.
604 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
605 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
606 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
607 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
608 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
609 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
610 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
611 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
613 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
614 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
615 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
616 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
619 * Arrows for making moves.
621 for (i
= 0; i
< state
->w
; i
++) {
622 draw_arrow(fe
, COORD(i
), COORD(0), +1, 0);
623 draw_arrow(fe
, COORD(i
+1), COORD(state
->h
), -1, 0);
625 for (i
= 0; i
< state
->h
; i
++) {
626 draw_arrow(fe
, COORD(state
->w
), COORD(i
), 0, +1);
627 draw_arrow(fe
, COORD(0), COORD(i
+1), 0, -1);
634 * Now draw each tile.
637 clip(fe
, COORD(0), COORD(0), TILE_SIZE
*state
->w
, TILE_SIZE
*state
->h
);
639 for (i
= 0; i
< state
->n
; i
++) {
642 * Figure out what should be displayed at this
643 * location. It's either a simple tile, or it's a
644 * transition between two tiles (in which case we say
645 * -1 because it must always be drawn).
648 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
655 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
656 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
660 * Figure out what to _actually_ draw, and where to
664 int x0
, y0
, x1
, y1
, dx
, dy
;
671 sense
= -oldstate
->last_movement_sense
;
673 sense
= state
->last_movement_sense
;
679 * FIXME: must be prepared to draw a double
680 * tile in some situations.
684 * Find the coordinates of this tile in the old and
687 x1
= COORD(X(state
, i
));
688 y1
= COORD(Y(state
, i
));
689 for (j
= 0; j
< oldstate
->n
; j
++)
690 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
692 assert(j
< oldstate
->n
);
693 x0
= COORD(X(state
, j
));
694 y0
= COORD(Y(state
, j
));
698 dx
!= TILE_SIZE
* sense
) {
699 dx
= (dx
< 0 ? dx
+ TILE_SIZE
* state
->w
:
700 dx
- TILE_SIZE
* state
->w
);
701 assert(abs(dx
) == TILE_SIZE
);
705 dy
!= TILE_SIZE
* sense
) {
706 dy
= (dy
< 0 ? dy
+ TILE_SIZE
* state
->h
:
707 dy
- TILE_SIZE
* state
->h
);
708 assert(abs(dy
) == TILE_SIZE
);
711 c
= (animtime
/ ANIM_TIME
);
712 if (c
< 0.0F
) c
= 0.0F
;
713 if (c
> 1.0F
) c
= 1.0F
;
715 x
= x0
+ (int)(c
* dx
);
716 y
= y0
+ (int)(c
* dy
);
717 x2
= x1
- dx
+ (int)(c
* dx
);
718 y2
= y1
- dy
+ (int)(c
* dy
);
720 x
= COORD(X(state
, i
));
721 y
= COORD(Y(state
, i
));
725 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
726 if (x2
!= -1 || y2
!= -1)
727 draw_tile(fe
, state
, x2
, y2
, t
, bgcolour
);
734 ds
->bgcolour
= bgcolour
;
737 * Update the status bar.
743 * Don't show the new status until we're also showing the
744 * new _state_ - after the game animation is complete.
749 sprintf(statusbuf
, "%sMoves: %d",
750 (state
->completed ?
"COMPLETED! " : ""),
751 (state
->completed ? state
->completed
: state
->movecount
));
753 status_bar(fe
, statusbuf
);
757 static float game_anim_length(game_state
*oldstate
,
758 game_state
*newstate
, int dir
)
763 static float game_flash_length(game_state
*oldstate
,
764 game_state
*newstate
, int dir
)
766 if (!oldstate
->completed
&& newstate
->completed
)
767 return 2 * FLASH_FRAME
;
772 static int game_wants_statusbar(void)
778 #define thegame sixteen
781 const struct game thegame
= {
782 "Sixteen", "games.sixteen",
789 TRUE
, game_configure
, custom_params
,
796 FALSE
, game_text_format
,
807 game_wants_statusbar
,