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 just_used_solve
; /* used to suppress undo animation */
47 int used_solve
; /* used to suppress completion flash */
48 int movecount
, movetarget
;
49 int last_movement_sense
;
52 static game_params
*default_params(void)
54 game_params
*ret
= snew(game_params
);
62 static 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
);
86 static void free_params(game_params
*params
)
91 static game_params
*dup_params(game_params
*params
)
93 game_params
*ret
= snew(game_params
);
94 *ret
= *params
; /* structure copy */
98 static game_params
*decode_params(char const *string
)
100 game_params
*ret
= default_params();
102 ret
->w
= ret
->h
= atoi(string
);
103 while (*string
&& isdigit(*string
)) string
++;
104 if (*string
== 'x') {
106 ret
->h
= atoi(string
);
107 while (*string
&& isdigit((unsigned char)*string
))
110 if (*string
== 'm') {
112 ret
->movetarget
= atoi(string
);
113 while (*string
&& isdigit((unsigned char)*string
))
120 static char *encode_params(game_params
*params
)
124 sprintf(data
, "%dx%d", params
->w
, params
->h
);
129 static config_item
*game_configure(game_params
*params
)
134 ret
= snewn(4, config_item
);
136 ret
[0].name
= "Width";
137 ret
[0].type
= C_STRING
;
138 sprintf(buf
, "%d", params
->w
);
139 ret
[0].sval
= dupstr(buf
);
142 ret
[1].name
= "Height";
143 ret
[1].type
= C_STRING
;
144 sprintf(buf
, "%d", params
->h
);
145 ret
[1].sval
= dupstr(buf
);
148 ret
[2].name
= "Number of shuffling moves";
149 ret
[2].type
= C_STRING
;
150 sprintf(buf
, "%d", params
->movetarget
);
151 ret
[2].sval
= dupstr(buf
);
162 static game_params
*custom_params(config_item
*cfg
)
164 game_params
*ret
= snew(game_params
);
166 ret
->w
= atoi(cfg
[0].sval
);
167 ret
->h
= atoi(cfg
[1].sval
);
168 ret
->movetarget
= atoi(cfg
[2].sval
);
173 static char *validate_params(game_params
*params
)
175 if (params
->w
< 2 && params
->h
< 2)
176 return "Width and height must both be at least two";
181 static int perm_parity(int *perm
, int n
)
187 for (i
= 0; i
< n
-1; i
++)
188 for (j
= i
+1; j
< n
; j
++)
189 if (perm
[i
] > perm
[j
])
195 static char *new_game_seed(game_params
*params
, random_state
*rs
,
204 n
= params
->w
* params
->h
;
206 tiles
= snewn(n
, int);
208 if (params
->movetarget
) {
209 int prevstart
= -1, prevoffset
= -1, prevdirection
= 0, nrepeats
= 0;
212 * Shuffle the old-fashioned way, by making a series of
213 * single moves on the grid.
216 for (i
= 0; i
< n
; i
++)
219 for (i
= 0; i
< params
->movetarget
; i
++) {
220 int start
, offset
, len
, direction
;
224 * Choose a move to make. We can choose from any row
228 j
= random_upto(rs
, params
->w
+ params
->h
);
237 start
= (j
- params
->w
) * params
->w
;
242 direction
= -1 + 2 * random_upto(rs
, 2);
245 * To at least _try_ to avoid boring cases, check that
246 * this move doesn't directly undo the previous one, or
247 * repeat it so many times as to turn it into fewer
250 if (start
== prevstart
&& offset
== prevoffset
) {
251 if (direction
== -prevdirection
)
252 continue; /* inverse of previous move */
253 else if (2 * (nrepeats
+1) > len
)
254 continue; /* previous move repeated too often */
257 /* If we didn't `continue', we've found an OK move to make. */
262 * Now save the move into the `prev' variables.
264 if (start
== prevstart
&& offset
== prevoffset
) {
269 prevdirection
= direction
;
277 start
+= (len
-1) * offset
;
281 for (j
= 0; j
+1 < len
; j
++)
282 tiles
[start
+ j
*offset
] = tiles
[start
+ (j
+1)*offset
];
283 tiles
[start
+ (len
-1) * offset
] = tmp
;
288 used
= snewn(n
, int);
290 for (i
= 0; i
< n
; i
++) {
296 * If both dimensions are odd, there is a parity
299 if (params
->w
& params
->h
& 1)
305 * Place everything except (possibly) the last two tiles.
307 for (x
= 0, i
= n
; i
> stop
; i
--) {
308 int k
= i
> 1 ?
random_upto(rs
, i
) : 0;
311 for (j
= 0; j
< n
; j
++)
312 if (!used
[j
] && (k
-- == 0))
315 assert(j
< n
&& !used
[j
]);
318 while (tiles
[x
] >= 0)
326 * Find the last two locations, and the last two
329 while (tiles
[x
] >= 0)
334 while (tiles
[x
] >= 0)
339 for (i
= 0; i
< n
; i
++)
343 for (i
= p1
+1; i
< n
; i
++)
349 * Try the last two tiles one way round. If that fails,
354 if (perm_parity(tiles
, n
) != 0) {
357 assert(perm_parity(tiles
, n
) == 0);
365 * Now construct the game seed, by describing the tile array as
366 * a simple sequence of comma-separated integers.
370 for (i
= 0; i
< n
; i
++) {
374 k
= sprintf(buf
, "%d,", tiles
[i
]+1);
376 ret
= sresize(ret
, retlen
+ k
+ 1, char);
377 strcpy(ret
+ retlen
, buf
);
380 ret
[retlen
-1] = '\0'; /* delete last comma */
387 static void game_free_aux_info(game_aux_info
*aux
)
389 assert(!"Shouldn't happen");
393 static char *validate_seed(game_params
*params
, char *seed
)
399 area
= params
->w
* params
->h
;
403 used
= snewn(area
, int);
404 for (i
= 0; i
< area
; i
++)
407 for (i
= 0; i
< area
; i
++) {
411 if (*p
< '0' || *p
> '9') {
412 err
= "Not enough numbers in string";
415 while (*p
>= '0' && *p
<= '9')
417 if (i
< area
-1 && *p
!= ',') {
418 err
= "Expected comma after number";
421 else if (i
== area
-1 && *p
) {
422 err
= "Excess junk at end of string";
426 if (n
< 1 || n
> area
) {
427 err
= "Number out of range";
431 err
= "Number used twice";
436 if (*p
) p
++; /* eat comma */
444 static game_state
*new_game(game_params
*params
, char *seed
)
446 game_state
*state
= snew(game_state
);
450 state
->w
= params
->w
;
451 state
->h
= params
->h
;
452 state
->n
= params
->w
* params
->h
;
453 state
->tiles
= snewn(state
->n
, int);
457 for (i
= 0; i
< state
->n
; i
++) {
459 state
->tiles
[i
] = atoi(p
);
460 while (*p
&& *p
!= ',')
462 if (*p
) p
++; /* eat comma */
466 state
->completed
= state
->movecount
= 0;
467 state
->movetarget
= params
->movetarget
;
468 state
->used_solve
= state
->just_used_solve
= FALSE
;
469 state
->last_movement_sense
= 0;
474 static game_state
*dup_game(game_state
*state
)
476 game_state
*ret
= snew(game_state
);
481 ret
->tiles
= snewn(state
->w
* state
->h
, int);
482 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
483 ret
->completed
= state
->completed
;
484 ret
->movecount
= state
->movecount
;
485 ret
->movetarget
= state
->movetarget
;
486 ret
->used_solve
= state
->used_solve
;
487 ret
->just_used_solve
= state
->just_used_solve
;
488 ret
->last_movement_sense
= state
->last_movement_sense
;
493 static void free_game(game_state
*state
)
498 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
501 game_state
*ret
= dup_game(state
);
505 * Simply replace the grid with a solved one. For this game,
506 * this isn't a useful operation for actually telling the user
507 * what they should have done, but it is useful for
508 * conveniently being able to get hold of a clean state from
509 * which to practise manoeuvres.
511 for (i
= 0; i
< ret
->n
; i
++)
513 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
514 ret
->completed
= ret
->movecount
= 1;
519 static char *game_text_format(game_state
*state
)
521 char *ret
, *p
, buf
[80];
522 int x
, y
, col
, maxlen
;
525 * First work out how many characters we need to display each
528 col
= sprintf(buf
, "%d", state
->n
);
531 * Now we know the exact total size of the grid we're going to
532 * produce: it's got h rows, each containing w lots of col, w-1
533 * spaces and a trailing newline.
535 maxlen
= state
->h
* state
->w
* (col
+1);
537 ret
= snewn(maxlen
+1, char);
540 for (y
= 0; y
< state
->h
; y
++) {
541 for (x
= 0; x
< state
->w
; x
++) {
542 int v
= state
->tiles
[state
->w
*y
+x
];
543 sprintf(buf
, "%*d", col
, v
);
553 assert(p
- ret
== maxlen
);
558 static game_ui
*new_ui(game_state
*state
)
563 static void free_ui(game_ui
*ui
)
567 static game_state
*make_move(game_state
*from
, game_ui
*ui
,
568 int x
, int y
, int button
)
571 int dx
, dy
, tx
, ty
, n
;
574 if (button
!= LEFT_BUTTON
&& button
!= RIGHT_BUTTON
)
579 if (cx
== -1 && cy
>= 0 && cy
< from
->h
)
580 n
= from
->w
, dx
= +1, dy
= 0;
581 else if (cx
== from
->w
&& cy
>= 0 && cy
< from
->h
)
582 n
= from
->w
, dx
= -1, dy
= 0;
583 else if (cy
== -1 && cx
>= 0 && cx
< from
->w
)
584 n
= from
->h
, dy
= +1, dx
= 0;
585 else if (cy
== from
->h
&& cx
>= 0 && cx
< from
->w
)
586 n
= from
->h
, dy
= -1, dx
= 0;
588 return NULL
; /* invalid click location */
590 /* reverse direction if right hand button is pressed */
591 if (button
== RIGHT_BUTTON
)
593 dx
= -dx
; if (dx
) cx
= from
->w
- 1 - cx
;
594 dy
= -dy
; if (dy
) cy
= from
->h
- 1 - cy
;
597 ret
= dup_game(from
);
598 ret
->just_used_solve
= FALSE
; /* zero this in a hurry */
603 tx
= (cx
+ dx
+ from
->w
) % from
->w
;
604 ty
= (cy
+ dy
+ from
->h
) % from
->h
;
605 ret
->tiles
[C(ret
, cx
, cy
)] = from
->tiles
[C(from
, tx
, ty
)];
610 ret
->last_movement_sense
= -(dx
+dy
);
613 * See if the game has been completed.
615 if (!ret
->completed
) {
616 ret
->completed
= ret
->movecount
;
617 for (n
= 0; n
< ret
->n
; n
++)
618 if (ret
->tiles
[n
] != n
+1)
619 ret
->completed
= FALSE
;
625 /* ----------------------------------------------------------------------
629 struct game_drawstate
{
635 static void game_size(game_params
*params
, int *x
, int *y
)
637 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
638 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
641 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
643 float *ret
= snewn(3 * NCOLOURS
, float);
647 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
650 * Drop the background colour so that the highlight is
651 * noticeably brighter than it while still being under 1.
653 max
= ret
[COL_BACKGROUND
*3];
654 for (i
= 1; i
< 3; i
++)
655 if (ret
[COL_BACKGROUND
*3+i
] > max
)
656 max
= ret
[COL_BACKGROUND
*3+i
];
657 if (max
* 1.2F
> 1.0F
) {
658 for (i
= 0; i
< 3; i
++)
659 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
662 for (i
= 0; i
< 3; i
++) {
663 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
664 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
665 ret
[COL_TEXT
* 3 + i
] = 0.0;
668 *ncolours
= NCOLOURS
;
672 static game_drawstate
*game_new_drawstate(game_state
*state
)
674 struct game_drawstate
*ds
= snew(struct game_drawstate
);
680 ds
->bgcolour
= COL_BACKGROUND
;
681 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
682 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
688 static void game_free_drawstate(game_drawstate
*ds
)
694 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
695 int tile
, int flash_colour
)
698 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
704 coords
[0] = x
+ TILE_SIZE
- 1;
705 coords
[1] = y
+ TILE_SIZE
- 1;
706 coords
[2] = x
+ TILE_SIZE
- 1;
709 coords
[5] = y
+ TILE_SIZE
- 1;
710 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
711 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
715 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
716 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
718 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
719 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
722 sprintf(str
, "%d", tile
);
723 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
724 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
727 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
730 static void draw_arrow(frontend
*fe
, int x
, int y
, int xdx
, int xdy
)
733 int ydy
= -xdx
, ydx
= xdy
;
735 #define POINT(n, xx, yy) ( \
736 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
737 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
739 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
740 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
741 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
742 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
743 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
744 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
745 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
747 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
748 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
751 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
752 game_state
*state
, int dir
, game_ui
*ui
,
753 float animtime
, float flashtime
)
758 int frame
= (int)(flashtime
/ FLASH_FRAME
);
759 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
761 bgcolour
= COL_BACKGROUND
;
767 TILE_SIZE
* state
->w
+ 2 * BORDER
,
768 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
769 draw_update(fe
, 0, 0,
770 TILE_SIZE
* state
->w
+ 2 * BORDER
,
771 TILE_SIZE
* state
->h
+ 2 * BORDER
);
774 * Recessed area containing the whole puzzle.
776 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
777 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
778 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
779 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
780 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
781 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
782 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
783 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
785 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
786 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
787 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
788 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
791 * Arrows for making moves.
793 for (i
= 0; i
< state
->w
; i
++) {
794 draw_arrow(fe
, COORD(i
), COORD(0), +1, 0);
795 draw_arrow(fe
, COORD(i
+1), COORD(state
->h
), -1, 0);
797 for (i
= 0; i
< state
->h
; i
++) {
798 draw_arrow(fe
, COORD(state
->w
), COORD(i
), 0, +1);
799 draw_arrow(fe
, COORD(0), COORD(i
+1), 0, -1);
806 * Now draw each tile.
809 clip(fe
, COORD(0), COORD(0), TILE_SIZE
*state
->w
, TILE_SIZE
*state
->h
);
811 for (i
= 0; i
< state
->n
; i
++) {
814 * Figure out what should be displayed at this
815 * location. It's either a simple tile, or it's a
816 * transition between two tiles (in which case we say
817 * -1 because it must always be drawn).
820 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
827 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
828 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
832 * Figure out what to _actually_ draw, and where to
836 int x0
, y0
, x1
, y1
, dx
, dy
;
843 sense
= -oldstate
->last_movement_sense
;
845 sense
= state
->last_movement_sense
;
851 * FIXME: must be prepared to draw a double
852 * tile in some situations.
856 * Find the coordinates of this tile in the old and
859 x1
= COORD(X(state
, i
));
860 y1
= COORD(Y(state
, i
));
861 for (j
= 0; j
< oldstate
->n
; j
++)
862 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
864 assert(j
< oldstate
->n
);
865 x0
= COORD(X(state
, j
));
866 y0
= COORD(Y(state
, j
));
870 dx
!= TILE_SIZE
* sense
) {
871 dx
= (dx
< 0 ? dx
+ TILE_SIZE
* state
->w
:
872 dx
- TILE_SIZE
* state
->w
);
873 assert(abs(dx
) == TILE_SIZE
);
877 dy
!= TILE_SIZE
* sense
) {
878 dy
= (dy
< 0 ? dy
+ TILE_SIZE
* state
->h
:
879 dy
- TILE_SIZE
* state
->h
);
880 assert(abs(dy
) == TILE_SIZE
);
883 c
= (animtime
/ ANIM_TIME
);
884 if (c
< 0.0F
) c
= 0.0F
;
885 if (c
> 1.0F
) c
= 1.0F
;
887 x
= x0
+ (int)(c
* dx
);
888 y
= y0
+ (int)(c
* dy
);
889 x2
= x1
- dx
+ (int)(c
* dx
);
890 y2
= y1
- dy
+ (int)(c
* dy
);
892 x
= COORD(X(state
, i
));
893 y
= COORD(Y(state
, i
));
897 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
898 if (x2
!= -1 || y2
!= -1)
899 draw_tile(fe
, state
, x2
, y2
, t
, bgcolour
);
906 ds
->bgcolour
= bgcolour
;
909 * Update the status bar.
915 * Don't show the new status until we're also showing the
916 * new _state_ - after the game animation is complete.
921 if (state
->used_solve
)
922 sprintf(statusbuf
, "Moves since auto-solve: %d",
923 state
->movecount
- state
->completed
);
925 sprintf(statusbuf
, "%sMoves: %d",
926 (state
->completed ?
"COMPLETED! " : ""),
927 (state
->completed ? state
->completed
: state
->movecount
));
928 if (state
->movetarget
)
929 sprintf(statusbuf
+strlen(statusbuf
), " (target %d)",
933 status_bar(fe
, statusbuf
);
937 static float game_anim_length(game_state
*oldstate
,
938 game_state
*newstate
, int dir
)
940 if ((dir
> 0 && newstate
->just_used_solve
) ||
941 (dir
< 0 && oldstate
->just_used_solve
))
947 static float game_flash_length(game_state
*oldstate
,
948 game_state
*newstate
, int dir
)
950 if (!oldstate
->completed
&& newstate
->completed
&&
951 !oldstate
->used_solve
&& !newstate
->used_solve
)
952 return 2 * FLASH_FRAME
;
957 static int game_wants_statusbar(void)
963 #define thegame sixteen
966 const struct game thegame
= {
967 "Sixteen", "games.sixteen",
974 TRUE
, game_configure
, custom_params
,
983 TRUE
, game_text_format
,
994 game_wants_statusbar
,