2 * slide.c: Implementation of the block-sliding puzzle `Klotski'.
8 * - The dragging semantics are still subtly wrong in complex
11 * - Improve the generator.
12 * * actually, we seem to be mostly sensible already now. I
13 * want more choice over the type of main block and location
14 * of the exit/target, and I think I probably ought to give
15 * up on compactness and just bite the bullet and have the
16 * target area right outside the main wall, but mostly I
18 * * the move limit tends to make the game _slower_ to
19 * generate, which is odd. Perhaps investigate why.
21 * - Improve the graphics.
22 * * All the colours are a bit wishy-washy. _Some_ dark
23 * colours would surely not be excessive? Probably darken
24 * the tiles, the walls and the main block, and leave the
26 * * The cattle grid effect is still disgusting. Think of
27 * something completely different.
28 * * I think TRCIRC and BLCIRC should actually be drawn, as a
29 * pair of differently coloured octants. Haul out the
30 * Bresenham code, I suspect.
31 * * The highlight for next-piece-to-move in the solver is
32 * excessive, and the shadow blends in too well with the
33 * piece lowlights. Adjust both.
47 * The implementation of this game revolves around the insight
48 * which makes an exhaustive-search solver feasible: although
49 * there are many blocks which can be rearranged in many ways, any
50 * two blocks of the same shape are _indistinguishable_ and hence
51 * the number of _distinct_ board layouts is generally much
52 * smaller. So we adopt a representation for board layouts which
53 * is inherently canonical, i.e. there are no two distinct
54 * representations which encode indistinguishable layouts.
56 * The way we do this is to encode each square of the board, in
57 * the normal left-to-right top-to-bottom order, as being one of
58 * the following things:
59 * - the first square (in the given order) of a block (`anchor')
60 * - special case of the above: the anchor for the _main_ block
61 * (i.e. the one which the aim of the game is to get to the
63 * - a subsequent square of a block whose previous square was N
65 * - an impassable wall
67 * (We also separately store data about which board positions are
68 * forcefields only passable by the main block. We can't encode
69 * that in the main board data, because then the main block would
70 * destroy forcefields as it went over them.)
72 * Hence, for example, a 2x2 square block would be encoded as
73 * ANCHOR, followed by DIST(1), and w-2 squares later on there
74 * would be DIST(w-1) followed by DIST(1). So if you start at the
75 * last of those squares, the DIST numbers give you a linked list
76 * pointing back through all the other squares in the same block.
78 * So the solver simply does a bfs over all reachable positions,
79 * encoding them in this format and storing them in a tree234 to
80 * ensure it doesn't ever revisit an already-analysed position.
85 * The colours are arranged here so that every base colour is
86 * directly followed by its highlight colour and then its
87 * lowlight colour. Do not break this, or draw_tile() will get
94 COL_DRAGGING_HIGHLIGHT
,
95 COL_DRAGGING_LOWLIGHT
,
100 COL_MAIN_DRAGGING_HIGHLIGHT
,
101 COL_MAIN_DRAGGING_LOWLIGHT
,
103 COL_TARGET_HIGHLIGHT
,
109 * Board layout is a simple array of bytes. Each byte holds:
111 #define ANCHOR 255 /* top-left-most square of some piece */
112 #define MAINANCHOR 254 /* anchor of _main_ piece */
113 #define EMPTY 253 /* empty square */
114 #define WALL 252 /* immovable wall */
116 /* all other values indicate distance back to previous square of same block */
117 #define ISDIST(x) ( (unsigned char)((x)-1) <= MAXDIST-1 )
119 #define ISANCHOR(x) ( (x)==ANCHOR || (x)==MAINANCHOR )
120 #define ISBLOCK(x) ( ISANCHOR(x) || ISDIST(x) )
123 * MAXDIST is the largest DIST value we can encode. This must
124 * therefore also be the maximum puzzle width in theory (although
125 * solver running time will dictate a much smaller limit in
128 #define MAXWID MAXDIST
135 struct game_immutable_state
{
137 unsigned char *forcefield
;
140 struct game_solution
{
142 int *moves
; /* just like from solve_board() */
148 unsigned char *board
;
149 int tx
, ty
; /* target coords for MAINANCHOR */
150 int minmoves
; /* for display only */
151 int lastmoved
, lastmoved_pos
; /* for move counting */
155 struct game_immutable_state
*imm
;
156 struct game_solution
*soln
;
160 static game_params
*default_params(void)
162 game_params
*ret
= snew(game_params
);
171 static const struct game_params slide_presets
[] = {
177 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
182 if (i
< 0 || i
>= lenof(slide_presets
))
185 ret
= snew(game_params
);
186 *ret
= slide_presets
[i
];
188 sprintf(str
, "%dx%d", ret
->w
, ret
->h
);
189 if (ret
->maxmoves
>= 0)
190 sprintf(str
+ strlen(str
), ", max %d moves", ret
->maxmoves
);
192 sprintf(str
+ strlen(str
), ", no move limit");
199 static void free_params(game_params
*params
)
204 static game_params
*dup_params(game_params
*params
)
206 game_params
*ret
= snew(game_params
);
207 *ret
= *params
; /* structure copy */
211 static void decode_params(game_params
*params
, char const *string
)
213 params
->w
= params
->h
= atoi(string
);
214 while (*string
&& isdigit((unsigned char)*string
)) string
++;
215 if (*string
== 'x') {
217 params
->h
= atoi(string
);
218 while (*string
&& isdigit((unsigned char)*string
)) string
++;
220 if (*string
== 'm') {
222 params
->maxmoves
= atoi(string
);
223 while (*string
&& isdigit((unsigned char)*string
)) string
++;
224 } else if (*string
== 'u') {
226 params
->maxmoves
= -1;
230 static char *encode_params(game_params
*params
, int full
)
234 sprintf(data
, "%dx%d", params
->w
, params
->h
);
235 if (params
->maxmoves
>= 0)
236 sprintf(data
+ strlen(data
), "m%d", params
->maxmoves
);
238 sprintf(data
+ strlen(data
), "u");
243 static config_item
*game_configure(game_params
*params
)
248 ret
= snewn(4, config_item
);
250 ret
[0].name
= "Width";
251 ret
[0].type
= C_STRING
;
252 sprintf(buf
, "%d", params
->w
);
253 ret
[0].sval
= dupstr(buf
);
256 ret
[1].name
= "Height";
257 ret
[1].type
= C_STRING
;
258 sprintf(buf
, "%d", params
->h
);
259 ret
[1].sval
= dupstr(buf
);
262 ret
[2].name
= "Solution length limit";
263 ret
[2].type
= C_STRING
;
264 sprintf(buf
, "%d", params
->maxmoves
);
265 ret
[2].sval
= dupstr(buf
);
276 static game_params
*custom_params(config_item
*cfg
)
278 game_params
*ret
= snew(game_params
);
280 ret
->w
= atoi(cfg
[0].sval
);
281 ret
->h
= atoi(cfg
[1].sval
);
282 ret
->maxmoves
= atoi(cfg
[2].sval
);
287 static char *validate_params(game_params
*params
, int full
)
289 if (params
->w
> MAXWID
)
290 return "Width must be at most " STR(MAXWID
);
293 return "Width must be at least 5";
295 return "Height must be at least 4";
300 static char *board_text_format(int w
, int h
, unsigned char *data
,
301 unsigned char *forcefield
)
304 int *dsf
= snew_dsf(wh
);
306 int retpos
, retlen
= (w
*2+2)*(h
*2+1)+1;
307 char *ret
= snewn(retlen
, char);
309 for (i
= 0; i
< wh
; i
++)
311 dsf_merge(dsf
, i
- data
[i
], i
);
313 for (y
= 0; y
< 2*h
+1; y
++) {
314 for (x
= 0; x
< 2*w
+1; x
++) {
316 int i
= (y
/2)*w
+(x
/2);
318 #define dtype(i) (ISBLOCK(data[i]) ? \
319 dsf_canonify(dsf, i) : data[i])
320 #define dchar(t) ((t)==EMPTY ? ' ' : (t)==WALL ? '#' : \
321 data[t] == MAINANCHOR ? '*' : '%')
323 if (y
% 2 && x
% 2) {
326 } else if (y
% 2 && !(x
% 2)) {
327 int j1
= (x
> 0 ?
dtype(i
-1) : -1);
328 int j2
= (x
< 2*w ?
dtype(i
) : -1);
333 } else if (!(y
% 2) && (x
% 2)) {
334 int j1
= (y
> 0 ?
dtype(i
-w
) : -1);
335 int j2
= (y
< 2*h ?
dtype(i
) : -1);
341 int j1
= (x
> 0 && y
> 0 ?
dtype(i
-w
-1) : -1);
342 int j2
= (x
> 0 && y
< 2*h ?
dtype(i
-1) : -1);
343 int j3
= (x
< 2*w
&& y
> 0 ?
dtype(i
-w
) : -1);
344 int j4
= (x
< 2*w
&& y
< 2*h ?
dtype(i
) : -1);
345 if (j1
== j2
&& j2
== j3
&& j3
== j4
)
347 else if (j1
== j2
&& j3
== j4
)
349 else if (j1
== j3
&& j2
== j4
)
355 assert(retpos
< retlen
);
358 assert(retpos
< retlen
);
359 ret
[retpos
++] = '\n';
361 assert(retpos
< retlen
);
362 ret
[retpos
++] = '\0';
363 assert(retpos
== retlen
);
368 /* ----------------------------------------------------------------------
373 * During solver execution, the set of visited board positions is
374 * stored as a tree234 of the following structures. `w', `h' and
375 * `data' are obvious in meaning; `dist' represents the minimum
376 * distance to reach this position from the starting point.
378 * `prev' links each board to the board position from which it was
379 * most efficiently derived.
388 static int boardcmp(void *av
, void *bv
)
390 struct board
*a
= (struct board
*)av
;
391 struct board
*b
= (struct board
*)bv
;
392 return memcmp(a
->data
, b
->data
, a
->w
* a
->h
);
395 static struct board
*newboard(int w
, int h
, unsigned char *data
)
397 struct board
*b
= malloc(sizeof(struct board
) + w
*h
);
398 b
->data
= (unsigned char *)b
+ sizeof(struct board
);
399 memcpy(b
->data
, data
, w
*h
);
408 * The actual solver. Given a board, attempt to find the minimum
409 * length of move sequence which moves MAINANCHOR to (tx,ty), or
410 * -1 if no solution exists. Returns that minimum length.
412 * Also, if `moveout' is provided, writes out the moves in the
413 * form of a sequence of pairs of integers indicating the source
414 * and destination points of the anchor of the moved piece in each
415 * move. Exactly twice as many integers are written as the number
416 * returned from solve_board(), and `moveout' receives an int *
417 * which is a pointer to a dynamically allocated array.
419 static int solve_board(int w
, int h
, unsigned char *board
,
420 unsigned char *forcefield
, int tx
, int ty
,
421 int movelimit
, int **moveout
)
424 struct board
*b
, *b2
, *b3
;
425 int *next
, *anchors
, *which
;
426 int *movereached
, *movequeue
, mqhead
, mqtail
;
427 tree234
*sorted
, *queue
;
432 #ifdef SOLVER_DIAGNOSTICS
434 char *t
= board_text_format(w
, h
, board
);
435 for (i
= 0; i
< h
; i
++) {
436 for (j
= 0; j
< w
; j
++) {
437 int c
= board
[i
*w
+j
];
440 else if (c
== MAINANCHOR
)
442 else if (c
== ANCHOR
)
452 printf("Starting solver for:\n%s\n", t
);
457 sorted
= newtree234(boardcmp
);
458 queue
= newtree234(NULL
);
460 b
= newboard(w
, h
, board
);
463 addpos234(queue
, b
, 0);
466 next
= snewn(wh
, int);
467 anchors
= snewn(wh
, int);
468 which
= snewn(wh
, int);
469 movereached
= snewn(wh
, int);
470 movequeue
= snewn(wh
, int);
473 while ((b
= delpos234(queue
, 0)) != NULL
) {
475 if (movelimit
>= 0 && b
->dist
>= movelimit
) {
477 * The problem is not soluble in under `movelimit'
478 * moves, so we can quit right now.
483 if (b
->dist
!= lastdist
) {
484 #ifdef SOLVER_DIAGNOSTICS
485 printf("dist %d (%d)\n", b
->dist
, count234(sorted
));
490 * Find all the anchors and form a linked list of the
491 * squares within each block.
493 for (i
= 0; i
< wh
; i
++) {
497 if (ISANCHOR(b
->data
[i
])) {
500 } else if (ISDIST(b
->data
[i
])) {
508 * For each anchor, do an array-based BFS to find all the
509 * places we can slide it to.
511 for (i
= 0; i
< wh
; i
++) {
516 for (j
= 0; j
< wh
; j
++)
517 movereached
[j
] = FALSE
;
518 movequeue
[mqtail
++] = i
;
519 while (mqhead
< mqtail
) {
520 int pos
= movequeue
[mqhead
++];
523 * Try to move in each direction from here.
525 for (dir
= 0; dir
< 4; dir
++) {
526 int dx
= (dir
== 0 ?
-1 : dir
== 1 ?
+1 : 0);
527 int dy
= (dir
== 2 ?
-1 : dir
== 3 ?
+1 : 0);
528 int offset
= dy
*w
+ dx
;
529 int newpos
= pos
+ offset
;
533 * For each square involved in this block,
534 * check to see if the square d spaces away
535 * from it is either empty or part of the same
538 for (j
= i
; j
>= 0; j
= next
[j
]) {
539 int jy
= (pos
+j
-i
) / w
+ dy
, jx
= (pos
+j
-i
) % w
+ dx
;
540 if (jy
>= 0 && jy
< h
&& jx
>= 0 && jx
< w
&&
541 ((b
->data
[j
+d
] == EMPTY
|| which
[j
+d
] == i
) &&
542 (b
->data
[i
] == MAINANCHOR
|| !forcefield
[j
+d
])))
548 continue; /* this direction wasn't feasible */
551 * If we've already tried moving this piece
554 if (movereached
[newpos
])
556 movereached
[newpos
] = TRUE
;
557 movequeue
[mqtail
++] = newpos
;
560 * We have a viable move. Make it.
562 b2
= newboard(w
, h
, b
->data
);
563 for (j
= i
; j
>= 0; j
= next
[j
])
565 for (j
= i
; j
>= 0; j
= next
[j
])
566 b2
->data
[j
+d
] = b
->data
[j
];
568 b3
= add234(sorted
, b2
);
570 sfree(b2
); /* we already got one */
572 b2
->dist
= b
->dist
+ 1;
574 addpos234(queue
, b2
, qlen
++);
575 if (b2
->data
[ty
*w
+tx
] == MAINANCHOR
)
576 goto done
; /* search completed! */
590 * Now b2 represents the solved position. Backtrack to
591 * output the solution.
593 *moveout
= snewn(ret
* 2, int);
597 int from
= -1, to
= -1;
602 * Scan b and b2 to find out which piece has
605 for (i
= 0; i
< wh
; i
++) {
606 if (ISANCHOR(b
->data
[i
]) && !ISANCHOR(b2
->data
[i
])) {
609 } else if (!ISANCHOR(b
->data
[i
]) && ISANCHOR(b2
->data
[i
])){
615 assert(from
>= 0 && to
>= 0);
617 (*moveout
)[--j
] = to
;
618 (*moveout
)[--j
] = from
;
625 ret
= -1; /* no solution */
632 while ((b
= delpos234(sorted
, 0)) != NULL
)
645 /* ----------------------------------------------------------------------
646 * Random board generation.
649 static void generate_board(int w
, int h
, int *rtx
, int *rty
, int *minmoves
,
650 random_state
*rs
, unsigned char **rboard
,
651 unsigned char **rforcefield
, int movelimit
)
654 unsigned char *board
, *board2
, *forcefield
;
655 unsigned char *tried_merge
;
657 int *list
, nlist
, pos
;
663 * Set up a board and fill it with singletons, except for a
666 board
= snewn(wh
, unsigned char);
667 forcefield
= snewn(wh
, unsigned char);
668 board2
= snewn(wh
, unsigned char);
669 memset(board
, ANCHOR
, wh
);
670 memset(forcefield
, FALSE
, wh
);
671 for (i
= 0; i
< w
; i
++)
672 board
[i
] = board
[i
+w
*(h
-1)] = WALL
;
673 for (i
= 0; i
< h
; i
++)
674 board
[i
*w
] = board
[i
*w
+(w
-1)] = WALL
;
676 tried_merge
= snewn(wh
* wh
, unsigned char);
677 memset(tried_merge
, 0, wh
*wh
);
681 * Invent a main piece at one extreme. (FIXME: vary the
682 * extreme, and the piece.)
684 board
[w
+1] = MAINANCHOR
;
685 board
[w
+2] = DIST(1);
686 board
[w
*2+1] = DIST(w
-1);
687 board
[w
*2+2] = DIST(1);
690 * Invent a target position. (FIXME: vary this too.)
694 forcefield
[ty
*w
+tx
+1] = forcefield
[(ty
+1)*w
+tx
+1] = TRUE
;
695 board
[ty
*w
+tx
+1] = board
[(ty
+1)*w
+tx
+1] = EMPTY
;
698 * Gradually remove singletons until the game becomes soluble.
700 for (j
= w
; j
-- > 0 ;)
701 for (i
= h
; i
-- > 0 ;)
702 if (board
[i
*w
+j
] == ANCHOR
) {
704 * See if the board is already soluble.
706 if ((moves
= solve_board(w
, h
, board
, forcefield
,
707 tx
, ty
, movelimit
, NULL
)) >= 0)
711 * Otherwise, remove this piece.
713 board
[i
*w
+j
] = EMPTY
;
715 assert(!"We shouldn't get here");
719 * Make a list of all the inter-block edges on the board.
721 list
= snewn(wh
*2, int);
723 for (i
= 0; i
+1 < w
; i
++)
724 for (j
= 0; j
< h
; j
++)
725 list
[nlist
++] = (j
*w
+i
) * 2 + 0; /* edge to the right of j*w+i */
726 for (j
= 0; j
+1 < h
; j
++)
727 for (i
= 0; i
< w
; i
++)
728 list
[nlist
++] = (j
*w
+i
) * 2 + 1; /* edge below j*w+i */
731 * Now go through that list in random order, trying to merge
732 * the blocks on each side of each edge.
734 shuffle(list
, nlist
, sizeof(*list
), rs
);
740 y1
= y2
= pos
/ (w
*2);
741 x1
= x2
= (pos
/ 2) % w
;
750 * Immediately abandon the attempt if we've already tried
751 * to merge the same pair of blocks along a different
754 c1
= dsf_canonify(dsf
, p1
);
755 c2
= dsf_canonify(dsf
, p2
);
756 if (tried_merge
[c1
* wh
+ c2
])
760 * In order to be mergeable, these two squares must each
761 * either be, or belong to, a non-main anchor, and their
762 * anchors must also be distinct.
764 if (!ISBLOCK(board
[p1
]) || !ISBLOCK(board
[p2
]))
766 while (ISDIST(board
[p1
]))
768 while (ISDIST(board
[p2
]))
770 if (board
[p1
] == MAINANCHOR
|| board
[p2
] == MAINANCHOR
|| p1
== p2
)
774 * We can merge these blocks. Try it, and see if the
775 * puzzle remains soluble.
777 memcpy(board2
, board
, wh
);
779 while (p1
< wh
|| p2
< wh
) {
781 * p1 and p2 are the squares at the head of each block
782 * list. Pick the smaller one and put it on the output
789 assert(i
- j
<= MAXDIST
);
790 board
[i
] = DIST(i
- j
);
795 * Now advance whichever list that came from.
800 } while (p1
< wh
&& board
[p1
] != DIST(p1
-i
));
804 } while (p2
< wh
&& board
[p2
] != DIST(p2
-i
));
807 j
= solve_board(w
, h
, board
, forcefield
, tx
, ty
, movelimit
, NULL
);
810 * Didn't work. Revert the merge.
812 memcpy(board
, board2
, wh
);
813 tried_merge
[c1
* wh
+ c2
] = tried_merge
[c2
* wh
+ c1
] = TRUE
;
819 dsf_merge(dsf
, c1
, c2
);
820 c
= dsf_canonify(dsf
, c1
);
821 for (i
= 0; i
< wh
; i
++)
822 tried_merge
[c
*wh
+i
] = (tried_merge
[c1
*wh
+i
] |
823 tried_merge
[c2
*wh
+i
]);
824 for (i
= 0; i
< wh
; i
++)
825 tried_merge
[i
*wh
+c
] = (tried_merge
[i
*wh
+c1
] |
826 tried_merge
[i
*wh
+c2
]);
835 *rforcefield
= forcefield
;
839 /* ----------------------------------------------------------------------
840 * End of solver/generator code.
843 static char *new_game_desc(game_params
*params
, random_state
*rs
,
844 char **aux
, int interactive
)
846 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
847 int tx
, ty
, minmoves
;
848 unsigned char *board
, *forcefield
;
852 generate_board(params
->w
, params
->h
, &tx
, &ty
, &minmoves
, rs
,
853 &board
, &forcefield
, params
->maxmoves
);
854 #ifdef GENERATOR_DIAGNOSTICS
856 char *t
= board_text_format(params
->w
, params
->h
, board
);
863 * Encode as a game ID.
865 ret
= snewn(wh
* 6 + 40, char);
869 if (ISDIST(board
[i
])) {
870 p
+= sprintf(p
, "d%d", board
[i
]);
874 int b
= board
[i
], f
= forcefield
[i
];
875 int c
= (b
== ANCHOR ?
'a' :
876 b
== MAINANCHOR ?
'm' :
878 /* b == WALL ? */ 'w');
882 while (i
< wh
&& board
[i
] == b
&& forcefield
[i
] == f
)
885 p
+= sprintf(p
, "%d", count
);
888 p
+= sprintf(p
, ",%d,%d,%d", tx
, ty
, minmoves
);
889 ret
= sresize(ret
, p
+1 - ret
, char);
897 static char *validate_desc(game_params
*params
, char *desc
)
899 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
901 int mains
= 0, mpos
= -1;
902 int i
, tx
, ty
, minmoves
;
905 active
= snewn(wh
, int);
906 link
= snewn(wh
, int);
909 while (*desc
&& *desc
!= ',') {
911 ret
= "Too much data in game description";
916 if (*desc
== 'f' || *desc
== 'F') {
919 ret
= "Expected another character after 'f' in game "
925 if (*desc
== 'd' || *desc
== 'D') {
929 if (!isdigit((unsigned char)*desc
)) {
930 ret
= "Expected a number after 'd' in game description";
934 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
936 if (dist
<= 0 || dist
> i
) {
937 ret
= "Out-of-range number after 'd' in game description";
941 if (!active
[i
- dist
]) {
942 ret
= "Invalid back-reference in game description";
949 active
[link
[i
]] = FALSE
;
955 if (!strchr("aAmMeEwW", c
)) {
956 ret
= "Invalid character in game description";
959 if (isdigit((unsigned char)*desc
)) {
961 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
963 if (i
+ count
> wh
) {
964 ret
= "Too much data in game description";
967 while (count
-- > 0) {
968 active
[i
] = (strchr("aAmM", c
) != NULL
);
970 if (strchr("mM", c
) != NULL
) {
979 ret
= (mains
== 0 ?
"No main piece specified in game description" :
980 "More than one main piece specified in game description");
984 ret
= "Not enough data in game description";
989 * Now read the target coordinates.
991 i
= sscanf(desc
, ",%d,%d,%d", &tx
, &ty
, &minmoves
);
993 ret
= "No target coordinates specified";
996 * (but minmoves is optional)
1008 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1010 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
1014 state
= snew(game_state
);
1017 state
->board
= snewn(wh
, unsigned char);
1018 state
->lastmoved
= state
->lastmoved_pos
= -1;
1019 state
->movecount
= 0;
1020 state
->imm
= snew(struct game_immutable_state
);
1021 state
->imm
->refcount
= 1;
1022 state
->imm
->forcefield
= snewn(wh
, unsigned char);
1026 while (*desc
&& *desc
!= ',') {
1037 if (*desc
== 'd' || *desc
== 'D') {
1042 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
1044 state
->board
[i
] = DIST(dist
);
1045 state
->imm
->forcefield
[i
] = f
;
1052 if (isdigit((unsigned char)*desc
)) {
1054 while (*desc
&& isdigit((unsigned char)*desc
)) desc
++;
1056 assert(i
+ count
<= wh
);
1058 c
= (c
== 'a' || c
== 'A' ? ANCHOR
:
1059 c
== 'm' || c
== 'M' ? MAINANCHOR
:
1060 c
== 'e' || c
== 'E' ? EMPTY
:
1061 /* c == 'w' || c == 'W' ? */ WALL
);
1063 while (count
-- > 0) {
1064 state
->board
[i
] = c
;
1065 state
->imm
->forcefield
[i
] = f
;
1072 * Now read the target coordinates.
1074 state
->tx
= state
->ty
= 0;
1075 state
->minmoves
= -1;
1076 i
= sscanf(desc
, ",%d,%d,%d", &state
->tx
, &state
->ty
, &state
->minmoves
);
1078 if (state
->board
[state
->ty
*w
+state
->tx
] == MAINANCHOR
)
1079 state
->completed
= 0; /* already complete! */
1081 state
->completed
= -1;
1083 state
->cheated
= FALSE
;
1085 state
->soln_index
= -1;
1090 static game_state
*dup_game(game_state
*state
)
1092 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1093 game_state
*ret
= snew(game_state
);
1097 ret
->board
= snewn(wh
, unsigned char);
1098 memcpy(ret
->board
, state
->board
, wh
);
1099 ret
->tx
= state
->tx
;
1100 ret
->ty
= state
->ty
;
1101 ret
->minmoves
= state
->minmoves
;
1102 ret
->lastmoved
= state
->lastmoved
;
1103 ret
->lastmoved_pos
= state
->lastmoved_pos
;
1104 ret
->movecount
= state
->movecount
;
1105 ret
->completed
= state
->completed
;
1106 ret
->cheated
= state
->cheated
;
1107 ret
->imm
= state
->imm
;
1108 ret
->imm
->refcount
++;
1109 ret
->soln
= state
->soln
;
1110 ret
->soln_index
= state
->soln_index
;
1112 ret
->soln
->refcount
++;
1117 static void free_game(game_state
*state
)
1119 if (--state
->imm
->refcount
<= 0) {
1120 sfree(state
->imm
->forcefield
);
1123 if (state
->soln
&& --state
->soln
->refcount
<= 0) {
1124 sfree(state
->soln
->moves
);
1127 sfree(state
->board
);
1131 static char *solve_game(game_state
*state
, game_state
*currstate
,
1132 char *aux
, char **error
)
1140 * Run the solver and attempt to find the shortest solution
1141 * from the current position.
1143 nmoves
= solve_board(state
->w
, state
->h
, state
->board
,
1144 state
->imm
->forcefield
, state
->tx
, state
->ty
,
1148 *error
= "Unable to find a solution to this puzzle";
1152 *error
= "Puzzle is already solved";
1157 * Encode the resulting solution as a move string.
1159 ret
= snewn(nmoves
* 40, char);
1163 for (i
= 0; i
< nmoves
; i
++) {
1164 p
+= sprintf(p
, "%c%d-%d", sep
, moves
[i
*2], moves
[i
*2+1]);
1169 assert(p
- ret
< nmoves
* 40);
1170 ret
= sresize(ret
, p
+1 - ret
, char);
1175 static char *game_text_format(game_state
*state
)
1177 return board_text_format(state
->w
, state
->h
, state
->board
,
1178 state
->imm
->forcefield
);
1184 int drag_offset_x
, drag_offset_y
;
1186 unsigned char *reachable
;
1187 int *bfs_queue
; /* used as scratch in interpret_move */
1190 static game_ui
*new_ui(game_state
*state
)
1192 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1193 game_ui
*ui
= snew(game_ui
);
1195 ui
->dragging
= FALSE
;
1196 ui
->drag_anchor
= ui
->drag_currpos
= -1;
1197 ui
->drag_offset_x
= ui
->drag_offset_y
= -1;
1198 ui
->reachable
= snewn(wh
, unsigned char);
1199 memset(ui
->reachable
, 0, wh
);
1200 ui
->bfs_queue
= snewn(wh
, int);
1205 static void free_ui(game_ui
*ui
)
1207 sfree(ui
->bfs_queue
);
1208 sfree(ui
->reachable
);
1212 static char *encode_ui(game_ui
*ui
)
1217 static void decode_ui(game_ui
*ui
, char *encoding
)
1221 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1222 game_state
*newstate
)
1226 #define PREFERRED_TILESIZE 32
1227 #define TILESIZE (ds->tilesize)
1228 #define BORDER (TILESIZE/2)
1229 #define COORD(x) ( (x) * TILESIZE + BORDER )
1230 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1231 #define BORDER_WIDTH (1 + TILESIZE/20)
1232 #define HIGHLIGHT_WIDTH (1 + TILESIZE/16)
1234 #define FLASH_INTERVAL 0.10F
1235 #define FLASH_TIME 3*FLASH_INTERVAL
1237 struct game_drawstate
{
1240 unsigned long *grid
; /* what's currently displayed */
1244 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1245 int x
, int y
, int button
)
1247 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1251 if (button
== LEFT_BUTTON
) {
1255 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
||
1256 !ISBLOCK(state
->board
[ty
*w
+tx
]))
1257 return NULL
; /* this click has no effect */
1260 * User has clicked on a block. Find the block's anchor
1261 * and register that we've started dragging it.
1264 while (ISDIST(state
->board
[i
]))
1265 i
-= state
->board
[i
];
1266 assert(i
>= 0 && i
< wh
);
1268 ui
->dragging
= TRUE
;
1269 ui
->drag_anchor
= i
;
1270 ui
->drag_offset_x
= tx
- (i
% w
);
1271 ui
->drag_offset_y
= ty
- (i
/ w
);
1272 ui
->drag_currpos
= i
;
1275 * Now we immediately bfs out from the current location of
1276 * the anchor, to find all the places to which this block
1279 memset(ui
->reachable
, FALSE
, wh
);
1281 ui
->reachable
[i
] = TRUE
;
1282 ui
->bfs_queue
[qtail
++] = i
;
1283 for (j
= i
; j
< wh
; j
++)
1284 if (state
->board
[j
] == DIST(j
- i
))
1286 while (qhead
< qtail
) {
1287 int pos
= ui
->bfs_queue
[qhead
++];
1288 int x
= pos
% w
, y
= pos
/ w
;
1291 for (dir
= 0; dir
< 4; dir
++) {
1292 int dx
= (dir
== 0 ?
-1 : dir
== 1 ?
+1 : 0);
1293 int dy
= (dir
== 2 ?
-1 : dir
== 3 ?
+1 : 0);
1296 if (x
+ dx
< 0 || x
+ dx
>= w
||
1297 y
+ dy
< 0 || y
+ dy
>= h
)
1300 newpos
= pos
+ dy
*w
+ dx
;
1301 if (ui
->reachable
[newpos
])
1302 continue; /* already done this one */
1305 * Now search the grid to see if the block we're
1306 * dragging could fit into this space.
1308 for (j
= i
; j
>= 0; j
= (ISDIST(state
->board
[j
]) ?
1309 j
- state
->board
[j
] : -1)) {
1310 int jx
= (j
+pos
-ui
->drag_anchor
) % w
;
1311 int jy
= (j
+pos
-ui
->drag_anchor
) / w
;
1314 if (jx
+ dx
< 0 || jx
+ dx
>= w
||
1315 jy
+ dy
< 0 || jy
+ dy
>= h
)
1316 break; /* this position isn't valid at all */
1318 j2
= (j
+pos
-ui
->drag_anchor
) + dy
*w
+ dx
;
1320 if (state
->board
[j2
] == EMPTY
&&
1321 (!state
->imm
->forcefield
[j2
] ||
1322 state
->board
[ui
->drag_anchor
] == MAINANCHOR
))
1324 while (ISDIST(state
->board
[j2
]))
1325 j2
-= state
->board
[j2
];
1326 assert(j2
>= 0 && j2
< wh
);
1327 if (j2
== ui
->drag_anchor
)
1335 * If we got to the end of that loop without
1336 * disqualifying this position, mark it as
1337 * reachable for this drag.
1339 ui
->reachable
[newpos
] = TRUE
;
1340 ui
->bfs_queue
[qtail
++] = newpos
;
1346 * And that's it. Update the display to reflect the start
1350 } else if (button
== LEFT_DRAG
&& ui
->dragging
) {
1354 tx
-= ui
->drag_offset_x
;
1355 ty
-= ui
->drag_offset_y
;
1356 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
||
1357 !ui
->reachable
[ty
*w
+tx
])
1358 return NULL
; /* this drag has no effect */
1360 ui
->drag_currpos
= ty
*w
+tx
;
1362 } else if (button
== LEFT_RELEASE
&& ui
->dragging
) {
1363 char data
[256], *str
;
1366 * Terminate the drag, and if the piece has actually moved
1367 * then return a move string quoting the old and new
1368 * locations of the piece's anchor.
1370 if (ui
->drag_anchor
!= ui
->drag_currpos
) {
1371 sprintf(data
, "M%d-%d", ui
->drag_anchor
, ui
->drag_currpos
);
1374 str
= ""; /* null move; just update the UI */
1376 ui
->dragging
= FALSE
;
1377 ui
->drag_anchor
= ui
->drag_currpos
= -1;
1378 ui
->drag_offset_x
= ui
->drag_offset_y
= -1;
1379 memset(ui
->reachable
, 0, wh
);
1382 } else if (button
== ' ' && state
->soln
) {
1384 * Make the next move in the stored solution.
1389 a1
= state
->soln
->moves
[state
->soln_index
*2];
1390 a2
= state
->soln
->moves
[state
->soln_index
*2+1];
1391 if (a1
== state
->lastmoved_pos
)
1392 a1
= state
->lastmoved
;
1394 sprintf(data
, "M%d-%d", a1
, a2
);
1395 return dupstr(data
);
1401 static int move_piece(int w
, int h
, const unsigned char *src
,
1402 unsigned char *dst
, unsigned char *ff
, int from
, int to
)
1407 if (!ISANCHOR(dst
[from
]))
1411 * Scan to the far end of the piece's linked list.
1413 for (i
= j
= from
; j
< wh
; j
++)
1414 if (src
[j
] == DIST(j
- i
))
1418 * Remove the piece from its old location in the new
1421 for (j
= i
; j
>= 0; j
= (ISDIST(src
[j
]) ? j
- src
[j
] : -1))
1425 * And put it back in at the new location.
1427 for (j
= i
; j
>= 0; j
= (ISDIST(src
[j
]) ? j
- src
[j
] : -1)) {
1428 int jn
= j
+ to
- from
;
1429 if (jn
< 0 || jn
>= wh
)
1431 if (dst
[jn
] == EMPTY
&& (!ff
[jn
] || src
[from
] == MAINANCHOR
)) {
1441 static game_state
*execute_move(game_state
*state
, char *move
)
1443 int w
= state
->w
, h
= state
->h
/* , wh = w*h */;
1445 int a1
, a2
, n
, movesize
;
1446 game_state
*ret
= dup_game(state
);
1452 * This is a solve move, so we just set up a stored
1455 if (ret
->soln
&& --ret
->soln
->refcount
<= 0) {
1456 sfree(ret
->soln
->moves
);
1459 ret
->soln
= snew(struct game_solution
);
1460 ret
->soln
->nmoves
= 0;
1461 ret
->soln
->moves
= NULL
;
1462 ret
->soln
->refcount
= 1;
1463 ret
->soln_index
= 0;
1464 ret
->cheated
= TRUE
;
1469 if (sscanf(move
, "%d-%d%n", &a1
, &a2
, &n
) != 2) {
1475 * Special case: if the first move in the solution
1476 * involves the piece for which we already have a
1477 * partial stored move, adjust the source point to
1478 * the original starting point of that piece.
1480 if (ret
->soln
->nmoves
== 0 && a1
== ret
->lastmoved
)
1481 a1
= ret
->lastmoved_pos
;
1483 if (ret
->soln
->nmoves
>= movesize
) {
1484 movesize
= (ret
->soln
->nmoves
+ 48) * 4 / 3;
1485 ret
->soln
->moves
= sresize(ret
->soln
->moves
,
1489 ret
->soln
->moves
[2*ret
->soln
->nmoves
] = a1
;
1490 ret
->soln
->moves
[2*ret
->soln
->nmoves
+1] = a2
;
1491 ret
->soln
->nmoves
++;
1495 move
++; /* eat comma */
1497 } else if (c
== 'M') {
1499 if (sscanf(move
, "%d-%d%n", &a1
, &a2
, &n
) != 2 ||
1500 !move_piece(w
, h
, state
->board
, ret
->board
,
1501 state
->imm
->forcefield
, a1
, a2
)) {
1505 if (a1
== ret
->lastmoved
) {
1507 * If the player has moved the same piece as they
1508 * moved last time, don't increment the move
1509 * count. In fact, if they've put the piece back
1510 * where it started from, _decrement_ the move
1513 if (a2
== ret
->lastmoved_pos
) {
1514 ret
->movecount
--; /* reverted last move */
1515 ret
->lastmoved
= ret
->lastmoved_pos
= -1;
1517 ret
->lastmoved
= a2
;
1518 /* don't change lastmoved_pos */
1521 ret
->lastmoved
= a2
;
1522 ret
->lastmoved_pos
= a1
;
1527 * If we have a stored solution path, see if we've
1528 * strayed from it or successfully made the next move
1531 if (ret
->soln
&& ret
->lastmoved_pos
>= 0) {
1532 if (ret
->lastmoved_pos
!=
1533 ret
->soln
->moves
[ret
->soln_index
*2]) {
1534 /* strayed from the path */
1535 ret
->soln
->refcount
--;
1536 assert(ret
->soln
->refcount
> 0);
1537 /* `state' at least still exists */
1539 ret
->soln_index
= -1;
1540 } else if (ret
->lastmoved
==
1541 ret
->soln
->moves
[ret
->soln_index
*2+1]) {
1542 /* advanced along the path */
1544 if (ret
->soln_index
>= ret
->soln
->nmoves
) {
1545 /* finished the path! */
1546 ret
->soln
->refcount
--;
1547 assert(ret
->soln
->refcount
> 0);
1548 /* `state' at least still exists */
1550 ret
->soln_index
= -1;
1555 if (ret
->board
[a2
] == MAINANCHOR
&&
1556 a2
== ret
->ty
* w
+ ret
->tx
&& ret
->completed
< 0)
1557 ret
->completed
= ret
->movecount
;
1574 /* ----------------------------------------------------------------------
1578 static void game_compute_size(game_params
*params
, int tilesize
,
1581 /* fool the macros */
1582 struct dummy
{ int tilesize
; } dummy
= { tilesize
}, *ds
= &dummy
;
1584 *x
= params
->w
* TILESIZE
+ 2*BORDER
;
1585 *y
= params
->h
* TILESIZE
+ 2*BORDER
;
1588 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1589 game_params
*params
, int tilesize
)
1591 ds
->tilesize
= tilesize
;
1594 static void raise_colour(float *target
, float *src
, float *limit
)
1597 for (i
= 0; i
< 3; i
++)
1598 target
[i
] = (2*src
[i
] + limit
[i
]) / 3;
1601 static float *game_colours(frontend
*fe
, int *ncolours
)
1603 float *ret
= snewn(3 * NCOLOURS
, float);
1605 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_HIGHLIGHT
, COL_LOWLIGHT
);
1608 * When dragging a tile, we light it up a bit.
1610 raise_colour(ret
+3*COL_DRAGGING
,
1611 ret
+3*COL_BACKGROUND
, ret
+3*COL_HIGHLIGHT
);
1612 raise_colour(ret
+3*COL_DRAGGING_HIGHLIGHT
,
1613 ret
+3*COL_HIGHLIGHT
, ret
+3*COL_HIGHLIGHT
);
1614 raise_colour(ret
+3*COL_DRAGGING_LOWLIGHT
,
1615 ret
+3*COL_LOWLIGHT
, ret
+3*COL_HIGHLIGHT
);
1618 * The main tile is tinted blue.
1620 ret
[COL_MAIN
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0];
1621 ret
[COL_MAIN
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1];
1622 ret
[COL_MAIN
* 3 + 2] = ret
[COL_HIGHLIGHT
* 3 + 2];
1623 game_mkhighlight_specific(fe
, ret
, COL_MAIN
,
1624 COL_MAIN_HIGHLIGHT
, COL_MAIN_LOWLIGHT
);
1627 * And we light that up a bit too when dragging.
1629 raise_colour(ret
+3*COL_MAIN_DRAGGING
,
1630 ret
+3*COL_MAIN
, ret
+3*COL_MAIN_HIGHLIGHT
);
1631 raise_colour(ret
+3*COL_MAIN_DRAGGING_HIGHLIGHT
,
1632 ret
+3*COL_MAIN_HIGHLIGHT
, ret
+3*COL_MAIN_HIGHLIGHT
);
1633 raise_colour(ret
+3*COL_MAIN_DRAGGING_LOWLIGHT
,
1634 ret
+3*COL_MAIN_LOWLIGHT
, ret
+3*COL_MAIN_HIGHLIGHT
);
1637 * The target area on the floor is tinted green.
1639 ret
[COL_TARGET
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0];
1640 ret
[COL_TARGET
* 3 + 1] = ret
[COL_HIGHLIGHT
* 3 + 1];
1641 ret
[COL_TARGET
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
1642 game_mkhighlight_specific(fe
, ret
, COL_TARGET
,
1643 COL_TARGET_HIGHLIGHT
, COL_TARGET_LOWLIGHT
);
1645 *ncolours
= NCOLOURS
;
1649 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1651 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1652 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1658 ds
->started
= FALSE
;
1659 ds
->grid
= snewn(wh
, unsigned long);
1660 for (i
= 0; i
< wh
; i
++)
1661 ds
->grid
[i
] = ~(unsigned long)0;
1666 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1672 #define BG_NORMAL 0x00000001UL
1673 #define BG_TARGET 0x00000002UL
1674 #define BG_FORCEFIELD 0x00000004UL
1675 #define FLASH_LOW 0x00000008UL
1676 #define FLASH_HIGH 0x00000010UL
1677 #define FG_WALL 0x00000020UL
1678 #define FG_MAIN 0x00000040UL
1679 #define FG_NORMAL 0x00000080UL
1680 #define FG_DRAGGING 0x00000100UL
1681 #define FG_SHADOW 0x00000200UL
1682 #define FG_SOLVEPIECE 0x00000400UL
1683 #define FG_MAINPIECESH 11
1684 #define FG_SHADOWSH 19
1686 #define PIECE_LBORDER 0x00000001UL
1687 #define PIECE_TBORDER 0x00000002UL
1688 #define PIECE_RBORDER 0x00000004UL
1689 #define PIECE_BBORDER 0x00000008UL
1690 #define PIECE_TLCORNER 0x00000010UL
1691 #define PIECE_TRCORNER 0x00000020UL
1692 #define PIECE_BLCORNER 0x00000040UL
1693 #define PIECE_BRCORNER 0x00000080UL
1694 #define PIECE_MASK 0x000000FFUL
1699 #define TYPE_MASK 0xF000
1700 #define COL_MASK 0x0FFF
1701 #define TYPE_RECT 0x0000
1702 #define TYPE_TLCIRC 0x4000
1703 #define TYPE_TRCIRC 0x5000
1704 #define TYPE_BLCIRC 0x6000
1705 #define TYPE_BRCIRC 0x7000
1706 static void maybe_rect(drawing
*dr
, int x
, int y
, int w
, int h
, int coltype
)
1708 int colour
= coltype
& COL_MASK
, type
= coltype
& TYPE_MASK
;
1710 if (colour
> NCOLOURS
)
1712 if (type
== TYPE_RECT
) {
1713 draw_rect(dr
, x
, y
, w
, h
, colour
);
1717 clip(dr
, x
, y
, w
, h
);
1727 draw_circle(dr
, cx
, cy
, r
, colour
, colour
);
1733 static void draw_wallpart(drawing
*dr
, game_drawstate
*ds
,
1734 int tx
, int ty
, unsigned long val
,
1735 int cl
, int cc
, int ch
)
1737 draw_rect(dr
, tx
, ty
, TILESIZE
, TILESIZE
, cc
);
1738 if (val
& PIECE_LBORDER
)
1739 draw_rect(dr
, tx
, ty
, HIGHLIGHT_WIDTH
, TILESIZE
,
1741 if (val
& PIECE_RBORDER
)
1742 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1743 HIGHLIGHT_WIDTH
, TILESIZE
, cl
);
1744 if (val
& PIECE_TBORDER
)
1745 draw_rect(dr
, tx
, ty
, TILESIZE
, HIGHLIGHT_WIDTH
, ch
);
1746 if (val
& PIECE_BBORDER
)
1747 draw_rect(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1748 TILESIZE
, HIGHLIGHT_WIDTH
, cl
);
1749 if (!((PIECE_BBORDER
| PIECE_LBORDER
) &~ val
))
1750 draw_rect(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1751 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cc
);
1752 if (!((PIECE_TBORDER
| PIECE_RBORDER
) &~ val
))
1753 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1754 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cc
);
1755 if (val
& PIECE_TLCORNER
)
1756 draw_rect(dr
, tx
, ty
, HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, ch
);
1757 if (val
& PIECE_BRCORNER
)
1758 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
,
1759 ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1760 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cl
);
1763 static void draw_piecepart(drawing
*dr
, game_drawstate
*ds
,
1764 int tx
, int ty
, unsigned long val
,
1765 int cl
, int cc
, int ch
)
1770 * Drawing the blocks is hellishly fiddly. The blocks don't
1771 * stretch to the full size of the tile; there's a border
1772 * around them of size BORDER_WIDTH. Then they have bevelled
1773 * borders of size HIGHLIGHT_WIDTH, and also rounded corners.
1775 * I tried for some time to find a clean and clever way to
1776 * figure out what needed drawing from the corner and border
1777 * flags, but in the end the cleanest way I could find was the
1778 * following. We divide the grid square into 25 parts by
1779 * ruling four horizontal and four vertical lines across it;
1780 * those lines are at BORDER_WIDTH and BORDER_WIDTH +
1781 * HIGHLIGHT_WIDTH from the top, from the bottom, from the
1782 * left and from the right. Then we carefully consider each of
1783 * the resulting 25 sections of square, and decide separately
1784 * what needs to go in it based on the flags. In complicated
1785 * cases there can be up to five possibilities affecting any
1786 * given section (no corner or border flags, just the corner
1787 * flag, one border flag, the other border flag, both border
1788 * flags). So there's a lot of very fiddly logic here and all
1789 * I could really think to do was give it my best shot and
1790 * then test it and correct all the typos. Not fun to write,
1791 * and I'm sure it isn't fun to read either, but it seems to
1796 x
[1] = x
[0] + BORDER_WIDTH
;
1797 x
[2] = x
[1] + HIGHLIGHT_WIDTH
;
1798 x
[5] = tx
+ TILESIZE
;
1799 x
[4] = x
[5] - BORDER_WIDTH
;
1800 x
[3] = x
[4] - HIGHLIGHT_WIDTH
;
1803 y
[1] = y
[0] + BORDER_WIDTH
;
1804 y
[2] = y
[1] + HIGHLIGHT_WIDTH
;
1805 y
[5] = ty
+ TILESIZE
;
1806 y
[4] = y
[5] - BORDER_WIDTH
;
1807 y
[3] = y
[4] - HIGHLIGHT_WIDTH
;
1809 #define RECT(p,q) x[p], y[q], x[(p)+1]-x[p], y[(q)+1]-y[q]
1811 maybe_rect(dr
, RECT(0,0),
1812 (val
& (PIECE_TLCORNER
| PIECE_TBORDER
|
1813 PIECE_LBORDER
)) ?
-1 : cc
);
1814 maybe_rect(dr
, RECT(1,0),
1815 (val
& PIECE_TLCORNER
) ? ch
: (val
& PIECE_TBORDER
) ?
-1 :
1816 (val
& PIECE_LBORDER
) ? ch
: cc
);
1817 maybe_rect(dr
, RECT(2,0),
1818 (val
& PIECE_TBORDER
) ?
-1 : cc
);
1819 maybe_rect(dr
, RECT(3,0),
1820 (val
& PIECE_TRCORNER
) ? cl
: (val
& PIECE_TBORDER
) ?
-1 :
1821 (val
& PIECE_RBORDER
) ? cl
: cc
);
1822 maybe_rect(dr
, RECT(4,0),
1823 (val
& (PIECE_TRCORNER
| PIECE_TBORDER
|
1824 PIECE_RBORDER
)) ?
-1 : cc
);
1825 maybe_rect(dr
, RECT(0,1),
1826 (val
& PIECE_TLCORNER
) ? ch
: (val
& PIECE_LBORDER
) ?
-1 :
1827 (val
& PIECE_TBORDER
) ? ch
: cc
);
1828 maybe_rect(dr
, RECT(1,1),
1829 (val
& PIECE_TLCORNER
) ? cc
: -1);
1830 maybe_rect(dr
, RECT(1,1),
1831 (val
& PIECE_TLCORNER
) ? ch
| TYPE_TLCIRC
:
1832 !((PIECE_TBORDER
| PIECE_LBORDER
) &~ val
) ? ch
| TYPE_BRCIRC
:
1833 (val
& (PIECE_TBORDER
| PIECE_LBORDER
)) ? ch
: cc
);
1834 maybe_rect(dr
, RECT(2,1),
1835 (val
& PIECE_TBORDER
) ? ch
: cc
);
1836 maybe_rect(dr
, RECT(3,1),
1837 (val
& (PIECE_TBORDER
| PIECE_RBORDER
)) == PIECE_TBORDER ? ch
:
1838 (val
& (PIECE_TBORDER
| PIECE_RBORDER
)) == PIECE_RBORDER ? cl
:
1839 !((PIECE_TBORDER
|PIECE_RBORDER
) &~ val
) ? cc
| TYPE_BLCIRC
:
1841 maybe_rect(dr
, RECT(4,1),
1842 (val
& PIECE_TRCORNER
) ? ch
: (val
& PIECE_RBORDER
) ?
-1 :
1843 (val
& PIECE_TBORDER
) ? ch
: cc
);
1844 maybe_rect(dr
, RECT(0,2),
1845 (val
& PIECE_LBORDER
) ?
-1 : cc
);
1846 maybe_rect(dr
, RECT(1,2),
1847 (val
& PIECE_LBORDER
) ? ch
: cc
);
1848 maybe_rect(dr
, RECT(2,2),
1850 maybe_rect(dr
, RECT(3,2),
1851 (val
& PIECE_RBORDER
) ? cl
: cc
);
1852 maybe_rect(dr
, RECT(4,2),
1853 (val
& PIECE_RBORDER
) ?
-1 : cc
);
1854 maybe_rect(dr
, RECT(0,3),
1855 (val
& PIECE_BLCORNER
) ? cl
: (val
& PIECE_LBORDER
) ?
-1 :
1856 (val
& PIECE_BBORDER
) ? cl
: cc
);
1857 maybe_rect(dr
, RECT(1,3),
1858 (val
& (PIECE_BBORDER
| PIECE_LBORDER
)) == PIECE_BBORDER ? cl
:
1859 (val
& (PIECE_BBORDER
| PIECE_LBORDER
)) == PIECE_LBORDER ? ch
:
1860 !((PIECE_BBORDER
|PIECE_LBORDER
) &~ val
) ? cc
| TYPE_TRCIRC
:
1862 maybe_rect(dr
, RECT(2,3),
1863 (val
& PIECE_BBORDER
) ? cl
: cc
);
1864 maybe_rect(dr
, RECT(3,3),
1865 (val
& PIECE_BRCORNER
) ? cc
: -1);
1866 maybe_rect(dr
, RECT(3,3),
1867 (val
& PIECE_BRCORNER
) ? cl
| TYPE_BRCIRC
:
1868 !((PIECE_BBORDER
| PIECE_RBORDER
) &~ val
) ? cl
| TYPE_TLCIRC
:
1869 (val
& (PIECE_BBORDER
| PIECE_RBORDER
)) ? cl
: cc
);
1870 maybe_rect(dr
, RECT(4,3),
1871 (val
& PIECE_BRCORNER
) ? cl
: (val
& PIECE_RBORDER
) ?
-1 :
1872 (val
& PIECE_BBORDER
) ? cl
: cc
);
1873 maybe_rect(dr
, RECT(0,4),
1874 (val
& (PIECE_BLCORNER
| PIECE_BBORDER
| PIECE_LBORDER
)) ?
-1 : cc
);
1875 maybe_rect(dr
, RECT(1,4),
1876 (val
& PIECE_BLCORNER
) ? ch
: (val
& PIECE_BBORDER
) ?
-1 :
1877 (val
& PIECE_LBORDER
) ? ch
: cc
);
1878 maybe_rect(dr
, RECT(2,4),
1879 (val
& PIECE_BBORDER
) ?
-1 : cc
);
1880 maybe_rect(dr
, RECT(3,4),
1881 (val
& PIECE_BRCORNER
) ? cl
: (val
& PIECE_BBORDER
) ?
-1 :
1882 (val
& PIECE_RBORDER
) ? cl
: cc
);
1883 maybe_rect(dr
, RECT(4,4),
1884 (val
& (PIECE_BRCORNER
| PIECE_BBORDER
|
1885 PIECE_RBORDER
)) ?
-1 : cc
);
1890 static void draw_tile(drawing
*dr
, game_drawstate
*ds
,
1891 int x
, int y
, unsigned long val
)
1893 int tx
= COORD(x
), ty
= COORD(y
);
1897 * Draw the tile background.
1899 if (val
& BG_TARGET
)
1902 cc
= COL_BACKGROUND
;
1905 if (val
& FLASH_LOW
)
1907 else if (val
& FLASH_HIGH
)
1910 draw_rect(dr
, tx
, ty
, TILESIZE
, TILESIZE
, cc
);
1911 if (val
& BG_FORCEFIELD
) {
1913 * Cattle-grid effect to indicate that nothing but the
1914 * main block can slide over this square.
1916 int n
= 3 * (TILESIZE
/ (3*HIGHLIGHT_WIDTH
));
1919 for (i
= 1; i
< n
; i
+= 3) {
1920 draw_rect(dr
, tx
,ty
+(TILESIZE
*i
/n
), TILESIZE
,HIGHLIGHT_WIDTH
, cl
);
1921 draw_rect(dr
, tx
+(TILESIZE
*i
/n
),ty
, HIGHLIGHT_WIDTH
,TILESIZE
, cl
);
1926 * Draw the tile midground: a shadow of a block, for
1927 * displaying partial solutions.
1929 if (val
& FG_SHADOW
) {
1930 draw_piecepart(dr
, ds
, tx
, ty
, (val
>> FG_SHADOWSH
) & PIECE_MASK
,
1935 * Draw the tile foreground, i.e. some section of a block or
1938 if (val
& FG_WALL
) {
1939 cc
= COL_BACKGROUND
;
1942 if (val
& FLASH_LOW
)
1944 else if (val
& FLASH_HIGH
)
1947 draw_wallpart(dr
, ds
, tx
, ty
, (val
>> FG_MAINPIECESH
) & PIECE_MASK
,
1949 } else if (val
& (FG_MAIN
| FG_NORMAL
)) {
1950 if (val
& FG_DRAGGING
)
1951 cc
= (val
& FG_MAIN ? COL_MAIN_DRAGGING
: COL_DRAGGING
);
1953 cc
= (val
& FG_MAIN ? COL_MAIN
: COL_BACKGROUND
);
1957 if (val
& FLASH_LOW
)
1959 else if (val
& (FLASH_HIGH
| FG_SOLVEPIECE
))
1962 draw_piecepart(dr
, ds
, tx
, ty
, (val
>> FG_MAINPIECESH
) & PIECE_MASK
,
1966 draw_update(dr
, tx
, ty
, TILESIZE
, TILESIZE
);
1969 static unsigned long find_piecepart(int w
, int h
, int *dsf
, int x
, int y
)
1972 int canon
= dsf_canonify(dsf
, i
);
1973 unsigned long val
= 0;
1975 if (x
== 0 || canon
!= dsf_canonify(dsf
, i
-1))
1976 val
|= PIECE_LBORDER
;
1977 if (y
== 0 || canon
!= dsf_canonify(dsf
, i
-w
))
1978 val
|= PIECE_TBORDER
;
1979 if (x
== w
-1 || canon
!= dsf_canonify(dsf
, i
+1))
1980 val
|= PIECE_RBORDER
;
1981 if (y
== h
-1 || canon
!= dsf_canonify(dsf
, i
+w
))
1982 val
|= PIECE_BBORDER
;
1983 if (!(val
& (PIECE_TBORDER
| PIECE_LBORDER
)) &&
1984 canon
!= dsf_canonify(dsf
, i
-1-w
))
1985 val
|= PIECE_TLCORNER
;
1986 if (!(val
& (PIECE_TBORDER
| PIECE_RBORDER
)) &&
1987 canon
!= dsf_canonify(dsf
, i
+1-w
))
1988 val
|= PIECE_TRCORNER
;
1989 if (!(val
& (PIECE_BBORDER
| PIECE_LBORDER
)) &&
1990 canon
!= dsf_canonify(dsf
, i
-1+w
))
1991 val
|= PIECE_BLCORNER
;
1992 if (!(val
& (PIECE_BBORDER
| PIECE_RBORDER
)) &&
1993 canon
!= dsf_canonify(dsf
, i
+1+w
))
1994 val
|= PIECE_BRCORNER
;
1998 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1999 game_state
*state
, int dir
, game_ui
*ui
,
2000 float animtime
, float flashtime
)
2002 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
2003 unsigned char *board
;
2005 int x
, y
, mainanchor
, mainpos
, dragpos
, solvepos
, solvesrc
, solvedst
;
2009 * The initial contents of the window are not guaranteed
2010 * and can vary with front ends. To be on the safe side,
2011 * all games should start by drawing a big
2012 * background-colour rectangle covering the whole window.
2014 draw_rect(dr
, 0, 0, 10*ds
->tilesize
, 10*ds
->tilesize
, COL_BACKGROUND
);
2019 * Construct the board we'll be displaying (which may be
2020 * different from the one in state if ui describes a drag in
2023 board
= snewn(wh
, unsigned char);
2024 memcpy(board
, state
->board
, wh
);
2026 int mpret
= move_piece(w
, h
, state
->board
, board
,
2027 state
->imm
->forcefield
,
2028 ui
->drag_anchor
, ui
->drag_currpos
);
2033 solvesrc
= state
->soln
->moves
[state
->soln_index
*2];
2034 solvedst
= state
->soln
->moves
[state
->soln_index
*2+1];
2035 if (solvesrc
== state
->lastmoved_pos
)
2036 solvesrc
= state
->lastmoved
;
2037 if (solvesrc
== ui
->drag_anchor
)
2038 solvesrc
= ui
->drag_currpos
;
2040 solvesrc
= solvedst
= -1;
2043 * Build a dsf out of that board, so we can conveniently tell
2044 * which edges are connected and which aren't.
2048 for (y
= 0; y
< h
; y
++)
2049 for (x
= 0; x
< w
; x
++) {
2052 if (ISDIST(board
[i
]))
2053 dsf_merge(dsf
, i
, i
- board
[i
]);
2054 if (board
[i
] == MAINANCHOR
)
2056 if (board
[i
] == WALL
) {
2057 if (x
> 0 && board
[i
-1] == WALL
)
2058 dsf_merge(dsf
, i
, i
-1);
2059 if (y
> 0 && board
[i
-w
] == WALL
)
2060 dsf_merge(dsf
, i
, i
-w
);
2063 assert(mainanchor
>= 0);
2064 mainpos
= dsf_canonify(dsf
, mainanchor
);
2065 dragpos
= ui
->drag_currpos
> 0 ?
dsf_canonify(dsf
, ui
->drag_currpos
) : -1;
2066 solvepos
= solvesrc
>= 0 ?
dsf_canonify(dsf
, solvesrc
) : -1;
2069 * Now we can construct the data about what we want to draw.
2071 for (y
= 0; y
< h
; y
++)
2072 for (x
= 0; x
< w
; x
++) {
2079 * See if this square is part of the target area.
2081 j
= i
+ mainanchor
- (state
->ty
* w
+ state
->tx
);
2082 while (j
>= 0 && j
< wh
&& ISDIST(board
[j
]))
2084 if (j
== mainanchor
)
2089 if (state
->imm
->forcefield
[i
])
2090 val
|= BG_FORCEFIELD
;
2092 if (flashtime
> 0) {
2093 int flashtype
= (int)(flashtime
/ FLASH_INTERVAL
) & 1;
2094 val
|= (flashtype ? FLASH_LOW
: FLASH_HIGH
);
2097 if (board
[i
] != EMPTY
) {
2098 canon
= dsf_canonify(dsf
, i
);
2100 if (board
[i
] == WALL
)
2102 else if (canon
== mainpos
)
2106 if (canon
== dragpos
)
2108 if (canon
== solvepos
)
2109 val
|= FG_SOLVEPIECE
;
2112 * Now look around to see if other squares
2113 * belonging to the same block are adjacent to us.
2115 val
|= find_piecepart(w
, h
, dsf
, x
, y
) << FG_MAINPIECESH
;
2119 * If we're in the middle of showing a solution,
2120 * display a shadow piece for the target of the
2123 if (solvepos
>= 0) {
2124 int si
= i
- solvedst
+ solvesrc
;
2125 if (si
>= 0 && si
< wh
&& dsf_canonify(dsf
, si
) == solvepos
) {
2126 val
|= find_piecepart(w
, h
, dsf
,
2127 si
% w
, si
/ w
) << FG_SHADOWSH
;
2132 if (val
!= ds
->grid
[i
]) {
2133 draw_tile(dr
, ds
, x
, y
, val
);
2139 * Update the status bar.
2142 char statusbuf
[256];
2144 sprintf(statusbuf
, "%sMoves: %d",
2145 (state
->completed
>= 0 ?
2146 (state
->cheated ?
"Auto-solved. " : "COMPLETED! ") :
2147 (state
->cheated ?
"Auto-solver used. " : "")),
2148 (state
->completed
>= 0 ? state
->completed
: state
->movecount
));
2149 if (state
->minmoves
>= 0)
2150 sprintf(statusbuf
+strlen(statusbuf
), " (min %d)",
2153 status_bar(dr
, statusbuf
);
2160 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2161 int dir
, game_ui
*ui
)
2166 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2167 int dir
, game_ui
*ui
)
2169 if (oldstate
->completed
< 0 && newstate
->completed
>= 0)
2175 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2180 static void game_print_size(game_params
*params
, float *x
, float *y
)
2184 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2189 #define thegame nullgame
2192 const struct game thegame
= {
2193 "Slide", NULL
, NULL
,
2200 TRUE
, game_configure
, custom_params
,
2208 TRUE
, game_text_format
,
2216 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
2219 game_free_drawstate
,
2223 FALSE
, FALSE
, game_print_size
, game_print
,
2224 TRUE
, /* wants_statusbar */
2225 FALSE
, game_timing_state
,
2229 #ifdef STANDALONE_SOLVER
2233 int main(int argc
, char **argv
)
2237 char *id
= NULL
, *desc
, *err
;
2239 int ret
, really_verbose
= FALSE
;
2242 while (--argc
> 0) {
2244 if (!strcmp(p
, "-v")) {
2245 really_verbose
= TRUE
;
2246 } else if (!strcmp(p
, "-c")) {
2248 } else if (*p
== '-') {
2249 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
2257 fprintf(stderr
, "usage: %s [-c | -v] <game_id>\n", argv
[0]);
2261 desc
= strchr(id
, ':');
2263 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
2268 p
= default_params();
2269 decode_params(p
, id
);
2270 err
= validate_desc(p
, desc
);
2272 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
2275 s
= new_game(NULL
, p
, desc
);
2277 ret
= solve_board(s
->w
, s
->h
, s
->board
, s
->imm
->forcefield
,
2278 s
->tx
, s
->ty
, -1, &moves
);
2280 printf("No solution found\n");
2284 printf("%d moves required\n", ret
);
2289 char *text
= board_text_format(s
->w
, s
->h
, s
->board
,
2290 s
->imm
->forcefield
);
2293 printf("position %d:\n%s", index
, text
);
2299 moveret
= move_piece(s
->w
, s
->h
, s
->board
,
2300 s2
->board
, s
->imm
->forcefield
,
2301 moves
[index
*2], moves
[index
*2+1]);