2 * slide.c: Implementation of the block-sliding puzzle `Klotski'.
8 * - Improve the generator.
9 * * actually, we seem to be mostly sensible already now. I
10 * want more choice over the type of main block and location
11 * of the exit/target, and I think I probably ought to give
12 * up on compactness and just bite the bullet and have the
13 * target area right outside the main wall, but mostly I
15 * * the move limit tends to make the game _slower_ to
16 * generate, which is odd. Perhaps investigate why.
18 * - Improve the graphics.
19 * * All the colours are a bit wishy-washy. _Some_ dark
20 * colours would surely not be excessive? Probably darken
21 * the tiles, the walls and the main block, and leave the
23 * * The cattle grid effect is still disgusting. Think of
24 * something completely different.
25 * * The highlight for next-piece-to-move in the solver is
26 * excessive, and the shadow blends in too well with the
27 * piece lowlights. Adjust both.
41 * The implementation of this game revolves around the insight
42 * which makes an exhaustive-search solver feasible: although
43 * there are many blocks which can be rearranged in many ways, any
44 * two blocks of the same shape are _indistinguishable_ and hence
45 * the number of _distinct_ board layouts is generally much
46 * smaller. So we adopt a representation for board layouts which
47 * is inherently canonical, i.e. there are no two distinct
48 * representations which encode indistinguishable layouts.
50 * The way we do this is to encode each square of the board, in
51 * the normal left-to-right top-to-bottom order, as being one of
52 * the following things:
53 * - the first square (in the given order) of a block (`anchor')
54 * - special case of the above: the anchor for the _main_ block
55 * (i.e. the one which the aim of the game is to get to the
57 * - a subsequent square of a block whose previous square was N
59 * - an impassable wall
61 * (We also separately store data about which board positions are
62 * forcefields only passable by the main block. We can't encode
63 * that in the main board data, because then the main block would
64 * destroy forcefields as it went over them.)
66 * Hence, for example, a 2x2 square block would be encoded as
67 * ANCHOR, followed by DIST(1), and w-2 squares later on there
68 * would be DIST(w-1) followed by DIST(1). So if you start at the
69 * last of those squares, the DIST numbers give you a linked list
70 * pointing back through all the other squares in the same block.
72 * So the solver simply does a bfs over all reachable positions,
73 * encoding them in this format and storing them in a tree234 to
74 * ensure it doesn't ever revisit an already-analysed position.
79 * The colours are arranged here so that every base colour is
80 * directly followed by its highlight colour and then its
81 * lowlight colour. Do not break this, or draw_tile() will get
88 COL_DRAGGING_HIGHLIGHT
,
89 COL_DRAGGING_LOWLIGHT
,
94 COL_MAIN_DRAGGING_HIGHLIGHT
,
95 COL_MAIN_DRAGGING_LOWLIGHT
,
103 * Board layout is a simple array of bytes. Each byte holds:
105 #define ANCHOR 255 /* top-left-most square of some piece */
106 #define MAINANCHOR 254 /* anchor of _main_ piece */
107 #define EMPTY 253 /* empty square */
108 #define WALL 252 /* immovable wall */
110 /* all other values indicate distance back to previous square of same block */
111 #define ISDIST(x) ( (unsigned char)((x)-1) <= MAXDIST-1 )
113 #define ISANCHOR(x) ( (x)==ANCHOR || (x)==MAINANCHOR )
114 #define ISBLOCK(x) ( ISANCHOR(x) || ISDIST(x) )
117 * MAXDIST is the largest DIST value we can encode. This must
118 * therefore also be the maximum puzzle width in theory (although
119 * solver running time will dictate a much smaller limit in
122 #define MAXWID MAXDIST
129 struct game_immutable_state
{
131 unsigned char *forcefield
;
134 struct game_solution
{
136 int *moves
; /* just like from solve_board() */
142 unsigned char *board
;
143 int tx
, ty
; /* target coords for MAINANCHOR */
144 int minmoves
; /* for display only */
145 int lastmoved
, lastmoved_pos
; /* for move counting */
149 struct game_immutable_state
*imm
;
150 struct game_solution
*soln
;
154 static game_params
*default_params(void)
156 game_params
*ret
= snew(game_params
);
165 static const struct game_params slide_presets
[] = {
171 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
176 if (i
< 0 || i
>= lenof(slide_presets
))
179 ret
= snew(game_params
);
180 *ret
= slide_presets
[i
];
182 sprintf(str
, "%dx%d", ret
->w
, ret
->h
);
183 if (ret
->maxmoves
>= 0)
184 sprintf(str
+ strlen(str
), ", max %d moves", ret
->maxmoves
);
186 sprintf(str
+ strlen(str
), ", no move limit");
193 static void free_params(game_params
*params
)
198 static game_params
*dup_params(game_params
*params
)
200 game_params
*ret
= snew(game_params
);
201 *ret
= *params
; /* structure copy */
205 static void decode_params(game_params
*params
, char const *string
)
207 params
->w
= params
->h
= atoi(string
);
208 while (*string
&& isdigit((unsigned char)*string
)) string
++;
209 if (*string
== 'x') {
211 params
->h
= atoi(string
);
212 while (*string
&& isdigit((unsigned char)*string
)) string
++;
214 if (*string
== 'm') {
216 params
->maxmoves
= atoi(string
);
217 while (*string
&& isdigit((unsigned char)*string
)) string
++;
218 } else if (*string
== 'u') {
220 params
->maxmoves
= -1;
224 static char *encode_params(game_params
*params
, int full
)
228 sprintf(data
, "%dx%d", params
->w
, params
->h
);
229 if (params
->maxmoves
>= 0)
230 sprintf(data
+ strlen(data
), "m%d", params
->maxmoves
);
232 sprintf(data
+ strlen(data
), "u");
237 static config_item
*game_configure(game_params
*params
)
242 ret
= snewn(4, config_item
);
244 ret
[0].name
= "Width";
245 ret
[0].type
= C_STRING
;
246 sprintf(buf
, "%d", params
->w
);
247 ret
[0].sval
= dupstr(buf
);
250 ret
[1].name
= "Height";
251 ret
[1].type
= C_STRING
;
252 sprintf(buf
, "%d", params
->h
);
253 ret
[1].sval
= dupstr(buf
);
256 ret
[2].name
= "Solution length limit";
257 ret
[2].type
= C_STRING
;
258 sprintf(buf
, "%d", params
->maxmoves
);
259 ret
[2].sval
= dupstr(buf
);
270 static game_params
*custom_params(config_item
*cfg
)
272 game_params
*ret
= snew(game_params
);
274 ret
->w
= atoi(cfg
[0].sval
);
275 ret
->h
= atoi(cfg
[1].sval
);
276 ret
->maxmoves
= atoi(cfg
[2].sval
);
281 static char *validate_params(game_params
*params
, int full
)
283 if (params
->w
> MAXWID
)
284 return "Width must be at most " STR(MAXWID
);
287 return "Width must be at least 5";
289 return "Height must be at least 4";
294 static char *board_text_format(int w
, int h
, unsigned char *data
,
295 unsigned char *forcefield
)
298 int *dsf
= snew_dsf(wh
);
300 int retpos
, retlen
= (w
*2+2)*(h
*2+1)+1;
301 char *ret
= snewn(retlen
, char);
303 for (i
= 0; i
< wh
; i
++)
305 dsf_merge(dsf
, i
- data
[i
], i
);
307 for (y
= 0; y
< 2*h
+1; y
++) {
308 for (x
= 0; x
< 2*w
+1; x
++) {
310 int i
= (y
/2)*w
+(x
/2);
312 #define dtype(i) (ISBLOCK(data[i]) ? \
313 dsf_canonify(dsf, i) : data[i])
314 #define dchar(t) ((t)==EMPTY ? ' ' : (t)==WALL ? '#' : \
315 data[t] == MAINANCHOR ? '*' : '%')
317 if (y
% 2 && x
% 2) {
320 } else if (y
% 2 && !(x
% 2)) {
321 int j1
= (x
> 0 ?
dtype(i
-1) : -1);
322 int j2
= (x
< 2*w ?
dtype(i
) : -1);
327 } else if (!(y
% 2) && (x
% 2)) {
328 int j1
= (y
> 0 ?
dtype(i
-w
) : -1);
329 int j2
= (y
< 2*h ?
dtype(i
) : -1);
335 int j1
= (x
> 0 && y
> 0 ?
dtype(i
-w
-1) : -1);
336 int j2
= (x
> 0 && y
< 2*h ?
dtype(i
-1) : -1);
337 int j3
= (x
< 2*w
&& y
> 0 ?
dtype(i
-w
) : -1);
338 int j4
= (x
< 2*w
&& y
< 2*h ?
dtype(i
) : -1);
339 if (j1
== j2
&& j2
== j3
&& j3
== j4
)
341 else if (j1
== j2
&& j3
== j4
)
343 else if (j1
== j3
&& j2
== j4
)
349 assert(retpos
< retlen
);
352 assert(retpos
< retlen
);
353 ret
[retpos
++] = '\n';
355 assert(retpos
< retlen
);
356 ret
[retpos
++] = '\0';
357 assert(retpos
== retlen
);
362 /* ----------------------------------------------------------------------
367 * During solver execution, the set of visited board positions is
368 * stored as a tree234 of the following structures. `w', `h' and
369 * `data' are obvious in meaning; `dist' represents the minimum
370 * distance to reach this position from the starting point.
372 * `prev' links each board to the board position from which it was
373 * most efficiently derived.
382 static int boardcmp(void *av
, void *bv
)
384 struct board
*a
= (struct board
*)av
;
385 struct board
*b
= (struct board
*)bv
;
386 return memcmp(a
->data
, b
->data
, a
->w
* a
->h
);
389 static struct board
*newboard(int w
, int h
, unsigned char *data
)
391 struct board
*b
= malloc(sizeof(struct board
) + w
*h
);
392 b
->data
= (unsigned char *)b
+ sizeof(struct board
);
393 memcpy(b
->data
, data
, w
*h
);
402 * The actual solver. Given a board, attempt to find the minimum
403 * length of move sequence which moves MAINANCHOR to (tx,ty), or
404 * -1 if no solution exists. Returns that minimum length.
406 * Also, if `moveout' is provided, writes out the moves in the
407 * form of a sequence of pairs of integers indicating the source
408 * and destination points of the anchor of the moved piece in each
409 * move. Exactly twice as many integers are written as the number
410 * returned from solve_board(), and `moveout' receives an int *
411 * which is a pointer to a dynamically allocated array.
413 static int solve_board(int w
, int h
, unsigned char *board
,
414 unsigned char *forcefield
, int tx
, int ty
,
415 int movelimit
, int **moveout
)
418 struct board
*b
, *b2
, *b3
;
419 int *next
, *anchors
, *which
;
420 int *movereached
, *movequeue
, mqhead
, mqtail
;
421 tree234
*sorted
, *queue
;
426 #ifdef SOLVER_DIAGNOSTICS
428 char *t
= board_text_format(w
, h
, board
);
429 for (i
= 0; i
< h
; i
++) {
430 for (j
= 0; j
< w
; j
++) {
431 int c
= board
[i
*w
+j
];
434 else if (c
== MAINANCHOR
)
436 else if (c
== ANCHOR
)
446 printf("Starting solver for:\n%s\n", t
);
451 sorted
= newtree234(boardcmp
);
452 queue
= newtree234(NULL
);
454 b
= newboard(w
, h
, board
);
457 addpos234(queue
, b
, 0);
460 next
= snewn(wh
, int);
461 anchors
= snewn(wh
, int);
462 which
= snewn(wh
, int);
463 movereached
= snewn(wh
, int);
464 movequeue
= snewn(wh
, int);
467 while ((b
= delpos234(queue
, 0)) != NULL
) {
469 if (movelimit
>= 0 && b
->dist
>= movelimit
) {
471 * The problem is not soluble in under `movelimit'
472 * moves, so we can quit right now.
477 if (b
->dist
!= lastdist
) {
478 #ifdef SOLVER_DIAGNOSTICS
479 printf("dist %d (%d)\n", b
->dist
, count234(sorted
));
484 * Find all the anchors and form a linked list of the
485 * squares within each block.
487 for (i
= 0; i
< wh
; i
++) {
491 if (ISANCHOR(b
->data
[i
])) {
494 } else if (ISDIST(b
->data
[i
])) {
502 * For each anchor, do an array-based BFS to find all the
503 * places we can slide it to.
505 for (i
= 0; i
< wh
; i
++) {
510 for (j
= 0; j
< wh
; j
++)
511 movereached
[j
] = FALSE
;
512 movequeue
[mqtail
++] = i
;
513 while (mqhead
< mqtail
) {
514 int pos
= movequeue
[mqhead
++];
517 * Try to move in each direction from here.
519 for (dir
= 0; dir
< 4; dir
++) {
520 int dx
= (dir
== 0 ?
-1 : dir
== 1 ?
+1 : 0);
521 int dy
= (dir
== 2 ?
-1 : dir
== 3 ?
+1 : 0);
522 int offset
= dy
*w
+ dx
;
523 int newpos
= pos
+ offset
;
527 * For each square involved in this block,
528 * check to see if the square d spaces away
529 * from it is either empty or part of the same
532 for (j
= i
; j
>= 0; j
= next
[j
]) {
533 int jy
= (pos
+j
-i
) / w
+ dy
, jx
= (pos
+j
-i
) % w
+ dx
;
534 if (jy
>= 0 && jy
< h
&& jx
>= 0 && jx
< w
&&
535 ((b
->data
[j
+d
] == EMPTY
|| which
[j
+d
] == i
) &&
536 (b
->data
[i
] == MAINANCHOR
|| !forcefield
[j
+d
])))
542 continue; /* this direction wasn't feasible */
545 * If we've already tried moving this piece
548 if (movereached
[newpos
])
550 movereached
[newpos
] = TRUE
;
551 movequeue
[mqtail
++] = newpos
;
554 * We have a viable move. Make it.
556 b2
= newboard(w
, h
, b
->data
);
557 for (j
= i
; j
>= 0; j
= next
[j
])
559 for (j
= i
; j
>= 0; j
= next
[j
])
560 b2
->data
[j
+d
] = b
->data
[j
];
562 b3
= add234(sorted
, b2
);
564 sfree(b2
); /* we already got one */
566 b2
->dist
= b
->dist
+ 1;
568 addpos234(queue
, b2
, qlen
++);
569 if (b2
->data
[ty
*w
+tx
] == MAINANCHOR
)
570 goto done
; /* search completed! */
584 * Now b2 represents the solved position. Backtrack to
585 * output the solution.
587 *moveout
= snewn(ret
* 2, int);
591 int from
= -1, to
= -1;
596 * Scan b and b2 to find out which piece has
599 for (i
= 0; i
< wh
; i
++) {
600 if (ISANCHOR(b
->data
[i
]) && !ISANCHOR(b2
->data
[i
])) {
603 } else if (!ISANCHOR(b
->data
[i
]) && ISANCHOR(b2
->data
[i
])){
609 assert(from
>= 0 && to
>= 0);
611 (*moveout
)[--j
] = to
;
612 (*moveout
)[--j
] = from
;
619 ret
= -1; /* no solution */
626 while ((b
= delpos234(sorted
, 0)) != NULL
)
639 /* ----------------------------------------------------------------------
640 * Random board generation.
643 static void generate_board(int w
, int h
, int *rtx
, int *rty
, int *minmoves
,
644 random_state
*rs
, unsigned char **rboard
,
645 unsigned char **rforcefield
, int movelimit
)
648 unsigned char *board
, *board2
, *forcefield
;
649 unsigned char *tried_merge
;
651 int *list
, nlist
, pos
;
654 int moves
= 0; /* placate optimiser */
657 * Set up a board and fill it with singletons, except for a
660 board
= snewn(wh
, unsigned char);
661 forcefield
= snewn(wh
, unsigned char);
662 board2
= snewn(wh
, unsigned char);
663 memset(board
, ANCHOR
, wh
);
664 memset(forcefield
, FALSE
, wh
);
665 for (i
= 0; i
< w
; i
++)
666 board
[i
] = board
[i
+w
*(h
-1)] = WALL
;
667 for (i
= 0; i
< h
; i
++)
668 board
[i
*w
] = board
[i
*w
+(w
-1)] = WALL
;
670 tried_merge
= snewn(wh
* wh
, unsigned char);
671 memset(tried_merge
, 0, wh
*wh
);
675 * Invent a main piece at one extreme. (FIXME: vary the
676 * extreme, and the piece.)
678 board
[w
+1] = MAINANCHOR
;
679 board
[w
+2] = DIST(1);
680 board
[w
*2+1] = DIST(w
-1);
681 board
[w
*2+2] = DIST(1);
684 * Invent a target position. (FIXME: vary this too.)
688 forcefield
[ty
*w
+tx
+1] = forcefield
[(ty
+1)*w
+tx
+1] = TRUE
;
689 board
[ty
*w
+tx
+1] = board
[(ty
+1)*w
+tx
+1] = EMPTY
;
692 * Gradually remove singletons until the game becomes soluble.
694 for (j
= w
; j
-- > 0 ;)
695 for (i
= h
; i
-- > 0 ;)
696 if (board
[i
*w
+j
] == ANCHOR
) {
698 * See if the board is already soluble.
700 if ((moves
= solve_board(w
, h
, board
, forcefield
,
701 tx
, ty
, movelimit
, NULL
)) >= 0)
705 * Otherwise, remove this piece.
707 board
[i
*w
+j
] = EMPTY
;
709 assert(!"We shouldn't get here");
713 * Make a list of all the inter-block edges on the board.
715 list
= snewn(wh
*2, int);
717 for (i
= 0; i
+1 < w
; i
++)
718 for (j
= 0; j
< h
; j
++)
719 list
[nlist
++] = (j
*w
+i
) * 2 + 0; /* edge to the right of j*w+i */
720 for (j
= 0; j
+1 < h
; j
++)
721 for (i
= 0; i
< w
; i
++)
722 list
[nlist
++] = (j
*w
+i
) * 2 + 1; /* edge below j*w+i */
725 * Now go through that list in random order, trying to merge
726 * the blocks on each side of each edge.
728 shuffle(list
, nlist
, sizeof(*list
), rs
);
734 y1
= y2
= pos
/ (w
*2);
735 x1
= x2
= (pos
/ 2) % w
;
744 * Immediately abandon the attempt if we've already tried
745 * to merge the same pair of blocks along a different
748 c1
= dsf_canonify(dsf
, p1
);
749 c2
= dsf_canonify(dsf
, p2
);
750 if (tried_merge
[c1
* wh
+ c2
])
754 * In order to be mergeable, these two squares must each
755 * either be, or belong to, a non-main anchor, and their
756 * anchors must also be distinct.
758 if (!ISBLOCK(board
[p1
]) || !ISBLOCK(board
[p2
]))
760 while (ISDIST(board
[p1
]))
762 while (ISDIST(board
[p2
]))
764 if (board
[p1
] == MAINANCHOR
|| board
[p2
] == MAINANCHOR
|| p1
== p2
)
768 * We can merge these blocks. Try it, and see if the
769 * puzzle remains soluble.
771 memcpy(board2
, board
, wh
);
773 while (p1
< wh
|| p2
< wh
) {
775 * p1 and p2 are the squares at the head of each block
776 * list. Pick the smaller one and put it on the output
783 assert(i
- j
<= MAXDIST
);
784 board
[i
] = DIST(i
- j
);
789 * Now advance whichever list that came from.
794 } while (p1
< wh
&& board
[p1
] != DIST(p1
-i
));
798 } while (p2
< wh
&& board
[p2
] != DIST(p2
-i
));
801 j
= solve_board(w
, h
, board
, forcefield
, tx
, ty
, movelimit
, NULL
);
804 * Didn't work. Revert the merge.
806 memcpy(board
, board2
, wh
);
807 tried_merge
[c1
* wh
+ c2
] = tried_merge
[c2
* wh
+ c1
] = TRUE
;
813 dsf_merge(dsf
, c1
, c2
);
814 c
= dsf_canonify(dsf
, c1
);
815 for (i
= 0; i
< wh
; i
++)
816 tried_merge
[c
*wh
+i
] = (tried_merge
[c1
*wh
+i
] |
817 tried_merge
[c2
*wh
+i
]);
818 for (i
= 0; i
< wh
; i
++)
819 tried_merge
[i
*wh
+c
] = (tried_merge
[i
*wh
+c1
] |
820 tried_merge
[i
*wh
+c2
]);
832 *rforcefield
= forcefield
;
836 /* ----------------------------------------------------------------------
837 * End of solver/generator code.
840 static char *new_game_desc(game_params
*params
, random_state
*rs
,
841 char **aux
, int interactive
)
843 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
844 int tx
, ty
, minmoves
;
845 unsigned char *board
, *forcefield
;
849 generate_board(params
->w
, params
->h
, &tx
, &ty
, &minmoves
, rs
,
850 &board
, &forcefield
, params
->maxmoves
);
851 #ifdef GENERATOR_DIAGNOSTICS
853 char *t
= board_text_format(params
->w
, params
->h
, board
);
860 * Encode as a game ID.
862 ret
= snewn(wh
* 6 + 40, char);
866 if (ISDIST(board
[i
])) {
867 p
+= sprintf(p
, "d%d", board
[i
]);
871 int b
= board
[i
], f
= forcefield
[i
];
872 int c
= (b
== ANCHOR ?
'a' :
873 b
== MAINANCHOR ?
'm' :
875 /* b == WALL ? */ 'w');
879 while (i
< wh
&& board
[i
] == b
&& forcefield
[i
] == f
)
882 p
+= sprintf(p
, "%d", count
);
885 p
+= sprintf(p
, ",%d,%d,%d", tx
, ty
, minmoves
);
886 ret
= sresize(ret
, p
+1 - ret
, char);
894 static char *validate_desc(game_params
*params
, char *desc
)
896 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
898 int mains
= 0, mpos
= -1;
899 int i
, tx
, ty
, minmoves
;
902 active
= snewn(wh
, int);
903 link
= snewn(wh
, int);
906 while (*desc
&& *desc
!= ',') {
908 ret
= "Too much data in game description";
913 if (*desc
== 'f' || *desc
== 'F') {
916 ret
= "Expected another character after 'f' in game "
922 if (*desc
== 'd' || *desc
== 'D') {
926 if (!isdigit((unsigned char)*desc
)) {
927 ret
= "Expected a number after 'd' in game description";
931 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
933 if (dist
<= 0 || dist
> i
) {
934 ret
= "Out-of-range number after 'd' in game description";
938 if (!active
[i
- dist
]) {
939 ret
= "Invalid back-reference in game description";
946 active
[link
[i
]] = FALSE
;
952 if (!strchr("aAmMeEwW", c
)) {
953 ret
= "Invalid character in game description";
956 if (isdigit((unsigned char)*desc
)) {
958 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
960 if (i
+ count
> wh
) {
961 ret
= "Too much data in game description";
964 while (count
-- > 0) {
965 active
[i
] = (strchr("aAmM", c
) != NULL
);
967 if (strchr("mM", c
) != NULL
) {
976 ret
= (mains
== 0 ?
"No main piece specified in game description" :
977 "More than one main piece specified in game description");
981 ret
= "Not enough data in game description";
986 * Now read the target coordinates.
988 i
= sscanf(desc
, ",%d,%d,%d", &tx
, &ty
, &minmoves
);
990 ret
= "No target coordinates specified";
993 * (but minmoves is optional)
1005 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1007 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
1011 state
= snew(game_state
);
1014 state
->board
= snewn(wh
, unsigned char);
1015 state
->lastmoved
= state
->lastmoved_pos
= -1;
1016 state
->movecount
= 0;
1017 state
->imm
= snew(struct game_immutable_state
);
1018 state
->imm
->refcount
= 1;
1019 state
->imm
->forcefield
= snewn(wh
, unsigned char);
1023 while (*desc
&& *desc
!= ',') {
1034 if (*desc
== 'd' || *desc
== 'D') {
1039 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
1041 state
->board
[i
] = DIST(dist
);
1042 state
->imm
->forcefield
[i
] = f
;
1049 if (isdigit((unsigned char)*desc
)) {
1051 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
1053 assert(i
+ count
<= wh
);
1055 c
= (c
== 'a' || c
== 'A' ? ANCHOR
:
1056 c
== 'm' || c
== 'M' ? MAINANCHOR
:
1057 c
== 'e' || c
== 'E' ? EMPTY
:
1058 /* c == 'w' || c == 'W' ? */ WALL
);
1060 while (count
-- > 0) {
1061 state
->board
[i
] = c
;
1062 state
->imm
->forcefield
[i
] = f
;
1069 * Now read the target coordinates.
1071 state
->tx
= state
->ty
= 0;
1072 state
->minmoves
= -1;
1073 i
= sscanf(desc
, ",%d,%d,%d", &state
->tx
, &state
->ty
, &state
->minmoves
);
1075 if (state
->board
[state
->ty
*w
+state
->tx
] == MAINANCHOR
)
1076 state
->completed
= 0; /* already complete! */
1078 state
->completed
= -1;
1080 state
->cheated
= FALSE
;
1082 state
->soln_index
= -1;
1087 static game_state
*dup_game(game_state
*state
)
1089 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1090 game_state
*ret
= snew(game_state
);
1094 ret
->board
= snewn(wh
, unsigned char);
1095 memcpy(ret
->board
, state
->board
, wh
);
1096 ret
->tx
= state
->tx
;
1097 ret
->ty
= state
->ty
;
1098 ret
->minmoves
= state
->minmoves
;
1099 ret
->lastmoved
= state
->lastmoved
;
1100 ret
->lastmoved_pos
= state
->lastmoved_pos
;
1101 ret
->movecount
= state
->movecount
;
1102 ret
->completed
= state
->completed
;
1103 ret
->cheated
= state
->cheated
;
1104 ret
->imm
= state
->imm
;
1105 ret
->imm
->refcount
++;
1106 ret
->soln
= state
->soln
;
1107 ret
->soln_index
= state
->soln_index
;
1109 ret
->soln
->refcount
++;
1114 static void free_game(game_state
*state
)
1116 if (--state
->imm
->refcount
<= 0) {
1117 sfree(state
->imm
->forcefield
);
1120 if (state
->soln
&& --state
->soln
->refcount
<= 0) {
1121 sfree(state
->soln
->moves
);
1124 sfree(state
->board
);
1128 static char *solve_game(game_state
*state
, game_state
*currstate
,
1129 char *aux
, char **error
)
1137 * Run the solver and attempt to find the shortest solution
1138 * from the current position.
1140 nmoves
= solve_board(state
->w
, state
->h
, state
->board
,
1141 state
->imm
->forcefield
, state
->tx
, state
->ty
,
1145 *error
= "Unable to find a solution to this puzzle";
1149 *error
= "Puzzle is already solved";
1154 * Encode the resulting solution as a move string.
1156 ret
= snewn(nmoves
* 40, char);
1160 for (i
= 0; i
< nmoves
; i
++) {
1161 p
+= sprintf(p
, "%c%d-%d", sep
, moves
[i
*2], moves
[i
*2+1]);
1166 assert(p
- ret
< nmoves
* 40);
1167 ret
= sresize(ret
, p
+1 - ret
, char);
1172 static int game_can_format_as_text_now(game_params
*params
)
1177 static char *game_text_format(game_state
*state
)
1179 return board_text_format(state
->w
, state
->h
, state
->board
,
1180 state
->imm
->forcefield
);
1186 int drag_offset_x
, drag_offset_y
;
1188 unsigned char *reachable
;
1189 int *bfs_queue
; /* used as scratch in interpret_move */
1192 static game_ui
*new_ui(game_state
*state
)
1194 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1195 game_ui
*ui
= snew(game_ui
);
1197 ui
->dragging
= FALSE
;
1198 ui
->drag_anchor
= ui
->drag_currpos
= -1;
1199 ui
->drag_offset_x
= ui
->drag_offset_y
= -1;
1200 ui
->reachable
= snewn(wh
, unsigned char);
1201 memset(ui
->reachable
, 0, wh
);
1202 ui
->bfs_queue
= snewn(wh
, int);
1207 static void free_ui(game_ui
*ui
)
1209 sfree(ui
->bfs_queue
);
1210 sfree(ui
->reachable
);
1214 static char *encode_ui(game_ui
*ui
)
1219 static void decode_ui(game_ui
*ui
, char *encoding
)
1223 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1224 game_state
*newstate
)
1228 #define PREFERRED_TILESIZE 32
1229 #define TILESIZE (ds->tilesize)
1230 #define BORDER (TILESIZE/2)
1231 #define COORD(x) ( (x) * TILESIZE + BORDER )
1232 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1233 #define BORDER_WIDTH (1 + TILESIZE/20)
1234 #define HIGHLIGHT_WIDTH (1 + TILESIZE/16)
1236 #define FLASH_INTERVAL 0.10F
1237 #define FLASH_TIME 3*FLASH_INTERVAL
1239 struct game_drawstate
{
1242 unsigned long *grid
; /* what's currently displayed */
1246 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1247 int x
, int y
, int button
)
1249 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1253 if (button
== LEFT_BUTTON
) {
1257 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
||
1258 !ISBLOCK(state
->board
[ty
*w
+tx
]))
1259 return NULL
; /* this click has no effect */
1262 * User has clicked on a block. Find the block's anchor
1263 * and register that we've started dragging it.
1266 while (ISDIST(state
->board
[i
]))
1267 i
-= state
->board
[i
];
1268 assert(i
>= 0 && i
< wh
);
1270 ui
->dragging
= TRUE
;
1271 ui
->drag_anchor
= i
;
1272 ui
->drag_offset_x
= tx
- (i
% w
);
1273 ui
->drag_offset_y
= ty
- (i
/ w
);
1274 ui
->drag_currpos
= i
;
1277 * Now we immediately bfs out from the current location of
1278 * the anchor, to find all the places to which this block
1281 memset(ui
->reachable
, FALSE
, wh
);
1283 ui
->reachable
[i
] = TRUE
;
1284 ui
->bfs_queue
[qtail
++] = i
;
1285 for (j
= i
; j
< wh
; j
++)
1286 if (state
->board
[j
] == DIST(j
- i
))
1288 while (qhead
< qtail
) {
1289 int pos
= ui
->bfs_queue
[qhead
++];
1290 int x
= pos
% w
, y
= pos
/ w
;
1293 for (dir
= 0; dir
< 4; dir
++) {
1294 int dx
= (dir
== 0 ?
-1 : dir
== 1 ?
+1 : 0);
1295 int dy
= (dir
== 2 ?
-1 : dir
== 3 ?
+1 : 0);
1298 if (x
+ dx
< 0 || x
+ dx
>= w
||
1299 y
+ dy
< 0 || y
+ dy
>= h
)
1302 newpos
= pos
+ dy
*w
+ dx
;
1303 if (ui
->reachable
[newpos
])
1304 continue; /* already done this one */
1307 * Now search the grid to see if the block we're
1308 * dragging could fit into this space.
1310 for (j
= i
; j
>= 0; j
= (ISDIST(state
->board
[j
]) ?
1311 j
- state
->board
[j
] : -1)) {
1312 int jx
= (j
+pos
-ui
->drag_anchor
) % w
;
1313 int jy
= (j
+pos
-ui
->drag_anchor
) / w
;
1316 if (jx
+ dx
< 0 || jx
+ dx
>= w
||
1317 jy
+ dy
< 0 || jy
+ dy
>= h
)
1318 break; /* this position isn't valid at all */
1320 j2
= (j
+pos
-ui
->drag_anchor
) + dy
*w
+ dx
;
1322 if (state
->board
[j2
] == EMPTY
&&
1323 (!state
->imm
->forcefield
[j2
] ||
1324 state
->board
[ui
->drag_anchor
] == MAINANCHOR
))
1326 while (ISDIST(state
->board
[j2
]))
1327 j2
-= state
->board
[j2
];
1328 assert(j2
>= 0 && j2
< wh
);
1329 if (j2
== ui
->drag_anchor
)
1337 * If we got to the end of that loop without
1338 * disqualifying this position, mark it as
1339 * reachable for this drag.
1341 ui
->reachable
[newpos
] = TRUE
;
1342 ui
->bfs_queue
[qtail
++] = newpos
;
1348 * And that's it. Update the display to reflect the start
1352 } else if (button
== LEFT_DRAG
&& ui
->dragging
) {
1353 int dist
, distlimit
, dx
, dy
, s
, px
, py
;
1358 tx
-= ui
->drag_offset_x
;
1359 ty
-= ui
->drag_offset_y
;
1362 * Now search outwards from (tx,ty), in order of Manhattan
1363 * distance, until we find a reachable square.
1366 distlimit
= max(distlimit
, h
+ty
);
1367 distlimit
= max(distlimit
, tx
);
1368 distlimit
= max(distlimit
, ty
);
1369 for (dist
= 0; dist
<= distlimit
; dist
++) {
1370 for (dx
= -dist
; dx
<= dist
; dx
++)
1371 for (s
= -1; s
<= +1; s
+= 2) {
1372 dy
= s
* (dist
- abs(dx
));
1375 if (px
>= 0 && px
< w
&& py
>= 0 && py
< h
&&
1376 ui
->reachable
[py
*w
+px
]) {
1377 ui
->drag_currpos
= py
*w
+px
;
1382 return NULL
; /* give up - this drag has no effect */
1383 } else if (button
== LEFT_RELEASE
&& ui
->dragging
) {
1384 char data
[256], *str
;
1387 * Terminate the drag, and if the piece has actually moved
1388 * then return a move string quoting the old and new
1389 * locations of the piece's anchor.
1391 if (ui
->drag_anchor
!= ui
->drag_currpos
) {
1392 sprintf(data
, "M%d-%d", ui
->drag_anchor
, ui
->drag_currpos
);
1395 str
= ""; /* null move; just update the UI */
1397 ui
->dragging
= FALSE
;
1398 ui
->drag_anchor
= ui
->drag_currpos
= -1;
1399 ui
->drag_offset_x
= ui
->drag_offset_y
= -1;
1400 memset(ui
->reachable
, 0, wh
);
1403 } else if (button
== ' ' && state
->soln
) {
1405 * Make the next move in the stored solution.
1410 a1
= state
->soln
->moves
[state
->soln_index
*2];
1411 a2
= state
->soln
->moves
[state
->soln_index
*2+1];
1412 if (a1
== state
->lastmoved_pos
)
1413 a1
= state
->lastmoved
;
1415 sprintf(data
, "M%d-%d", a1
, a2
);
1416 return dupstr(data
);
1422 static int move_piece(int w
, int h
, const unsigned char *src
,
1423 unsigned char *dst
, unsigned char *ff
, int from
, int to
)
1428 if (!ISANCHOR(dst
[from
]))
1432 * Scan to the far end of the piece's linked list.
1434 for (i
= j
= from
; j
< wh
; j
++)
1435 if (src
[j
] == DIST(j
- i
))
1439 * Remove the piece from its old location in the new
1442 for (j
= i
; j
>= 0; j
= (ISDIST(src
[j
]) ? j
- src
[j
] : -1))
1446 * And put it back in at the new location.
1448 for (j
= i
; j
>= 0; j
= (ISDIST(src
[j
]) ? j
- src
[j
] : -1)) {
1449 int jn
= j
+ to
- from
;
1450 if (jn
< 0 || jn
>= wh
)
1452 if (dst
[jn
] == EMPTY
&& (!ff
[jn
] || src
[from
] == MAINANCHOR
)) {
1462 static game_state
*execute_move(game_state
*state
, char *move
)
1464 int w
= state
->w
, h
= state
->h
/* , wh = w*h */;
1466 int a1
, a2
, n
, movesize
;
1467 game_state
*ret
= dup_game(state
);
1473 * This is a solve move, so we just set up a stored
1476 if (ret
->soln
&& --ret
->soln
->refcount
<= 0) {
1477 sfree(ret
->soln
->moves
);
1480 ret
->soln
= snew(struct game_solution
);
1481 ret
->soln
->nmoves
= 0;
1482 ret
->soln
->moves
= NULL
;
1483 ret
->soln
->refcount
= 1;
1484 ret
->soln_index
= 0;
1485 ret
->cheated
= TRUE
;
1490 if (sscanf(move
, "%d-%d%n", &a1
, &a2
, &n
) != 2) {
1496 * Special case: if the first move in the solution
1497 * involves the piece for which we already have a
1498 * partial stored move, adjust the source point to
1499 * the original starting point of that piece.
1501 if (ret
->soln
->nmoves
== 0 && a1
== ret
->lastmoved
)
1502 a1
= ret
->lastmoved_pos
;
1504 if (ret
->soln
->nmoves
>= movesize
) {
1505 movesize
= (ret
->soln
->nmoves
+ 48) * 4 / 3;
1506 ret
->soln
->moves
= sresize(ret
->soln
->moves
,
1510 ret
->soln
->moves
[2*ret
->soln
->nmoves
] = a1
;
1511 ret
->soln
->moves
[2*ret
->soln
->nmoves
+1] = a2
;
1512 ret
->soln
->nmoves
++;
1516 move
++; /* eat comma */
1518 } else if (c
== 'M') {
1520 if (sscanf(move
, "%d-%d%n", &a1
, &a2
, &n
) != 2 ||
1521 !move_piece(w
, h
, state
->board
, ret
->board
,
1522 state
->imm
->forcefield
, a1
, a2
)) {
1526 if (a1
== ret
->lastmoved
) {
1528 * If the player has moved the same piece as they
1529 * moved last time, don't increment the move
1530 * count. In fact, if they've put the piece back
1531 * where it started from, _decrement_ the move
1534 if (a2
== ret
->lastmoved_pos
) {
1535 ret
->movecount
--; /* reverted last move */
1536 ret
->lastmoved
= ret
->lastmoved_pos
= -1;
1538 ret
->lastmoved
= a2
;
1539 /* don't change lastmoved_pos */
1542 ret
->lastmoved
= a2
;
1543 ret
->lastmoved_pos
= a1
;
1548 * If we have a stored solution path, see if we've
1549 * strayed from it or successfully made the next move
1552 if (ret
->soln
&& ret
->lastmoved_pos
>= 0) {
1553 if (ret
->lastmoved_pos
!=
1554 ret
->soln
->moves
[ret
->soln_index
*2]) {
1555 /* strayed from the path */
1556 ret
->soln
->refcount
--;
1557 assert(ret
->soln
->refcount
> 0);
1558 /* `state' at least still exists */
1560 ret
->soln_index
= -1;
1561 } else if (ret
->lastmoved
==
1562 ret
->soln
->moves
[ret
->soln_index
*2+1]) {
1563 /* advanced along the path */
1565 if (ret
->soln_index
>= ret
->soln
->nmoves
) {
1566 /* finished the path! */
1567 ret
->soln
->refcount
--;
1568 assert(ret
->soln
->refcount
> 0);
1569 /* `state' at least still exists */
1571 ret
->soln_index
= -1;
1576 if (ret
->board
[a2
] == MAINANCHOR
&&
1577 a2
== ret
->ty
* w
+ ret
->tx
&& ret
->completed
< 0)
1578 ret
->completed
= ret
->movecount
;
1595 /* ----------------------------------------------------------------------
1599 static void game_compute_size(game_params
*params
, int tilesize
,
1602 /* fool the macros */
1603 struct dummy
{ int tilesize
; } dummy
, *ds
= &dummy
;
1604 dummy
.tilesize
= tilesize
;
1606 *x
= params
->w
* TILESIZE
+ 2*BORDER
;
1607 *y
= params
->h
* TILESIZE
+ 2*BORDER
;
1610 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1611 game_params
*params
, int tilesize
)
1613 ds
->tilesize
= tilesize
;
1616 static void raise_colour(float *target
, float *src
, float *limit
)
1619 for (i
= 0; i
< 3; i
++)
1620 target
[i
] = (2*src
[i
] + limit
[i
]) / 3;
1623 static float *game_colours(frontend
*fe
, int *ncolours
)
1625 float *ret
= snewn(3 * NCOLOURS
, float);
1627 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_HIGHLIGHT
, COL_LOWLIGHT
);
1630 * When dragging a tile, we light it up a bit.
1632 raise_colour(ret
+3*COL_DRAGGING
,
1633 ret
+3*COL_BACKGROUND
, ret
+3*COL_HIGHLIGHT
);
1634 raise_colour(ret
+3*COL_DRAGGING_HIGHLIGHT
,
1635 ret
+3*COL_HIGHLIGHT
, ret
+3*COL_HIGHLIGHT
);
1636 raise_colour(ret
+3*COL_DRAGGING_LOWLIGHT
,
1637 ret
+3*COL_LOWLIGHT
, ret
+3*COL_HIGHLIGHT
);
1640 * The main tile is tinted blue.
1642 ret
[COL_MAIN
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0];
1643 ret
[COL_MAIN
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1];
1644 ret
[COL_MAIN
* 3 + 2] = ret
[COL_HIGHLIGHT
* 3 + 2];
1645 game_mkhighlight_specific(fe
, ret
, COL_MAIN
,
1646 COL_MAIN_HIGHLIGHT
, COL_MAIN_LOWLIGHT
);
1649 * And we light that up a bit too when dragging.
1651 raise_colour(ret
+3*COL_MAIN_DRAGGING
,
1652 ret
+3*COL_MAIN
, ret
+3*COL_MAIN_HIGHLIGHT
);
1653 raise_colour(ret
+3*COL_MAIN_DRAGGING_HIGHLIGHT
,
1654 ret
+3*COL_MAIN_HIGHLIGHT
, ret
+3*COL_MAIN_HIGHLIGHT
);
1655 raise_colour(ret
+3*COL_MAIN_DRAGGING_LOWLIGHT
,
1656 ret
+3*COL_MAIN_LOWLIGHT
, ret
+3*COL_MAIN_HIGHLIGHT
);
1659 * The target area on the floor is tinted green.
1661 ret
[COL_TARGET
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0];
1662 ret
[COL_TARGET
* 3 + 1] = ret
[COL_HIGHLIGHT
* 3 + 1];
1663 ret
[COL_TARGET
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
1664 game_mkhighlight_specific(fe
, ret
, COL_TARGET
,
1665 COL_TARGET_HIGHLIGHT
, COL_TARGET_LOWLIGHT
);
1667 *ncolours
= NCOLOURS
;
1671 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1673 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1674 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1680 ds
->started
= FALSE
;
1681 ds
->grid
= snewn(wh
, unsigned long);
1682 for (i
= 0; i
< wh
; i
++)
1683 ds
->grid
[i
] = ~(unsigned long)0;
1688 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1694 #define BG_NORMAL 0x00000001UL
1695 #define BG_TARGET 0x00000002UL
1696 #define BG_FORCEFIELD 0x00000004UL
1697 #define FLASH_LOW 0x00000008UL
1698 #define FLASH_HIGH 0x00000010UL
1699 #define FG_WALL 0x00000020UL
1700 #define FG_MAIN 0x00000040UL
1701 #define FG_NORMAL 0x00000080UL
1702 #define FG_DRAGGING 0x00000100UL
1703 #define FG_SHADOW 0x00000200UL
1704 #define FG_SOLVEPIECE 0x00000400UL
1705 #define FG_MAINPIECESH 11
1706 #define FG_SHADOWSH 19
1708 #define PIECE_LBORDER 0x00000001UL
1709 #define PIECE_TBORDER 0x00000002UL
1710 #define PIECE_RBORDER 0x00000004UL
1711 #define PIECE_BBORDER 0x00000008UL
1712 #define PIECE_TLCORNER 0x00000010UL
1713 #define PIECE_TRCORNER 0x00000020UL
1714 #define PIECE_BLCORNER 0x00000040UL
1715 #define PIECE_BRCORNER 0x00000080UL
1716 #define PIECE_MASK 0x000000FFUL
1721 #define TYPE_MASK 0xF000
1722 #define COL_MASK 0x0FFF
1723 #define TYPE_RECT 0x0000
1724 #define TYPE_TLCIRC 0x4000
1725 #define TYPE_TRCIRC 0x5000
1726 #define TYPE_BLCIRC 0x6000
1727 #define TYPE_BRCIRC 0x7000
1728 static void maybe_rect(drawing
*dr
, int x
, int y
, int w
, int h
,
1729 int coltype
, int col2
)
1731 int colour
= coltype
& COL_MASK
, type
= coltype
& TYPE_MASK
;
1733 if (colour
> NCOLOURS
)
1735 if (type
== TYPE_RECT
) {
1736 draw_rect(dr
, x
, y
, w
, h
, colour
);
1740 clip(dr
, x
, y
, w
, h
);
1750 if (col2
== -1 || col2
== coltype
) {
1752 draw_circle(dr
, cx
, cy
, r
, colour
, colour
);
1755 * We aim to draw a quadrant of a circle in two
1756 * different colours. We do this using Bresenham's
1757 * algorithm directly, because the Puzzles drawing API
1758 * doesn't have a draw-sector primitive.
1760 int bx
, by
, bd
, bd2
;
1761 int xm
= (type
& 0x1000 ?
-1 : +1);
1762 int ym
= (type
& 0x2000 ?
-1 : +1);
1772 int x1
= cx
+xm
*bx
, y1
= cy
+ym
*bx
;
1775 x2
= cx
+xm
*by
; y2
= y1
;
1776 draw_rect(dr
, min(x1
,x2
), min(y1
,y2
),
1777 abs(x1
-x2
)+1, abs(y1
-y2
)+1, colour
);
1778 x2
= x1
; y2
= cy
+ym
*by
;
1779 draw_rect(dr
, min(x1
,x2
), min(y1
,y2
),
1780 abs(x1
-x2
)+1, abs(y1
-y2
)+1, col2
);
1784 bd2
= bd
- (2*by
- 1);
1785 if (abs(bd2
) < abs(bd
)) {
1797 static void draw_wallpart(drawing
*dr
, game_drawstate
*ds
,
1798 int tx
, int ty
, unsigned long val
,
1799 int cl
, int cc
, int ch
)
1803 draw_rect(dr
, tx
, ty
, TILESIZE
, TILESIZE
, cc
);
1804 if (val
& PIECE_LBORDER
)
1805 draw_rect(dr
, tx
, ty
, HIGHLIGHT_WIDTH
, TILESIZE
,
1807 if (val
& PIECE_RBORDER
)
1808 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1809 HIGHLIGHT_WIDTH
, TILESIZE
, cl
);
1810 if (val
& PIECE_TBORDER
)
1811 draw_rect(dr
, tx
, ty
, TILESIZE
, HIGHLIGHT_WIDTH
, ch
);
1812 if (val
& PIECE_BBORDER
)
1813 draw_rect(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1814 TILESIZE
, HIGHLIGHT_WIDTH
, cl
);
1815 if (!((PIECE_BBORDER
| PIECE_LBORDER
) &~ val
)) {
1816 draw_rect(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1817 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cl
);
1818 clip(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1819 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
);
1821 coords
[1] = ty
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1822 coords
[2] = tx
+ HIGHLIGHT_WIDTH
;
1823 coords
[3] = ty
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1825 coords
[5] = ty
+ TILESIZE
;
1826 draw_polygon(dr
, coords
, 3, ch
, ch
);
1828 } else if (val
& PIECE_BLCORNER
) {
1829 draw_rect(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1830 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, ch
);
1831 clip(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1832 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
);
1834 coords
[1] = ty
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1835 coords
[2] = tx
+ HIGHLIGHT_WIDTH
;
1836 coords
[3] = ty
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1838 coords
[5] = ty
+ TILESIZE
;
1839 draw_polygon(dr
, coords
, 3, cl
, cl
);
1842 if (!((PIECE_TBORDER
| PIECE_RBORDER
) &~ val
)) {
1843 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1844 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cl
);
1845 clip(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1846 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
);
1847 coords
[0] = tx
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1849 coords
[2] = tx
+ TILESIZE
;
1851 coords
[4] = tx
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1852 coords
[5] = ty
+ HIGHLIGHT_WIDTH
;
1853 draw_polygon(dr
, coords
, 3, ch
, ch
);
1855 } else if (val
& PIECE_TRCORNER
) {
1856 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1857 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, ch
);
1858 clip(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1859 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
);
1860 coords
[0] = tx
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1862 coords
[2] = tx
+ TILESIZE
;
1864 coords
[4] = tx
+ TILESIZE
- HIGHLIGHT_WIDTH
- 1;
1865 coords
[5] = ty
+ HIGHLIGHT_WIDTH
;
1866 draw_polygon(dr
, coords
, 3, cl
, cl
);
1869 if (val
& PIECE_TLCORNER
)
1870 draw_rect(dr
, tx
, ty
, HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, ch
);
1871 if (val
& PIECE_BRCORNER
)
1872 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
,
1873 ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1874 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cl
);
1877 static void draw_piecepart(drawing
*dr
, game_drawstate
*ds
,
1878 int tx
, int ty
, unsigned long val
,
1879 int cl
, int cc
, int ch
)
1884 * Drawing the blocks is hellishly fiddly. The blocks don't
1885 * stretch to the full size of the tile; there's a border
1886 * around them of size BORDER_WIDTH. Then they have bevelled
1887 * borders of size HIGHLIGHT_WIDTH, and also rounded corners.
1889 * I tried for some time to find a clean and clever way to
1890 * figure out what needed drawing from the corner and border
1891 * flags, but in the end the cleanest way I could find was the
1892 * following. We divide the grid square into 25 parts by
1893 * ruling four horizontal and four vertical lines across it;
1894 * those lines are at BORDER_WIDTH and BORDER_WIDTH +
1895 * HIGHLIGHT_WIDTH from the top, from the bottom, from the
1896 * left and from the right. Then we carefully consider each of
1897 * the resulting 25 sections of square, and decide separately
1898 * what needs to go in it based on the flags. In complicated
1899 * cases there can be up to five possibilities affecting any
1900 * given section (no corner or border flags, just the corner
1901 * flag, one border flag, the other border flag, both border
1902 * flags). So there's a lot of very fiddly logic here and all
1903 * I could really think to do was give it my best shot and
1904 * then test it and correct all the typos. Not fun to write,
1905 * and I'm sure it isn't fun to read either, but it seems to
1910 x
[1] = x
[0] + BORDER_WIDTH
;
1911 x
[2] = x
[1] + HIGHLIGHT_WIDTH
;
1912 x
[5] = tx
+ TILESIZE
;
1913 x
[4] = x
[5] - BORDER_WIDTH
;
1914 x
[3] = x
[4] - HIGHLIGHT_WIDTH
;
1917 y
[1] = y
[0] + BORDER_WIDTH
;
1918 y
[2] = y
[1] + HIGHLIGHT_WIDTH
;
1919 y
[5] = ty
+ TILESIZE
;
1920 y
[4] = y
[5] - BORDER_WIDTH
;
1921 y
[3] = y
[4] - HIGHLIGHT_WIDTH
;
1923 #define RECT(p,q) x[p], y[q], x[(p)+1]-x[p], y[(q)+1]-y[q]
1925 maybe_rect(dr
, RECT(0,0),
1926 (val
& (PIECE_TLCORNER
| PIECE_TBORDER
|
1927 PIECE_LBORDER
)) ?
-1 : cc
, -1);
1928 maybe_rect(dr
, RECT(1,0),
1929 (val
& PIECE_TLCORNER
) ? ch
: (val
& PIECE_TBORDER
) ?
-1 :
1930 (val
& PIECE_LBORDER
) ? ch
: cc
, -1);
1931 maybe_rect(dr
, RECT(2,0),
1932 (val
& PIECE_TBORDER
) ?
-1 : cc
, -1);
1933 maybe_rect(dr
, RECT(3,0),
1934 (val
& PIECE_TRCORNER
) ? cl
: (val
& PIECE_TBORDER
) ?
-1 :
1935 (val
& PIECE_RBORDER
) ? cl
: cc
, -1);
1936 maybe_rect(dr
, RECT(4,0),
1937 (val
& (PIECE_TRCORNER
| PIECE_TBORDER
|
1938 PIECE_RBORDER
)) ?
-1 : cc
, -1);
1939 maybe_rect(dr
, RECT(0,1),
1940 (val
& PIECE_TLCORNER
) ? ch
: (val
& PIECE_LBORDER
) ?
-1 :
1941 (val
& PIECE_TBORDER
) ? ch
: cc
, -1);
1942 maybe_rect(dr
, RECT(1,1),
1943 (val
& PIECE_TLCORNER
) ? cc
: -1, -1);
1944 maybe_rect(dr
, RECT(1,1),
1945 (val
& PIECE_TLCORNER
) ? ch
| TYPE_TLCIRC
:
1946 !((PIECE_TBORDER
| PIECE_LBORDER
) &~ val
) ? ch
| TYPE_BRCIRC
:
1947 (val
& (PIECE_TBORDER
| PIECE_LBORDER
)) ? ch
: cc
, -1);
1948 maybe_rect(dr
, RECT(2,1),
1949 (val
& PIECE_TBORDER
) ? ch
: cc
, -1);
1950 maybe_rect(dr
, RECT(3,1),
1951 (val
& PIECE_TRCORNER
) ? cc
: -1, -1);
1952 maybe_rect(dr
, RECT(3,1),
1953 (val
& (PIECE_TBORDER
| PIECE_RBORDER
)) == PIECE_TBORDER ? ch
:
1954 (val
& (PIECE_TBORDER
| PIECE_RBORDER
)) == PIECE_RBORDER ? cl
:
1955 !((PIECE_TBORDER
|PIECE_RBORDER
) &~ val
) ? cl
| TYPE_BLCIRC
:
1956 (val
& PIECE_TRCORNER
) ? cl
| TYPE_TRCIRC
:
1958 maybe_rect(dr
, RECT(4,1),
1959 (val
& PIECE_TRCORNER
) ? ch
: (val
& PIECE_RBORDER
) ?
-1 :
1960 (val
& PIECE_TBORDER
) ? ch
: cc
, -1);
1961 maybe_rect(dr
, RECT(0,2),
1962 (val
& PIECE_LBORDER
) ?
-1 : cc
, -1);
1963 maybe_rect(dr
, RECT(1,2),
1964 (val
& PIECE_LBORDER
) ? ch
: cc
, -1);
1965 maybe_rect(dr
, RECT(2,2),
1967 maybe_rect(dr
, RECT(3,2),
1968 (val
& PIECE_RBORDER
) ? cl
: cc
, -1);
1969 maybe_rect(dr
, RECT(4,2),
1970 (val
& PIECE_RBORDER
) ?
-1 : cc
, -1);
1971 maybe_rect(dr
, RECT(0,3),
1972 (val
& PIECE_BLCORNER
) ? cl
: (val
& PIECE_LBORDER
) ?
-1 :
1973 (val
& PIECE_BBORDER
) ? cl
: cc
, -1);
1974 maybe_rect(dr
, RECT(1,3),
1975 (val
& PIECE_BLCORNER
) ? cc
: -1, -1);
1976 maybe_rect(dr
, RECT(1,3),
1977 (val
& (PIECE_BBORDER
| PIECE_LBORDER
)) == PIECE_BBORDER ? cl
:
1978 (val
& (PIECE_BBORDER
| PIECE_LBORDER
)) == PIECE_LBORDER ? ch
:
1979 !((PIECE_BBORDER
|PIECE_LBORDER
) &~ val
) ? ch
| TYPE_TRCIRC
:
1980 (val
& PIECE_BLCORNER
) ? ch
| TYPE_BLCIRC
:
1982 maybe_rect(dr
, RECT(2,3),
1983 (val
& PIECE_BBORDER
) ? cl
: cc
, -1);
1984 maybe_rect(dr
, RECT(3,3),
1985 (val
& PIECE_BRCORNER
) ? cc
: -1, -1);
1986 maybe_rect(dr
, RECT(3,3),
1987 (val
& PIECE_BRCORNER
) ? cl
| TYPE_BRCIRC
:
1988 !((PIECE_BBORDER
| PIECE_RBORDER
) &~ val
) ? cl
| TYPE_TLCIRC
:
1989 (val
& (PIECE_BBORDER
| PIECE_RBORDER
)) ? cl
: cc
, -1);
1990 maybe_rect(dr
, RECT(4,3),
1991 (val
& PIECE_BRCORNER
) ? cl
: (val
& PIECE_RBORDER
) ?
-1 :
1992 (val
& PIECE_BBORDER
) ? cl
: cc
, -1);
1993 maybe_rect(dr
, RECT(0,4),
1994 (val
& (PIECE_BLCORNER
| PIECE_BBORDER
|
1995 PIECE_LBORDER
)) ?
-1 : cc
, -1);
1996 maybe_rect(dr
, RECT(1,4),
1997 (val
& PIECE_BLCORNER
) ? ch
: (val
& PIECE_BBORDER
) ?
-1 :
1998 (val
& PIECE_LBORDER
) ? ch
: cc
, -1);
1999 maybe_rect(dr
, RECT(2,4),
2000 (val
& PIECE_BBORDER
) ?
-1 : cc
, -1);
2001 maybe_rect(dr
, RECT(3,4),
2002 (val
& PIECE_BRCORNER
) ? cl
: (val
& PIECE_BBORDER
) ?
-1 :
2003 (val
& PIECE_RBORDER
) ? cl
: cc
, -1);
2004 maybe_rect(dr
, RECT(4,4),
2005 (val
& (PIECE_BRCORNER
| PIECE_BBORDER
|
2006 PIECE_RBORDER
)) ?
-1 : cc
, -1);
2011 static void draw_tile(drawing
*dr
, game_drawstate
*ds
,
2012 int x
, int y
, unsigned long val
)
2014 int tx
= COORD(x
), ty
= COORD(y
);
2018 * Draw the tile background.
2020 if (val
& BG_TARGET
)
2023 cc
= COL_BACKGROUND
;
2026 if (val
& FLASH_LOW
)
2028 else if (val
& FLASH_HIGH
)
2031 draw_rect(dr
, tx
, ty
, TILESIZE
, TILESIZE
, cc
);
2032 if (val
& BG_FORCEFIELD
) {
2034 * Cattle-grid effect to indicate that nothing but the
2035 * main block can slide over this square.
2037 int n
= 3 * (TILESIZE
/ (3*HIGHLIGHT_WIDTH
));
2040 for (i
= 1; i
< n
; i
+= 3) {
2041 draw_rect(dr
, tx
,ty
+(TILESIZE
*i
/n
), TILESIZE
,HIGHLIGHT_WIDTH
, cl
);
2042 draw_rect(dr
, tx
+(TILESIZE
*i
/n
),ty
, HIGHLIGHT_WIDTH
,TILESIZE
, cl
);
2047 * Draw the tile midground: a shadow of a block, for
2048 * displaying partial solutions.
2050 if (val
& FG_SHADOW
) {
2051 draw_piecepart(dr
, ds
, tx
, ty
, (val
>> FG_SHADOWSH
) & PIECE_MASK
,
2056 * Draw the tile foreground, i.e. some section of a block or
2059 if (val
& FG_WALL
) {
2060 cc
= COL_BACKGROUND
;
2063 if (val
& FLASH_LOW
)
2065 else if (val
& FLASH_HIGH
)
2068 draw_wallpart(dr
, ds
, tx
, ty
, (val
>> FG_MAINPIECESH
) & PIECE_MASK
,
2070 } else if (val
& (FG_MAIN
| FG_NORMAL
)) {
2071 if (val
& FG_DRAGGING
)
2072 cc
= (val
& FG_MAIN ? COL_MAIN_DRAGGING
: COL_DRAGGING
);
2074 cc
= (val
& FG_MAIN ? COL_MAIN
: COL_BACKGROUND
);
2078 if (val
& FLASH_LOW
)
2080 else if (val
& (FLASH_HIGH
| FG_SOLVEPIECE
))
2083 draw_piecepart(dr
, ds
, tx
, ty
, (val
>> FG_MAINPIECESH
) & PIECE_MASK
,
2087 draw_update(dr
, tx
, ty
, TILESIZE
, TILESIZE
);
2090 static unsigned long find_piecepart(int w
, int h
, int *dsf
, int x
, int y
)
2093 int canon
= dsf_canonify(dsf
, i
);
2094 unsigned long val
= 0;
2096 if (x
== 0 || canon
!= dsf_canonify(dsf
, i
-1))
2097 val
|= PIECE_LBORDER
;
2098 if (y
== 0 || canon
!= dsf_canonify(dsf
, i
-w
))
2099 val
|= PIECE_TBORDER
;
2100 if (x
== w
-1 || canon
!= dsf_canonify(dsf
, i
+1))
2101 val
|= PIECE_RBORDER
;
2102 if (y
== h
-1 || canon
!= dsf_canonify(dsf
, i
+w
))
2103 val
|= PIECE_BBORDER
;
2104 if (!(val
& (PIECE_TBORDER
| PIECE_LBORDER
)) &&
2105 canon
!= dsf_canonify(dsf
, i
-1-w
))
2106 val
|= PIECE_TLCORNER
;
2107 if (!(val
& (PIECE_TBORDER
| PIECE_RBORDER
)) &&
2108 canon
!= dsf_canonify(dsf
, i
+1-w
))
2109 val
|= PIECE_TRCORNER
;
2110 if (!(val
& (PIECE_BBORDER
| PIECE_LBORDER
)) &&
2111 canon
!= dsf_canonify(dsf
, i
-1+w
))
2112 val
|= PIECE_BLCORNER
;
2113 if (!(val
& (PIECE_BBORDER
| PIECE_RBORDER
)) &&
2114 canon
!= dsf_canonify(dsf
, i
+1+w
))
2115 val
|= PIECE_BRCORNER
;
2119 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2120 game_state
*state
, int dir
, game_ui
*ui
,
2121 float animtime
, float flashtime
)
2123 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
2124 unsigned char *board
;
2126 int x
, y
, mainanchor
, mainpos
, dragpos
, solvepos
, solvesrc
, solvedst
;
2130 * The initial contents of the window are not guaranteed
2131 * and can vary with front ends. To be on the safe side,
2132 * all games should start by drawing a big
2133 * background-colour rectangle covering the whole window.
2135 draw_rect(dr
, 0, 0, 10*ds
->tilesize
, 10*ds
->tilesize
, COL_BACKGROUND
);
2140 * Construct the board we'll be displaying (which may be
2141 * different from the one in state if ui describes a drag in
2144 board
= snewn(wh
, unsigned char);
2145 memcpy(board
, state
->board
, wh
);
2147 int mpret
= move_piece(w
, h
, state
->board
, board
,
2148 state
->imm
->forcefield
,
2149 ui
->drag_anchor
, ui
->drag_currpos
);
2154 solvesrc
= state
->soln
->moves
[state
->soln_index
*2];
2155 solvedst
= state
->soln
->moves
[state
->soln_index
*2+1];
2156 if (solvesrc
== state
->lastmoved_pos
)
2157 solvesrc
= state
->lastmoved
;
2158 if (solvesrc
== ui
->drag_anchor
)
2159 solvesrc
= ui
->drag_currpos
;
2161 solvesrc
= solvedst
= -1;
2164 * Build a dsf out of that board, so we can conveniently tell
2165 * which edges are connected and which aren't.
2169 for (y
= 0; y
< h
; y
++)
2170 for (x
= 0; x
< w
; x
++) {
2173 if (ISDIST(board
[i
]))
2174 dsf_merge(dsf
, i
, i
- board
[i
]);
2175 if (board
[i
] == MAINANCHOR
)
2177 if (board
[i
] == WALL
) {
2178 if (x
> 0 && board
[i
-1] == WALL
)
2179 dsf_merge(dsf
, i
, i
-1);
2180 if (y
> 0 && board
[i
-w
] == WALL
)
2181 dsf_merge(dsf
, i
, i
-w
);
2184 assert(mainanchor
>= 0);
2185 mainpos
= dsf_canonify(dsf
, mainanchor
);
2186 dragpos
= ui
->drag_currpos
> 0 ?
dsf_canonify(dsf
, ui
->drag_currpos
) : -1;
2187 solvepos
= solvesrc
>= 0 ?
dsf_canonify(dsf
, solvesrc
) : -1;
2190 * Now we can construct the data about what we want to draw.
2192 for (y
= 0; y
< h
; y
++)
2193 for (x
= 0; x
< w
; x
++) {
2200 * See if this square is part of the target area.
2202 j
= i
+ mainanchor
- (state
->ty
* w
+ state
->tx
);
2203 while (j
>= 0 && j
< wh
&& ISDIST(board
[j
]))
2205 if (j
== mainanchor
)
2210 if (state
->imm
->forcefield
[i
])
2211 val
|= BG_FORCEFIELD
;
2213 if (flashtime
> 0) {
2214 int flashtype
= (int)(flashtime
/ FLASH_INTERVAL
) & 1;
2215 val
|= (flashtype ? FLASH_LOW
: FLASH_HIGH
);
2218 if (board
[i
] != EMPTY
) {
2219 canon
= dsf_canonify(dsf
, i
);
2221 if (board
[i
] == WALL
)
2223 else if (canon
== mainpos
)
2227 if (canon
== dragpos
)
2229 if (canon
== solvepos
)
2230 val
|= FG_SOLVEPIECE
;
2233 * Now look around to see if other squares
2234 * belonging to the same block are adjacent to us.
2236 val
|= find_piecepart(w
, h
, dsf
, x
, y
) << FG_MAINPIECESH
;
2240 * If we're in the middle of showing a solution,
2241 * display a shadow piece for the target of the
2244 if (solvepos
>= 0) {
2245 int si
= i
- solvedst
+ solvesrc
;
2246 if (si
>= 0 && si
< wh
&& dsf_canonify(dsf
, si
) == solvepos
) {
2247 val
|= find_piecepart(w
, h
, dsf
,
2248 si
% w
, si
/ w
) << FG_SHADOWSH
;
2253 if (val
!= ds
->grid
[i
]) {
2254 draw_tile(dr
, ds
, x
, y
, val
);
2260 * Update the status bar.
2263 char statusbuf
[256];
2265 sprintf(statusbuf
, "%sMoves: %d",
2266 (state
->completed
>= 0 ?
2267 (state
->cheated ?
"Auto-solved. " : "COMPLETED! ") :
2268 (state
->cheated ?
"Auto-solver used. " : "")),
2269 (state
->completed
>= 0 ? state
->completed
: state
->movecount
));
2270 if (state
->minmoves
>= 0)
2271 sprintf(statusbuf
+strlen(statusbuf
), " (min %d)",
2274 status_bar(dr
, statusbuf
);
2281 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2282 int dir
, game_ui
*ui
)
2287 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2288 int dir
, game_ui
*ui
)
2290 if (oldstate
->completed
< 0 && newstate
->completed
>= 0)
2296 static int game_is_solved(game_state
*state
)
2298 return state
->completed
;
2301 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2306 static void game_print_size(game_params
*params
, float *x
, float *y
)
2310 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2315 #define thegame slide
2318 const struct game thegame
= {
2319 "Slide", NULL
, NULL
,
2326 TRUE
, game_configure
, custom_params
,
2334 TRUE
, game_can_format_as_text_now
, game_text_format
,
2342 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
2345 game_free_drawstate
,
2350 FALSE
, FALSE
, game_print_size
, game_print
,
2351 TRUE
, /* wants_statusbar */
2352 FALSE
, game_timing_state
,
2356 #ifdef STANDALONE_SOLVER
2360 int main(int argc
, char **argv
)
2364 char *id
= NULL
, *desc
, *err
;
2366 int ret
, really_verbose
= FALSE
;
2369 while (--argc
> 0) {
2371 if (!strcmp(p
, "-v")) {
2372 really_verbose
= TRUE
;
2373 } else if (!strcmp(p
, "-c")) {
2375 } else if (*p
== '-') {
2376 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
2384 fprintf(stderr
, "usage: %s [-c | -v] <game_id>\n", argv
[0]);
2388 desc
= strchr(id
, ':');
2390 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
2395 p
= default_params();
2396 decode_params(p
, id
);
2397 err
= validate_desc(p
, desc
);
2399 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
2402 s
= new_game(NULL
, p
, desc
);
2404 ret
= solve_board(s
->w
, s
->h
, s
->board
, s
->imm
->forcefield
,
2405 s
->tx
, s
->ty
, -1, &moves
);
2407 printf("No solution found\n");
2411 printf("%d moves required\n", ret
);
2416 char *text
= board_text_format(s
->w
, s
->h
, s
->board
,
2417 s
->imm
->forcefield
);
2420 printf("position %d:\n%s", index
, text
);
2426 moveret
= move_piece(s
->w
, s
->h
, s
->board
,
2427 s2
->board
, s
->imm
->forcefield
,
2428 moves
[index
*2], moves
[index
*2+1]);