2 * galaxies.c: implementation of 'Tentai Show' from Nikoli,
3 * also sometimes called 'Spiral Galaxies'.
7 * Grid is stored as size (2n-1), holding edges as well as spaces
8 * (and thus vertices too, at edge intersections).
10 * Any dot will thus be positioned at one of our grid points,
11 * which saves any faffing with half-of-a-square stuff.
13 * Edges have on/off state; obviously the actual edges of the
14 * board are fixed to on, and everything else starts as off.
18 * Think about how to display remote groups of tiles?
24 * Nikoli's example [web site has wrong highlighting]
25 * (at http://www.nikoli.co.jp/en/puzzles/astronomical_show/):
28 * The 'spiral galaxies puzzles are NP-complete' paper
29 * (at http://www.stetson.edu/~efriedma/papers/spiral.pdf):
30 * 7x7:chpgdqqqoezdddki
32 * Puzzle competition pdf examples
33 * (at http://www.puzzleratings.org/Yurekli2006puz.pdf):
34 * 6x6:EDbaMucCohbrecEi
35 * 10x10:beFbufEEzowDlxldibMHezBQzCdcFzjlci
36 * 13x13:dCemIHFFkJajjgDfdbdBzdzEgjccoPOcztHjBczLDjczqktJjmpreivvNcggFi
52 int solver_show_working
;
53 #define solvep(x) do { if (solver_show_working) { printf x; } } while(0)
71 A(RECURSIVE,Recursive,r)
73 #define ENUM(upper,title,lower) DIFF_ ## upper,
74 #define TITLE(upper,title,lower) #title,
75 #define ENCODE(upper,title,lower) #lower
76 #define CONFIG(upper,title,lower) ":" #title
78 DIFF_IMPOSSIBLE
, DIFF_AMBIGUOUS
, DIFF_UNFINISHED
, DIFF_MAX
};
79 static char const *const galaxies_diffnames
[] = {
80 DIFFLIST(TITLE
) "Impossible", "Ambiguous", "Unfinished" };
81 static char const galaxies_diffchars
[] = DIFFLIST(ENCODE
);
82 #define DIFFCONFIG DIFFLIST(CONFIG)
85 /* X and Y is the area of the board as seen by
86 * the user, not the (2n+1) area the game uses. */
90 enum { s_tile
, s_edge
, s_vertex
};
92 #define F_DOT 1 /* there's a dot here */
93 #define F_EDGE_SET 2 /* the edge is set */
94 #define F_TILE_ASSOC 4 /* this tile is associated with a dot. */
95 #define F_DOT_BLACK 8 /* (ui only) dot is black. */
96 #define F_MARK 16 /* scratch flag */
97 #define F_REACHABLE 32
99 #define F_MULTIPLE 128
100 #define F_DOT_HOLD 256
103 typedef struct space
{
104 int x
, y
; /* its position */
107 int dotx
, doty
; /* if flags & F_TILE_ASSOC */
108 int nassoc
; /* if flags & F_DOT */
111 #define INGRID(s,x,y) ((x) >= 0 && (y) >= 0 && \
112 (x) < (state)->sx && (y) < (state)->sy)
113 #define INUI(s,x,y) ((x) > 0 && (y) > 0 && \
114 (x) < ((state)->sx-1) && (y) < ((state)->sy-1))
116 #define GRID(s,g,x,y) ((s)->g[((y)*(s)->sx)+(x)])
117 #define SPACE(s,x,y) GRID(s,grid,x,y)
120 int w
, h
; /* size from params */
121 int sx
, sy
; /* allocated size, (2x-1)*(2y-1) */
123 int completed
, used_solve
;
127 midend
*me
; /* to call supersede_game_desc */
128 int cdiff
; /* difficulty of current puzzle (for status bar),
132 /* ----------------------------------------------------------
133 * Game parameters and presets
136 /* make up some sensible default sizes */
138 #define DEFAULT_PRESET 1
140 static const game_params galaxies_presets
[] = {
143 { 7, 7, DIFF_RECURSIVE
},
144 { 10, 10, DIFF_EASY
},
145 { 10, 10, DIFF_HARD
},
146 { 15, 15, DIFF_EASY
},
147 { 15, 15, DIFF_HARD
},
150 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
155 if (i
< 0 || i
>= lenof(galaxies_presets
))
158 ret
= snew(game_params
);
159 *ret
= galaxies_presets
[i
]; /* structure copy */
161 sprintf(buf
, "%dx%d %s", ret
->w
, ret
->h
,
162 galaxies_diffnames
[ret
->diff
]);
164 if (name
) *name
= dupstr(buf
);
169 static game_params
*default_params(void)
172 game_fetch_preset(DEFAULT_PRESET
, NULL
, &ret
);
176 static void free_params(game_params
*params
)
181 static game_params
*dup_params(game_params
*params
)
183 game_params
*ret
= snew(game_params
);
184 *ret
= *params
; /* structure copy */
188 static void decode_params(game_params
*params
, char const *string
)
190 params
->h
= params
->w
= atoi(string
);
191 params
->diff
= DIFF_EASY
;
192 while (*string
&& isdigit((unsigned char)*string
)) string
++;
193 if (*string
== 'x') {
195 params
->h
= atoi(string
);
196 while (*string
&& isdigit((unsigned char)*string
)) string
++;
198 if (*string
== 'd') {
201 for (i
= 0; i
<= DIFF_RECURSIVE
; i
++)
202 if (*string
== galaxies_diffchars
[i
])
204 if (*string
) string
++;
208 static char *encode_params(game_params
*params
, int full
)
211 sprintf(str
, "%dx%d", params
->w
, params
->h
);
213 sprintf(str
+ strlen(str
), "d%c", galaxies_diffchars
[params
->diff
]);
217 static config_item
*game_configure(game_params
*params
)
222 ret
= snewn(4, config_item
);
224 ret
[0].name
= "Width";
225 ret
[0].type
= C_STRING
;
226 sprintf(buf
, "%d", params
->w
);
227 ret
[0].sval
= dupstr(buf
);
230 ret
[1].name
= "Height";
231 ret
[1].type
= C_STRING
;
232 sprintf(buf
, "%d", params
->h
);
233 ret
[1].sval
= dupstr(buf
);
236 ret
[2].name
= "Difficulty";
237 ret
[2].type
= C_CHOICES
;
238 ret
[2].sval
= DIFFCONFIG
;
239 ret
[2].ival
= params
->diff
;
249 static game_params
*custom_params(config_item
*cfg
)
251 game_params
*ret
= snew(game_params
);
253 ret
->w
= atoi(cfg
[0].sval
);
254 ret
->h
= atoi(cfg
[1].sval
);
255 ret
->diff
= cfg
[2].ival
;
260 static char *validate_params(game_params
*params
, int full
)
262 if (params
->w
< 3 || params
->h
< 3)
263 return "Width and height must both be at least 3";
265 * This shouldn't be able to happen at all, since decode_params
266 * and custom_params will never generate anything that isn't
269 assert(params
->diff
<= DIFF_RECURSIVE
);
274 /* ----------------------------------------------------------
275 * Game utility functions.
278 static void add_dot(space
*space
) {
279 assert(!(space
->flags
& F_DOT
));
280 space
->flags
|= F_DOT
;
284 static void remove_dot(space
*space
) {
285 assert(space
->flags
& F_DOT
);
286 space
->flags
&= ~F_DOT
;
289 static void remove_assoc(game_state
*state
, space
*tile
) {
290 if (tile
->flags
& F_TILE_ASSOC
) {
291 SPACE(state
, tile
->dotx
, tile
->doty
).nassoc
--;
292 tile
->flags
&= ~F_TILE_ASSOC
;
298 static void add_assoc(game_state
*state
, space
*tile
, space
*dot
) {
299 remove_assoc(state
, tile
);
301 tile
->flags
|= F_TILE_ASSOC
;
305 debug(("add_assoc sp %d %d --> dot %d,%d, new nassoc %d.\n",
306 tile
->x
, tile
->y
, dot
->x
, dot
->y
, dot
->nassoc
));
309 static struct space
*sp2dot(game_state
*state
, int x
, int y
)
311 struct space
*sp
= &SPACE(state
, x
, y
);
312 if (!(sp
->flags
& F_TILE_ASSOC
)) return NULL
;
313 return &SPACE(state
, sp
->dotx
, sp
->doty
);
316 #define IS_VERTICAL_EDGE(x) ((x % 2) == 0)
318 static char *game_text_format(game_state
*state
)
320 int maxlen
= (state
->sx
+1)*state
->sy
, x
, y
;
324 ret
= snewn(maxlen
+1, char);
327 for (y
= 0; y
< state
->sy
; y
++) {
328 for (x
= 0; x
< state
->sx
; x
++) {
329 sp
= &SPACE(state
, x
, y
);
330 if (sp
->flags
& F_DOT
)
332 else if (sp
->flags
& (F_REACHABLE
|F_MULTIPLE
|F_MARK
))
333 *p
++ = (sp
->flags
& F_MULTIPLE
) ?
'M' :
334 (sp
->flags
& F_REACHABLE
) ?
'R' : 'X';
338 if (sp
->flags
& F_TILE_ASSOC
) {
339 space
*dot
= sp2dot(state
, sp
->x
, sp
->y
);
340 if (dot
->flags
& F_DOT
)
341 *p
++ = (dot
->flags
& F_DOT_BLACK
) ?
'B' : 'W';
343 *p
++ = '?'; /* association with not-a-dot. */
353 if (sp
->flags
& F_EDGE_SET
)
354 *p
++ = (IS_VERTICAL_EDGE(x
)) ?
'|' : '-';
360 assert(!"shouldn't get here!");
367 assert(p
- ret
== maxlen
);
373 static void dbg_state(game_state
*state
)
376 char *temp
= game_text_format(state
);
377 debug(("%s\n", temp
));
382 /* Space-enumeration callbacks should all return 1 for 'progress made',
383 * -1 for 'impossible', and 0 otherwise. */
384 typedef int (*space_cb
)(game_state
*state
, space
*sp
, void *ctx
);
386 #define IMPOSSIBLE_QUITS 1
388 static int foreach_sub(game_state
*state
, space_cb cb
, unsigned int f
,
389 void *ctx
, int startx
, int starty
)
391 int x
, y
, progress
= 0, impossible
= 0, ret
;
394 for (y
= starty
; y
< state
->sy
; y
+= 2) {
395 sp
= &SPACE(state
, startx
, y
);
396 for (x
= startx
; x
< state
->sx
; x
+= 2) {
397 ret
= cb(state
, sp
, ctx
);
399 if (f
& IMPOSSIBLE_QUITS
) return -1;
401 } else if (ret
== 1) {
407 return impossible ?
-1 : progress
;
410 static int foreach_tile(game_state
*state
, space_cb cb
, unsigned int f
,
413 return foreach_sub(state
, cb
, f
, ctx
, 1, 1);
416 static int foreach_edge(game_state
*state
, space_cb cb
, unsigned int f
,
421 ret1
= foreach_sub(state
, cb
, f
, ctx
, 0, 1);
422 ret2
= foreach_sub(state
, cb
, f
, ctx
, 1, 0);
424 if (ret1
== -1 || ret2
== -1) return -1;
425 return (ret1
|| ret2
) ?
1 : 0;
429 static int foreach_vertex(game_state
*state
, space_cb cb
, unsigned int f
,
432 return foreach_sub(state
, cb
, f
, ctx
, 0, 0);
437 static int is_same_assoc(game_state
*state
,
438 int x1
, int y1
, int x2
, int y2
)
440 struct space
*s1
, *s2
;
442 if (!INGRID(state
, x1
, y1
) || !INGRID(state
, x2
, y2
))
445 s1
= &SPACE(state
, x1
, y1
);
446 s2
= &SPACE(state
, x2
, y2
);
447 assert(s1
->type
== s_tile
&& s2
->type
== s_tile
);
448 if ((s1
->flags
& F_TILE_ASSOC
) && (s2
->flags
& F_TILE_ASSOC
) &&
449 s1
->dotx
== s2
->dotx
&& s1
->doty
== s2
->doty
)
451 return 0; /* 0 if not same or not both associated. */
456 static int edges_into_vertex(game_state
*state
,
459 int dx
, dy
, nx
, ny
, count
= 0;
461 assert(SPACE(state
, x
, y
).type
== s_vertex
);
462 for (dx
= -1; dx
<= 1; dx
++) {
463 for (dy
= -1; dy
<= 1; dy
++) {
464 if (dx
!= 0 && dy
!= 0) continue;
465 if (dx
== 0 && dy
== 0) continue;
467 nx
= x
+dx
; ny
= y
+dy
;
468 if (!INGRID(state
, nx
, ny
)) continue;
469 assert(SPACE(state
, nx
, ny
).type
== s_edge
);
470 if (SPACE(state
, nx
, ny
).flags
& F_EDGE_SET
)
478 static struct space
*space_opposite_dot(struct game_state
*state
,
479 struct space
*sp
, struct space
*dot
)
488 if (!INGRID(state
, tx
, ty
)) return NULL
;
490 sp2
= &SPACE(state
, tx
, ty
);
491 assert(sp2
->type
== sp
->type
);
495 static struct space
*tile_opposite(struct game_state
*state
, struct space
*sp
)
499 assert(sp
->flags
& F_TILE_ASSOC
);
500 dot
= &SPACE(state
, sp
->dotx
, sp
->doty
);
501 return space_opposite_dot(state
, sp
, dot
);
504 static int dotfortile(game_state
*state
, space
*tile
, space
*dot
)
506 space
*tile_opp
= space_opposite_dot(state
, tile
, dot
);
508 if (!tile_opp
) return 0; /* opposite would be off grid */
509 if (tile_opp
->flags
& F_TILE_ASSOC
&&
510 (tile_opp
->dotx
!= dot
->x
|| tile_opp
->doty
!= dot
->y
))
511 return 0; /* opposite already associated with diff. dot */
515 static void adjacencies(struct game_state
*state
, struct space
*sp
,
516 struct space
**a1s
, struct space
**a2s
)
518 int dxs
[4] = {-1, 1, 0, 0}, dys
[4] = {0, 0, -1, 1};
521 /* this function needs optimising. */
523 for (n
= 0; n
< 4; n
++) {
527 if (INGRID(state
, x
, y
)) {
528 a1s
[n
] = &SPACE(state
, x
, y
);
530 x
+= dxs
[n
]; y
+= dys
[n
];
532 if (INGRID(state
, x
, y
))
533 a2s
[n
] = &SPACE(state
, x
, y
);
537 a1s
[n
] = a2s
[n
] = NULL
;
542 static int outline_tile_fordot(game_state
*state
, space
*tile
, int mark
)
544 struct space
*tadj
[4], *eadj
[4];
545 int i
, didsth
= 0, edge
, same
;
547 assert(tile
->type
== s_tile
);
548 adjacencies(state
, tile
, eadj
, tadj
);
549 for (i
= 0; i
< 4; i
++) {
550 if (!eadj
[i
]) continue;
552 edge
= (eadj
[i
]->flags
& F_EDGE_SET
) ?
1 : 0;
554 if (!(tile
->flags
& F_TILE_ASSOC
))
555 same
= (tadj
[i
]->flags
& F_TILE_ASSOC
) ?
0 : 1;
557 same
= ((tadj
[i
]->flags
& F_TILE_ASSOC
) &&
558 tile
->dotx
== tadj
[i
]->dotx
&&
559 tile
->doty
== tadj
[i
]->doty
) ?
1 : 0;
563 if (!edge
&& !same
) {
564 if (mark
) eadj
[i
]->flags
|= F_EDGE_SET
;
566 } else if (edge
&& same
) {
567 if (mark
) eadj
[i
]->flags
&= ~F_EDGE_SET
;
574 static void tiles_from_edge(struct game_state
*state
,
575 struct space
*sp
, struct space
**ts
)
579 if (IS_VERTICAL_EDGE(sp
->x
)) {
580 xs
[0] = sp
->x
-1; ys
[0] = sp
->y
;
581 xs
[1] = sp
->x
+1; ys
[1] = sp
->y
;
583 xs
[0] = sp
->x
; ys
[0] = sp
->y
-1;
584 xs
[1] = sp
->x
; ys
[1] = sp
->y
+1;
586 ts
[0] = INGRID(state
, xs
[0], ys
[0]) ?
&SPACE(state
, xs
[0], ys
[0]) : NULL
;
587 ts
[1] = INGRID(state
, xs
[1], ys
[1]) ?
&SPACE(state
, xs
[1], ys
[1]) : NULL
;
590 /* Check all tiles are associated with something, and all shapes
591 * are the correct symmetry (i.e. all tiles have a matching tile
592 * the opposite direction from the dot) */
593 static int cccb_assoc(game_state
*state
, space
*tile
, void *unused
)
595 assert(tile
->type
== s_tile
);
597 if (!(tile
->flags
& F_TILE_ASSOC
)) return -1;
606 static int dgs_cb_check(game_state
*state
, space
*tile
, void *vctx
)
608 struct dgs_ctx
*ctx
= (struct dgs_ctx
*)vctx
;
611 if (!(tile
->flags
& F_TILE_ASSOC
)) return 0;
612 if (tile
->dotx
!= ctx
->dot
->x
||
613 tile
->doty
!= ctx
->dot
->y
) return 0;
617 /* Check this tile has an opposite associated with same dot. */
618 opp
= tile_opposite(state
, tile
);
619 if (!opp
|| !(opp
->flags
& F_TILE_ASSOC
)) return -1;
620 if (opp
->dotx
!= tile
->dotx
|| opp
->doty
!= tile
->doty
) return -1;
622 /* Check its edges are correct */
623 if (outline_tile_fordot(state
, tile
, 0) == 1)
624 return -1; /* there was some fixing required, we're wrong. */
629 static int dot_good_shape(game_state
*state
, space
*dot
, int mark
)
636 if (mark
) dot
->flags
&= ~F_GOOD
;
638 if (foreach_tile(state
, dgs_cb_check
, 0, &ctx
) == -1)
640 if (ctx
.ndots
== 0) return 0; /* no dots assoc. with tile. */
643 debug(("marking dot %d,%d good tile.\n", dot
->x
, dot
->y
));
644 dot
->flags
|= F_GOOD
;
649 static int check_complete(game_state
*state
, int mark_errors
)
653 /* Are all tiles associated? */
654 if (foreach_tile(state
, cccb_assoc
, 0, NULL
) == -1)
657 /* Check all dots are associated, and their tiles are well-formed. */
658 for (i
= 0; i
< state
->ndots
; i
++) {
659 if (!dot_good_shape(state
, state
->dots
[i
], mark_errors
))
663 /*if (complete == 1) printf("Complete!\n");*/
667 /* Returns a move string for use by 'solve'; if you don't want the
668 * initial 'S;' use ret[2]. */
669 static char *diff_game(game_state
*src
, game_state
*dest
, int issolve
)
671 int movelen
= 0, movesize
= 256, x
, y
, len
;
672 char *move
= snewn(movesize
, char), buf
[80], *sep
= "";
673 char achar
= issolve ?
'a' : 'A';
676 assert(src
->sx
== dest
->sx
&& src
->sy
== dest
->sy
);
679 move
[movelen
++] = 'S';
682 move
[movelen
] = '\0';
683 for (x
= 0; x
< src
->sx
; x
++) {
684 for (y
= 0; y
< src
->sy
; y
++) {
685 sps
= &SPACE(src
, x
, y
);
686 spd
= &SPACE(dest
, x
, y
);
688 assert(sps
->type
== spd
->type
);
691 if (sps
->type
== s_tile
) {
692 if ((sps
->flags
& F_TILE_ASSOC
) &&
693 (spd
->flags
& F_TILE_ASSOC
)) {
694 if (sps
->dotx
!= spd
->dotx
||
695 sps
->doty
!= spd
->doty
)
696 /* Both associated; change association, if different */
697 len
= sprintf(buf
, "%s%c%d,%d,%d,%d", sep
,
698 (int)achar
, x
, y
, spd
->dotx
, spd
->doty
);
699 } else if (sps
->flags
& F_TILE_ASSOC
)
700 /* Only src associated; remove. */
701 len
= sprintf(buf
, "%sU%d,%d", sep
, x
, y
);
702 else if (spd
->flags
& F_TILE_ASSOC
)
703 /* Only dest associated; add. */
704 len
= sprintf(buf
, "%s%c%d,%d,%d,%d", sep
,
705 (int)achar
, x
, y
, spd
->dotx
, spd
->doty
);
706 } else if (sps
->type
== s_edge
) {
707 if ((sps
->flags
& F_EDGE_SET
) != (spd
->flags
& F_EDGE_SET
))
708 /* edge flags are different; flip them. */
709 len
= sprintf(buf
, "%sE%d,%d", sep
, x
, y
);
712 if (movelen
+ len
>= movesize
) {
713 movesize
= movelen
+ len
+ 256;
714 move
= sresize(move
, movesize
, char);
716 strcpy(move
+ movelen
, buf
);
722 debug(("diff_game src then dest:\n"));
725 debug(("diff string %s\n", move
));
729 /* Returns 1 if a dot here would not be too close to any other dots
730 * (and would avoid other game furniture). */
731 static int dot_is_possible(game_state
*state
, space
*sp
, int allow_assoc
)
733 int bx
= 0, by
= 0, dx
, dy
;
740 if (IS_VERTICAL_EDGE(sp
->x
)) {
750 for (dx
= -bx
; dx
<= bx
; dx
++) {
751 for (dy
= -by
; dy
<= by
; dy
++) {
752 if (!INGRID(state
, sp
->x
+dx
, sp
->y
+dy
)) continue;
754 adj
= &SPACE(state
, sp
->x
+dx
, sp
->y
+dy
);
756 if (!allow_assoc
&& (adj
->flags
& F_TILE_ASSOC
))
759 if (dx
!= 0 || dy
!= 0) {
760 /* Other than our own square, no dots nearby. */
761 if (adj
->flags
& (F_DOT
))
765 /* We don't want edges within our rectangle
766 * (but don't care about edges on the edge) */
767 if (abs(dx
) < bx
&& abs(dy
) < by
&&
768 adj
->flags
& F_EDGE_SET
)
775 /* ----------------------------------------------------------
776 * Game generation, structure creation, and descriptions.
779 static game_state
*blank_game(int w
, int h
)
781 game_state
*state
= snew(game_state
);
789 state
->grid
= snewn(state
->sx
* state
->sy
, struct space
);
790 state
->completed
= state
->used_solve
= 0;
792 for (x
= 0; x
< state
->sx
; x
++) {
793 for (y
= 0; y
< state
->sy
; y
++) {
794 struct space
*sp
= &SPACE(state
, x
, y
);
795 memset(sp
, 0, sizeof(struct space
));
798 if ((x
% 2) == 0 && (y
% 2) == 0)
800 else if ((x
% 2) == 0 || (y
% 2) == 0) {
802 if (x
== 0 || y
== 0 || x
== state
->sx
-1 || y
== state
->sy
-1)
803 sp
->flags
|= F_EDGE_SET
;
812 state
->me
= NULL
; /* filled in by new_game. */
818 static void game_update_dots(game_state
*state
)
820 int i
, n
, sz
= state
->sx
* state
->sy
;
822 if (state
->dots
) sfree(state
->dots
);
825 for (i
= 0; i
< sz
; i
++) {
826 if (state
->grid
[i
].flags
& F_DOT
) state
->ndots
++;
828 state
->dots
= snewn(state
->ndots
, space
*);
830 for (i
= 0; i
< sz
; i
++) {
831 if (state
->grid
[i
].flags
& F_DOT
)
832 state
->dots
[n
++] = &state
->grid
[i
];
836 static void clear_game(game_state
*state
, int cleardots
)
840 /* don't erase edge flags around outline! */
841 for (x
= 1; x
< state
->sx
-1; x
++) {
842 for (y
= 1; y
< state
->sy
-1; y
++) {
844 SPACE(state
, x
, y
).flags
= 0;
846 SPACE(state
, x
, y
).flags
&= (F_DOT
|F_DOT_BLACK
);
849 if (cleardots
) game_update_dots(state
);
852 static game_state
*dup_game(game_state
*state
)
854 game_state
*ret
= blank_game(state
->w
, state
->h
);
856 ret
->completed
= state
->completed
;
857 ret
->used_solve
= state
->used_solve
;
859 memcpy(ret
->grid
, state
->grid
,
860 ret
->sx
*ret
->sy
*sizeof(struct space
));
862 game_update_dots(ret
);
865 ret
->cdiff
= state
->cdiff
;
870 static void free_game(game_state
*state
)
872 if (state
->dots
) sfree(state
->dots
);
877 /* Game description is a sequence of letters representing the number
878 * of spaces (a = 0, y = 24) before the next dot; a-y for a white dot,
879 * and A-Y for a black dot. 'z' is 25 spaces (and no dot).
881 * I know it's a bitch to generate by hand, so we provide
885 static char *encode_game(game_state
*state
)
891 area
= (state
->sx
-2) * (state
->sy
-2);
893 desc
= snewn(area
, char);
896 for (y
= 1; y
< state
->sy
-1; y
++) {
897 for (x
= 1; x
< state
->sx
-1; x
++) {
898 f
= SPACE(state
, x
, y
).flags
;
900 /* a/A is 0 spaces between, b/B is 1 space, ...
901 * y/Y is 24 spaces, za/zA is 25 spaces, ...
902 * It's easier to count from 0 because we then
903 * don't have to special-case the top left-hand corner
904 * (which could be a dot with 0 spaces before it). */
912 *p
++ = ((f
& F_DOT_BLACK
) ?
'A' : 'a') + run
;
917 assert(p
- desc
< area
);
919 desc
= sresize(desc
, p
- desc
, char);
926 space
*olddot
, *newdot
;
929 enum { MD_CHECK
, MD_MOVE
};
931 static int movedot_cb(game_state
*state
, space
*tile
, void *vctx
)
933 struct movedot
*md
= (struct movedot
*)vctx
;
934 space
*newopp
= NULL
;
936 assert(tile
->type
== s_tile
);
937 assert(md
->olddot
&& md
->newdot
);
939 if (!(tile
->flags
& F_TILE_ASSOC
)) return 0;
940 if (tile
->dotx
!= md
->olddot
->x
|| tile
->doty
!= md
->olddot
->y
)
943 newopp
= space_opposite_dot(state
, tile
, md
->newdot
);
947 /* If the tile is associated with the old dot, check its
948 * opposite wrt the _new_ dot is empty or same assoc. */
949 if (!newopp
) return -1; /* no new opposite */
950 if (newopp
->flags
& F_TILE_ASSOC
) {
951 if (newopp
->dotx
!= md
->olddot
->x
||
952 newopp
->doty
!= md
->olddot
->y
)
953 return -1; /* associated, but wrong dot. */
958 /* Move dot associations: anything that was associated
959 * with the old dot, and its opposite wrt the new dot,
960 * become associated with the new dot. */
962 debug(("Associating %d,%d and %d,%d with new dot %d,%d.\n",
963 tile
->x
, tile
->y
, newopp
->x
, newopp
->y
,
964 md
->newdot
->x
, md
->newdot
->y
));
965 add_assoc(state
, tile
, md
->newdot
);
966 add_assoc(state
, newopp
, md
->newdot
);
967 return 1; /* we did something! */
972 /* For the given dot, first see if we could expand it into all the given
973 * extra spaces (by checking for empty spaces on the far side), and then
974 * see if we can move the dot to shift the CoG to include the new spaces.
976 static int dot_expand_or_move(game_state
*state
, space
*dot
,
977 space
**toadd
, int nadd
)
980 int i
, ret
, nnew
, cx
, cy
;
983 debug(("dot_expand_or_move: %d tiles for dot %d,%d\n",
984 nadd
, dot
->x
, dot
->y
));
985 for (i
= 0; i
< nadd
; i
++)
986 debug(("dot_expand_or_move: dot %d,%d\n",
987 toadd
[i
]->x
, toadd
[i
]->y
));
988 assert(dot
->flags
& F_DOT
);
990 /* First off, could we just expand the current dot's tile to cover
991 * the space(s) passed in and their opposites? */
992 for (i
= 0; i
< nadd
; i
++) {
993 tileopp
= space_opposite_dot(state
, toadd
[i
], dot
);
994 if (!tileopp
) goto noexpand
;
995 if (tileopp
->flags
& F_TILE_ASSOC
) goto noexpand
;
997 /* OK, all spaces have valid empty opposites: associate spaces and
998 * opposites with our dot. */
999 for (i
= 0; i
< nadd
; i
++) {
1000 tileopp
= space_opposite_dot(state
, toadd
[i
], dot
);
1001 add_assoc(state
, toadd
[i
], dot
);
1002 add_assoc(state
, tileopp
, dot
);
1003 debug(("Added associations %d,%d and %d,%d --> %d,%d\n",
1004 toadd
[i
]->x
, toadd
[i
]->y
,
1005 tileopp
->x
, tileopp
->y
,
1012 /* Otherwise, try to move dot so as to encompass given spaces: */
1013 /* first, alculate the 'centre of gravity' of the new dot. */
1014 nnew
= dot
->nassoc
+ nadd
; /* number of tiles assoc. with new dot. */
1015 cx
= dot
->x
* dot
->nassoc
;
1016 cy
= dot
->y
* dot
->nassoc
;
1017 for (i
= 0; i
< nadd
; i
++) {
1021 /* If the CoG isn't a whole number, it's not possible. */
1022 if ((cx
% nnew
) != 0 || (cy
% nnew
) != 0) {
1023 debug(("Unable to move dot %d,%d, CoG not whole number.\n",
1027 cx
/= nnew
; cy
/= nnew
;
1029 /* Check whether all spaces in the old tile would have a good
1030 * opposite wrt the new dot. */
1032 md
.newdot
= &SPACE(state
, cx
, cy
);
1034 ret
= foreach_tile(state
, movedot_cb
, IMPOSSIBLE_QUITS
, &md
);
1036 debug(("Unable to move dot %d,%d, new dot not symmetrical.\n",
1040 /* Also check whether all spaces we're adding would have a good
1041 * opposite wrt the new dot. */
1042 for (i
= 0; i
< nadd
; i
++) {
1043 tileopp
= space_opposite_dot(state
, toadd
[i
], md
.newdot
);
1044 if (tileopp
&& (tileopp
->flags
& F_TILE_ASSOC
) &&
1045 (tileopp
->dotx
!= dot
->x
|| tileopp
->doty
!= dot
->y
)) {
1049 debug(("Unable to move dot %d,%d, new dot not symmetrical.\n",
1055 /* If we've got here, we're ok. First, associate all of 'toadd'
1056 * with the _old_ dot (so they'll get fixed up, with their opposites,
1057 * in the next step). */
1058 for (i
= 0; i
< nadd
; i
++) {
1059 debug(("Associating to-add %d,%d with old dot %d,%d.\n",
1060 toadd
[i
]->x
, toadd
[i
]->y
, dot
->x
, dot
->y
));
1061 add_assoc(state
, toadd
[i
], dot
);
1064 /* Finally, move the dot and fix up all the old associations. */
1065 debug(("Moving dot at %d,%d to %d,%d\n",
1066 dot
->x
, dot
->y
, md
.newdot
->x
, md
.newdot
->y
));
1071 ret
= foreach_tile(state
, movedot_cb
, 0, &md
);
1078 /* Hard-code to a max. of 2x2 squares, for speed (less malloc) */
1080 #define MAX_OUTSIDE 8
1082 #define MAX_TILE_PERC 20
1084 static int generate_try_block(game_state
*state
, random_state
*rs
,
1085 int x1
, int y1
, int x2
, int y2
)
1087 int x
, y
, nadd
= 0, nout
= 0, i
, maxsz
;
1088 space
*sp
, *toadd
[MAX_TOADD
], *outside
[MAX_OUTSIDE
], *dot
;
1090 if (!INGRID(state
, x1
, y1
) || !INGRID(state
, x2
, y2
)) return 0;
1092 /* We limit the maximum size of tiles to be ~2*sqrt(area); so,
1093 * a 5x5 grid shouldn't have anything >10 tiles, a 20x20 grid
1094 * nothing >40 tiles. */
1095 maxsz
= (int)sqrt((double)(state
->w
* state
->h
)) * 2;
1096 debug(("generate_try_block, maxsz %d\n", maxsz
));
1098 /* Make a static list of the spaces; if any space is already
1099 * associated then quit immediately. */
1100 for (x
= x1
; x
<= x2
; x
+= 2) {
1101 for (y
= y1
; y
<= y2
; y
+= 2) {
1102 assert(nadd
< MAX_TOADD
);
1103 sp
= &SPACE(state
, x
, y
);
1104 assert(sp
->type
== s_tile
);
1105 if (sp
->flags
& F_TILE_ASSOC
) return 0;
1110 /* Make a list of the spaces outside of our block, and shuffle it. */
1111 #define OUTSIDE(x, y) do { \
1112 if (INGRID(state, (x), (y))) { \
1113 assert(nout < MAX_OUTSIDE); \
1114 outside[nout++] = &SPACE(state, (x), (y)); \
1117 for (x
= x1
; x
<= x2
; x
+= 2) {
1121 for (y
= y1
; y
<= y2
; y
+= 2) {
1125 shuffle(outside
, nout
, sizeof(space
*), rs
);
1127 for (i
= 0; i
< nout
; i
++) {
1128 if (!(outside
[i
]->flags
& F_TILE_ASSOC
)) continue;
1129 dot
= &SPACE(state
, outside
[i
]->dotx
, outside
[i
]->doty
);
1130 if (dot
->nassoc
>= maxsz
) {
1131 debug(("Not adding to dot %d,%d, large enough (%d) already.\n",
1132 dot
->x
, dot
->y
, dot
->nassoc
));
1135 if (dot_expand_or_move(state
, dot
, toadd
, nadd
)) return 1;
1140 #ifdef STANDALONE_SOLVER
1142 #define MAXTRIES maxtries
1147 static int solver_obvious_dot(game_state
*state
,space
*dot
);
1151 static void generate_pass(game_state
*state
, random_state
*rs
, int *scratch
,
1152 int perc
, unsigned int flags
)
1154 int sz
= state
->sx
*state
->sy
, nspc
, i
, ret
;
1156 shuffle(scratch
, sz
, sizeof(int), rs
);
1158 /* This bug took me a, er, little while to track down. On PalmOS,
1159 * which has 16-bit signed ints, puzzles over about 9x9 started
1160 * failing to generate because the nspc calculation would start
1161 * to overflow, causing the dots not to be filled in properly. */
1162 nspc
= (int)(((long)perc
* (long)sz
) / 100L);
1163 debug(("generate_pass: %d%% (%d of %dx%d) squares, flags 0x%x\n",
1164 perc
, nspc
, state
->sx
, state
->sy
, flags
));
1166 for (i
= 0; i
< nspc
; i
++) {
1167 space
*sp
= &state
->grid
[scratch
[i
]];
1168 int x1
= sp
->x
, y1
= sp
->y
, x2
= sp
->x
, y2
= sp
->y
;
1170 if (sp
->type
== s_edge
) {
1171 if (IS_VERTICAL_EDGE(sp
->x
)) {
1177 if (sp
->type
!= s_vertex
) {
1178 /* heuristic; expanding from vertices tends to generate lots of
1179 * too-big regions of tiles. */
1180 if (generate_try_block(state
, rs
, x1
, y1
, x2
, y2
))
1181 continue; /* we expanded successfully. */
1184 if (!(flags
& GP_DOTS
)) continue;
1186 if ((sp
->type
== s_edge
) && (i
% 2)) {
1187 debug(("Omitting edge %d,%d as half-of.\n", sp
->x
, sp
->y
));
1191 /* If we've got here we might want to put a dot down. Check
1192 * if we can, and add one if so. */
1193 if (dot_is_possible(state
, sp
, 0)) {
1195 ret
= solver_obvious_dot(state
, sp
);
1197 debug(("Added dot (and obvious associations) at %d,%d\n",
1205 static int solver_state(game_state
*state
, int maxdiff
);
1207 static char *new_game_desc(game_params
*params
, random_state
*rs
,
1208 char **aux
, int interactive
)
1210 game_state
*state
= blank_game(params
->w
, params
->h
), *copy
;
1212 int *scratch
, sz
= state
->sx
*state
->sy
, i
;
1213 int diff
, ntries
= 0;
1215 /* Random list of squares to try and process, one-by-one. */
1216 scratch
= snewn(sz
, int);
1217 for (i
= 0; i
< sz
; i
++) scratch
[i
] = i
;
1220 clear_game(state
, 1);
1223 //generate_pass(state, rs, scratch, 10, GP_DOTS);
1224 //generate_pass(state, rs, scratch, 100, 0);
1225 generate_pass(state
, rs
, scratch
, 100, GP_DOTS
);
1227 game_update_dots(state
);
1231 char *tmp
= encode_game(state
);
1232 debug(("new_game_desc state %dx%d:%s\n", params
->w
, params
->h
, tmp
));
1237 copy
= dup_game(state
);
1238 clear_game(copy
, 0);
1240 diff
= solver_state(copy
, params
->diff
);
1243 assert(diff
!= DIFF_IMPOSSIBLE
);
1244 if (diff
!= params
->diff
) {
1245 if (ntries
< MAXTRIES
) goto generate
;
1248 desc
= encode_game(state
);
1249 #ifndef STANDALONE_SOLVER
1250 debug(("new_game_desc generated: \n"));
1260 static int solver_obvious(game_state
*state
);
1262 static int dots_too_close(game_state
*state
)
1264 /* Quick-and-dirty check, using half the solver:
1265 * solver_obvious will only fail if the dots are
1266 * too close together, so dot-proximity associations
1268 game_state
*tmp
= dup_game(state
);
1269 int ret
= solver_obvious(tmp
);
1271 return (ret
== -1) ?
1 : 0;
1274 static game_state
*load_game(game_params
*params
, char *desc
,
1277 game_state
*state
= blank_game(params
->w
, params
->h
);
1289 if (n
>= 'a' && n
<= 'y') {
1292 } else if (n
>= 'A' && n
<= 'Y') {
1296 why
= "Invalid characters in game description"; goto fail
;
1298 /* if we got here we incremented i and have a dot to add. */
1299 y
= (i
/ (state
->sx
-2)) + 1;
1300 x
= (i
% (state
->sx
-2)) + 1;
1301 if (!INUI(state
, x
, y
)) {
1302 why
= "Too much data to fit in grid"; goto fail
;
1304 add_dot(&SPACE(state
, x
, y
));
1305 SPACE(state
, x
, y
).flags
|= df
;
1308 game_update_dots(state
);
1310 if (dots_too_close(state
)) {
1311 why
= "Dots too close together"; goto fail
;
1318 if (why_r
) *why_r
= why
;
1322 static char *validate_desc(game_params
*params
, char *desc
)
1325 game_state
*dummy
= load_game(params
, desc
, &why
);
1334 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1336 game_state
*state
= load_game(params
, desc
, NULL
);
1338 assert("Unable to load ?validated game.");
1347 /* ----------------------------------------------------------
1348 * Solver and all its little wizards.
1351 int solver_recurse_depth
;
1353 typedef struct solver_ctx
{
1355 int sz
; /* state->sx * state->sy */
1356 space
**scratch
; /* size sz */
1360 static solver_ctx
*new_solver(game_state
*state
)
1362 solver_ctx
*sctx
= snew(solver_ctx
);
1363 sctx
->state
= state
;
1364 sctx
->sz
= state
->sx
*state
->sy
;
1365 sctx
->scratch
= snewn(sctx
->sz
, space
*);
1369 static void free_solver(solver_ctx
*sctx
)
1371 sfree(sctx
->scratch
);
1375 /* Solver ideas so far:
1377 * For any empty space, work out how many dots it could associate
1379 * it needs line-of-sight
1380 * it needs an empty space on the far side
1381 * any adjacent lines need corresponding line possibilities.
1384 /* The solver_ctx should keep a list of dot positions, for quicker looping.
1386 * Solver techniques, in order of difficulty:
1387 * obvious adjacency to dots
1388 * transferring tiles to opposite side
1389 * transferring lines to opposite side
1390 * one possible dot for a given tile based on opposite availability
1391 * tile with 3 definite edges next to an associated tile must associate
1394 * one possible dot for a given tile based on line-of-sight
1397 static int solver_add_assoc(game_state
*state
, space
*tile
, int dx
, int dy
,
1400 space
*dot
, *tile_opp
;
1402 dot
= &SPACE(state
, dx
, dy
);
1403 tile_opp
= space_opposite_dot(state
, tile
, dot
);
1405 assert(tile
->type
== s_tile
);
1406 if (tile
->flags
& F_TILE_ASSOC
) {
1407 if ((tile
->dotx
!= dx
) || (tile
->doty
!= dy
)) {
1408 solvep(("%*sSet %d,%d --> %d,%d (%s) impossible; "
1409 "already --> %d,%d.\n",
1410 solver_recurse_depth
*4, "",
1411 tile
->x
, tile
->y
, dx
, dy
, why
,
1412 tile
->dotx
, tile
->doty
));
1415 return 0; /* no-op */
1418 solvep(("%*s%d,%d --> %d,%d impossible, no opposite tile.\n",
1419 solver_recurse_depth
*4, "", tile
->x
, tile
->y
, dx
, dy
));
1422 if (tile_opp
->flags
& F_TILE_ASSOC
&&
1423 (tile_opp
->dotx
!= dx
|| tile_opp
->doty
!= dy
)) {
1424 solvep(("%*sSet %d,%d --> %d,%d (%s) impossible; "
1425 "opposite already --> %d,%d.\n",
1426 solver_recurse_depth
*4, "",
1427 tile
->x
, tile
->y
, dx
, dy
, why
,
1428 tile_opp
->dotx
, tile_opp
->doty
));
1432 add_assoc(state
, tile
, dot
);
1433 add_assoc(state
, tile_opp
, dot
);
1434 solvep(("%*sSetting %d,%d --> %d,%d (%s).\n",
1435 solver_recurse_depth
*4, "",
1436 tile
->x
, tile
->y
,dx
, dy
, why
));
1437 solvep(("%*sSetting %d,%d --> %d,%d (%s, opposite).\n",
1438 solver_recurse_depth
*4, "",
1439 tile_opp
->x
, tile_opp
->y
, dx
, dy
, why
));
1443 static int solver_obvious_dot(game_state
*state
, space
*dot
)
1445 int dx
, dy
, ret
, didsth
= 0;
1448 debug(("%*ssolver_obvious_dot for %d,%d.\n",
1449 solver_recurse_depth
*4, "", dot
->x
, dot
->y
));
1451 assert(dot
->flags
& F_DOT
);
1452 for (dx
= -1; dx
<= 1; dx
++) {
1453 for (dy
= -1; dy
<= 1; dy
++) {
1454 if (!INGRID(state
, dot
->x
+dx
, dot
->y
+dy
)) continue;
1456 tile
= &SPACE(state
, dot
->x
+dx
, dot
->y
+dy
);
1457 if (tile
->type
== s_tile
) {
1458 ret
= solver_add_assoc(state
, tile
, dot
->x
, dot
->y
,
1460 if (ret
< 0) return -1;
1461 if (ret
> 0) didsth
= 1;
1468 static int solver_obvious(game_state
*state
)
1470 int i
, didsth
= 0, ret
;
1472 debug(("%*ssolver_obvious.\n", solver_recurse_depth
*4, ""));
1474 for (i
= 0; i
< state
->ndots
; i
++) {
1475 ret
= solver_obvious_dot(state
, state
->dots
[i
]);
1476 if (ret
< 0) return -1;
1477 if (ret
> 0) didsth
= 1;
1482 static int solver_lines_opposite_cb(game_state
*state
, space
*edge
, void *ctx
)
1484 int didsth
= 0, n
, dx
, dy
;
1485 space
*tiles
[2], *tile_opp
, *edge_opp
;
1487 assert(edge
->type
== s_edge
);
1489 tiles_from_edge(state
, edge
, tiles
);
1491 /* if tiles[0] && tiles[1] && they're both associated
1492 * and they're both associated with different dots,
1493 * ensure the line is set. */
1494 if (!(edge
->flags
& F_EDGE_SET
) &&
1495 tiles
[0] && tiles
[1] &&
1496 (tiles
[0]->flags
& F_TILE_ASSOC
) &&
1497 (tiles
[1]->flags
& F_TILE_ASSOC
) &&
1498 (tiles
[0]->dotx
!= tiles
[1]->dotx
||
1499 tiles
[0]->doty
!= tiles
[1]->doty
)) {
1500 /* No edge, but the two adjacent tiles are both
1501 * associated with different dots; add the edge. */
1502 solvep(("%*sSetting edge %d,%d - tiles different dots.\n",
1503 solver_recurse_depth
*4, "", edge
->x
, edge
->y
));
1504 edge
->flags
|= F_EDGE_SET
;
1508 if (!(edge
->flags
& F_EDGE_SET
)) return didsth
;
1509 for (n
= 0; n
< 2; n
++) {
1510 if (!tiles
[n
]) continue;
1511 assert(tiles
[n
]->type
== s_tile
);
1512 if (!(tiles
[n
]->flags
& F_TILE_ASSOC
)) continue;
1514 tile_opp
= tile_opposite(state
, tiles
[n
]);
1516 solvep(("%*simpossible: edge %d,%d has assoc. tile %d,%d"
1517 " with no opposite.\n",
1518 solver_recurse_depth
*4, "",
1519 edge
->x
, edge
->y
, tiles
[n
]->x
, tiles
[n
]->y
));
1520 /* edge of tile has no opposite edge (off grid?);
1521 * this is impossible. */
1525 dx
= tiles
[n
]->x
- edge
->x
;
1526 dy
= tiles
[n
]->y
- edge
->y
;
1527 assert(INGRID(state
, tile_opp
->x
+dx
, tile_opp
->y
+dy
));
1528 edge_opp
= &SPACE(state
, tile_opp
->x
+dx
, tile_opp
->y
+dy
);
1529 if (!(edge_opp
->flags
& F_EDGE_SET
)) {
1530 solvep(("%*sSetting edge %d,%d as opposite %d,%d\n",
1531 solver_recurse_depth
*4, "",
1532 tile_opp
->x
-dx
, tile_opp
->y
-dy
, edge
->x
, edge
->y
));
1533 edge_opp
->flags
|= F_EDGE_SET
;
1540 static int solver_spaces_oneposs_cb(game_state
*state
, space
*tile
, void *ctx
)
1543 struct space
*edgeadj
[4], *tileadj
[4];
1546 assert(tile
->type
== s_tile
);
1547 if (tile
->flags
& F_TILE_ASSOC
) return 0;
1549 adjacencies(state
, tile
, edgeadj
, tileadj
);
1551 /* Empty tile. If each edge is either set, or associated with
1552 * the same dot, we must also associate with dot. */
1553 eset
= 0; dotx
= -1; doty
= -1;
1554 for (n
= 0; n
< 4; n
++) {
1556 assert(edgeadj
[n
]->type
== s_edge
);
1557 if (edgeadj
[n
]->flags
& F_EDGE_SET
) {
1561 assert(tileadj
[n
]->type
== s_tile
);
1563 /* If an adjacent tile is empty we can't make any deductions.*/
1564 if (!(tileadj
[n
]->flags
& F_TILE_ASSOC
))
1567 /* If an adjacent tile is assoc. with a different dot
1568 * we can't make any deductions. */
1569 if (dotx
!= -1 && doty
!= -1 &&
1570 (tileadj
[n
]->dotx
!= dotx
||
1571 tileadj
[n
]->doty
!= doty
))
1574 dotx
= tileadj
[n
]->dotx
;
1575 doty
= tileadj
[n
]->doty
;
1579 solvep(("%*simpossible: empty tile %d,%d has 4 edges\n",
1580 solver_recurse_depth
*4, "",
1584 assert(dotx
!= -1 && doty
!= -1);
1586 ret
= solver_add_assoc(state
, tile
, dotx
, doty
, "rest are edges");
1587 if (ret
== -1) return -1;
1588 assert(ret
!= 0); /* really should have done something. */
1593 /* Improved algorithm for tracking line-of-sight from dots, and not spaces.
1595 * The solver_ctx already stores a list of dots: the algorithm proceeds by
1596 * expanding outwards from each dot in turn, expanding first to the boundary
1597 * of its currently-connected tile and then to all empty tiles that could see
1598 * it. Empty tiles will be flagged with a 'can see dot <x,y>' sticker.
1600 * Expansion will happen by (symmetrically opposite) pairs of squares; if
1601 * a square hasn't an opposite number there's no point trying to expand through
1602 * it. Empty tiles will therefore also be tagged in pairs.
1604 * If an empty tile already has a 'can see dot <x,y>' tag from a previous dot,
1605 * it (and its partner) gets untagged (or, rather, a 'can see two dots' tag)
1606 * because we're looking for single-dot possibilities.
1608 * Once we've gone through all the dots, any which still have a 'can see dot'
1609 * tag get associated with that dot (because it must have been the only one);
1610 * any without any tag (i.e. that could see _no_ dots) cause an impossibility
1613 * The expansion will happen each time with a stored list of (space *) pairs,
1614 * rather than a mark-and-sweep idea; that's horrifically inefficient.
1616 * expansion algorithm:
1618 * * allocate list of (space *) the size of s->sx*s->sy.
1619 * * allocate second grid for (flags, dotx, doty) size of sx*sy.
1621 * clear second grid (flags = 0, all dotx and doty = 0)
1622 * flags: F_REACHABLE, F_MULTIPLE
1625 * * for each dot, start with one pair of tiles that are associated with it --
1626 * * vertex --> (dx+1, dy+1), (dx-1, dy-1)
1627 * * edge --> (adj1, adj2)
1628 * * tile --> (tile, tile) ???
1629 * * mark that pair of tiles with F_MARK, clear all other F_MARKs.
1630 * * add two tiles to start of list.
1632 * set start = 0, end = next = 2
1634 * from (start to end-1, step 2) {
1635 * * we have two tiles (t1, t2), opposites wrt our dot.
1636 * * for each (at1) sensible adjacent tile to t1 (i.e. not past an edge):
1637 * * work out at2 as the opposite to at1
1638 * * assert at1 and at2 have the same F_MARK values.
1639 * * if at1 & F_MARK ignore it (we've been there on a previous sweep)
1640 * * if either are associated with a different dot
1641 * * mark both with F_MARK (so we ignore them later)
1642 * * otherwise (assoc. with our dot, or empty):
1643 * * mark both with F_MARK
1644 * * add their space * values to the end of the list, set next += 2.
1648 * * we didn't add any new squares; exit the loop.
1650 * * set start = next+1, end = next. go round again
1652 * We've finished expanding from the dot. Now, for each square we have
1653 * in our list (--> each square with F_MARK):
1654 * * if the tile is empty:
1655 * * if F_REACHABLE was already set
1658 * * set F_REACHABLE, set dotx and doty to our dot.
1660 * Then, continue the whole thing for each dot in turn.
1662 * Once we've done for each dot, go through the entire grid looking for
1663 * empty tiles: for each empty tile:
1664 * if F_REACHABLE and not F_MULTIPLE, set that dot (and its double)
1665 * if !F_REACHABLE, return as impossible.
1669 /* Returns 1 if this tile is either already associated with this dot,
1671 static int solver_expand_checkdot(space
*tile
, space
*dot
)
1673 if (!(tile
->flags
& F_TILE_ASSOC
)) return 1;
1674 if (tile
->dotx
== dot
->x
&& tile
->doty
== dot
->y
) return 1;
1678 static void solver_expand_fromdot(game_state
*state
, space
*dot
, solver_ctx
*sctx
)
1680 int i
, j
, x
, y
, start
, end
, next
;
1683 /* Clear the grid of the (space) flags we'll use. */
1685 /* This is well optimised; analysis showed that:
1686 for (i = 0; i < sctx->sz; i++) state->grid[i].flags &= ~F_MARK;
1687 took up ~85% of the total function time! */
1688 for (y
= 1; y
< state
->sy
; y
+= 2) {
1689 sp
= &SPACE(state
, 1, y
);
1690 for (x
= 1; x
< state
->sx
; x
+= 2, sp
+= 2)
1691 sp
->flags
&= ~F_MARK
;
1694 /* Seed the list of marked squares with two that must be associated
1695 * with our dot (possibly the same space) */
1696 if (dot
->type
== s_tile
) {
1697 sctx
->scratch
[0] = sctx
->scratch
[1] = dot
;
1698 } else if (dot
->type
== s_edge
) {
1699 tiles_from_edge(state
, dot
, sctx
->scratch
);
1700 } else if (dot
->type
== s_vertex
) {
1701 /* pick two of the opposite ones arbitrarily. */
1702 sctx
->scratch
[0] = &SPACE(state
, dot
->x
-1, dot
->y
-1);
1703 sctx
->scratch
[1] = &SPACE(state
, dot
->x
+1, dot
->y
+1);
1705 assert(sctx
->scratch
[0]->flags
& F_TILE_ASSOC
);
1706 assert(sctx
->scratch
[1]->flags
& F_TILE_ASSOC
);
1708 sctx
->scratch
[0]->flags
|= F_MARK
;
1709 sctx
->scratch
[1]->flags
|= F_MARK
;
1711 debug(("%*sexpand from dot %d,%d seeded with %d,%d and %d,%d.\n",
1712 solver_recurse_depth
*4, "", dot
->x
, dot
->y
,
1713 sctx
->scratch
[0]->x
, sctx
->scratch
[0]->y
,
1714 sctx
->scratch
[1]->x
, sctx
->scratch
[1]->y
));
1716 start
= 0; end
= 2; next
= 2;
1719 debug(("%*sexpand: start %d, end %d, next %d\n",
1720 solver_recurse_depth
*4, "", start
, end
, next
));
1721 for (i
= start
; i
< end
; i
+= 2) {
1722 space
*t1
= sctx
->scratch
[i
]/*, *t2 = sctx->scratch[i+1]*/;
1723 space
*edges
[4], *tileadj
[4], *tileadj2
;
1725 adjacencies(state
, t1
, edges
, tileadj
);
1727 for (j
= 0; j
< 4; j
++) {
1729 if (edges
[j
]->flags
& F_EDGE_SET
) continue;
1732 if (tileadj
[j
]->flags
& F_MARK
) continue; /* seen before. */
1734 /* We have a tile adjacent to t1; find its opposite. */
1735 tileadj2
= space_opposite_dot(state
, tileadj
[j
], dot
);
1737 debug(("%*sMarking %d,%d, no opposite.\n",
1738 solver_recurse_depth
*4, "",
1739 tileadj
[j
]->x
, tileadj
[j
]->y
));
1740 tileadj
[j
]->flags
|= F_MARK
;
1741 continue; /* no opposite, so mark for next time. */
1743 /* If the tile had an opposite we should have either seen both of
1744 * these, or neither of these, before. */
1745 assert(!(tileadj2
->flags
& F_MARK
));
1747 if (solver_expand_checkdot(tileadj
[j
], dot
) &&
1748 solver_expand_checkdot(tileadj2
, dot
)) {
1749 /* Both tiles could associate with this dot; add them to
1751 debug(("%*sAdding %d,%d and %d,%d to possibles list.\n",
1752 solver_recurse_depth
*4, "",
1753 tileadj
[j
]->x
, tileadj
[j
]->y
, tileadj2
->x
, tileadj2
->y
));
1754 sctx
->scratch
[next
++] = tileadj
[j
];
1755 sctx
->scratch
[next
++] = tileadj2
;
1757 /* Either way, we've seen these tiles already so mark them. */
1758 debug(("%*sMarking %d,%d and %d,%d.\n",
1759 solver_recurse_depth
*4, "",
1760 tileadj
[j
]->x
, tileadj
[j
]->y
, tileadj2
->x
, tileadj2
->y
));
1761 tileadj
[j
]->flags
|= F_MARK
;
1762 tileadj2
->flags
|= F_MARK
;
1766 /* We added more squares; go back and try again. */
1767 start
= end
; end
= next
; goto expand
;
1770 /* We've expanded as far as we can go. Now we update the main flags
1771 * on all tiles we've expanded into -- if they were empty, we have
1772 * found possible associations for this dot. */
1773 for (i
= 0; i
< end
; i
++) {
1774 if (sctx
->scratch
[i
]->flags
& F_TILE_ASSOC
) continue;
1775 if (sctx
->scratch
[i
]->flags
& F_REACHABLE
) {
1776 /* This is (at least) the second dot this tile could
1777 * associate with. */
1778 debug(("%*sempty tile %d,%d could assoc. other dot %d,%d\n",
1779 solver_recurse_depth
*4, "",
1780 sctx
->scratch
[i
]->x
, sctx
->scratch
[i
]->y
, dot
->x
, dot
->y
));
1781 sctx
->scratch
[i
]->flags
|= F_MULTIPLE
;
1783 /* This is the first (possibly only) dot. */
1784 debug(("%*sempty tile %d,%d could assoc. 1st dot %d,%d\n",
1785 solver_recurse_depth
*4, "",
1786 sctx
->scratch
[i
]->x
, sctx
->scratch
[i
]->y
, dot
->x
, dot
->y
));
1787 sctx
->scratch
[i
]->flags
|= F_REACHABLE
;
1788 sctx
->scratch
[i
]->dotx
= dot
->x
;
1789 sctx
->scratch
[i
]->doty
= dot
->y
;
1795 static int solver_expand_postcb(game_state
*state
, space
*tile
, void *ctx
)
1797 assert(tile
->type
== s_tile
);
1799 if (tile
->flags
& F_TILE_ASSOC
) return 0;
1801 if (!(tile
->flags
& F_REACHABLE
)) {
1802 solvep(("%*simpossible: space (%d,%d) can reach no dots.\n",
1803 solver_recurse_depth
*4, "", tile
->x
, tile
->y
));
1806 if (tile
->flags
& F_MULTIPLE
) return 0;
1808 return solver_add_assoc(state
, tile
, tile
->dotx
, tile
->doty
,
1809 "single possible dot after expansion");
1812 static int solver_expand_dots(game_state
*state
, solver_ctx
*sctx
)
1816 for (i
= 0; i
< sctx
->sz
; i
++)
1817 state
->grid
[i
].flags
&= ~(F_REACHABLE
|F_MULTIPLE
);
1819 for (i
= 0; i
< state
->ndots
; i
++)
1820 solver_expand_fromdot(state
, state
->dots
[i
], sctx
);
1822 return foreach_tile(state
, solver_expand_postcb
, IMPOSSIBLE_QUITS
, sctx
);
1825 struct recurse_ctx
{
1830 static int solver_recurse_cb(game_state
*state
, space
*tile
, void *ctx
)
1832 struct recurse_ctx
*rctx
= (struct recurse_ctx
*)ctx
;
1835 assert(tile
->type
== s_tile
);
1836 if (tile
->flags
& F_TILE_ASSOC
) return 0;
1838 /* We're unassociated: count up all the dots we could associate with. */
1839 for (i
= 0; i
< state
->ndots
; i
++) {
1840 if (dotfortile(state
, tile
, state
->dots
[i
]))
1843 if (n
> rctx
->bestn
) {
1850 static int solver_state(game_state
*state
, int maxdiff
);
1852 #define MAXRECURSE 5
1854 static int solver_recurse(game_state
*state
, int maxdiff
)
1856 int diff
= DIFF_IMPOSSIBLE
, ret
, n
, gsz
= state
->sx
* state
->sy
;
1857 space
*ingrid
, *outgrid
= NULL
, *bestopp
;
1858 struct recurse_ctx rctx
;
1860 if (solver_recurse_depth
>= MAXRECURSE
) {
1861 solvep(("Limiting recursion to %d, returning.", MAXRECURSE
));
1862 return DIFF_UNFINISHED
;
1865 /* Work out the cell to recurse on; go through all unassociated tiles
1866 * and find which one has the most possible dots it could associate
1871 foreach_tile(state
, solver_recurse_cb
, 0, &rctx
);
1872 if (rctx
.bestn
== 0) return DIFF_IMPOSSIBLE
; /* or assert? */
1875 solvep(("%*sRecursing around %d,%d, with %d possible dots.\n",
1876 solver_recurse_depth
*4, "",
1877 rctx
.best
->x
, rctx
.best
->y
, rctx
.bestn
));
1879 #ifdef STANDALONE_SOLVER
1880 solver_recurse_depth
++;
1883 ingrid
= snewn(gsz
, struct space
);
1884 memcpy(ingrid
, state
->grid
, gsz
* sizeof(struct space
));
1886 for (n
= 0; n
< state
->ndots
; n
++) {
1887 memcpy(state
->grid
, ingrid
, gsz
* sizeof(struct space
));
1889 if (!dotfortile(state
, rctx
.best
, state
->dots
[n
])) continue;
1891 /* set cell (temporarily) pointing to that dot. */
1892 solver_add_assoc(state
, rctx
.best
,
1893 state
->dots
[n
]->x
, state
->dots
[n
]->y
,
1894 "Attempting for recursion");
1896 ret
= solver_state(state
, maxdiff
);
1898 if (diff
== DIFF_IMPOSSIBLE
&& ret
!= DIFF_IMPOSSIBLE
) {
1899 /* we found our first solved grid; copy it away. */
1901 outgrid
= snewn(gsz
, struct space
);
1902 memcpy(outgrid
, state
->grid
, gsz
* sizeof(struct space
));
1904 /* reset cell back to unassociated. */
1905 bestopp
= tile_opposite(state
, rctx
.best
);
1906 assert(bestopp
&& bestopp
->flags
& F_TILE_ASSOC
);
1908 remove_assoc(state
, rctx
.best
);
1909 remove_assoc(state
, bestopp
);
1911 if (ret
== DIFF_AMBIGUOUS
|| ret
== DIFF_UNFINISHED
)
1913 else if (ret
== DIFF_IMPOSSIBLE
)
1916 /* precisely one solution */
1917 if (diff
== DIFF_IMPOSSIBLE
)
1918 diff
= DIFF_RECURSIVE
;
1920 diff
= DIFF_AMBIGUOUS
;
1922 /* if we've found >1 solution, or ran out of recursion,
1923 * give up immediately. */
1924 if (diff
== DIFF_AMBIGUOUS
|| diff
== DIFF_UNFINISHED
)
1928 #ifdef STANDALONE_SOLVER
1929 solver_recurse_depth
--;
1933 /* we found (at least one) soln; copy it back to state */
1934 memcpy(state
->grid
, outgrid
, gsz
* sizeof(struct space
));
1941 static int solver_state(game_state
*state
, int maxdiff
)
1943 solver_ctx
*sctx
= new_solver(state
);
1944 int ret
, diff
= DIFF_EASY
;
1946 ret
= solver_obvious(state
);
1948 diff
= DIFF_IMPOSSIBLE
;
1952 #define CHECKRET(d) do { \
1953 if (ret < 0) { diff = DIFF_IMPOSSIBLE; goto got_result; } \
1954 if (ret > 0) { diff = max(diff, (d)); goto cont; } \
1959 ret
= foreach_edge(state
, solver_lines_opposite_cb
,
1960 IMPOSSIBLE_QUITS
, sctx
);
1961 CHECKRET(DIFF_EASY
);
1963 ret
= foreach_tile(state
, solver_spaces_oneposs_cb
,
1964 IMPOSSIBLE_QUITS
, sctx
);
1965 CHECKRET(DIFF_EASY
);
1967 /* more easy stuff? */
1969 if (maxdiff
<= DIFF_EASY
)
1972 ret
= solver_expand_dots(state
, sctx
);
1973 CHECKRET(DIFF_HARD
);
1975 if (maxdiff
<= DIFF_HARD
)
1980 /* if we reach here, we've made no deductions, so we terminate. */
1984 if (check_complete(state
, 0)) goto got_result
;
1986 diff
= (maxdiff
>= DIFF_RECURSIVE
) ?
1987 solver_recurse(state
, maxdiff
) : DIFF_UNFINISHED
;
1991 #ifndef STANDALONE_SOLVER
1992 debug(("solver_state ends:\n"));
2000 static char *solve_game(game_state
*state
, game_state
*currstate
,
2001 char *aux
, char **error
)
2003 game_state
*tosolve
;
2008 tosolve
= dup_game(currstate
);
2009 diff
= solver_state(tosolve
, DIFF_RECURSIVE
);
2010 if (diff
!= DIFF_UNFINISHED
&& diff
!= DIFF_IMPOSSIBLE
) {
2011 debug(("solve_game solved with current state.\n"));
2016 tosolve
= dup_game(state
);
2017 diff
= solver_state(tosolve
, DIFF_RECURSIVE
);
2018 if (diff
!= DIFF_UNFINISHED
&& diff
!= DIFF_IMPOSSIBLE
) {
2019 debug(("solve_game solved with original state.\n"));
2028 * Clear tile associations: the solution will only include the
2031 for (i
= 0; i
< tosolve
->sx
*tosolve
->sy
; i
++)
2032 tosolve
->grid
[i
].flags
&= ~F_TILE_ASSOC
;
2033 ret
= diff_game(currstate
, tosolve
, 1);
2039 /* ----------------------------------------------------------
2045 int dx
, dy
; /* pixel coords of drag pos. */
2046 int dotx
, doty
; /* grid coords of dot we're dragging from. */
2047 int srcx
, srcy
; /* grid coords of drag start */
2050 static game_ui
*new_ui(game_state
*state
)
2052 game_ui
*ui
= snew(game_ui
);
2053 ui
->dragging
= FALSE
;
2057 static void free_ui(game_ui
*ui
)
2062 static char *encode_ui(game_ui
*ui
)
2067 static void decode_ui(game_ui
*ui
, char *encoding
)
2071 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2072 game_state
*newstate
)
2076 #define FLASH_TIME 0.15F
2078 #define PREFERRED_TILE_SIZE 32
2079 #define TILE_SIZE (ds->tilesize)
2080 #define DOT_SIZE (TILE_SIZE / 4)
2081 #define EDGE_THICKNESS (TILE_SIZE / 16)
2082 #define BORDER TILE_SIZE
2084 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
2085 #define SCOORD(x) ( ((x) * TILE_SIZE)/2 + BORDER )
2086 #define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
2088 #define DRAW_WIDTH (BORDER * 2 + ds->w * TILE_SIZE)
2089 #define DRAW_HEIGHT (BORDER * 2 + ds->h * TILE_SIZE)
2091 struct game_drawstate
{
2095 unsigned long *grid
;
2099 int dragging
, dragx
, dragy
;
2101 int *colour_scratch
;
2104 #define CORNER_TOLERANCE 0.15F
2105 #define CENTRE_TOLERANCE 0.15F
2108 * Round FP coordinates to the centre of the nearest edge.
2111 static void coord_round_to_edge(float x
, float y
, int *xr
, int *yr
)
2113 float xs
, ys
, xv
, yv
, dx
, dy
;
2116 * Find the nearest square-centre.
2118 xs
= (float)floor(x
) + 0.5F
;
2119 ys
= (float)floor(y
) + 0.5F
;
2122 * Find the nearest grid vertex.
2124 xv
= (float)floor(x
+ 0.5F
);
2125 yv
= (float)floor(y
+ 0.5F
);
2128 * Determine whether the horizontal or vertical edge from that
2129 * vertex alongside that square is closer to us, by comparing
2130 * distances from the square cente.
2132 dx
= (float)fabs(x
- xs
);
2133 dy
= (float)fabs(y
- ys
);
2135 /* Vertical edge: x-coord of corner,
2136 * y-coord of square centre. */
2138 *yr
= 1 + 2 * (int)floor(ys
);
2140 /* Horizontal edge: x-coord of square centre,
2141 * y-coord of corner. */
2142 *xr
= 1 + 2 * (int)floor(xs
);
2149 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
2150 int x
, int y
, int button
)
2156 px
= 2*FROMCOORD((float)x
) + 0.5;
2157 py
= 2*FROMCOORD((float)y
) + 0.5;
2161 if (button
== 'C' || button
== 'c') return dupstr("C");
2163 if (button
== 'S' || button
== 's') {
2165 game_state
*tmp
= dup_game(state
);
2166 state
->cdiff
= solver_state(tmp
, DIFF_RECURSIVE
-1);
2167 ret
= diff_game(state
, tmp
, 0);
2172 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
2173 if (!INUI(state
, px
, py
)) return NULL
;
2174 sp
= &SPACE(state
, px
, py
);
2175 if (!dot_is_possible(state
, sp
, 1)) return NULL
;
2176 sprintf(buf
, "%c%d,%d",
2177 (char)((button
== LEFT_BUTTON
) ?
'D' : 'd'), px
, py
);
2184 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
2185 int x
, int y
, int button
)
2187 /* UI operations (play mode):
2189 * Toggle edge (set/unset) (left-click on edge)
2190 * Associate space with dot (left-drag from dot)
2191 * Unassociate space (left-drag from space off grid)
2192 * Autofill lines around shape? (right-click?)
2194 * (edit mode; will clear all lines/associations)
2196 * Add or remove dot (left-click)
2201 struct space
*sp
, *dot
;
2203 if (button
== 'H' || button
== 'h' ||
2204 button
== 'S' || button
== 's') {
2206 game_state
*tmp
= dup_game(state
);
2207 if (button
== 'H' || button
== 'h')
2208 solver_obvious(tmp
);
2210 solver_state(tmp
, DIFF_RECURSIVE
-1);
2211 ret
= diff_game(state
, tmp
, 0);
2216 if (button
== LEFT_BUTTON
) {
2217 coord_round_to_edge(FROMCOORD((float)x
), FROMCOORD((float)y
),
2220 if (!INUI(state
, px
, py
)) return NULL
;
2222 sp
= &SPACE(state
, px
, py
);
2223 assert(sp
->type
== s_edge
);
2225 sprintf(buf
, "E%d,%d", px
, py
);
2228 } else if (button
== RIGHT_BUTTON
) {
2231 px
= 2*FROMCOORD((float)x
) + 0.5;
2232 py
= 2*FROMCOORD((float)y
) + 0.5;
2237 * If there's a dot anywhere nearby, we pick up an arrow
2238 * pointing at that dot.
2240 for (py1
= py
-1; py1
<= py
+1; py1
++)
2241 for (px1
= px
-1; px1
<= px
+1; px1
++) {
2242 if (px1
>= 0 && px1
< state
->sx
&&
2243 py1
>= 0 && py1
< state
->sx
&&
2244 x
>= SCOORD(px1
-1) && x
< SCOORD(px1
+1) &&
2245 y
>= SCOORD(py1
-1) && y
< SCOORD(py1
+1) &&
2246 SPACE(state
, px1
, py1
).flags
& F_DOT
) {
2248 * Found a dot. Begin a drag from it.
2250 dot
= &SPACE(state
, px1
, py1
);
2253 goto done
; /* multi-level break */
2258 * Otherwise, find the nearest _square_, and pick up the
2259 * same arrow as it's got on it, if any.
2262 px
= 2*FROMCOORD(x
+TILE_SIZE
) - 1;
2263 py
= 2*FROMCOORD(y
+TILE_SIZE
) - 1;
2264 if (px
>= 0 && px
< state
->sx
&& py
>= 0 && py
< state
->sx
) {
2265 sp
= &SPACE(state
, px
, py
);
2266 if (sp
->flags
& F_TILE_ASSOC
) {
2267 dot
= &SPACE(state
, sp
->dotx
, sp
->doty
);
2276 * Now, if we've managed to find a dot, begin a drag.
2279 ui
->dragging
= TRUE
;
2286 } else if (button
== RIGHT_DRAG
&& ui
->dragging
) {
2287 /* just move the drag coords. */
2291 } else if (button
== RIGHT_RELEASE
&& ui
->dragging
) {
2292 ui
->dragging
= FALSE
;
2295 * Drags are always targeted at a single square.
2297 px
= 2*FROMCOORD(x
+TILE_SIZE
) - 1;
2298 py
= 2*FROMCOORD(y
+TILE_SIZE
) - 1;
2301 * Dragging an arrow on to the same square it started from
2302 * is a null move; just update the ui and finish.
2304 if (px
== ui
->srcx
&& py
== ui
->srcy
)
2311 * Otherwise, we remove the arrow from its starting
2312 * square if we didn't start from a dot...
2314 if ((ui
->srcx
!= ui
->dotx
|| ui
->srcy
!= ui
->doty
) &&
2315 SPACE(state
, ui
->srcx
, ui
->srcy
).flags
& F_TILE_ASSOC
) {
2316 sprintf(buf
+ strlen(buf
), "%sU%d,%d", sep
, ui
->srcx
, ui
->srcy
);
2321 * ... and if the square we're moving it _to_ is valid, we
2322 * add one there instead.
2324 if (INUI(state
, px
, py
)) {
2325 sp
= &SPACE(state
, px
, py
);
2327 if (!(sp
->flags
& F_DOT
) && !(sp
->flags
& F_TILE_ASSOC
))
2328 sprintf(buf
+ strlen(buf
), "%sA%d,%d,%d,%d",
2329 sep
, px
, py
, ui
->dotx
, ui
->doty
);
2342 static int check_complete_in_play(game_state
*state
, int *dsf
, int *colours
)
2344 int w
= state
->w
, h
= state
->h
;
2349 int minx
, miny
, maxx
, maxy
;
2355 dsf
= snew_dsf(w
*h
);
2363 * During actual game play, completion checking is done on the
2364 * basis of the edges rather than the square associations. So
2365 * first we must go through the grid figuring out the connected
2366 * components into which the edges divide it.
2368 for (y
= 0; y
< h
; y
++)
2369 for (x
= 0; x
< w
; x
++) {
2370 if (y
+1 < h
&& !(SPACE(state
, 2*x
+1, 2*y
+2).flags
& F_EDGE_SET
))
2371 dsf_merge(dsf
, y
*w
+x
, (y
+1)*w
+x
);
2372 if (x
+1 < w
&& !(SPACE(state
, 2*x
+2, 2*y
+1).flags
& F_EDGE_SET
))
2373 dsf_merge(dsf
, y
*w
+x
, y
*w
+(x
+1));
2377 * That gives us our connected components. Now, for each
2378 * component, decide whether it's _valid_. A valid component is
2381 * - is 180-degree rotationally symmetric
2382 * - has a dot at its centre of symmetry
2383 * - has no other dots anywhere within it (including on its
2385 * - contains no internal edges (i.e. edges separating two
2386 * squares which are both part of the component).
2390 * First, go through the grid finding the bounding box of each
2393 sqdata
= snewn(w
*h
, struct sqdata
);
2394 for (i
= 0; i
< w
*h
; i
++) {
2395 sqdata
[i
].minx
= w
+1;
2396 sqdata
[i
].miny
= h
+1;
2397 sqdata
[i
].maxx
= sqdata
[i
].maxy
= -1;
2398 sqdata
[i
].valid
= FALSE
;
2400 for (y
= 0; y
< h
; y
++)
2401 for (x
= 0; x
< w
; x
++) {
2402 i
= dsf_canonify(dsf
, y
*w
+x
);
2403 if (sqdata
[i
].minx
> x
)
2405 if (sqdata
[i
].maxx
< x
)
2407 if (sqdata
[i
].miny
> y
)
2409 if (sqdata
[i
].maxy
< y
)
2411 sqdata
[i
].valid
= TRUE
;
2415 * Now we're in a position to loop over each actual component
2416 * and figure out where its centre of symmetry has to be if
2419 for (i
= 0; i
< w
*h
; i
++)
2420 if (sqdata
[i
].valid
) {
2421 sqdata
[i
].cx
= sqdata
[i
].minx
+ sqdata
[i
].maxx
+ 1;
2422 sqdata
[i
].cy
= sqdata
[i
].miny
+ sqdata
[i
].maxy
+ 1;
2423 if (!(SPACE(state
, sqdata
[i
].cx
, sqdata
[i
].cy
).flags
& F_DOT
))
2424 sqdata
[i
].valid
= FALSE
; /* no dot at centre of symmetry */
2425 if (SPACE(state
, sqdata
[i
].cx
, sqdata
[i
].cy
).flags
& F_DOT_BLACK
)
2426 sqdata
[i
].colour
= 2;
2428 sqdata
[i
].colour
= 1;
2432 * Now we loop over the whole grid again, this time finding
2433 * extraneous dots (any dot which wholly or partially overlaps
2434 * a square and is not at the centre of symmetry of that
2435 * square's component disqualifies the component from validity)
2436 * and extraneous edges (any edge separating two squares
2437 * belonging to the same component also disqualifies that
2440 for (y
= 1; y
< state
->sy
-1; y
++)
2441 for (x
= 1; x
< state
->sx
-1; x
++) {
2442 space
*sp
= &SPACE(state
, x
, y
);
2444 if (sp
->flags
& F_DOT
) {
2446 * There's a dot here. Use it to disqualify any
2447 * component which deserves it.
2450 for (cy
= (y
-1) >> 1; cy
<= y
>> 1; cy
++)
2451 for (cx
= (x
-1) >> 1; cx
<= x
>> 1; cx
++) {
2452 i
= dsf_canonify(dsf
, cy
*w
+cx
);
2453 if (x
!= sqdata
[i
].cx
|| y
!= sqdata
[i
].cy
)
2454 sqdata
[i
].valid
= FALSE
;
2458 if (sp
->flags
& F_EDGE_SET
) {
2460 * There's an edge here. Use it to disqualify a
2461 * component if necessary.
2463 int cx1
= (x
-1) >> 1, cx2
= x
>> 1;
2464 int cy1
= (y
-1) >> 1, cy2
= y
>> 1;
2465 assert((cx1
==cx2
) ^ (cy1
==cy2
));
2466 i
= dsf_canonify(dsf
, cy1
*w
+cx1
);
2467 if (i
== dsf_canonify(dsf
, cy2
*w
+cx2
))
2468 sqdata
[i
].valid
= FALSE
;
2473 * And finally we test rotational symmetry: for each square in
2474 * the grid, find which component it's in, test that that
2475 * component also has a square in the symmetric position, and
2476 * disqualify it if it doesn't.
2478 for (y
= 0; y
< h
; y
++)
2479 for (x
= 0; x
< w
; x
++) {
2482 i
= dsf_canonify(dsf
, y
*w
+x
);
2484 x2
= sqdata
[i
].cx
- 1 - x
;
2485 y2
= sqdata
[i
].cy
- 1 - y
;
2486 if (i
!= dsf_canonify(dsf
, y2
*w
+x2
))
2487 sqdata
[i
].valid
= FALSE
;
2491 * That's it. We now have all the connected components marked
2492 * as valid or not valid. So now we return a `colours' array if
2493 * we were asked for one, and also we return an overall
2494 * true/false value depending on whether _every_ square in the
2495 * grid is part of a valid component.
2498 for (i
= 0; i
< w
*h
; i
++) {
2499 int ci
= dsf_canonify(dsf
, i
);
2500 int thisok
= sqdata
[ci
].valid
;
2502 colours
[i
] = thisok ? sqdata
[ci
].colour
: 0;
2503 ret
= ret
&& thisok
;
2513 static game_state
*execute_move(game_state
*state
, char *move
)
2515 int x
, y
, ax
, ay
, n
, dx
, dy
;
2516 game_state
*ret
= dup_game(state
);
2517 struct space
*sp
, *dot
;
2519 debug(("%s\n", move
));
2523 if (c
== 'E' || c
== 'U' || c
== 'M'
2525 || c
== 'D' || c
== 'd'
2529 if (sscanf(move
, "%d,%d%n", &x
, &y
, &n
) != 2 ||
2533 sp
= &SPACE(ret
, x
, y
);
2535 if (c
== 'D' || c
== 'd') {
2536 unsigned int currf
, newf
, maskf
;
2538 if (!dot_is_possible(state
, sp
, 1)) goto badmove
;
2540 newf
= F_DOT
| (c
== 'd' ? F_DOT_BLACK
: 0);
2541 currf
= GRID(ret
, grid
, x
, y
).flags
;
2542 maskf
= F_DOT
| F_DOT_BLACK
;
2543 /* if we clicked 'white dot':
2544 * white --> empty, empty --> white, black --> white.
2545 * if we clicker 'black dot':
2546 * black --> empty, empty --> black, white --> black.
2548 if (currf
& maskf
) {
2549 sp
->flags
&= ~maskf
;
2550 if ((currf
& maskf
) != newf
)
2554 sp
->nassoc
= 0; /* edit-mode disallows associations. */
2555 game_update_dots(ret
);
2559 if (sp
->type
!= s_edge
) goto badmove
;
2560 sp
->flags
^= F_EDGE_SET
;
2561 } else if (c
== 'U') {
2562 if (sp
->type
!= s_tile
|| !(sp
->flags
& F_TILE_ASSOC
))
2564 remove_assoc(ret
, sp
);
2565 } else if (c
== 'M') {
2566 if (!(sp
->flags
& F_DOT
)) goto badmove
;
2567 sp
->flags
^= F_DOT_HOLD
;
2570 } else if (c
== 'A' || c
== 'a') {
2572 if (sscanf(move
, "%d,%d,%d,%d%n", &x
, &y
, &ax
, &ay
, &n
) != 4 ||
2573 x
< 1 || y
< 1 || x
>= (state
->sx
-1) || y
>= (state
->sy
-1) ||
2574 ax
< 1 || ay
< 1 || ax
>= (state
->sx
-1) || ay
>= (state
->sy
-1))
2577 dot
= &GRID(ret
, grid
, ax
, ay
);
2578 if (!(dot
->flags
& F_DOT
))goto badmove
;
2579 if (dot
->flags
& F_DOT_HOLD
) goto badmove
;
2581 for (dx
= -1; dx
<= 1; dx
++) {
2582 for (dy
= -1; dy
<= 1; dy
++) {
2583 sp
= &GRID(ret
, grid
, x
+dx
, y
+dy
);
2584 if (sp
->type
!= s_tile
) continue;
2585 if (sp
->flags
& F_TILE_ASSOC
) {
2586 space
*dot
= &SPACE(state
, sp
->dotx
, sp
->doty
);
2587 if (dot
->flags
& F_DOT_HOLD
) continue;
2589 add_assoc(state
, sp
, dot
);
2594 } else if (c
== 'C') {
2598 } else if (c
== 'S') {
2608 if (check_complete_in_play(ret
, NULL
, NULL
))
2617 /* ----------------------------------------------------------------------
2621 /* Lines will be much smaller size than squares; say, 1/8 the size?
2623 * Need a 'top-left corner of location XxY' to take this into account;
2624 * alternaticaly, that could give the middle of that location, and the
2625 * drawing code would just know the expected dimensions.
2627 * We also need something to take a click and work out what it was
2628 * we were interested in. Clicking on vertices is required because
2629 * we may want to drag from them, for example.
2632 static void game_compute_size(game_params
*params
, int sz
,
2635 struct { int tilesize
, w
, h
; } ads
, *ds
= &ads
;
2645 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
2646 game_params
*params
, int sz
)
2650 assert(TILE_SIZE
> 0);
2653 ds
->bl
= blitter_new(dr
, TILE_SIZE
, TILE_SIZE
);
2656 static float *game_colours(frontend
*fe
, int *ncolours
)
2658 float *ret
= snewn(3 * NCOLOURS
, float);
2662 * We call game_mkhighlight to ensure the background colour
2663 * isn't completely white. We don't actually use the high- and
2664 * lowlight colours it generates.
2666 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_WHITEBG
, COL_BLACKBG
);
2668 for (i
= 0; i
< 3; i
++) {
2670 * Currently, white dots and white-background squares are
2673 ret
[COL_WHITEDOT
* 3 + i
] = 1.0F
;
2674 ret
[COL_WHITEBG
* 3 + i
] = 1.0F
;
2677 * But black-background squares are a dark grey, whereas
2678 * black dots are really black.
2680 ret
[COL_BLACKDOT
* 3 + i
] = 0.0F
;
2681 ret
[COL_BLACKBG
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.3F
;
2684 * In unfilled squares, we draw a faint gridwork.
2686 ret
[COL_GRID
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
2689 * Edges and arrows are filled in in pure black.
2691 ret
[COL_EDGE
* 3 + i
] = 0.0F
;
2692 ret
[COL_ARROW
* 3 + i
] = 0.0F
;
2696 /* tinge the edit background to bluey */
2697 ret
[COL_BACKGROUND
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 0.8F
;
2698 ret
[COL_BACKGROUND
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 0] * 0.8F
;
2699 ret
[COL_BACKGROUND
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 0] * 1.4F
;
2700 if (ret
[COL_BACKGROUND
* 3 + 2] > 1.0F
) ret
[COL_BACKGROUND
* 3 + 2] = 1.0F
;
2703 *ncolours
= NCOLOURS
;
2707 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
2709 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2716 ds
->grid
= snewn(ds
->w
*ds
->h
, unsigned long);
2717 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
2718 ds
->grid
[i
] = 0xFFFFFFFFUL
;
2719 ds
->dx
= snewn(ds
->w
*ds
->h
, int);
2720 ds
->dy
= snewn(ds
->w
*ds
->h
, int);
2723 ds
->dragging
= FALSE
;
2724 ds
->dragx
= ds
->dragy
= 0;
2726 ds
->colour_scratch
= snewn(ds
->w
* ds
->h
, int);
2731 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2733 sfree(ds
->colour_scratch
);
2734 if (ds
->bl
) blitter_free(dr
, ds
->bl
);
2741 #define DRAW_EDGE_L 0x0001
2742 #define DRAW_EDGE_R 0x0002
2743 #define DRAW_EDGE_U 0x0004
2744 #define DRAW_EDGE_D 0x0008
2745 #define DRAW_CORNER_UL 0x0010
2746 #define DRAW_CORNER_UR 0x0020
2747 #define DRAW_CORNER_DL 0x0040
2748 #define DRAW_CORNER_DR 0x0080
2749 #define DRAW_WHITE 0x0100
2750 #define DRAW_BLACK 0x0200
2751 #define DRAW_ARROW 0x0400
2752 #define DOT_SHIFT_C 11
2753 #define DOT_SHIFT_M 2
2754 #define DOT_WHITE 1UL
2755 #define DOT_BLACK 2UL
2758 * Draw an arrow centred on (cx,cy), pointing in the direction
2759 * (ddx,ddy). (I.e. pointing at the point (cx+ddx, cy+ddy).
2761 static void draw_arrow(drawing
*dr
, game_drawstate
*ds
,
2762 int cx
, int cy
, int ddx
, int ddy
)
2764 float vlen
= sqrt(ddx
*ddx
+ddy
*ddy
);
2765 float xdx
= ddx
/vlen
, xdy
= ddy
/vlen
;
2766 float ydx
= -xdy
, ydy
= xdx
;
2767 int e1x
= cx
+ xdx
*TILE_SIZE
/3, e1y
= cy
+ xdy
*TILE_SIZE
/3;
2768 int e2x
= cx
- xdx
*TILE_SIZE
/3, e2y
= cy
- xdy
*TILE_SIZE
/3;
2769 int adx
= (ydx
-xdx
)*TILE_SIZE
/8, ady
= (ydy
-xdy
)*TILE_SIZE
/8;
2770 int adx2
= (-ydx
-xdx
)*TILE_SIZE
/8, ady2
= (-ydy
-xdy
)*TILE_SIZE
/8;
2772 draw_line(dr
, e1x
, e1y
, e2x
, e2y
, COL_ARROW
);
2773 draw_line(dr
, e1x
, e1y
, e1x
+adx
, e1y
+ady
, COL_ARROW
);
2774 draw_line(dr
, e1x
, e1y
, e1x
+adx2
, e1y
+ady2
, COL_ARROW
);
2777 static void draw_square(drawing
*dr
, game_drawstate
*ds
, int x
, int y
,
2778 unsigned long flags
, int ddx
, int ddy
)
2780 int lx
= COORD(x
), ly
= COORD(y
);
2784 clip(dr
, lx
, ly
, TILE_SIZE
, TILE_SIZE
);
2787 * Draw the tile background.
2789 draw_rect(dr
, lx
, ly
, TILE_SIZE
, TILE_SIZE
,
2790 (flags
& DRAW_WHITE ? COL_WHITEBG
:
2791 flags
& DRAW_BLACK ? COL_BLACKBG
: COL_BACKGROUND
));
2796 gridcol
= (flags
& DRAW_BLACK ? COL_BLACKDOT
: COL_GRID
);
2797 draw_rect(dr
, lx
, ly
, 1, TILE_SIZE
, gridcol
);
2798 draw_rect(dr
, lx
, ly
, TILE_SIZE
, 1, gridcol
);
2803 if (flags
& DRAW_ARROW
)
2804 draw_arrow(dr
, ds
, lx
+ TILE_SIZE
/2, ly
+ TILE_SIZE
/2, ddx
, ddy
);
2809 if (flags
& DRAW_EDGE_L
)
2810 draw_rect(dr
, lx
, ly
, EDGE_THICKNESS
, TILE_SIZE
, COL_EDGE
);
2811 if (flags
& DRAW_EDGE_R
)
2812 draw_rect(dr
, lx
+ TILE_SIZE
- EDGE_THICKNESS
+ 1, ly
,
2813 EDGE_THICKNESS
- 1, TILE_SIZE
, COL_EDGE
);
2814 if (flags
& DRAW_EDGE_U
)
2815 draw_rect(dr
, lx
, ly
, TILE_SIZE
, EDGE_THICKNESS
, COL_EDGE
);
2816 if (flags
& DRAW_EDGE_D
)
2817 draw_rect(dr
, lx
, ly
+ TILE_SIZE
- EDGE_THICKNESS
+ 1,
2818 TILE_SIZE
, EDGE_THICKNESS
- 1, COL_EDGE
);
2819 if (flags
& DRAW_CORNER_UL
)
2820 draw_rect(dr
, lx
, ly
, EDGE_THICKNESS
, EDGE_THICKNESS
, COL_EDGE
);
2821 if (flags
& DRAW_CORNER_UR
)
2822 draw_rect(dr
, lx
+ TILE_SIZE
- EDGE_THICKNESS
+ 1, ly
,
2823 EDGE_THICKNESS
- 1, EDGE_THICKNESS
, COL_EDGE
);
2824 if (flags
& DRAW_CORNER_DL
)
2825 draw_rect(dr
, lx
, ly
+ TILE_SIZE
- EDGE_THICKNESS
+ 1,
2826 EDGE_THICKNESS
, EDGE_THICKNESS
- 1, COL_EDGE
);
2827 if (flags
& DRAW_CORNER_DR
)
2828 draw_rect(dr
, lx
+ TILE_SIZE
- EDGE_THICKNESS
+ 1,
2829 ly
+ TILE_SIZE
- EDGE_THICKNESS
+ 1,
2830 EDGE_THICKNESS
- 1, EDGE_THICKNESS
- 1, COL_EDGE
);
2835 for (dy
= 0; dy
< 3; dy
++)
2836 for (dx
= 0; dx
< 3; dx
++) {
2837 int dotval
= (flags
>> (DOT_SHIFT_C
+ DOT_SHIFT_M
*(dy
*3+dx
)));
2838 dotval
&= (1 << DOT_SHIFT_M
)-1;
2841 draw_circle(dr
, lx
+dx
*TILE_SIZE
/2, ly
+dy
*TILE_SIZE
/2,
2843 (dotval
== 1 ? COL_WHITEDOT
: COL_BLACKDOT
),
2848 draw_update(dr
, lx
, ly
, TILE_SIZE
, TILE_SIZE
);
2851 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2852 game_state
*state
, int dir
, game_ui
*ui
,
2853 float animtime
, float flashtime
)
2855 int w
= ds
->w
, h
= ds
->h
;
2856 int x
, y
, flashing
= FALSE
;
2858 if (flashtime
> 0) {
2859 int frame
= (int)(flashtime
/ FLASH_TIME
);
2860 flashing
= (frame
% 2 == 0);
2865 blitter_load(dr
, ds
->bl
, ds
->dragx
, ds
->dragy
);
2866 draw_update(dr
, ds
->dragx
, ds
->dragy
, TILE_SIZE
, TILE_SIZE
);
2867 ds
->dragging
= FALSE
;
2871 draw_rect(dr
, 0, 0, DRAW_WIDTH
, DRAW_HEIGHT
, COL_BACKGROUND
);
2872 draw_rect(dr
, BORDER
- EDGE_THICKNESS
+ 1, BORDER
- EDGE_THICKNESS
+ 1,
2873 w
*TILE_SIZE
+ EDGE_THICKNESS
*2 - 1,
2874 h
*TILE_SIZE
+ EDGE_THICKNESS
*2 - 1, COL_EDGE
);
2875 draw_update(dr
, 0, 0, DRAW_WIDTH
, DRAW_HEIGHT
);
2879 check_complete_in_play(state
, NULL
, ds
->colour_scratch
);
2881 for (y
= 0; y
< h
; y
++)
2882 for (x
= 0; x
< w
; x
++) {
2883 unsigned long flags
= 0;
2884 int ddx
= 0, ddy
= 0;
2889 * Set up the flags for this square. Firstly, see if we
2892 if (SPACE(state
, x
*2, y
*2+1).flags
& F_EDGE_SET
)
2893 flags
|= DRAW_EDGE_L
;
2894 if (SPACE(state
, x
*2+2, y
*2+1).flags
& F_EDGE_SET
)
2895 flags
|= DRAW_EDGE_R
;
2896 if (SPACE(state
, x
*2+1, y
*2).flags
& F_EDGE_SET
)
2897 flags
|= DRAW_EDGE_U
;
2898 if (SPACE(state
, x
*2+1, y
*2+2).flags
& F_EDGE_SET
)
2899 flags
|= DRAW_EDGE_D
;
2902 * Also, mark corners of neighbouring edges.
2904 if ((x
> 0 && SPACE(state
, x
*2-1, y
*2).flags
& F_EDGE_SET
) ||
2905 (y
> 0 && SPACE(state
, x
*2, y
*2-1).flags
& F_EDGE_SET
))
2906 flags
|= DRAW_CORNER_UL
;
2907 if ((x
+1 < w
&& SPACE(state
, x
*2+3, y
*2).flags
& F_EDGE_SET
) ||
2908 (y
> 0 && SPACE(state
, x
*2+2, y
*2-1).flags
& F_EDGE_SET
))
2909 flags
|= DRAW_CORNER_UR
;
2910 if ((x
> 0 && SPACE(state
, x
*2-1, y
*2+2).flags
& F_EDGE_SET
) ||
2911 (y
+1 < h
&& SPACE(state
, x
*2, y
*2+3).flags
& F_EDGE_SET
))
2912 flags
|= DRAW_CORNER_DL
;
2913 if ((x
+1 < w
&& SPACE(state
, x
*2+3, y
*2+2).flags
& F_EDGE_SET
) ||
2914 (y
+1 < h
&& SPACE(state
, x
*2+2, y
*2+3).flags
& F_EDGE_SET
))
2915 flags
|= DRAW_CORNER_DR
;
2918 * If this square is part of a valid region, paint it
2919 * that region's colour. Exception: if we're flashing,
2920 * everything goes briefly back to background colour.
2922 sp
= &SPACE(state
, x
*2+1, y
*2+1);
2923 if (ds
->colour_scratch
[y
*w
+x
] && !flashing
) {
2924 flags
|= (ds
->colour_scratch
[y
*w
+x
] == 2 ?
2925 DRAW_BLACK
: DRAW_WHITE
);
2929 * If this square is associated with a dot but it isn't
2930 * part of a valid region, draw an arrow in it pointing
2931 * in the direction of that dot.
2933 * Exception: if this is the source point of an active
2934 * drag, we don't draw the arrow.
2936 if ((sp
->flags
& F_TILE_ASSOC
) && !ds
->colour_scratch
[y
*w
+x
]) {
2937 if (ui
->dragging
&& ui
->srcx
== x
*2+1 && ui
->srcy
== y
*2+1) {
2939 } else if (sp
->doty
!= y
*2+1 || sp
->dotx
!= x
*2+1) {
2940 flags
|= DRAW_ARROW
;
2941 ddy
= sp
->doty
- (y
*2+1);
2942 ddx
= sp
->dotx
- (x
*2+1);
2947 * Now go through the nine possible places we could
2950 for (dy
= 0; dy
< 3; dy
++)
2951 for (dx
= 0; dx
< 3; dx
++) {
2952 sp
= &SPACE(state
, x
*2+dx
, y
*2+dy
);
2953 if (sp
->flags
& F_DOT
) {
2954 unsigned long dotval
= (sp
->flags
& F_DOT_BLACK ?
2955 DOT_BLACK
: DOT_WHITE
);
2956 flags
|= dotval
<< (DOT_SHIFT_C
+
2957 DOT_SHIFT_M
*(dy
*3+dx
));
2962 * Now we have everything we're going to need. Draw the
2965 if (ds
->grid
[y
*w
+x
] != flags
||
2966 ds
->dx
[y
*w
+x
] != ddx
||
2967 ds
->dy
[y
*w
+x
] != ddy
) {
2968 draw_square(dr
, ds
, x
, y
, flags
, ddx
, ddy
);
2969 ds
->grid
[y
*w
+x
] = flags
;
2970 ds
->dx
[y
*w
+x
] = ddx
;
2971 ds
->dy
[y
*w
+x
] = ddy
;
2976 ds
->dragging
= TRUE
;
2977 ds
->dragx
= ui
->dx
- TILE_SIZE
/2;
2978 ds
->dragy
= ui
->dy
- TILE_SIZE
/2;
2979 blitter_save(dr
, ds
->bl
, ds
->dragx
, ds
->dragy
);
2980 draw_arrow(dr
, ds
, ui
->dx
, ui
->dy
,
2981 SCOORD(ui
->dotx
) - ui
->dx
,
2982 SCOORD(ui
->doty
) - ui
->dy
);
2987 if (state
->cdiff
!= -1)
2988 sprintf(buf
, "Puzzle is %s.", galaxies_diffnames
[state
->cdiff
]);
2991 status_bar(dr
, buf
);
2996 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2997 int dir
, game_ui
*ui
)
3002 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
3003 int dir
, game_ui
*ui
)
3005 if ((!oldstate
->completed
&& newstate
->completed
) &&
3006 !(newstate
->used_solve
))
3007 return 3 * FLASH_TIME
;
3012 static int game_timing_state(game_state
*state
, game_ui
*ui
)
3018 static void game_print_size(game_params
*params
, float *x
, float *y
)
3023 * 8mm squares by default. (There isn't all that much detail
3024 * that needs to go in each square.)
3026 game_compute_size(params
, 800, &pw
, &ph
);
3031 static void game_print(drawing
*dr
, game_state
*state
, int sz
)
3033 int w
= state
->w
, h
= state
->h
;
3034 int white
, black
, blackish
;
3038 int ncoords
= 0, coordsize
= 0;
3040 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
3041 game_drawstate ads
, *ds
= &ads
;
3044 white
= print_grey_colour(dr
, HATCH_CLEAR
, 1.0F
);
3045 black
= print_grey_colour(dr
, HATCH_SOLID
, 0.0F
);
3046 blackish
= print_grey_colour(dr
, HATCH_X
, 0.5F
);
3049 * Get the completion information.
3051 dsf
= snewn(w
* h
, int);
3052 colours
= snewn(w
* h
, int);
3053 check_complete_in_play(state
, dsf
, colours
);
3058 print_line_width(dr
, TILE_SIZE
/ 64);
3059 for (x
= 1; x
< w
; x
++)
3060 draw_line(dr
, COORD(x
), COORD(0), COORD(x
), COORD(h
), black
);
3061 for (y
= 1; y
< h
; y
++)
3062 draw_line(dr
, COORD(0), COORD(y
), COORD(w
), COORD(y
), black
);
3065 * Shade the completed regions. Just in case any particular
3066 * printing platform deals badly with adjacent
3067 * similarly-hatched regions, we'll fill each one as a single
3070 for (i
= 0; i
< w
*h
; i
++) {
3071 j
= dsf_canonify(dsf
, i
);
3072 if (colours
[j
] != 0) {
3076 * This is the first square we've run into belonging to
3077 * this polyomino, which means an edge of the polyomino
3078 * is certain to be to our left. (After we finish
3079 * tracing round it, we'll set the colours[] entry to
3080 * zero to prevent accidentally doing it again.)
3090 * We are currently sitting on square (x,y), which
3091 * we know to be in our polyomino, and we also know
3092 * that (x+dx,y+dy) is not. The way I visualise
3093 * this is that we're standing to the right of a
3094 * boundary line, stretching our left arm out to
3095 * point to the exterior square on the far side.
3099 * First, check if we've gone round the entire
3103 (x
== i
%w
&& y
== i
/w
&& dx
== -1 && dy
== 0))
3107 * Add to our coordinate list the coordinate
3108 * backwards and to the left of where we are.
3110 if (ncoords
+ 2 > coordsize
) {
3111 coordsize
= (ncoords
* 3 / 2) + 64;
3112 coords
= sresize(coords
, coordsize
, int);
3114 coords
[ncoords
++] = COORD((2*x
+1 + dx
+ dy
) / 2);
3115 coords
[ncoords
++] = COORD((2*y
+1 + dy
- dx
) / 2);
3118 * Follow the edge round. If the square directly in
3119 * front of us is not part of the polyomino, we
3120 * turn right; if it is and so is the square in
3121 * front of (x+dx,y+dy), we turn left; otherwise we
3124 if (x
-dy
< 0 || x
-dy
>= w
|| y
+dx
< 0 || y
+dx
>= h
||
3125 dsf_canonify(dsf
, (y
+dx
)*w
+(x
-dy
)) != j
) {
3130 } else if (x
+dx
-dy
>= 0 && x
+dx
-dy
< w
&&
3131 y
+dy
+dx
>= 0 && y
+dy
+dx
< h
&&
3132 dsf_canonify(dsf
, (y
+dy
+dx
)*w
+(x
+dx
-dy
)) == j
) {
3149 * Now we have our polygon complete, so fill it.
3151 draw_polygon(dr
, coords
, ncoords
/2,
3152 colours
[j
] == 2 ? blackish
: -1, black
);
3155 * And mark this polyomino as done.
3164 for (y
= 0; y
<= h
; y
++)
3165 for (x
= 0; x
<= w
; x
++) {
3166 if (x
< w
&& SPACE(state
, x
*2+1, y
*2).flags
& F_EDGE_SET
)
3167 draw_rect(dr
, COORD(x
)-EDGE_THICKNESS
, COORD(y
)-EDGE_THICKNESS
,
3168 EDGE_THICKNESS
* 2 + TILE_SIZE
, EDGE_THICKNESS
* 2,
3170 if (y
< h
&& SPACE(state
, x
*2, y
*2+1).flags
& F_EDGE_SET
)
3171 draw_rect(dr
, COORD(x
)-EDGE_THICKNESS
, COORD(y
)-EDGE_THICKNESS
,
3172 EDGE_THICKNESS
* 2, EDGE_THICKNESS
* 2 + TILE_SIZE
,
3179 for (y
= 0; y
<= 2*h
; y
++)
3180 for (x
= 0; x
<= 2*w
; x
++)
3181 if (SPACE(state
, x
, y
).flags
& F_DOT
) {
3182 draw_circle(dr
, COORD(x
/2.0), COORD(y
/2.0), DOT_SIZE
,
3183 (SPACE(state
, x
, y
).flags
& F_DOT_BLACK ?
3184 black
: white
), black
);
3194 #define thegame galaxies
3197 const struct game thegame
= {
3198 "Galaxies", "games.galaxies", "galaxies",
3205 TRUE
, game_configure
, custom_params
,
3217 TRUE
, game_text_format
,
3225 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
3228 game_free_drawstate
,
3233 FALSE
, FALSE
, NULL
, NULL
,
3234 TRUE
, /* wants_statusbar */
3236 TRUE
, TRUE
, game_print_size
, game_print
,
3237 FALSE
, /* wants_statusbar */
3239 FALSE
, game_timing_state
,
3243 #ifdef STANDALONE_SOLVER
3249 static void usage_exit(const char *msg
)
3252 fprintf(stderr
, "%s: %s\n", quis
, msg
);
3253 fprintf(stderr
, "Usage: %s [--seed SEED] --soak <params> | [game_id [game_id ...]]\n", quis
);
3257 static void dump_state(game_state
*state
)
3259 char *temp
= game_text_format(state
);
3260 printf("%s\n", temp
);
3264 static int gen(game_params
*p
, random_state
*rs
, int debug
)
3271 solver_show_working
= debug
;
3273 printf("Generating a %dx%d %s puzzle.\n",
3274 p
->w
, p
->h
, galaxies_diffnames
[p
->diff
]);
3276 desc
= new_game_desc(p
, rs
, NULL
, 0);
3277 state
= new_game(NULL
, p
, desc
);
3280 diff
= solver_state(state
, DIFF_RECURSIVE
);
3281 printf("Generated %s game %dx%d:%s\n",
3282 galaxies_diffnames
[diff
], p
->w
, p
->h
, desc
);
3291 static void soak(game_params
*p
, random_state
*rs
)
3293 time_t tt_start
, tt_now
, tt_last
;
3296 int diff
, n
= 0, i
, diffs
[DIFF_MAX
], ndots
= 0, nspaces
= 0;
3299 solver_show_working
= 0;
3301 tt_start
= tt_now
= time(NULL
);
3302 for (i
= 0; i
< DIFF_MAX
; i
++) diffs
[i
] = 0;
3305 printf("Soak-generating a %dx%d grid, max. diff %s.\n",
3306 p
->w
, p
->h
, galaxies_diffnames
[p
->diff
]);
3308 for (i
= 0; i
< DIFF_MAX
; i
++)
3309 printf("%s%s", (i
== 0) ?
"" : ", ", galaxies_diffnames
[i
]);
3313 desc
= new_game_desc(p
, rs
, NULL
, 0);
3314 st
= new_game(NULL
, p
, desc
);
3315 diff
= solver_state(st
, p
->diff
);
3316 nspaces
+= st
->w
*st
->h
;
3317 for (i
= 0; i
< st
->sx
*st
->sy
; i
++)
3318 if (st
->grid
[i
].flags
& F_DOT
) ndots
++;
3324 tt_last
= time(NULL
);
3325 if (tt_last
> tt_now
) {
3327 printf("%d total, %3.1f/s, [",
3328 n
, (double)n
/ ((double)tt_now
- tt_start
));
3329 for (i
= 0; i
< DIFF_MAX
; i
++)
3330 printf("%s%.1f%%", (i
== 0) ?
"" : ", ",
3331 100.0 * ((double)diffs
[i
] / (double)n
));
3332 printf("], %.1f%% dots\n",
3333 100.0 * ((double)ndots
/ (double)nspaces
));
3338 int main(int argc
, char **argv
)
3341 char *id
= NULL
, *desc
, *err
;
3343 int diff
, do_soak
= 0, verbose
= 0;
3345 time_t seed
= time(NULL
);
3348 while (--argc
> 0) {
3350 if (!strcmp(p
, "-v")) {
3352 } else if (!strcmp(p
, "--seed")) {
3353 if (argc
== 0) usage_exit("--seed needs an argument");
3354 seed
= (time_t)atoi(*++argv
);
3356 } else if (!strcmp(p
, "--soak")) {
3358 } else if (*p
== '-') {
3359 usage_exit("unrecognised option");
3367 p
= default_params();
3368 rs
= random_new((void*)&seed
, sizeof(time_t));
3371 if (!id
) usage_exit("need one argument for --soak");
3372 decode_params(p
, *argv
);
3379 p
->w
= random_upto(rs
, 15) + 3;
3380 p
->h
= random_upto(rs
, 15) + 3;
3381 p
->diff
= random_upto(rs
, DIFF_RECURSIVE
);
3382 diff
= gen(p
, rs
, 0);
3387 desc
= strchr(id
, ':');
3389 decode_params(p
, id
);
3390 gen(p
, rs
, verbose
);
3393 solver_show_working
= 1;
3396 decode_params(p
, id
);
3397 err
= validate_desc(p
, desc
);
3399 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
3402 s
= new_game(NULL
, p
, desc
);
3403 diff
= solver_state(s
, DIFF_RECURSIVE
);
3405 printf("Puzzle is %s.\n", galaxies_diffnames
[diff
]);
3416 /* vim: set shiftwidth=4 tabstop=8: */