2 * slide.c: Implementation of the block-sliding puzzle `Klotski'.
9 * * try to generate a solution when Solve is pressed
10 * + from the start, or from here? From here, I fear.
11 * + hence, not much point saving the solution in an aux
13 * * Inertia-like method for telling the user the solution
14 * * standalone solver which draws diagrams
16 * - The dragging semantics are still subtly wrong in complex
19 * - Improve the generator.
20 * * actually, we seem to be mostly sensible already now. I
21 * want more choice over the type of main block and location
22 * of the exit/target, and I think I probably ought to give
23 * up on compactness and just bite the bullet and have the
24 * target area right outside the main wall, but mostly I
26 * * the move limit tends to make the game _slower_ to
27 * generate, which is odd. Perhaps investigate why.
29 * - Improve the graphics.
30 * * All the colours are a bit wishy-washy. _Some_ dark
31 * colours would surely not be excessive? Probably darken
32 * the tiles, the walls and the main block, and leave the
34 * * The cattle grid effect is still disgusting. Think of
35 * something completely different.
49 * The implementation of this game revolves around the insight
50 * which makes an exhaustive-search solver feasible: although
51 * there are many blocks which can be rearranged in many ways, any
52 * two blocks of the same shape are _indistinguishable_ and hence
53 * the number of _distinct_ board layouts is generally much
54 * smaller. So we adopt a representation for board layouts which
55 * is inherently canonical, i.e. there are no two distinct
56 * representations which encode indistinguishable layouts.
58 * The way we do this is to encode each square of the board, in
59 * the normal left-to-right top-to-bottom order, as being one of
60 * the following things:
61 * - the first square (in the given order) of a block (`anchor')
62 * - special case of the above: the anchor for the _main_ block
63 * (i.e. the one which the aim of the game is to get to the
65 * - a subsequent square of a block whose previous square was N
67 * - an impassable wall
69 * (We also separately store data about which board positions are
70 * forcefields only passable by the main block. We can't encode
71 * that in the main board data, because then the main block would
72 * destroy forcefields as it went over them.)
74 * Hence, for example, a 2x2 square block would be encoded as
75 * ANCHOR, followed by DIST(1), and w-2 squares later on there
76 * would be DIST(w-1) followed by DIST(1). So if you start at the
77 * last of those squares, the DIST numbers give you a linked list
78 * pointing back through all the other squares in the same block.
80 * So the solver simply does a bfs over all reachable positions,
81 * encoding them in this format and storing them in a tree234 to
82 * ensure it doesn't ever revisit an already-analysed position.
87 * The colours are arranged here so that every base colour is
88 * directly followed by its highlight colour and then its
89 * lowlight colour. Do not break this, or draw_tile() will get
96 COL_DRAGGING_HIGHLIGHT
,
97 COL_DRAGGING_LOWLIGHT
,
102 COL_MAIN_DRAGGING_HIGHLIGHT
,
103 COL_MAIN_DRAGGING_LOWLIGHT
,
105 COL_TARGET_HIGHLIGHT
,
111 * Board layout is a simple array of bytes. Each byte holds:
113 #define ANCHOR 255 /* top-left-most square of some piece */
114 #define MAINANCHOR 254 /* anchor of _main_ piece */
115 #define EMPTY 253 /* empty square */
116 #define WALL 252 /* immovable wall */
118 /* all other values indicate distance back to previous square of same block */
119 #define ISDIST(x) ( (unsigned char)((x)-1) <= MAXDIST-1 )
121 #define ISANCHOR(x) ( (x)==ANCHOR || (x)==MAINANCHOR )
122 #define ISBLOCK(x) ( ISANCHOR(x) || ISDIST(x) )
125 * MAXDIST is the largest DIST value we can encode. This must
126 * therefore also be the maximum puzzle width in theory (although
127 * solver running time will dictate a much smaller limit in
130 #define MAXWID MAXDIST
137 struct game_immutable_state
{
139 unsigned char *forcefield
;
144 unsigned char *board
;
145 int tx
, ty
; /* target coords for MAINANCHOR */
146 int minmoves
; /* for display only */
147 int lastmoved
, lastmoved_pos
; /* for move counting */
150 struct game_immutable_state
*imm
;
153 static game_params
*default_params(void)
155 game_params
*ret
= snew(game_params
);
164 static const struct game_params slide_presets
[] = {
170 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
175 if (i
< 0 || i
>= lenof(slide_presets
))
178 ret
= snew(game_params
);
179 *ret
= slide_presets
[i
];
181 sprintf(str
, "%dx%d", ret
->w
, ret
->h
);
182 if (ret
->maxmoves
>= 0)
183 sprintf(str
+ strlen(str
), ", max %d moves", ret
->maxmoves
);
185 sprintf(str
+ strlen(str
), ", no move limit");
192 static void free_params(game_params
*params
)
197 static game_params
*dup_params(game_params
*params
)
199 game_params
*ret
= snew(game_params
);
200 *ret
= *params
; /* structure copy */
204 static void decode_params(game_params
*params
, char const *string
)
206 params
->w
= params
->h
= atoi(string
);
207 while (*string
&& isdigit((unsigned char)*string
)) string
++;
208 if (*string
== 'x') {
210 params
->h
= atoi(string
);
211 while (*string
&& isdigit((unsigned char)*string
)) string
++;
213 if (*string
== 'm') {
215 params
->maxmoves
= atoi(string
);
216 while (*string
&& isdigit((unsigned char)*string
)) string
++;
217 } else if (*string
== 'u') {
219 params
->maxmoves
= -1;
223 static char *encode_params(game_params
*params
, int full
)
227 sprintf(data
, "%dx%d", params
->w
, params
->h
);
228 if (params
->maxmoves
>= 0)
229 sprintf(data
+ strlen(data
), "m%d", params
->maxmoves
);
231 sprintf(data
+ strlen(data
), "u");
236 static config_item
*game_configure(game_params
*params
)
241 ret
= snewn(4, config_item
);
243 ret
[0].name
= "Width";
244 ret
[0].type
= C_STRING
;
245 sprintf(buf
, "%d", params
->w
);
246 ret
[0].sval
= dupstr(buf
);
249 ret
[1].name
= "Height";
250 ret
[1].type
= C_STRING
;
251 sprintf(buf
, "%d", params
->h
);
252 ret
[1].sval
= dupstr(buf
);
255 ret
[2].name
= "Solution length limit";
256 ret
[2].type
= C_STRING
;
257 sprintf(buf
, "%d", params
->maxmoves
);
258 ret
[2].sval
= dupstr(buf
);
269 static game_params
*custom_params(config_item
*cfg
)
271 game_params
*ret
= snew(game_params
);
273 ret
->w
= atoi(cfg
[0].sval
);
274 ret
->h
= atoi(cfg
[1].sval
);
275 ret
->maxmoves
= atoi(cfg
[2].sval
);
280 static char *validate_params(game_params
*params
, int full
)
282 if (params
->w
> MAXWID
)
283 return "Width must be at most " STR(MAXWID
);
286 return "Width must be at least 5";
288 return "Height must be at least 4";
293 static char *board_text_format(int w
, int h
, unsigned char *data
,
294 unsigned char *forcefield
)
297 int *dsf
= snew_dsf(wh
);
299 int retpos
, retlen
= (w
*2+2)*(h
*2+1)+1;
300 char *ret
= snewn(retlen
, char);
302 for (i
= 0; i
< wh
; i
++)
304 dsf_merge(dsf
, i
- data
[i
], i
);
306 for (y
= 0; y
< 2*h
+1; y
++) {
307 for (x
= 0; x
< 2*w
+1; x
++) {
309 int i
= (y
/2)*w
+(x
/2);
311 #define dtype(i) (ISBLOCK(data[i]) ? \
312 dsf_canonify(dsf, i) : data[i])
313 #define dchar(t) ((t)==EMPTY ? ' ' : (t)==WALL ? '#' : \
314 data[t] == MAINANCHOR ? '*' : '%')
316 if (y
% 2 && x
% 2) {
319 } else if (y
% 2 && !(x
% 2)) {
320 int j1
= (x
> 0 ?
dtype(i
-1) : -1);
321 int j2
= (x
< 2*w ?
dtype(i
) : -1);
326 } else if (!(y
% 2) && (x
% 2)) {
327 int j1
= (y
> 0 ?
dtype(i
-w
) : -1);
328 int j2
= (y
< 2*h ?
dtype(i
) : -1);
334 int j1
= (x
> 0 && y
> 0 ?
dtype(i
-w
-1) : -1);
335 int j2
= (x
> 0 && y
< 2*h ?
dtype(i
-1) : -1);
336 int j3
= (x
< 2*w
&& y
> 0 ?
dtype(i
-w
) : -1);
337 int j4
= (x
< 2*w
&& y
< 2*h ?
dtype(i
) : -1);
338 if (j1
== j2
&& j2
== j3
&& j3
== j4
)
340 else if (j1
== j2
&& j3
== j4
)
342 else if (j1
== j3
&& j2
== j4
)
348 assert(retpos
< retlen
);
351 assert(retpos
< retlen
);
352 ret
[retpos
++] = '\n';
354 assert(retpos
< retlen
);
355 ret
[retpos
++] = '\0';
356 assert(retpos
== retlen
);
361 /* ----------------------------------------------------------------------
366 * During solver execution, the set of visited board positions is
367 * stored as a tree234 of the following structures. `w', `h' and
368 * `data' are obvious in meaning; `dist' represents the minimum
369 * distance to reach this position from the starting point.
371 * `prev' links each board to the board position from which it was
372 * most efficiently derived.
381 static int boardcmp(void *av
, void *bv
)
383 struct board
*a
= (struct board
*)av
;
384 struct board
*b
= (struct board
*)bv
;
385 return memcmp(a
->data
, b
->data
, a
->w
* a
->h
);
388 static struct board
*newboard(int w
, int h
, unsigned char *data
)
390 struct board
*b
= malloc(sizeof(struct board
) + w
*h
);
391 b
->data
= (unsigned char *)b
+ sizeof(struct board
);
392 memcpy(b
->data
, data
, w
*h
);
401 * The actual solver. Given a board, attempt to find the minimum
402 * length of move sequence which moves MAINANCHOR to (tx,ty), or
403 * -1 if no solution exists. Returns that minimum length.
405 * Also, if `moveout' is provided, writes out the moves in the
406 * form of a sequence of pairs of integers indicating the source
407 * and destination points of the anchor of the moved piece in each
408 * move. Exactly twice as many integers are written as the number
409 * returned from solve_board(), and `moveout' receives an int *
410 * which is a pointer to a dynamically allocated array.
412 static int solve_board(int w
, int h
, unsigned char *board
,
413 unsigned char *forcefield
, int tx
, int ty
,
414 int movelimit
, int **moveout
)
417 struct board
*b
, *b2
, *b3
;
418 int *next
, *anchors
, *which
;
419 int *movereached
, *movequeue
, mqhead
, mqtail
;
420 tree234
*sorted
, *queue
;
425 #ifdef SOLVER_DIAGNOSTICS
427 char *t
= board_text_format(w
, h
, board
);
428 for (i
= 0; i
< h
; i
++) {
429 for (j
= 0; j
< w
; j
++) {
430 int c
= board
[i
*w
+j
];
433 else if (c
== MAINANCHOR
)
435 else if (c
== ANCHOR
)
445 printf("Starting solver for:\n%s\n", t
);
450 sorted
= newtree234(boardcmp
);
451 queue
= newtree234(NULL
);
453 b
= newboard(w
, h
, board
);
456 addpos234(queue
, b
, 0);
459 next
= snewn(wh
, int);
460 anchors
= snewn(wh
, int);
461 which
= snewn(wh
, int);
462 movereached
= snewn(wh
, int);
463 movequeue
= snewn(wh
, int);
466 while ((b
= delpos234(queue
, 0)) != NULL
) {
468 if (movelimit
>= 0 && b
->dist
>= movelimit
) {
470 * The problem is not soluble in under `movelimit'
471 * moves, so we can quit right now.
476 if (b
->dist
!= lastdist
) {
477 #ifdef SOLVER_DIAGNOSTICS
478 printf("dist %d (%d)\n", b
->dist
, count234(sorted
));
483 * Find all the anchors and form a linked list of the
484 * squares within each block.
486 for (i
= 0; i
< wh
; i
++) {
490 if (ISANCHOR(b
->data
[i
])) {
493 } else if (ISDIST(b
->data
[i
])) {
501 * For each anchor, do an array-based BFS to find all the
502 * places we can slide it to.
504 for (i
= 0; i
< wh
; i
++) {
509 for (j
= 0; j
< wh
; j
++)
510 movereached
[j
] = FALSE
;
511 movequeue
[mqtail
++] = i
;
512 while (mqhead
< mqtail
) {
513 int pos
= movequeue
[mqhead
++];
516 * Try to move in each direction from here.
518 for (dir
= 0; dir
< 4; dir
++) {
519 int dx
= (dir
== 0 ?
-1 : dir
== 1 ?
+1 : 0);
520 int dy
= (dir
== 2 ?
-1 : dir
== 3 ?
+1 : 0);
521 int offset
= dy
*w
+ dx
;
522 int newpos
= pos
+ offset
;
526 * For each square involved in this block,
527 * check to see if the square d spaces away
528 * from it is either empty or part of the same
531 for (j
= i
; j
>= 0; j
= next
[j
]) {
532 int jy
= (pos
+j
-i
) / w
+ dy
, jx
= (pos
+j
-i
) % w
+ dx
;
533 if (jy
>= 0 && jy
< h
&& jx
>= 0 && jx
< w
&&
534 ((b
->data
[j
+d
] == EMPTY
|| which
[j
+d
] == i
) &&
535 (b
->data
[i
] == MAINANCHOR
|| !forcefield
[j
+d
])))
541 continue; /* this direction wasn't feasible */
544 * If we've already tried moving this piece
547 if (movereached
[newpos
])
549 movereached
[newpos
] = TRUE
;
550 movequeue
[mqtail
++] = newpos
;
553 * We have a viable move. Make it.
555 b2
= newboard(w
, h
, b
->data
);
556 for (j
= i
; j
>= 0; j
= next
[j
])
558 for (j
= i
; j
>= 0; j
= next
[j
])
559 b2
->data
[j
+d
] = b
->data
[j
];
561 b3
= add234(sorted
, b2
);
563 sfree(b2
); /* we already got one */
565 b2
->dist
= b
->dist
+ 1;
567 addpos234(queue
, b2
, qlen
++);
568 if (b2
->data
[ty
*w
+tx
] == MAINANCHOR
)
569 goto done
; /* search completed! */
583 * Now b2 represents the solved position. Backtrack to
584 * output the solution.
586 *moveout
= snewn(ret
* 2, int);
590 int from
= -1, to
= -1;
595 * Scan b and b2 to find out which piece has
598 for (i
= 0; i
< wh
; i
++) {
599 if (ISANCHOR(b
->data
[i
]) && !ISANCHOR(b2
->data
[i
])) {
602 } else if (!ISANCHOR(b
->data
[i
]) && ISANCHOR(b2
->data
[i
])){
608 assert(from
>= 0 && to
>= 0);
610 (*moveout
)[--j
] = to
;
611 (*moveout
)[--j
] = from
;
618 ret
= -1; /* no solution */
625 while ((b
= delpos234(sorted
, 0)) != NULL
)
638 /* ----------------------------------------------------------------------
639 * Random board generation.
642 static void generate_board(int w
, int h
, int *rtx
, int *rty
, int *minmoves
,
643 random_state
*rs
, unsigned char **rboard
,
644 unsigned char **rforcefield
, int movelimit
)
647 unsigned char *board
, *board2
, *forcefield
;
648 unsigned char *tried_merge
;
650 int *list
, nlist
, pos
;
656 * Set up a board and fill it with singletons, except for a
659 board
= snewn(wh
, unsigned char);
660 forcefield
= snewn(wh
, unsigned char);
661 board2
= snewn(wh
, unsigned char);
662 memset(board
, ANCHOR
, wh
);
663 memset(forcefield
, FALSE
, wh
);
664 for (i
= 0; i
< w
; i
++)
665 board
[i
] = board
[i
+w
*(h
-1)] = WALL
;
666 for (i
= 0; i
< h
; i
++)
667 board
[i
*w
] = board
[i
*w
+(w
-1)] = WALL
;
669 tried_merge
= snewn(wh
* wh
, unsigned char);
670 memset(tried_merge
, 0, wh
*wh
);
674 * Invent a main piece at one extreme. (FIXME: vary the
675 * extreme, and the piece.)
677 board
[w
+1] = MAINANCHOR
;
678 board
[w
+2] = DIST(1);
679 board
[w
*2+1] = DIST(w
-1);
680 board
[w
*2+2] = DIST(1);
683 * Invent a target position. (FIXME: vary this too.)
687 forcefield
[ty
*w
+tx
+1] = forcefield
[(ty
+1)*w
+tx
+1] = TRUE
;
688 board
[ty
*w
+tx
+1] = board
[(ty
+1)*w
+tx
+1] = EMPTY
;
691 * Gradually remove singletons until the game becomes soluble.
693 for (j
= w
; j
-- > 0 ;)
694 for (i
= h
; i
-- > 0 ;)
695 if (board
[i
*w
+j
] == ANCHOR
) {
697 * See if the board is already soluble.
699 if ((moves
= solve_board(w
, h
, board
, forcefield
,
700 tx
, ty
, movelimit
, NULL
)) >= 0)
704 * Otherwise, remove this piece.
706 board
[i
*w
+j
] = EMPTY
;
708 assert(!"We shouldn't get here");
712 * Make a list of all the inter-block edges on the board.
714 list
= snewn(wh
*2, int);
716 for (i
= 0; i
+1 < w
; i
++)
717 for (j
= 0; j
< h
; j
++)
718 list
[nlist
++] = (j
*w
+i
) * 2 + 0; /* edge to the right of j*w+i */
719 for (j
= 0; j
+1 < h
; j
++)
720 for (i
= 0; i
< w
; i
++)
721 list
[nlist
++] = (j
*w
+i
) * 2 + 1; /* edge below j*w+i */
724 * Now go through that list in random order, trying to merge
725 * the blocks on each side of each edge.
727 shuffle(list
, nlist
, sizeof(*list
), rs
);
733 y1
= y2
= pos
/ (w
*2);
734 x1
= x2
= (pos
/ 2) % w
;
743 * Immediately abandon the attempt if we've already tried
744 * to merge the same pair of blocks along a different
747 c1
= dsf_canonify(dsf
, p1
);
748 c2
= dsf_canonify(dsf
, p2
);
749 if (tried_merge
[c1
* wh
+ c2
])
753 * In order to be mergeable, these two squares must each
754 * either be, or belong to, a non-main anchor, and their
755 * anchors must also be distinct.
757 if (!ISBLOCK(board
[p1
]) || !ISBLOCK(board
[p2
]))
759 while (ISDIST(board
[p1
]))
761 while (ISDIST(board
[p2
]))
763 if (board
[p1
] == MAINANCHOR
|| board
[p2
] == MAINANCHOR
|| p1
== p2
)
767 * We can merge these blocks. Try it, and see if the
768 * puzzle remains soluble.
770 memcpy(board2
, board
, wh
);
772 while (p1
< wh
|| p2
< wh
) {
774 * p1 and p2 are the squares at the head of each block
775 * list. Pick the smaller one and put it on the output
782 assert(i
- j
<= MAXDIST
);
783 board
[i
] = DIST(i
- j
);
788 * Now advance whichever list that came from.
793 } while (p1
< wh
&& board
[p1
] != DIST(p1
-i
));
797 } while (p2
< wh
&& board
[p2
] != DIST(p2
-i
));
800 j
= solve_board(w
, h
, board
, forcefield
, tx
, ty
, movelimit
, NULL
);
803 * Didn't work. Revert the merge.
805 memcpy(board
, board2
, wh
);
806 tried_merge
[c1
* wh
+ c2
] = tried_merge
[c2
* wh
+ c1
] = TRUE
;
812 dsf_merge(dsf
, c1
, c2
);
813 c
= dsf_canonify(dsf
, c1
);
814 for (i
= 0; i
< wh
; i
++)
815 tried_merge
[c
*wh
+i
] = (tried_merge
[c1
*wh
+i
] |
816 tried_merge
[c2
*wh
+i
]);
817 for (i
= 0; i
< wh
; i
++)
818 tried_merge
[i
*wh
+c
] = (tried_merge
[i
*wh
+c1
] |
819 tried_merge
[i
*wh
+c2
]);
828 *rforcefield
= forcefield
;
832 /* ----------------------------------------------------------------------
833 * End of solver/generator code.
836 static char *new_game_desc(game_params
*params
, random_state
*rs
,
837 char **aux
, int interactive
)
839 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
840 int tx
, ty
, minmoves
;
841 unsigned char *board
, *forcefield
;
845 generate_board(params
->w
, params
->h
, &tx
, &ty
, &minmoves
, rs
,
846 &board
, &forcefield
, params
->maxmoves
);
847 #ifdef GENERATOR_DIAGNOSTICS
849 char *t
= board_text_format(params
->w
, params
->h
, board
);
856 * Encode as a game ID.
858 ret
= snewn(wh
* 6 + 40, char);
862 if (ISDIST(board
[i
])) {
863 p
+= sprintf(p
, "d%d", board
[i
]);
867 int b
= board
[i
], f
= forcefield
[i
];
868 int c
= (b
== ANCHOR ?
'a' :
869 b
== MAINANCHOR ?
'm' :
871 /* b == WALL ? */ 'w');
875 while (i
< wh
&& board
[i
] == b
&& forcefield
[i
] == f
)
878 p
+= sprintf(p
, "%d", count
);
881 p
+= sprintf(p
, ",%d,%d,%d", tx
, ty
, minmoves
);
882 ret
= sresize(ret
, p
+1 - ret
, char);
885 * FIXME: generate an aux string
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;
1083 static game_state
*dup_game(game_state
*state
)
1085 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1086 game_state
*ret
= snew(game_state
);
1090 ret
->board
= snewn(wh
, unsigned char);
1091 memcpy(ret
->board
, state
->board
, wh
);
1092 ret
->tx
= state
->tx
;
1093 ret
->ty
= state
->ty
;
1094 ret
->minmoves
= state
->minmoves
;
1095 ret
->lastmoved
= state
->lastmoved
;
1096 ret
->lastmoved_pos
= state
->lastmoved_pos
;
1097 ret
->movecount
= state
->movecount
;
1098 ret
->completed
= state
->completed
;
1099 ret
->imm
= state
->imm
;
1100 ret
->imm
->refcount
++;
1105 static void free_game(game_state
*state
)
1107 if (--state
->imm
->refcount
<= 0) {
1108 sfree(state
->imm
->forcefield
);
1111 sfree(state
->board
);
1115 static char *solve_game(game_state
*state
, game_state
*currstate
,
1116 char *aux
, char **error
)
1119 * FIXME: we have a solver, so use it
1121 * FIXME: we should have generated an aux string, so use that
1126 static char *game_text_format(game_state
*state
)
1128 return board_text_format(state
->w
, state
->h
, state
->board
,
1129 state
->imm
->forcefield
);
1135 int drag_offset_x
, drag_offset_y
;
1137 unsigned char *reachable
;
1138 int *bfs_queue
; /* used as scratch in interpret_move */
1141 static game_ui
*new_ui(game_state
*state
)
1143 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1144 game_ui
*ui
= snew(game_ui
);
1146 ui
->dragging
= FALSE
;
1147 ui
->drag_anchor
= ui
->drag_currpos
= -1;
1148 ui
->drag_offset_x
= ui
->drag_offset_y
= -1;
1149 ui
->reachable
= snewn(wh
, unsigned char);
1150 memset(ui
->reachable
, 0, wh
);
1151 ui
->bfs_queue
= snewn(wh
, int);
1156 static void free_ui(game_ui
*ui
)
1158 sfree(ui
->bfs_queue
);
1159 sfree(ui
->reachable
);
1163 static char *encode_ui(game_ui
*ui
)
1168 static void decode_ui(game_ui
*ui
, char *encoding
)
1172 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1173 game_state
*newstate
)
1177 #define PREFERRED_TILESIZE 32
1178 #define TILESIZE (ds->tilesize)
1179 #define BORDER (TILESIZE/2)
1180 #define COORD(x) ( (x) * TILESIZE + BORDER )
1181 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
1182 #define BORDER_WIDTH (1 + TILESIZE/20)
1183 #define HIGHLIGHT_WIDTH (1 + TILESIZE/16)
1185 #define FLASH_INTERVAL 0.10F
1186 #define FLASH_TIME 3*FLASH_INTERVAL
1188 struct game_drawstate
{
1191 unsigned long *grid
; /* what's currently displayed */
1195 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1196 int x
, int y
, int button
)
1198 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1202 if (button
== LEFT_BUTTON
) {
1206 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
||
1207 !ISBLOCK(state
->board
[ty
*w
+tx
]))
1208 return NULL
; /* this click has no effect */
1211 * User has clicked on a block. Find the block's anchor
1212 * and register that we've started dragging it.
1215 while (ISDIST(state
->board
[i
]))
1216 i
-= state
->board
[i
];
1217 assert(i
>= 0 && i
< wh
);
1219 ui
->dragging
= TRUE
;
1220 ui
->drag_anchor
= i
;
1221 ui
->drag_offset_x
= tx
- (i
% w
);
1222 ui
->drag_offset_y
= ty
- (i
/ w
);
1223 ui
->drag_currpos
= i
;
1226 * Now we immediately bfs out from the current location of
1227 * the anchor, to find all the places to which this block
1230 memset(ui
->reachable
, FALSE
, wh
);
1232 ui
->reachable
[i
] = TRUE
;
1233 ui
->bfs_queue
[qtail
++] = i
;
1234 for (j
= i
; j
< wh
; j
++)
1235 if (state
->board
[j
] == DIST(j
- i
))
1237 while (qhead
< qtail
) {
1238 int pos
= ui
->bfs_queue
[qhead
++];
1239 int x
= pos
% w
, y
= pos
/ w
;
1242 for (dir
= 0; dir
< 4; dir
++) {
1243 int dx
= (dir
== 0 ?
-1 : dir
== 1 ?
+1 : 0);
1244 int dy
= (dir
== 2 ?
-1 : dir
== 3 ?
+1 : 0);
1247 if (x
+ dx
< 0 || x
+ dx
>= w
||
1248 y
+ dy
< 0 || y
+ dy
>= h
)
1251 newpos
= pos
+ dy
*w
+ dx
;
1252 if (ui
->reachable
[newpos
])
1253 continue; /* already done this one */
1256 * Now search the grid to see if the block we're
1257 * dragging could fit into this space.
1259 for (j
= i
; j
>= 0; j
= (ISDIST(state
->board
[j
]) ?
1260 j
- state
->board
[j
] : -1)) {
1261 int jx
= (j
+pos
-ui
->drag_anchor
) % w
;
1262 int jy
= (j
+pos
-ui
->drag_anchor
) / w
;
1265 if (jx
+ dx
< 0 || jx
+ dx
>= w
||
1266 jy
+ dy
< 0 || jy
+ dy
>= h
)
1267 break; /* this position isn't valid at all */
1269 j2
= (j
+pos
-ui
->drag_anchor
) + dy
*w
+ dx
;
1271 if (state
->board
[j2
] == EMPTY
&&
1272 (!state
->imm
->forcefield
[j2
] ||
1273 state
->board
[ui
->drag_anchor
] == MAINANCHOR
))
1275 while (ISDIST(state
->board
[j2
]))
1276 j2
-= state
->board
[j2
];
1277 assert(j2
>= 0 && j2
< wh
);
1278 if (j2
== ui
->drag_anchor
)
1286 * If we got to the end of that loop without
1287 * disqualifying this position, mark it as
1288 * reachable for this drag.
1290 ui
->reachable
[newpos
] = TRUE
;
1291 ui
->bfs_queue
[qtail
++] = newpos
;
1297 * And that's it. Update the display to reflect the start
1301 } else if (button
== LEFT_DRAG
&& ui
->dragging
) {
1305 tx
-= ui
->drag_offset_x
;
1306 ty
-= ui
->drag_offset_y
;
1307 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
||
1308 !ui
->reachable
[ty
*w
+tx
])
1309 return NULL
; /* this drag has no effect */
1311 ui
->drag_currpos
= ty
*w
+tx
;
1313 } else if (button
== LEFT_RELEASE
&& ui
->dragging
) {
1314 char data
[256], *str
;
1317 * Terminate the drag, and if the piece has actually moved
1318 * then return a move string quoting the old and new
1319 * locations of the piece's anchor.
1321 if (ui
->drag_anchor
!= ui
->drag_currpos
) {
1322 sprintf(data
, "M%d-%d", ui
->drag_anchor
, ui
->drag_currpos
);
1325 str
= ""; /* null move; just update the UI */
1327 ui
->dragging
= FALSE
;
1328 ui
->drag_anchor
= ui
->drag_currpos
= -1;
1329 ui
->drag_offset_x
= ui
->drag_offset_y
= -1;
1330 memset(ui
->reachable
, 0, wh
);
1338 static int move_piece(int w
, int h
, const unsigned char *src
,
1339 unsigned char *dst
, unsigned char *ff
, int from
, int to
)
1344 if (!ISANCHOR(dst
[from
]))
1348 * Scan to the far end of the piece's linked list.
1350 for (i
= j
= from
; j
< wh
; j
++)
1351 if (src
[j
] == DIST(j
- i
))
1355 * Remove the piece from its old location in the new
1358 for (j
= i
; j
>= 0; j
= (ISDIST(src
[j
]) ? j
- src
[j
] : -1))
1362 * And put it back in at the new location.
1364 for (j
= i
; j
>= 0; j
= (ISDIST(src
[j
]) ? j
- src
[j
] : -1)) {
1365 int jn
= j
+ to
- from
;
1366 if (jn
< 0 || jn
>= wh
)
1368 if (dst
[jn
] == EMPTY
&& (!ff
[jn
] || src
[from
] == MAINANCHOR
)) {
1378 static game_state
*execute_move(game_state
*state
, char *move
)
1380 int w
= state
->w
, h
= state
->h
/* , wh = w*h */;
1383 game_state
*ret
= dup_game(state
);
1389 if (sscanf(move
, "%d-%d%n", &a1
, &a2
, &n
) != 2 ||
1390 !move_piece(w
, h
, state
->board
, ret
->board
,
1391 state
->imm
->forcefield
, a1
, a2
)) {
1395 if (a1
== ret
->lastmoved
) {
1397 * If the player has moved the same piece as they
1398 * moved last time, don't increment the move
1399 * count. In fact, if they've put the piece back
1400 * where it started from, _decrement_ the move
1403 if (a2
== ret
->lastmoved_pos
) {
1404 ret
->movecount
--; /* reverted last move */
1405 ret
->lastmoved
= ret
->lastmoved_pos
= -1;
1407 ret
->lastmoved
= a2
;
1408 /* don't change lastmoved_pos */
1411 ret
->lastmoved
= a2
;
1412 ret
->lastmoved_pos
= a1
;
1415 if (ret
->board
[a2
] == MAINANCHOR
&&
1416 a2
== ret
->ty
* w
+ ret
->tx
&& ret
->completed
< 0)
1417 ret
->completed
= ret
->movecount
;
1434 /* ----------------------------------------------------------------------
1438 static void game_compute_size(game_params
*params
, int tilesize
,
1441 /* fool the macros */
1442 struct dummy
{ int tilesize
; } dummy
= { tilesize
}, *ds
= &dummy
;
1444 *x
= params
->w
* TILESIZE
+ 2*BORDER
;
1445 *y
= params
->h
* TILESIZE
+ 2*BORDER
;
1448 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1449 game_params
*params
, int tilesize
)
1451 ds
->tilesize
= tilesize
;
1454 static void raise_colour(float *target
, float *src
, float *limit
)
1457 for (i
= 0; i
< 3; i
++)
1458 target
[i
] = (2*src
[i
] + limit
[i
]) / 3;
1461 static float *game_colours(frontend
*fe
, int *ncolours
)
1463 float *ret
= snewn(3 * NCOLOURS
, float);
1465 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_HIGHLIGHT
, COL_LOWLIGHT
);
1468 * When dragging a tile, we light it up a bit.
1470 raise_colour(ret
+3*COL_DRAGGING
,
1471 ret
+3*COL_BACKGROUND
, ret
+3*COL_HIGHLIGHT
);
1472 raise_colour(ret
+3*COL_DRAGGING_HIGHLIGHT
,
1473 ret
+3*COL_HIGHLIGHT
, ret
+3*COL_HIGHLIGHT
);
1474 raise_colour(ret
+3*COL_DRAGGING_LOWLIGHT
,
1475 ret
+3*COL_LOWLIGHT
, ret
+3*COL_HIGHLIGHT
);
1478 * The main tile is tinted blue.
1480 ret
[COL_MAIN
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0];
1481 ret
[COL_MAIN
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1];
1482 ret
[COL_MAIN
* 3 + 2] = ret
[COL_HIGHLIGHT
* 3 + 2];
1483 game_mkhighlight_specific(fe
, ret
, COL_MAIN
,
1484 COL_MAIN_HIGHLIGHT
, COL_MAIN_LOWLIGHT
);
1487 * And we light that up a bit too when dragging.
1489 raise_colour(ret
+3*COL_MAIN_DRAGGING
,
1490 ret
+3*COL_MAIN
, ret
+3*COL_MAIN_HIGHLIGHT
);
1491 raise_colour(ret
+3*COL_MAIN_DRAGGING_HIGHLIGHT
,
1492 ret
+3*COL_MAIN_HIGHLIGHT
, ret
+3*COL_MAIN_HIGHLIGHT
);
1493 raise_colour(ret
+3*COL_MAIN_DRAGGING_LOWLIGHT
,
1494 ret
+3*COL_MAIN_LOWLIGHT
, ret
+3*COL_MAIN_HIGHLIGHT
);
1497 * The target area on the floor is tinted green.
1499 ret
[COL_TARGET
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0];
1500 ret
[COL_TARGET
* 3 + 1] = ret
[COL_HIGHLIGHT
* 3 + 1];
1501 ret
[COL_TARGET
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
1502 game_mkhighlight_specific(fe
, ret
, COL_TARGET
,
1503 COL_TARGET_HIGHLIGHT
, COL_TARGET_LOWLIGHT
);
1505 *ncolours
= NCOLOURS
;
1509 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1511 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1512 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1518 ds
->started
= FALSE
;
1519 ds
->grid
= snewn(wh
, unsigned long);
1520 for (i
= 0; i
< wh
; i
++)
1521 ds
->grid
[i
] = ~(unsigned long)0;
1526 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1532 #define BG_NORMAL 0x00000001UL
1533 #define BG_TARGET 0x00000002UL
1534 #define BG_FORCEFIELD 0x00000004UL
1535 #define FLASH_LOW 0x00000008UL
1536 #define FLASH_HIGH 0x00000010UL
1537 #define FG_WALL 0x00000020UL
1538 #define FG_MAIN 0x00000040UL
1539 #define FG_NORMAL 0x00000080UL
1540 #define FG_DRAGGING 0x00000100UL
1541 #define FG_LBORDER 0x00000200UL
1542 #define FG_TBORDER 0x00000400UL
1543 #define FG_RBORDER 0x00000800UL
1544 #define FG_BBORDER 0x00001000UL
1545 #define FG_TLCORNER 0x00002000UL
1546 #define FG_TRCORNER 0x00004000UL
1547 #define FG_BLCORNER 0x00008000UL
1548 #define FG_BRCORNER 0x00010000UL
1553 #define TYPE_MASK 0xF000
1554 #define COL_MASK 0x0FFF
1555 #define TYPE_RECT 0x0000
1556 #define TYPE_TLCIRC 0x4000
1557 #define TYPE_TRCIRC 0x5000
1558 #define TYPE_BLCIRC 0x6000
1559 #define TYPE_BRCIRC 0x7000
1560 static void maybe_rect(drawing
*dr
, int x
, int y
, int w
, int h
, int coltype
)
1562 int colour
= coltype
& COL_MASK
, type
= coltype
& TYPE_MASK
;
1564 if (colour
> NCOLOURS
)
1566 if (type
== TYPE_RECT
) {
1567 draw_rect(dr
, x
, y
, w
, h
, colour
);
1571 clip(dr
, x
, y
, w
, h
);
1581 draw_circle(dr
, cx
, cy
, r
, colour
, colour
);
1587 static void draw_tile(drawing
*dr
, game_drawstate
*ds
,
1588 int x
, int y
, unsigned long val
)
1590 int tx
= COORD(x
), ty
= COORD(y
);
1594 * Draw the tile background.
1596 if (val
& BG_TARGET
)
1599 cc
= COL_BACKGROUND
;
1602 if (val
& FLASH_LOW
)
1604 else if (val
& FLASH_HIGH
)
1607 draw_rect(dr
, tx
, ty
, TILESIZE
, TILESIZE
, cc
);
1608 if (val
& BG_FORCEFIELD
) {
1610 * Cattle-grid effect to indicate that nothing but the
1611 * main block can slide over this square.
1613 int n
= 3 * (TILESIZE
/ (3*HIGHLIGHT_WIDTH
));
1616 for (i
= 1; i
< n
; i
+= 3) {
1617 draw_rect(dr
, tx
,ty
+(TILESIZE
*i
/n
), TILESIZE
,HIGHLIGHT_WIDTH
, cl
);
1618 draw_rect(dr
, tx
+(TILESIZE
*i
/n
),ty
, HIGHLIGHT_WIDTH
,TILESIZE
, cl
);
1623 * Draw the tile foreground, i.e. some section of a block or
1626 if (val
& FG_WALL
) {
1627 cc
= COL_BACKGROUND
;
1630 if (val
& FLASH_LOW
)
1632 else if (val
& FLASH_HIGH
)
1635 draw_rect(dr
, tx
, ty
, TILESIZE
, TILESIZE
, cc
);
1636 if (val
& FG_LBORDER
)
1637 draw_rect(dr
, tx
, ty
, HIGHLIGHT_WIDTH
, TILESIZE
,
1639 if (val
& FG_RBORDER
)
1640 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1641 HIGHLIGHT_WIDTH
, TILESIZE
, cl
);
1642 if (val
& FG_TBORDER
)
1643 draw_rect(dr
, tx
, ty
, TILESIZE
, HIGHLIGHT_WIDTH
, ch
);
1644 if (val
& FG_BBORDER
)
1645 draw_rect(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1646 TILESIZE
, HIGHLIGHT_WIDTH
, cl
);
1647 if (!((FG_BBORDER
| FG_LBORDER
) &~ val
))
1648 draw_rect(dr
, tx
, ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1649 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cc
);
1650 if (!((FG_TBORDER
| FG_RBORDER
) &~ val
))
1651 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
, ty
,
1652 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cc
);
1653 if (val
& FG_TLCORNER
)
1654 draw_rect(dr
, tx
, ty
, HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, ch
);
1655 if (val
& FG_BRCORNER
)
1656 draw_rect(dr
, tx
+TILESIZE
-HIGHLIGHT_WIDTH
,
1657 ty
+TILESIZE
-HIGHLIGHT_WIDTH
,
1658 HIGHLIGHT_WIDTH
, HIGHLIGHT_WIDTH
, cl
);
1659 } else if (val
& (FG_MAIN
| FG_NORMAL
)) {
1662 if (val
& FG_DRAGGING
)
1663 cc
= (val
& FG_MAIN ? COL_MAIN_DRAGGING
: COL_DRAGGING
);
1665 cc
= (val
& FG_MAIN ? COL_MAIN
: COL_BACKGROUND
);
1669 if (val
& FLASH_LOW
)
1671 else if (val
& FLASH_HIGH
)
1675 * Drawing the blocks is hellishly fiddly. The blocks
1676 * don't stretch to the full size of the tile; there's a
1677 * border around them of size BORDER_WIDTH. Then they have
1678 * bevelled borders of size HIGHLIGHT_WIDTH, and also
1681 * I tried for some time to find a clean and clever way to
1682 * figure out what needed drawing from the corner and
1683 * border flags, but in the end the cleanest way I could
1684 * find was the following. We divide the grid square into
1685 * 25 parts by ruling four horizontal and four vertical
1686 * lines across it; those lines are at BORDER_WIDTH and
1687 * BORDER_WIDTH+HIGHLIGHT_WIDTH from the top, from the
1688 * bottom, from the left and from the right. Then we
1689 * carefully consider each of the resulting 25 sections of
1690 * square, and decide separately what needs to go in it
1691 * based on the flags. In complicated cases there can be
1692 * up to five possibilities affecting any given section
1693 * (no corner or border flags, just the corner flag, one
1694 * border flag, the other border flag, both border flags).
1695 * So there's a lot of very fiddly logic here and all I
1696 * could really think to do was give it my best shot and
1697 * then test it and correct all the typos. Not fun to
1698 * write, and I'm sure it isn't fun to read either, but it
1703 x
[1] = x
[0] + BORDER_WIDTH
;
1704 x
[2] = x
[1] + HIGHLIGHT_WIDTH
;
1705 x
[5] = tx
+ TILESIZE
;
1706 x
[4] = x
[5] - BORDER_WIDTH
;
1707 x
[3] = x
[4] - HIGHLIGHT_WIDTH
;
1710 y
[1] = y
[0] + BORDER_WIDTH
;
1711 y
[2] = y
[1] + HIGHLIGHT_WIDTH
;
1712 y
[5] = ty
+ TILESIZE
;
1713 y
[4] = y
[5] - BORDER_WIDTH
;
1714 y
[3] = y
[4] - HIGHLIGHT_WIDTH
;
1716 #define RECT(p,q) x[p], y[q], x[(p)+1]-x[p], y[(q)+1]-y[q]
1718 maybe_rect(dr
, RECT(0,0),
1719 (val
& (FG_TLCORNER
| FG_TBORDER
| FG_LBORDER
)) ?
-1 : cc
);
1720 maybe_rect(dr
, RECT(1,0),
1721 (val
& FG_TLCORNER
) ? ch
: (val
& FG_TBORDER
) ?
-1 :
1722 (val
& FG_LBORDER
) ? ch
: cc
);
1723 maybe_rect(dr
, RECT(2,0),
1724 (val
& FG_TBORDER
) ?
-1 : cc
);
1725 maybe_rect(dr
, RECT(3,0),
1726 (val
& FG_TRCORNER
) ? cl
: (val
& FG_TBORDER
) ?
-1 :
1727 (val
& FG_RBORDER
) ? cl
: cc
);
1728 maybe_rect(dr
, RECT(4,0),
1729 (val
& (FG_TRCORNER
| FG_TBORDER
| FG_RBORDER
)) ?
-1 : cc
);
1730 maybe_rect(dr
, RECT(0,1),
1731 (val
& FG_TLCORNER
) ? ch
: (val
& FG_LBORDER
) ?
-1 :
1732 (val
& FG_TBORDER
) ? ch
: cc
);
1733 maybe_rect(dr
, RECT(1,1),
1734 (val
& FG_TLCORNER
) ? cc
: -1);
1735 maybe_rect(dr
, RECT(1,1),
1736 (val
& FG_TLCORNER
) ? ch
| TYPE_TLCIRC
:
1737 !((FG_TBORDER
| FG_LBORDER
) &~ val
) ? ch
| TYPE_BRCIRC
:
1738 (val
& (FG_TBORDER
| FG_LBORDER
)) ? ch
: cc
);
1739 maybe_rect(dr
, RECT(2,1),
1740 (val
& FG_TBORDER
) ? ch
: cc
);
1741 maybe_rect(dr
, RECT(3,1),
1742 (val
& (FG_TBORDER
| FG_RBORDER
)) == FG_TBORDER ? ch
:
1743 (val
& (FG_TBORDER
| FG_RBORDER
)) == FG_RBORDER ? cl
:
1744 !((FG_TBORDER
|FG_RBORDER
) &~ val
) ? cc
| TYPE_BLCIRC
: cc
);
1745 maybe_rect(dr
, RECT(4,1),
1746 (val
& FG_TRCORNER
) ? ch
: (val
& FG_RBORDER
) ?
-1 :
1747 (val
& FG_TBORDER
) ? ch
: cc
);
1748 maybe_rect(dr
, RECT(0,2),
1749 (val
& FG_LBORDER
) ?
-1 : cc
);
1750 maybe_rect(dr
, RECT(1,2),
1751 (val
& FG_LBORDER
) ? ch
: cc
);
1752 maybe_rect(dr
, RECT(2,2),
1754 maybe_rect(dr
, RECT(3,2),
1755 (val
& FG_RBORDER
) ? cl
: cc
);
1756 maybe_rect(dr
, RECT(4,2),
1757 (val
& FG_RBORDER
) ?
-1 : cc
);
1758 maybe_rect(dr
, RECT(0,3),
1759 (val
& FG_BLCORNER
) ? cl
: (val
& FG_LBORDER
) ?
-1 :
1760 (val
& FG_BBORDER
) ? cl
: cc
);
1761 maybe_rect(dr
, RECT(1,3),
1762 (val
& (FG_BBORDER
| FG_LBORDER
)) == FG_BBORDER ? cl
:
1763 (val
& (FG_BBORDER
| FG_LBORDER
)) == FG_LBORDER ? ch
:
1764 !((FG_BBORDER
|FG_LBORDER
) &~ val
) ? cc
| TYPE_TRCIRC
: cc
);
1765 maybe_rect(dr
, RECT(2,3),
1766 (val
& FG_BBORDER
) ? cl
: cc
);
1767 maybe_rect(dr
, RECT(3,3),
1768 (val
& FG_BRCORNER
) ? cc
: -1);
1769 maybe_rect(dr
, RECT(3,3),
1770 (val
& FG_BRCORNER
) ? cl
| TYPE_BRCIRC
:
1771 !((FG_BBORDER
| FG_RBORDER
) &~ val
) ? cl
| TYPE_TLCIRC
:
1772 (val
& (FG_BBORDER
| FG_RBORDER
)) ? cl
: cc
);
1773 maybe_rect(dr
, RECT(4,3),
1774 (val
& FG_BRCORNER
) ? cl
: (val
& FG_RBORDER
) ?
-1 :
1775 (val
& FG_BBORDER
) ? cl
: cc
);
1776 maybe_rect(dr
, RECT(0,4),
1777 (val
& (FG_BLCORNER
| FG_BBORDER
| FG_LBORDER
)) ?
-1 : cc
);
1778 maybe_rect(dr
, RECT(1,4),
1779 (val
& FG_BLCORNER
) ? ch
: (val
& FG_BBORDER
) ?
-1 :
1780 (val
& FG_LBORDER
) ? ch
: cc
);
1781 maybe_rect(dr
, RECT(2,4),
1782 (val
& FG_BBORDER
) ?
-1 : cc
);
1783 maybe_rect(dr
, RECT(3,4),
1784 (val
& FG_BRCORNER
) ? cl
: (val
& FG_BBORDER
) ?
-1 :
1785 (val
& FG_RBORDER
) ? cl
: cc
);
1786 maybe_rect(dr
, RECT(4,4),
1787 (val
& (FG_BRCORNER
| FG_BBORDER
| FG_RBORDER
)) ?
-1 : cc
);
1793 draw_update(dr
, tx
, ty
, TILESIZE
, TILESIZE
);
1796 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1797 game_state
*state
, int dir
, game_ui
*ui
,
1798 float animtime
, float flashtime
)
1800 int w
= state
->w
, h
= state
->h
, wh
= w
*h
;
1801 unsigned char *board
;
1803 int x
, y
, mainanchor
, mainpos
, dragpos
;
1807 * The initial contents of the window are not guaranteed
1808 * and can vary with front ends. To be on the safe side,
1809 * all games should start by drawing a big
1810 * background-colour rectangle covering the whole window.
1812 draw_rect(dr
, 0, 0, 10*ds
->tilesize
, 10*ds
->tilesize
, COL_BACKGROUND
);
1817 * Construct the board we'll be displaying (which may be
1818 * different from the one in state if ui describes a drag in
1821 board
= snewn(wh
, unsigned char);
1822 memcpy(board
, state
->board
, wh
);
1824 int mpret
= move_piece(w
, h
, state
->board
, board
,
1825 state
->imm
->forcefield
,
1826 ui
->drag_anchor
, ui
->drag_currpos
);
1831 * Build a dsf out of that board, so we can conveniently tell
1832 * which edges are connected and which aren't.
1836 for (y
= 0; y
< h
; y
++)
1837 for (x
= 0; x
< w
; x
++) {
1840 if (ISDIST(board
[i
]))
1841 dsf_merge(dsf
, i
, i
- board
[i
]);
1842 if (board
[i
] == MAINANCHOR
)
1844 if (board
[i
] == WALL
) {
1845 if (x
> 0 && board
[i
-1] == WALL
)
1846 dsf_merge(dsf
, i
, i
-1);
1847 if (y
> 0 && board
[i
-w
] == WALL
)
1848 dsf_merge(dsf
, i
, i
-w
);
1851 assert(mainanchor
>= 0);
1852 mainpos
= dsf_canonify(dsf
, mainanchor
);
1853 dragpos
= ui
->drag_currpos
> 0 ?
dsf_canonify(dsf
, ui
->drag_currpos
) : -1;
1856 * Now we can construct the data about what we want to draw.
1858 for (y
= 0; y
< h
; y
++)
1859 for (x
= 0; x
< w
; x
++) {
1866 * See if this square is part of the target area.
1868 j
= i
+ mainanchor
- (state
->ty
* w
+ state
->tx
);
1869 while (j
>= 0 && j
< wh
&& ISDIST(board
[j
]))
1871 if (j
== mainanchor
)
1876 if (state
->imm
->forcefield
[i
])
1877 val
|= BG_FORCEFIELD
;
1879 if (flashtime
> 0) {
1880 int flashtype
= (int)(flashtime
/ FLASH_INTERVAL
) & 1;
1881 val
|= (flashtype ? FLASH_LOW
: FLASH_HIGH
);
1884 if (board
[i
] != EMPTY
) {
1885 canon
= dsf_canonify(dsf
, i
);
1887 if (board
[i
] == WALL
)
1889 else if (canon
== mainpos
)
1893 if (canon
== dragpos
)
1897 * Now look around to see if other squares
1898 * belonging to the same block are adjacent to us.
1900 if (x
== 0 || canon
!= dsf_canonify(dsf
, i
-1))
1902 if (y
== 0 || canon
!= dsf_canonify(dsf
, i
-w
))
1904 if (x
== w
-1 || canon
!= dsf_canonify(dsf
, i
+1))
1906 if (y
== h
-1 || canon
!= dsf_canonify(dsf
, i
+w
))
1908 if (!(val
& (FG_TBORDER
| FG_LBORDER
)) &&
1909 canon
!= dsf_canonify(dsf
, i
-1-w
))
1911 if (!(val
& (FG_TBORDER
| FG_RBORDER
)) &&
1912 canon
!= dsf_canonify(dsf
, i
+1-w
))
1914 if (!(val
& (FG_BBORDER
| FG_LBORDER
)) &&
1915 canon
!= dsf_canonify(dsf
, i
-1+w
))
1917 if (!(val
& (FG_BBORDER
| FG_RBORDER
)) &&
1918 canon
!= dsf_canonify(dsf
, i
+1+w
))
1922 if (val
!= ds
->grid
[i
]) {
1923 draw_tile(dr
, ds
, x
, y
, val
);
1929 * Update the status bar.
1932 char statusbuf
[256];
1935 * FIXME: do something about auto-solve?
1937 sprintf(statusbuf
, "%sMoves: %d",
1938 (state
->completed
>= 0 ?
"COMPLETED! " : ""),
1939 (state
->completed
>= 0 ? state
->completed
: state
->movecount
));
1940 if (state
->minmoves
>= 0)
1941 sprintf(statusbuf
+strlen(statusbuf
), " (min %d)",
1944 status_bar(dr
, statusbuf
);
1951 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1952 int dir
, game_ui
*ui
)
1957 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1958 int dir
, game_ui
*ui
)
1960 if (oldstate
->completed
< 0 && newstate
->completed
>= 0)
1966 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1971 static void game_print_size(game_params
*params
, float *x
, float *y
)
1975 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1980 #define thegame nullgame
1983 const struct game thegame
= {
1984 "Slide", NULL
, NULL
,
1991 TRUE
, game_configure
, custom_params
,
1998 FALSE
, solve_game
, /* FIXME */
1999 TRUE
, game_text_format
,
2007 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
2010 game_free_drawstate
,
2014 FALSE
, FALSE
, game_print_size
, game_print
,
2015 TRUE
, /* wants_statusbar */
2016 FALSE
, game_timing_state
,
2020 #ifdef STANDALONE_SOLVER
2024 int main(int argc
, char **argv
)
2028 char *id
= NULL
, *desc
, *err
;
2030 int ret
, really_verbose
= FALSE
;
2033 while (--argc
> 0) {
2035 if (!strcmp(p
, "-v")) {
2036 really_verbose
= TRUE
;
2037 } else if (!strcmp(p
, "-c")) {
2039 } else if (*p
== '-') {
2040 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
2048 fprintf(stderr
, "usage: %s [-c | -v] <game_id>\n", argv
[0]);
2052 desc
= strchr(id
, ':');
2054 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
2059 p
= default_params();
2060 decode_params(p
, id
);
2061 err
= validate_desc(p
, desc
);
2063 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
2066 s
= new_game(NULL
, p
, desc
);
2068 ret
= solve_board(s
->w
, s
->h
, s
->board
, s
->imm
->forcefield
,
2069 s
->tx
, s
->ty
, -1, &moves
);
2071 printf("No solution found\n");
2075 printf("%d moves required\n", ret
);
2080 char *text
= board_text_format(s
->w
, s
->h
, s
->board
,
2081 s
->imm
->forcefield
);
2084 printf("position %d:\n%s", index
, text
);
2090 moveret
= move_piece(s
->w
, s
->h
, s
->board
,
2091 s2
->board
, s
->imm
->forcefield
,
2092 moves
[index
*2], moves
[index
*2+1]);