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 void decode_params(game_params
*ret
, char const *string
)
100 ret
->w
= ret
->h
= atoi(string
);
102 while (*string
&& isdigit(*string
)) string
++;
103 if (*string
== 'x') {
105 ret
->h
= atoi(string
);
106 while (*string
&& isdigit((unsigned char)*string
))
109 if (*string
== 'm') {
111 ret
->movetarget
= atoi(string
);
112 while (*string
&& isdigit((unsigned char)*string
))
117 static char *encode_params(game_params
*params
, int full
)
121 sprintf(data
, "%dx%d", params
->w
, params
->h
);
122 /* Shuffle limit is part of the limited parameters, because we have to
123 * supply the target move count. */
124 if (params
->movetarget
)
125 sprintf(data
+ strlen(data
), "m%d", params
->movetarget
);
130 static config_item
*game_configure(game_params
*params
)
135 ret
= snewn(4, config_item
);
137 ret
[0].name
= "Width";
138 ret
[0].type
= C_STRING
;
139 sprintf(buf
, "%d", params
->w
);
140 ret
[0].sval
= dupstr(buf
);
143 ret
[1].name
= "Height";
144 ret
[1].type
= C_STRING
;
145 sprintf(buf
, "%d", params
->h
);
146 ret
[1].sval
= dupstr(buf
);
149 ret
[2].name
= "Number of shuffling moves";
150 ret
[2].type
= C_STRING
;
151 sprintf(buf
, "%d", params
->movetarget
);
152 ret
[2].sval
= dupstr(buf
);
163 static game_params
*custom_params(config_item
*cfg
)
165 game_params
*ret
= snew(game_params
);
167 ret
->w
= atoi(cfg
[0].sval
);
168 ret
->h
= atoi(cfg
[1].sval
);
169 ret
->movetarget
= atoi(cfg
[2].sval
);
174 static char *validate_params(game_params
*params
)
176 if (params
->w
< 2 && params
->h
< 2)
177 return "Width and height must both be at least two";
182 static int perm_parity(int *perm
, int n
)
188 for (i
= 0; i
< n
-1; i
++)
189 for (j
= i
+1; j
< n
; j
++)
190 if (perm
[i
] > perm
[j
])
196 static char *new_game_desc(game_params
*params
, random_state
*rs
,
205 n
= params
->w
* params
->h
;
207 tiles
= snewn(n
, int);
209 if (params
->movetarget
) {
210 int prevstart
= -1, prevoffset
= -1, prevdirection
= 0, nrepeats
= 0;
213 * Shuffle the old-fashioned way, by making a series of
214 * single moves on the grid.
217 for (i
= 0; i
< n
; i
++)
220 for (i
= 0; i
< params
->movetarget
; i
++) {
221 int start
, offset
, len
, direction
;
225 * Choose a move to make. We can choose from any row
229 j
= random_upto(rs
, params
->w
+ params
->h
);
238 start
= (j
- params
->w
) * params
->w
;
243 direction
= -1 + 2 * random_upto(rs
, 2);
246 * To at least _try_ to avoid boring cases, check that
247 * this move doesn't directly undo the previous one, or
248 * repeat it so many times as to turn it into fewer
251 if (start
== prevstart
&& offset
== prevoffset
) {
252 if (direction
== -prevdirection
)
253 continue; /* inverse of previous move */
254 else if (2 * (nrepeats
+1) > len
)
255 continue; /* previous move repeated too often */
258 /* If we didn't `continue', we've found an OK move to make. */
263 * Now save the move into the `prev' variables.
265 if (start
== prevstart
&& offset
== prevoffset
) {
270 prevdirection
= direction
;
278 start
+= (len
-1) * offset
;
282 for (j
= 0; j
+1 < len
; j
++)
283 tiles
[start
+ j
*offset
] = tiles
[start
+ (j
+1)*offset
];
284 tiles
[start
+ (len
-1) * offset
] = tmp
;
289 used
= snewn(n
, int);
291 for (i
= 0; i
< n
; i
++) {
297 * If both dimensions are odd, there is a parity
300 if (params
->w
& params
->h
& 1)
306 * Place everything except (possibly) the last two tiles.
308 for (x
= 0, i
= n
; i
> stop
; i
--) {
309 int k
= i
> 1 ?
random_upto(rs
, i
) : 0;
312 for (j
= 0; j
< n
; j
++)
313 if (!used
[j
] && (k
-- == 0))
316 assert(j
< n
&& !used
[j
]);
319 while (tiles
[x
] >= 0)
327 * Find the last two locations, and the last two
330 while (tiles
[x
] >= 0)
335 while (tiles
[x
] >= 0)
340 for (i
= 0; i
< n
; i
++)
344 for (i
= p1
+1; i
< n
; i
++)
350 * Try the last two tiles one way round. If that fails,
355 if (perm_parity(tiles
, n
) != 0) {
358 assert(perm_parity(tiles
, n
) == 0);
366 * Now construct the game description, by describing the tile
367 * array as a simple sequence of comma-separated integers.
371 for (i
= 0; i
< n
; i
++) {
375 k
= sprintf(buf
, "%d,", tiles
[i
]+1);
377 ret
= sresize(ret
, retlen
+ k
+ 1, char);
378 strcpy(ret
+ retlen
, buf
);
381 ret
[retlen
-1] = '\0'; /* delete last comma */
388 static void game_free_aux_info(game_aux_info
*aux
)
390 assert(!"Shouldn't happen");
394 static char *validate_desc(game_params
*params
, char *desc
)
400 area
= params
->w
* params
->h
;
404 used
= snewn(area
, int);
405 for (i
= 0; i
< area
; i
++)
408 for (i
= 0; i
< area
; i
++) {
412 if (*p
< '0' || *p
> '9') {
413 err
= "Not enough numbers in string";
416 while (*p
>= '0' && *p
<= '9')
418 if (i
< area
-1 && *p
!= ',') {
419 err
= "Expected comma after number";
422 else if (i
== area
-1 && *p
) {
423 err
= "Excess junk at end of string";
427 if (n
< 1 || n
> area
) {
428 err
= "Number out of range";
432 err
= "Number used twice";
437 if (*p
) p
++; /* eat comma */
445 static game_state
*new_game(game_params
*params
, char *desc
)
447 game_state
*state
= snew(game_state
);
451 state
->w
= params
->w
;
452 state
->h
= params
->h
;
453 state
->n
= params
->w
* params
->h
;
454 state
->tiles
= snewn(state
->n
, int);
458 for (i
= 0; i
< state
->n
; i
++) {
460 state
->tiles
[i
] = atoi(p
);
461 while (*p
&& *p
!= ',')
463 if (*p
) p
++; /* eat comma */
467 state
->completed
= state
->movecount
= 0;
468 state
->movetarget
= params
->movetarget
;
469 state
->used_solve
= state
->just_used_solve
= FALSE
;
470 state
->last_movement_sense
= 0;
475 static game_state
*dup_game(game_state
*state
)
477 game_state
*ret
= snew(game_state
);
482 ret
->tiles
= snewn(state
->w
* state
->h
, int);
483 memcpy(ret
->tiles
, state
->tiles
, state
->w
* state
->h
* sizeof(int));
484 ret
->completed
= state
->completed
;
485 ret
->movecount
= state
->movecount
;
486 ret
->movetarget
= state
->movetarget
;
487 ret
->used_solve
= state
->used_solve
;
488 ret
->just_used_solve
= state
->just_used_solve
;
489 ret
->last_movement_sense
= state
->last_movement_sense
;
494 static void free_game(game_state
*state
)
499 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
502 game_state
*ret
= dup_game(state
);
506 * Simply replace the grid with a solved one. For this game,
507 * this isn't a useful operation for actually telling the user
508 * what they should have done, but it is useful for
509 * conveniently being able to get hold of a clean state from
510 * which to practise manoeuvres.
512 for (i
= 0; i
< ret
->n
; i
++)
514 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
515 ret
->completed
= ret
->movecount
= 1;
520 static char *game_text_format(game_state
*state
)
522 char *ret
, *p
, buf
[80];
523 int x
, y
, col
, maxlen
;
526 * First work out how many characters we need to display each
529 col
= sprintf(buf
, "%d", state
->n
);
532 * Now we know the exact total size of the grid we're going to
533 * produce: it's got h rows, each containing w lots of col, w-1
534 * spaces and a trailing newline.
536 maxlen
= state
->h
* state
->w
* (col
+1);
538 ret
= snewn(maxlen
+1, char);
541 for (y
= 0; y
< state
->h
; y
++) {
542 for (x
= 0; x
< state
->w
; x
++) {
543 int v
= state
->tiles
[state
->w
*y
+x
];
544 sprintf(buf
, "%*d", col
, v
);
554 assert(p
- ret
== maxlen
);
559 static game_ui
*new_ui(game_state
*state
)
564 static void free_ui(game_ui
*ui
)
568 static game_state
*make_move(game_state
*from
, game_ui
*ui
,
569 int x
, int y
, int button
)
572 int dx
, dy
, tx
, ty
, n
;
575 if (button
!= LEFT_BUTTON
&& button
!= RIGHT_BUTTON
)
580 if (cx
== -1 && cy
>= 0 && cy
< from
->h
)
581 n
= from
->w
, dx
= +1, dy
= 0;
582 else if (cx
== from
->w
&& cy
>= 0 && cy
< from
->h
)
583 n
= from
->w
, dx
= -1, dy
= 0;
584 else if (cy
== -1 && cx
>= 0 && cx
< from
->w
)
585 n
= from
->h
, dy
= +1, dx
= 0;
586 else if (cy
== from
->h
&& cx
>= 0 && cx
< from
->w
)
587 n
= from
->h
, dy
= -1, dx
= 0;
589 return NULL
; /* invalid click location */
591 /* reverse direction if right hand button is pressed */
592 if (button
== RIGHT_BUTTON
)
594 dx
= -dx
; if (dx
) cx
= from
->w
- 1 - cx
;
595 dy
= -dy
; if (dy
) cy
= from
->h
- 1 - cy
;
598 ret
= dup_game(from
);
599 ret
->just_used_solve
= FALSE
; /* zero this in a hurry */
604 tx
= (cx
+ dx
+ from
->w
) % from
->w
;
605 ty
= (cy
+ dy
+ from
->h
) % from
->h
;
606 ret
->tiles
[C(ret
, cx
, cy
)] = from
->tiles
[C(from
, tx
, ty
)];
611 ret
->last_movement_sense
= -(dx
+dy
);
614 * See if the game has been completed.
616 if (!ret
->completed
) {
617 ret
->completed
= ret
->movecount
;
618 for (n
= 0; n
< ret
->n
; n
++)
619 if (ret
->tiles
[n
] != n
+1)
620 ret
->completed
= FALSE
;
626 /* ----------------------------------------------------------------------
630 struct game_drawstate
{
636 static void game_size(game_params
*params
, int *x
, int *y
)
638 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
639 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
642 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
644 float *ret
= snewn(3 * NCOLOURS
, float);
648 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
651 * Drop the background colour so that the highlight is
652 * noticeably brighter than it while still being under 1.
654 max
= ret
[COL_BACKGROUND
*3];
655 for (i
= 1; i
< 3; i
++)
656 if (ret
[COL_BACKGROUND
*3+i
] > max
)
657 max
= ret
[COL_BACKGROUND
*3+i
];
658 if (max
* 1.2F
> 1.0F
) {
659 for (i
= 0; i
< 3; i
++)
660 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
663 for (i
= 0; i
< 3; i
++) {
664 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
665 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
666 ret
[COL_TEXT
* 3 + i
] = 0.0;
669 *ncolours
= NCOLOURS
;
673 static game_drawstate
*game_new_drawstate(game_state
*state
)
675 struct game_drawstate
*ds
= snew(struct game_drawstate
);
681 ds
->bgcolour
= COL_BACKGROUND
;
682 ds
->tiles
= snewn(ds
->w
*ds
->h
, int);
683 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
689 static void game_free_drawstate(game_drawstate
*ds
)
695 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
696 int tile
, int flash_colour
)
699 draw_rect(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
,
705 coords
[0] = x
+ TILE_SIZE
- 1;
706 coords
[1] = y
+ TILE_SIZE
- 1;
707 coords
[2] = x
+ TILE_SIZE
- 1;
710 coords
[5] = y
+ TILE_SIZE
- 1;
711 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
712 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
716 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
717 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
719 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
720 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
723 sprintf(str
, "%d", tile
);
724 draw_text(fe
, x
+ TILE_SIZE
/2, y
+ TILE_SIZE
/2,
725 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
728 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
731 static void draw_arrow(frontend
*fe
, int x
, int y
, int xdx
, int xdy
)
734 int ydy
= -xdx
, ydx
= xdy
;
736 #define POINT(n, xx, yy) ( \
737 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
738 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
740 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
741 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
742 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
743 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
744 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
745 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
746 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
748 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
749 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
752 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
753 game_state
*state
, int dir
, game_ui
*ui
,
754 float animtime
, float flashtime
)
759 int frame
= (int)(flashtime
/ FLASH_FRAME
);
760 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
762 bgcolour
= COL_BACKGROUND
;
768 TILE_SIZE
* state
->w
+ 2 * BORDER
,
769 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
770 draw_update(fe
, 0, 0,
771 TILE_SIZE
* state
->w
+ 2 * BORDER
,
772 TILE_SIZE
* state
->h
+ 2 * BORDER
);
775 * Recessed area containing the whole puzzle.
777 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
778 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
779 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
780 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
781 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
782 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
783 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
784 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
786 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
787 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
788 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
789 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
792 * Arrows for making moves.
794 for (i
= 0; i
< state
->w
; i
++) {
795 draw_arrow(fe
, COORD(i
), COORD(0), +1, 0);
796 draw_arrow(fe
, COORD(i
+1), COORD(state
->h
), -1, 0);
798 for (i
= 0; i
< state
->h
; i
++) {
799 draw_arrow(fe
, COORD(state
->w
), COORD(i
), 0, +1);
800 draw_arrow(fe
, COORD(0), COORD(i
+1), 0, -1);
807 * Now draw each tile.
810 clip(fe
, COORD(0), COORD(0), TILE_SIZE
*state
->w
, TILE_SIZE
*state
->h
);
812 for (i
= 0; i
< state
->n
; i
++) {
815 * Figure out what should be displayed at this
816 * location. It's either a simple tile, or it's a
817 * transition between two tiles (in which case we say
818 * -1 because it must always be drawn).
821 if (oldstate
&& oldstate
->tiles
[i
] != state
->tiles
[i
])
828 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
829 ds
->tiles
[i
] != t
|| ds
->tiles
[i
] == -1 || t
== -1) {
833 * Figure out what to _actually_ draw, and where to
837 int x0
, y0
, x1
, y1
, dx
, dy
;
844 sense
= -oldstate
->last_movement_sense
;
846 sense
= state
->last_movement_sense
;
852 * FIXME: must be prepared to draw a double
853 * tile in some situations.
857 * Find the coordinates of this tile in the old and
860 x1
= COORD(X(state
, i
));
861 y1
= COORD(Y(state
, i
));
862 for (j
= 0; j
< oldstate
->n
; j
++)
863 if (oldstate
->tiles
[j
] == state
->tiles
[i
])
865 assert(j
< oldstate
->n
);
866 x0
= COORD(X(state
, j
));
867 y0
= COORD(Y(state
, j
));
871 dx
!= TILE_SIZE
* sense
) {
872 dx
= (dx
< 0 ? dx
+ TILE_SIZE
* state
->w
:
873 dx
- TILE_SIZE
* state
->w
);
874 assert(abs(dx
) == TILE_SIZE
);
878 dy
!= TILE_SIZE
* sense
) {
879 dy
= (dy
< 0 ? dy
+ TILE_SIZE
* state
->h
:
880 dy
- TILE_SIZE
* state
->h
);
881 assert(abs(dy
) == TILE_SIZE
);
884 c
= (animtime
/ ANIM_TIME
);
885 if (c
< 0.0F
) c
= 0.0F
;
886 if (c
> 1.0F
) c
= 1.0F
;
888 x
= x0
+ (int)(c
* dx
);
889 y
= y0
+ (int)(c
* dy
);
890 x2
= x1
- dx
+ (int)(c
* dx
);
891 y2
= y1
- dy
+ (int)(c
* dy
);
893 x
= COORD(X(state
, i
));
894 y
= COORD(Y(state
, i
));
898 draw_tile(fe
, state
, x
, y
, t
, bgcolour
);
899 if (x2
!= -1 || y2
!= -1)
900 draw_tile(fe
, state
, x2
, y2
, t
, bgcolour
);
907 ds
->bgcolour
= bgcolour
;
910 * Update the status bar.
916 * Don't show the new status until we're also showing the
917 * new _state_ - after the game animation is complete.
922 if (state
->used_solve
)
923 sprintf(statusbuf
, "Moves since auto-solve: %d",
924 state
->movecount
- state
->completed
);
926 sprintf(statusbuf
, "%sMoves: %d",
927 (state
->completed ?
"COMPLETED! " : ""),
928 (state
->completed ? state
->completed
: state
->movecount
));
929 if (state
->movetarget
)
930 sprintf(statusbuf
+strlen(statusbuf
), " (target %d)",
934 status_bar(fe
, statusbuf
);
938 static float game_anim_length(game_state
*oldstate
,
939 game_state
*newstate
, int dir
)
941 if ((dir
> 0 && newstate
->just_used_solve
) ||
942 (dir
< 0 && oldstate
->just_used_solve
))
948 static float game_flash_length(game_state
*oldstate
,
949 game_state
*newstate
, int dir
)
951 if (!oldstate
->completed
&& newstate
->completed
&&
952 !oldstate
->used_solve
&& !newstate
->used_solve
)
953 return 2 * FLASH_FRAME
;
958 static int game_wants_statusbar(void)
964 #define thegame sixteen
967 const struct game thegame
= {
968 "Sixteen", "games.sixteen",
975 TRUE
, game_configure
, custom_params
,
984 TRUE
, game_text_format
,
995 game_wants_statusbar
,