2 * loopy.c: An implementation of the Nikoli game 'Loop the loop'.
5 * vim: set shiftwidth=4 :set textwidth=80:
11 * - setting very high recursion depth seems to cause memory
12 * munching: are we recursing before checking completion, by any
15 * - there's an interesting deductive technique which makes use of
16 * topology rather than just graph theory. Each _square_ in the
17 * grid is either inside or outside the loop; you can tell that
18 * two squares are on the same side of the loop if they're
19 * separated by an x (or, more generally, by a path crossing no
20 * LINE_UNKNOWNs and an even number of LINE_YESes), and on the
21 * opposite side of the loop if they're separated by a line (or
22 * an odd number of LINE_YESes and no LINE_UNKNOWNs). Oh, and
23 * any square separated from the outside of the grid by a
24 * LINE_YES or a LINE_NO is on the inside or outside
25 * respectively. So if you can track this for all squares, you
26 * can occasionally spot that two squares are separated by a
27 * LINE_UNKNOWN but their relative insideness is known, and
28 * therefore deduce the state of the edge between them.
29 * + An efficient way to track this would be by augmenting the
30 * disjoint set forest data structure. Each element, along
31 * with a pointer to a parent member of its equivalence
32 * class, would also carry a one-bit field indicating whether
33 * it was equal or opposite to its parent. Then you could
34 * keep flipping a bit as you ascended the tree during
35 * dsf_canonify(), and hence you'd be able to return the
36 * relationship of the input value to its ultimate parent
37 * (and also you could then get all those bits right when you
38 * went back up the tree rewriting). So you'd be able to
39 * query whether any two elements were known-equal,
40 * known-opposite, or not-known, and you could add new
41 * equalities or oppositenesses to increase your knowledge.
42 * (Of course the algorithm would have to fail an assertion
43 * if you tried to tell it two things it already knew to be
44 * opposite were equal, or vice versa!)
45 * This data structure would also be useful in the
46 * graph-theoretic part of the solver, where it could be used
47 * for storing information about which lines are known-identical
48 * or known-opposite. (For example if two lines bordering a 3
49 * are known-identical they must both be LINE_YES, and if they
50 * are known-opposite, the *other* two lines bordering that clue
51 * must be LINE_YES, etc). This may duplicate some
52 * functionality already present in the solver but it is more
53 * general and we could remove the old code, so that's no bad
67 #define PREFERRED_TILE_SIZE 32
68 #define TILE_SIZE (ds->tilesize)
69 #define LINEWIDTH TILE_SIZE / 16
70 #define BORDER (TILE_SIZE / 2)
72 #define FLASH_TIME 0.5F
74 #define HL_COUNT(state) ((state)->w * ((state)->h + 1))
75 #define VL_COUNT(state) (((state)->w + 1) * (state)->h)
76 #define DOT_COUNT(state) (((state)->w + 1) * ((state)->h + 1))
77 #define SQUARE_COUNT(state) ((state)->w * (state)->h)
79 #define ABOVE_SQUARE(state, i, j) ((state)->hl[(i) + (state)->w * (j)])
80 #define BELOW_SQUARE(state, i, j) ABOVE_SQUARE(state, i, (j)+1)
82 #define LEFTOF_SQUARE(state, i, j) ((state)->vl[(i) + ((state)->w + 1) * (j)])
83 #define RIGHTOF_SQUARE(state, i, j) LEFTOF_SQUARE(state, (i)+1, j)
85 #define LEGAL_DOT(state, i, j) ((i) >= 0 && (j) >= 0 && \
86 (i) <= (state)->w && (j) <= (state)->h)
89 * These macros return rvalues only, but can cope with being passed
90 * out-of-range coordinates.
92 #define ABOVE_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || j <= 0) ? \
93 LINE_NO : LV_ABOVE_DOT(state, i, j))
94 #define BELOW_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || j >= (state)->h) ? \
95 LINE_NO : LV_BELOW_DOT(state, i, j))
97 #define LEFTOF_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || i <= 0) ? \
98 LINE_NO : LV_LEFTOF_DOT(state, i, j))
99 #define RIGHTOF_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || i >= (state)->w)?\
100 LINE_NO : LV_RIGHTOF_DOT(state, i, j))
103 * These macros expect to be passed valid coordinates, and return
106 #define LV_BELOW_DOT(state, i, j) ((state)->vl[(i) + ((state)->w + 1) * (j)])
107 #define LV_ABOVE_DOT(state, i, j) LV_BELOW_DOT(state, i, (j)-1)
109 #define LV_RIGHTOF_DOT(state, i, j) ((state)->hl[(i) + (state)->w * (j)])
110 #define LV_LEFTOF_DOT(state, i, j) LV_RIGHTOF_DOT(state, (i)-1, j)
112 #define CLUE_AT(state, i, j) ((i < 0 || i >= (state)->w || \
113 j < 0 || j >= (state)->h) ? \
114 ' ' : LV_CLUE_AT(state, i, j))
116 #define LV_CLUE_AT(state, i, j) ((state)->clues[(i) + (state)->w * (j)])
118 #define OPP(dir) (dir == LINE_UNKNOWN ? LINE_UNKNOWN : \
119 dir == LINE_YES ? LINE_NO : LINE_YES)
121 static char *game_text_format(game_state
*state
);
132 * Difficulty levels. I do some macro ickery here to ensure that my
133 * enum and the various forms of my name list always match up.
135 #define DIFFLIST(A) \
138 #define ENUM(upper,title,lower) DIFF_ ## upper,
139 #define TITLE(upper,title,lower) #title,
140 #define ENCODE(upper,title,lower) #lower
141 #define CONFIG(upper,title,lower) ":" #title
142 enum { DIFFLIST(ENUM
) DIFFCOUNT
};
143 static char const *const loopy_diffnames
[] = { DIFFLIST(TITLE
) };
144 static char const loopy_diffchars
[] = DIFFLIST(ENCODE
);
145 #define DIFFCONFIG DIFFLIST(CONFIG)
147 /* LINE_YES_ERROR is only used in the drawing routine */
148 enum line_state
{ LINE_UNKNOWN
, LINE_YES
, LINE_NO
/*, LINE_YES_ERROR*/ };
150 enum direction
{ UP
, DOWN
, LEFT
, RIGHT
};
159 /* Put ' ' in a square that doesn't get a clue */
162 /* Arrays of line states, stored left-to-right, top-to-bottom */
171 static game_state
*dup_game(game_state
*state
)
173 game_state
*ret
= snew(game_state
);
177 ret
->solved
= state
->solved
;
178 ret
->cheated
= state
->cheated
;
180 ret
->clues
= snewn(SQUARE_COUNT(state
), char);
181 memcpy(ret
->clues
, state
->clues
, SQUARE_COUNT(state
));
183 ret
->hl
= snewn(HL_COUNT(state
), char);
184 memcpy(ret
->hl
, state
->hl
, HL_COUNT(state
));
186 ret
->vl
= snewn(VL_COUNT(state
), char);
187 memcpy(ret
->vl
, state
->vl
, VL_COUNT(state
));
189 ret
->recursion_depth
= state
->recursion_depth
;
194 static void free_game(game_state
*state
)
205 SOLVER_SOLVED
, /* This is the only solution the solver could find */
206 SOLVER_MISTAKE
, /* This is definitely not a solution */
207 SOLVER_AMBIGUOUS
, /* This _might_ be an ambiguous solution */
208 SOLVER_INCOMPLETE
/* This may be a partial solution */
211 typedef struct solver_state
{
213 /* XXX dot_atleastone[i,j, dline] is equivalent to */
214 /* dot_atmostone[i,j,OPP_DLINE(dline)] */
215 char *dot_atleastone
;
217 /* char *dline_identical; */
218 int recursion_remaining
;
219 enum solver_status solver_status
;
220 int *dotdsf
, *looplen
;
223 static solver_state
*new_solver_state(game_state
*state
) {
224 solver_state
*ret
= snew(solver_state
);
227 ret
->state
= dup_game(state
);
229 ret
->dot_atmostone
= snewn(DOT_COUNT(state
), char);
230 memset(ret
->dot_atmostone
, 0, DOT_COUNT(state
));
231 ret
->dot_atleastone
= snewn(DOT_COUNT(state
), char);
232 memset(ret
->dot_atleastone
, 0, DOT_COUNT(state
));
235 dline_identical
= snewn(DOT_COUNT(state
), char);
236 memset(dline_identical
, 0, DOT_COUNT(state
));
239 ret
->recursion_remaining
= state
->recursion_depth
;
240 ret
->solver_status
= SOLVER_INCOMPLETE
; /* XXX This may be a lie */
242 ret
->dotdsf
= snewn(DOT_COUNT(state
), int);
243 ret
->looplen
= snewn(DOT_COUNT(state
), int);
244 for (i
= 0; i
< DOT_COUNT(state
); i
++) {
252 static void free_solver_state(solver_state
*sstate
) {
254 free_game(sstate
->state
);
255 sfree(sstate
->dot_atleastone
);
256 sfree(sstate
->dot_atmostone
);
257 /* sfree(sstate->dline_identical); */
258 sfree(sstate
->dotdsf
);
259 sfree(sstate
->looplen
);
264 static solver_state
*dup_solver_state(solver_state
*sstate
) {
267 solver_state
*ret
= snew(solver_state
);
269 ret
->state
= state
= dup_game(sstate
->state
);
271 ret
->dot_atmostone
= snewn(DOT_COUNT(state
), char);
272 memcpy(ret
->dot_atmostone
, sstate
->dot_atmostone
, DOT_COUNT(state
));
274 ret
->dot_atleastone
= snewn(DOT_COUNT(state
), char);
275 memcpy(ret
->dot_atleastone
, sstate
->dot_atleastone
, DOT_COUNT(state
));
278 ret
->dline_identical
= snewn((state
->w
+ 1) * (state
->h
+ 1), char);
279 memcpy(ret
->dline_identical
, state
->dot_atmostone
,
280 (state
->w
+ 1) * (state
->h
+ 1));
283 ret
->recursion_remaining
= sstate
->recursion_remaining
;
284 ret
->solver_status
= sstate
->solver_status
;
286 ret
->dotdsf
= snewn(DOT_COUNT(state
), int);
287 ret
->looplen
= snewn(DOT_COUNT(state
), int);
288 memcpy(ret
->dotdsf
, sstate
->dotdsf
, DOT_COUNT(state
) * sizeof(int));
289 memcpy(ret
->looplen
, sstate
->looplen
, DOT_COUNT(state
) * sizeof(int));
295 * Merge two dots due to the existence of an edge between them.
296 * Updates the dsf tracking equivalence classes, and keeps track of
297 * the length of path each dot is currently a part of.
299 static void merge_dots(solver_state
*sstate
, int x1
, int y1
, int x2
, int y2
)
303 i
= y1
* (sstate
->state
->w
+ 1) + x1
;
304 j
= y2
* (sstate
->state
->w
+ 1) + x2
;
306 i
= dsf_canonify(sstate
->dotdsf
, i
);
307 j
= dsf_canonify(sstate
->dotdsf
, j
);
310 len
= sstate
->looplen
[i
] + sstate
->looplen
[j
];
311 dsf_merge(sstate
->dotdsf
, i
, j
);
312 i
= dsf_canonify(sstate
->dotdsf
, i
);
313 sstate
->looplen
[i
] = len
;
317 /* Count the number of lines of a particular type currently going into the
318 * given dot. Lines going off the edge of the board are assumed fixed no. */
319 static int dot_order(const game_state
* state
, int i
, int j
, char line_type
)
324 if (LEFTOF_DOT(state
, i
, j
) == line_type
)
327 if (line_type
== LINE_NO
)
331 if (RIGHTOF_DOT(state
, i
, j
) == line_type
)
334 if (line_type
== LINE_NO
)
338 if (ABOVE_DOT(state
, i
, j
) == line_type
)
341 if (line_type
== LINE_NO
)
345 if (BELOW_DOT(state
, i
, j
) == line_type
)
348 if (line_type
== LINE_NO
)
354 /* Count the number of lines of a particular type currently surrounding the
356 static int square_order(const game_state
* state
, int i
, int j
, char line_type
)
360 if (ABOVE_SQUARE(state
, i
, j
) == line_type
)
362 if (BELOW_SQUARE(state
, i
, j
) == line_type
)
364 if (LEFTOF_SQUARE(state
, i
, j
) == line_type
)
366 if (RIGHTOF_SQUARE(state
, i
, j
) == line_type
)
372 /* Set all lines bordering a dot of type old_type to type new_type */
373 static void dot_setall(game_state
*state
, int i
, int j
,
374 char old_type
, char new_type
)
376 /* printf("dot_setall([%d,%d], %d, %d)\n", i, j, old_type, new_type); */
377 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == old_type
)
378 LV_LEFTOF_DOT(state
, i
, j
) = new_type
;
379 if (i
< state
->w
&& RIGHTOF_DOT(state
, i
, j
) == old_type
)
380 LV_RIGHTOF_DOT(state
, i
, j
) = new_type
;
381 if (j
> 0 && ABOVE_DOT(state
, i
, j
) == old_type
)
382 LV_ABOVE_DOT(state
, i
, j
) = new_type
;
383 if (j
< state
->h
&& BELOW_DOT(state
, i
, j
) == old_type
)
384 LV_BELOW_DOT(state
, i
, j
) = new_type
;
386 /* Set all lines bordering a square of type old_type to type new_type */
387 static void square_setall(game_state
*state
, int i
, int j
,
388 char old_type
, char new_type
)
390 if (ABOVE_SQUARE(state
, i
, j
) == old_type
)
391 ABOVE_SQUARE(state
, i
, j
) = new_type
;
392 if (BELOW_SQUARE(state
, i
, j
) == old_type
)
393 BELOW_SQUARE(state
, i
, j
) = new_type
;
394 if (LEFTOF_SQUARE(state
, i
, j
) == old_type
)
395 LEFTOF_SQUARE(state
, i
, j
) = new_type
;
396 if (RIGHTOF_SQUARE(state
, i
, j
) == old_type
)
397 RIGHTOF_SQUARE(state
, i
, j
) = new_type
;
400 static game_params
*default_params(void)
402 game_params
*ret
= snew(game_params
);
411 ret
->diff
= DIFF_EASY
;
417 static game_params
*dup_params(game_params
*params
)
419 game_params
*ret
= snew(game_params
);
420 *ret
= *params
; /* structure copy */
424 static const struct {
427 } loopy_presets
[] = {
428 { "4x4 Easy", { 4, 4, DIFF_EASY
, 0 } },
429 { "4x4 Normal", { 4, 4, DIFF_NORMAL
, 0 } },
430 { "7x7 Easy", { 7, 7, DIFF_EASY
, 0 } },
431 { "7x7 Normal", { 7, 7, DIFF_NORMAL
, 0 } },
432 { "10x10 Easy", { 10, 10, DIFF_EASY
, 0 } },
434 { "10x10 Normal", { 10, 10, DIFF_NORMAL
, 0 } },
435 { "15x15 Easy", { 15, 15, DIFF_EASY
, 0 } },
436 { "30x20 Easy", { 30, 20, DIFF_EASY
, 0 } }
440 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
444 if (i
< 0 || i
>= lenof(loopy_presets
))
447 tmppar
= loopy_presets
[i
].params
;
448 *params
= dup_params(&tmppar
);
449 *name
= dupstr(loopy_presets
[i
].desc
);
454 static void free_params(game_params
*params
)
459 static void decode_params(game_params
*params
, char const *string
)
461 params
->h
= params
->w
= atoi(string
);
463 params
->diff
= DIFF_EASY
;
464 while (*string
&& isdigit((unsigned char)*string
)) string
++;
465 if (*string
== 'x') {
467 params
->h
= atoi(string
);
468 while (*string
&& isdigit((unsigned char)*string
)) string
++;
470 if (*string
== 'r') {
472 params
->rec
= atoi(string
);
473 while (*string
&& isdigit((unsigned char)*string
)) string
++;
475 if (*string
== 'd') {
479 for (i
= 0; i
< DIFFCOUNT
; i
++)
480 if (*string
== loopy_diffchars
[i
])
482 if (*string
) string
++;
486 static char *encode_params(game_params
*params
, int full
)
489 sprintf(str
, "%dx%d", params
->w
, params
->h
);
491 sprintf(str
+ strlen(str
), "r%dd%c", params
->rec
,
492 loopy_diffchars
[params
->diff
]);
496 static config_item
*game_configure(game_params
*params
)
501 ret
= snewn(4, config_item
);
503 ret
[0].name
= "Width";
504 ret
[0].type
= C_STRING
;
505 sprintf(buf
, "%d", params
->w
);
506 ret
[0].sval
= dupstr(buf
);
509 ret
[1].name
= "Height";
510 ret
[1].type
= C_STRING
;
511 sprintf(buf
, "%d", params
->h
);
512 ret
[1].sval
= dupstr(buf
);
515 ret
[2].name
= "Difficulty";
516 ret
[2].type
= C_CHOICES
;
517 ret
[2].sval
= DIFFCONFIG
;
518 ret
[2].ival
= params
->diff
;
528 static game_params
*custom_params(config_item
*cfg
)
530 game_params
*ret
= snew(game_params
);
532 ret
->w
= atoi(cfg
[0].sval
);
533 ret
->h
= atoi(cfg
[1].sval
);
535 ret
->diff
= cfg
[2].ival
;
540 static char *validate_params(game_params
*params
, int full
)
542 if (params
->w
< 4 || params
->h
< 4)
543 return "Width and height must both be at least 4";
545 return "Recursion depth can't be negative";
548 * This shouldn't be able to happen at all, since decode_params
549 * and custom_params will never generate anything that isn't
552 assert(params
->diff
>= 0 && params
->diff
< DIFFCOUNT
);
557 /* We're going to store a list of current candidate squares for lighting.
558 * Each square gets a 'score', which tells us how adding that square right
559 * now would affect the length of the solution loop. We're trying to
560 * maximise that quantity so will bias our random selection of squares to
561 * light towards those with high scores */
564 unsigned long random
;
568 static int get_square_cmpfn(void *v1
, void *v2
)
570 struct square
*s1
= (struct square
*)v1
;
571 struct square
*s2
= (struct square
*)v2
;
585 static int square_sort_cmpfn(void *v1
, void *v2
)
587 struct square
*s1
= (struct square
*)v1
;
588 struct square
*s2
= (struct square
*)v2
;
591 r
= s2
->score
- s1
->score
;
596 if (s1
->random
< s2
->random
)
598 else if (s1
->random
> s2
->random
)
602 * It's _just_ possible that two squares might have been given
603 * the same random value. In that situation, fall back to
604 * comparing based on the coordinates. This introduces a tiny
605 * directional bias, but not a significant one.
607 return get_square_cmpfn(v1
, v2
);
610 static void print_tree(tree234
*tree
)
615 printf("Print tree:\n");
616 while (i
< count234(tree
)) {
617 s
= (struct square
*)index234(tree
, i
);
619 printf(" [%d,%d], %d, %d\n", s
->x
, s
->y
, s
->score
, s
->random
);
625 enum { SQUARE_LIT
, SQUARE_UNLIT
};
627 #define SQUARE_STATE(i, j) \
628 (((i) < 0 || (i) >= params->w || \
629 (j) < 0 || (j) >= params->h) ? \
630 SQUARE_UNLIT : LV_SQUARE_STATE(i,j))
632 #define LV_SQUARE_STATE(i, j) board[(i) + params->w * (j)]
634 static void print_board(const game_params
*params
, const char *board
)
640 for (i
= 0; i
< params
->w
; i
++) {
644 for (j
= 0; j
< params
->h
; j
++) {
646 for (i
= 0; i
< params
->w
; i
++) {
647 printf("%c", SQUARE_STATE(i
, j
) ?
' ' : 'O');
654 static char *new_fullyclued_board(game_params
*params
, random_state
*rs
)
660 game_state
*state
= &s
;
661 int board_area
= SQUARE_COUNT(params
);
664 struct square
*square
, *tmpsquare
, *sq
;
665 struct square square_pos
;
667 /* These will contain exactly the same information, sorted into different
669 tree234
*lightable_squares_sorted
, *lightable_squares_gettable
;
671 #define SQUARE_REACHABLE(i,j) \
672 (t = (SQUARE_STATE(i-1, j) == SQUARE_LIT || \
673 SQUARE_STATE(i+1, j) == SQUARE_LIT || \
674 SQUARE_STATE(i, j-1) == SQUARE_LIT || \
675 SQUARE_STATE(i, j+1) == SQUARE_LIT), \
676 /* printf("SQUARE_REACHABLE(%d,%d) = %d\n", i, j, t), */ \
680 /* One situation in which we may not light a square is if that'll leave one
681 * square above/below and one left/right of us unlit, separated by a lit
682 * square diagnonal from us */
683 #define SQUARE_DIAGONAL_VIOLATION(i, j, h, v) \
684 (t = (SQUARE_STATE((i)+(h), (j)) == SQUARE_UNLIT && \
685 SQUARE_STATE((i), (j)+(v)) == SQUARE_UNLIT && \
686 SQUARE_STATE((i)+(h), (j)+(v)) == SQUARE_LIT), \
687 /* t ? printf("SQUARE_DIAGONAL_VIOLATION(%d, %d, %d, %d)\n",
691 /* We also may not light a square if it will form a loop of lit squares
692 * around some unlit squares, as then the game soln won't have a single
694 #define SQUARE_LOOP_VIOLATION(i, j, lit1, lit2) \
695 (SQUARE_STATE((i)+1, (j)) == lit1 && \
696 SQUARE_STATE((i)-1, (j)) == lit1 && \
697 SQUARE_STATE((i), (j)+1) == lit2 && \
698 SQUARE_STATE((i), (j)-1) == lit2)
700 #define CAN_LIGHT_SQUARE(i, j) \
701 (SQUARE_REACHABLE(i, j) && \
702 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, -1) && \
703 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, -1) && \
704 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, +1) && \
705 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, +1) && \
706 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_LIT, SQUARE_UNLIT) && \
707 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_UNLIT, SQUARE_LIT))
709 #define IS_LIGHTING_CANDIDATE(i, j) \
710 (SQUARE_STATE(i, j) == SQUARE_UNLIT && \
711 CAN_LIGHT_SQUARE(i,j))
713 /* The 'score' of a square reflects its current desirability for selection
714 * as the next square to light. We want to encourage moving into uncharted
715 * areas so we give scores according to how many of the square's neighbours
716 * are currently unlit. */
723 #define SQUARE_SCORE(i,j) \
724 (2*((SQUARE_STATE(i-1, j) == SQUARE_UNLIT) + \
725 (SQUARE_STATE(i+1, j) == SQUARE_UNLIT) + \
726 (SQUARE_STATE(i, j-1) == SQUARE_UNLIT) + \
727 (SQUARE_STATE(i, j+1) == SQUARE_UNLIT)) - 4)
729 /* When a square gets lit, this defines how far away from that square we
730 * need to go recomputing scores */
731 #define SCORE_DISTANCE 1
733 board
= snewn(board_area
, char);
734 clues
= snewn(board_area
, char);
736 state
->h
= params
->h
;
737 state
->w
= params
->w
;
738 state
->clues
= clues
;
741 memset(board
, SQUARE_UNLIT
, board_area
);
743 /* Seed the board with a single lit square near the middle */
746 if (params
->w
& 1 && random_bits(rs
, 1))
748 if (params
->h
& 1 && random_bits(rs
, 1))
751 LV_SQUARE_STATE(i
, j
) = SQUARE_LIT
;
753 /* We need a way of favouring squares that will increase our loopiness.
754 * We do this by maintaining a list of all candidate squares sorted by
755 * their score and choose randomly from that with appropriate skew.
756 * In order to avoid consistently biasing towards particular squares, we
757 * need the sort order _within_ each group of scores to be completely
758 * random. But it would be abusing the hospitality of the tree234 data
759 * structure if our comparison function were nondeterministic :-). So with
760 * each square we associate a random number that does not change during a
761 * particular run of the generator, and use that as a secondary sort key.
762 * Yes, this means we will be biased towards particular random squares in
763 * any one run but that doesn't actually matter. */
765 lightable_squares_sorted
= newtree234(square_sort_cmpfn
);
766 lightable_squares_gettable
= newtree234(get_square_cmpfn
);
767 #define ADD_SQUARE(s) \
769 /* printf("ADD SQUARE: [%d,%d], %d, %d\n",
770 s->x, s->y, s->score, s->random);*/ \
771 sq = add234(lightable_squares_sorted, s); \
773 sq = add234(lightable_squares_gettable, s); \
777 #define REMOVE_SQUARE(s) \
779 /* printf("DELETE SQUARE: [%d,%d], %d, %d\n",
780 s->x, s->y, s->score, s->random);*/ \
781 sq = del234(lightable_squares_sorted, s); \
783 sq = del234(lightable_squares_gettable, s); \
787 #define HANDLE_DIR(a, b) \
788 square = snew(struct square); \
789 square->x = (i)+(a); \
790 square->y = (j)+(b); \
792 square->random = random_bits(rs, 31); \
800 /* Light squares one at a time until the board is interesting enough */
803 /* We have count234(lightable_squares) possibilities, and in
804 * lightable_squares_sorted they are sorted with the most desirable
806 c
= count234(lightable_squares_sorted
);
809 assert(c
== count234(lightable_squares_gettable
));
811 /* Check that the best square available is any good */
812 square
= (struct square
*)index234(lightable_squares_sorted
, 0);
816 * We never want to _decrease_ the loop's perimeter. Making
817 * moves that leave the perimeter the same is occasionally
818 * useful: if it were _never_ done then the user would be
819 * able to deduce illicitly that any degree-zero vertex was
820 * on the outside of the loop. So we do it sometimes but
823 if (square
->score
< 0 || (square
->score
== 0 &&
824 random_upto(rs
, 2) == 0))
827 print_tree(lightable_squares_sorted
);
828 assert(square
->score
== SQUARE_SCORE(square
->x
, square
->y
));
829 assert(SQUARE_STATE(square
->x
, square
->y
) == SQUARE_UNLIT
);
830 assert(square
->x
>= 0 && square
->x
< params
->w
);
831 assert(square
->y
>= 0 && square
->y
< params
->h
);
832 /* printf("LIGHT SQUARE: [%d,%d], score = %d\n", square->x, square->y, square->score); */
834 /* Update data structures */
835 LV_SQUARE_STATE(square
->x
, square
->y
) = SQUARE_LIT
;
836 REMOVE_SQUARE(square
);
838 print_board(params
, board
);
840 /* We might have changed the score of any squares up to 2 units away in
842 for (b
= -SCORE_DISTANCE
; b
<= SCORE_DISTANCE
; b
++) {
843 for (a
= -SCORE_DISTANCE
; a
<= SCORE_DISTANCE
; a
++) {
846 square_pos
.x
= square
->x
+ a
;
847 square_pos
.y
= square
->y
+ b
;
848 /* printf("Refreshing score for [%d,%d]:\n", square_pos.x, square_pos.y); */
849 if (square_pos
.x
< 0 || square_pos
.x
>= params
->w
||
850 square_pos
.y
< 0 || square_pos
.y
>= params
->h
) {
851 /* printf(" Out of bounds\n"); */
854 tmpsquare
= find234(lightable_squares_gettable
, &square_pos
,
857 /* printf(" Removing\n"); */
858 assert(tmpsquare
->x
== square_pos
.x
);
859 assert(tmpsquare
->y
== square_pos
.y
);
860 assert(SQUARE_STATE(tmpsquare
->x
, tmpsquare
->y
) ==
862 REMOVE_SQUARE(tmpsquare
);
864 /* printf(" Creating\n"); */
865 tmpsquare
= snew(struct square
);
866 tmpsquare
->x
= square_pos
.x
;
867 tmpsquare
->y
= square_pos
.y
;
868 tmpsquare
->random
= random_bits(rs
, 31);
870 tmpsquare
->score
= SQUARE_SCORE(tmpsquare
->x
, tmpsquare
->y
);
872 if (IS_LIGHTING_CANDIDATE(tmpsquare
->x
, tmpsquare
->y
)) {
873 /* printf(" Adding\n"); */
874 ADD_SQUARE(tmpsquare
);
876 /* printf(" Destroying\n"); */
882 /* printf("\n\n"); */
885 while ((square
= delpos234(lightable_squares_gettable
, 0)) != NULL
)
887 freetree234(lightable_squares_gettable
);
888 freetree234(lightable_squares_sorted
);
890 /* Copy out all the clues */
891 for (j
= 0; j
< params
->h
; ++j
) {
892 for (i
= 0; i
< params
->w
; ++i
) {
893 c
= SQUARE_STATE(i
, j
);
894 LV_CLUE_AT(state
, i
, j
) = '0';
895 if (SQUARE_STATE(i
-1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
896 if (SQUARE_STATE(i
+1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
897 if (SQUARE_STATE(i
, j
-1) != c
) ++LV_CLUE_AT(state
, i
, j
);
898 if (SQUARE_STATE(i
, j
+1) != c
) ++LV_CLUE_AT(state
, i
, j
);
906 static solver_state
*solve_game_rec(const solver_state
*sstate
, int diff
);
908 static int game_has_unique_soln(const game_state
*state
, int diff
)
911 solver_state
*sstate_new
;
912 solver_state
*sstate
= new_solver_state((game_state
*)state
);
914 sstate_new
= solve_game_rec(sstate
, diff
);
916 ret
= (sstate_new
->solver_status
== SOLVER_SOLVED
);
918 free_solver_state(sstate_new
);
919 free_solver_state(sstate
);
924 /* Remove clues one at a time at random. */
925 static game_state
*remove_clues(game_state
*state
, random_state
*rs
, int diff
)
927 int *square_list
, squares
;
928 game_state
*ret
= dup_game(state
), *saved_ret
;
931 /* We need to remove some clues. We'll do this by forming a list of all
932 * available equivalence classes, shuffling it, then going along one at a
933 * time clearing every member of each equivalence class, where removing a
934 * class doesn't render the board unsolvable. */
935 squares
= state
->w
* state
->h
;
936 square_list
= snewn(squares
, int);
937 for (n
= 0; n
< squares
; ++n
) {
941 shuffle(square_list
, squares
, sizeof(int), rs
);
943 for (n
= 0; n
< squares
; ++n
) {
944 saved_ret
= dup_game(ret
);
945 LV_CLUE_AT(ret
, square_list
[n
] % state
->w
,
946 square_list
[n
] / state
->w
) = ' ';
947 if (game_has_unique_soln(ret
, diff
)) {
948 free_game(saved_ret
);
959 static char *validate_desc(game_params
*params
, char *desc
);
961 static char *new_game_desc(game_params
*params
, random_state
*rs
,
962 char **aux
, int interactive
)
964 /* solution and description both use run-length encoding in obvious ways */
966 char *description
= snewn(SQUARE_COUNT(params
) + 1, char);
967 char *dp
= description
;
970 game_state
*state
= snew(game_state
), *state_new
;
972 state
->h
= params
->h
;
973 state
->w
= params
->w
;
975 state
->hl
= snewn(HL_COUNT(params
), char);
976 state
->vl
= snewn(VL_COUNT(params
), char);
979 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
980 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
982 state
->solved
= state
->cheated
= FALSE
;
983 state
->recursion_depth
= params
->rec
;
985 /* Get a new random solvable board with all its clues filled in. Yes, this
986 * can loop for ever if the params are suitably unfavourable, but
987 * preventing games smaller than 4x4 seems to stop this happening */
990 state
->clues
= new_fullyclued_board(params
, rs
);
991 } while (!game_has_unique_soln(state
, params
->diff
));
993 state_new
= remove_clues(state
, rs
, params
->diff
);
997 if (params
->diff
> 0 && game_has_unique_soln(state
, params
->diff
-1)) {
998 /* Board is too easy */
999 goto newboard_please
;
1003 for (j
= 0; j
< params
->h
; ++j
) {
1004 for (i
= 0; i
< params
->w
; ++i
) {
1005 if (CLUE_AT(state
, i
, j
) == ' ') {
1006 if (empty_count
> 25) {
1007 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
1013 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
1016 dp
+= sprintf(dp
, "%c", (int)(CLUE_AT(state
, i
, j
)));
1021 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
1024 retval
= dupstr(description
);
1027 assert(!validate_desc(params
, retval
));
1032 /* We require that the params pass the test in validate_params and that the
1033 * description fills the entire game area */
1034 static char *validate_desc(game_params
*params
, char *desc
)
1038 for (; *desc
; ++desc
) {
1039 if (*desc
>= '0' && *desc
<= '9') {
1044 count
+= *desc
- 'a' + 1;
1047 return "Unknown character in description";
1050 if (count
< SQUARE_COUNT(params
))
1051 return "Description too short for board size";
1052 if (count
> SQUARE_COUNT(params
))
1053 return "Description too long for board size";
1058 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1061 game_state
*state
= snew(game_state
);
1062 int empties_to_make
= 0;
1064 const char *dp
= desc
;
1066 state
->recursion_depth
= 0; /* XXX pending removal, probably */
1068 state
->h
= params
->h
;
1069 state
->w
= params
->w
;
1071 state
->clues
= snewn(SQUARE_COUNT(params
), char);
1072 state
->hl
= snewn(HL_COUNT(params
), char);
1073 state
->vl
= snewn(VL_COUNT(params
), char);
1075 state
->solved
= state
->cheated
= FALSE
;
1077 for (j
= 0 ; j
< params
->h
; ++j
) {
1078 for (i
= 0 ; i
< params
->w
; ++i
) {
1079 if (empties_to_make
) {
1081 LV_CLUE_AT(state
, i
, j
) = ' ';
1087 if (n
>=0 && n
< 10) {
1088 LV_CLUE_AT(state
, i
, j
) = *dp
;
1092 LV_CLUE_AT(state
, i
, j
) = ' ';
1093 empties_to_make
= n
- 1;
1099 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
1100 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
1105 enum { LOOP_NONE
=0, LOOP_SOLN
, LOOP_NOT_SOLN
};
1107 /* Starting at dot [i,j] moves around 'state' removing lines until it's clear
1108 * whether or not the starting dot was on a loop. Returns boolean specifying
1109 * whether a loop was found. loop_status calls this and assumes that if state
1110 * has any lines set, this function will always remove at least one. */
1111 static int destructively_find_loop(game_state
*state
)
1113 int a
, b
, i
, j
, new_i
, new_j
, n
;
1116 lp
= (char *)memchr(state
->hl
, LINE_YES
, HL_COUNT(state
));
1118 /* We know we're going to return false but we have to fulfil our
1120 lp
= (char *)memchr(state
->vl
, LINE_YES
, VL_COUNT(state
));
1132 assert(i
+ j
* state
->w
== n
); /* because I'm feeling stupid */
1133 /* Save start position */
1137 /* Delete one line from the potential loop */
1138 if (LEFTOF_DOT(state
, i
, j
) == LINE_YES
) {
1139 LV_LEFTOF_DOT(state
, i
, j
) = LINE_NO
;
1141 } else if (ABOVE_DOT(state
, i
, j
) == LINE_YES
) {
1142 LV_ABOVE_DOT(state
, i
, j
) = LINE_NO
;
1144 } else if (RIGHTOF_DOT(state
, i
, j
) == LINE_YES
) {
1145 LV_RIGHTOF_DOT(state
, i
, j
) = LINE_NO
;
1147 } else if (BELOW_DOT(state
, i
, j
) == LINE_YES
) {
1148 LV_BELOW_DOT(state
, i
, j
) = LINE_NO
;
1155 /* From the current position of [i,j] there needs to be exactly one
1159 #define HANDLE_DIR(dir_dot, x, y) \
1160 if (dir_dot(state, i, j) == LINE_YES) { \
1161 if (new_i != -1 || new_j != -1) \
1165 LV_##dir_dot(state, i, j) = LINE_NO; \
1167 HANDLE_DIR(ABOVE_DOT
, 0, -1);
1168 HANDLE_DIR(BELOW_DOT
, 0, +1);
1169 HANDLE_DIR(LEFTOF_DOT
, -1, 0);
1170 HANDLE_DIR(RIGHTOF_DOT
, +1, 0);
1172 if (new_i
== -1 || new_j
== -1) {
1178 } while (i
!= a
|| j
!= b
);
1183 static int loop_status(game_state
*state
)
1186 game_state
*tmpstate
;
1187 int loop_found
= FALSE
, non_loop_found
= FALSE
, any_lines_found
= FALSE
;
1189 #define BAD_LOOP_FOUND \
1190 do { free_game(tmpstate); return LOOP_NOT_SOLN; } while(0)
1192 /* Repeatedly look for loops until we either run out of lines to consider
1193 * or discover for sure that the board fails on the grounds of having no
1195 tmpstate
= dup_game(state
);
1198 if (!memchr(tmpstate
->hl
, LINE_YES
, HL_COUNT(tmpstate
)) &&
1199 !memchr(tmpstate
->vl
, LINE_YES
, VL_COUNT(tmpstate
))) {
1202 any_lines_found
= TRUE
;
1206 if (destructively_find_loop(tmpstate
)) {
1211 non_loop_found
= TRUE
;
1215 free_game(tmpstate
);
1217 if (!any_lines_found
)
1220 if (non_loop_found
) {
1221 assert(!loop_found
); /* should have dealt with this already */
1225 /* Check that every clue is satisfied */
1226 for (j
= 0; j
< state
->h
; ++j
) {
1227 for (i
= 0; i
< state
->w
; ++i
) {
1228 n
= CLUE_AT(state
, i
, j
);
1230 if (square_order(state
, i
, j
, LINE_YES
) != n
- '0') {
1231 return LOOP_NOT_SOLN
;
1240 /* Sums the lengths of the numbers in range [0,n) */
1241 /* See equivalent function in solo.c for justification of this. */
1242 static int len_0_to_n(int n
)
1244 int len
= 1; /* Counting 0 as a bit of a special case */
1247 for (i
= 1; i
< n
; i
*= 10) {
1248 len
+= max(n
- i
, 0);
1254 static char *encode_solve_move(const game_state
*state
)
1258 /* This is going to return a string representing the moves needed to set
1259 * every line in a grid to be the same as the ones in 'state'. The exact
1260 * length of this string is predictable. */
1262 len
= 1; /* Count the 'S' prefix */
1263 /* Numbers in horizontal lines */
1264 /* Horizontal lines, x position */
1265 len
+= len_0_to_n(state
->w
) * (state
->h
+ 1);
1266 /* Horizontal lines, y position */
1267 len
+= len_0_to_n(state
->h
+ 1) * (state
->w
);
1268 /* Vertical lines, y position */
1269 len
+= len_0_to_n(state
->h
) * (state
->w
+ 1);
1270 /* Vertical lines, x position */
1271 len
+= len_0_to_n(state
->w
+ 1) * (state
->h
);
1272 /* For each line we also have two letters and a comma */
1273 len
+= 3 * (HL_COUNT(state
) + VL_COUNT(state
));
1275 ret
= snewn(len
+ 1, char);
1278 p
+= sprintf(p
, "S");
1280 for (j
= 0; j
< state
->h
+ 1; ++j
) {
1281 for (i
= 0; i
< state
->w
; ++i
) {
1282 switch (RIGHTOF_DOT(state
, i
, j
)) {
1284 p
+= sprintf(p
, "%d,%dhy", i
, j
);
1287 p
+= sprintf(p
, "%d,%dhn", i
, j
);
1290 /* I'm going to forgive this because I think the results
1292 /* assert(!"Solver produced incomplete solution!"); */
1297 for (j
= 0; j
< state
->h
; ++j
) {
1298 for (i
= 0; i
< state
->w
+ 1; ++i
) {
1299 switch (BELOW_DOT(state
, i
, j
)) {
1301 p
+= sprintf(p
, "%d,%dvy", i
, j
);
1304 p
+= sprintf(p
, "%d,%dvn", i
, j
);
1307 /* I'm going to forgive this because I think the results
1309 /* assert(!"Solver produced incomplete solution!"); */
1314 /* No point in doing sums like that if they're going to be wrong */
1315 assert(strlen(ret
) == (size_t)len
);
1319 /* BEGIN SOLVER IMPLEMENTATION */
1321 /* For each pair of lines through each dot we store a bit for whether
1322 * exactly one of those lines is ON, and in separate arrays we store whether
1323 * at least one is on and whether at most 1 is on. (If we know both or
1324 * neither is on that's already stored more directly.) That's six bits per
1325 * dot. Bit number n represents the lines shown in dot_type_dirs[n]. */
1336 #define OPP_DLINE(dline) (dline ^ 1)
1339 #define SQUARE_DLINES \
1340 HANDLE_DLINE(DLINE_UL, RIGHTOF_SQUARE, BELOW_SQUARE, 1, 1); \
1341 HANDLE_DLINE(DLINE_UR, LEFTOF_SQUARE, BELOW_SQUARE, 0, 1); \
1342 HANDLE_DLINE(DLINE_DL, RIGHTOF_SQUARE, ABOVE_SQUARE, 1, 0); \
1343 HANDLE_DLINE(DLINE_DR, LEFTOF_SQUARE, ABOVE_SQUARE, 0, 0);
1345 #define DOT_DLINES \
1346 HANDLE_DLINE(DLINE_VERT, ABOVE_DOT, BELOW_DOT); \
1347 HANDLE_DLINE(DLINE_HORIZ, LEFTOF_DOT, RIGHTOF_DOT); \
1348 HANDLE_DLINE(DLINE_UL, ABOVE_DOT, LEFTOF_DOT); \
1349 HANDLE_DLINE(DLINE_UR, ABOVE_DOT, RIGHTOF_DOT); \
1350 HANDLE_DLINE(DLINE_DL, BELOW_DOT, LEFTOF_DOT); \
1351 HANDLE_DLINE(DLINE_DR, BELOW_DOT, RIGHTOF_DOT);
1353 static void array_setall(char *array
, char from
, char to
, int len
)
1355 char *p
= array
, *p_old
= p
;
1356 int len_remaining
= len
;
1358 while ((p
= memchr(p
, from
, len_remaining
))) {
1360 len_remaining
-= p
- p_old
;
1366 static int game_states_equal(const game_state
*state1
,
1367 const game_state
*state2
)
1369 /* This deliberately doesn't check _all_ fields, just the ones that make a
1370 * game state 'interesting' from the POV of the solver */
1371 /* XXX review this */
1372 if (state1
== state2
)
1375 if (!state1
|| !state2
)
1378 if (state1
->w
!= state2
->w
|| state1
->h
!= state2
->h
)
1381 if (memcmp(state1
->hl
, state2
->hl
, HL_COUNT(state1
)))
1384 if (memcmp(state1
->vl
, state2
->vl
, VL_COUNT(state1
)))
1390 static int solver_states_equal(const solver_state
*sstate1
,
1391 const solver_state
*sstate2
)
1400 if (!game_states_equal(sstate1
->state
, sstate2
->state
)) {
1404 /* XXX fields missing, needs review */
1405 /* XXX we're deliberately not looking at solver_state as it's only a cache */
1407 if (memcmp(sstate1
->dot_atleastone
, sstate2
->dot_atleastone
,
1408 DOT_COUNT(sstate1
->state
))) {
1412 if (memcmp(sstate1
->dot_atmostone
, sstate2
->dot_atmostone
,
1413 DOT_COUNT(sstate1
->state
))) {
1417 /* handle dline_identical here */
1422 static void dot_setall_dlines(solver_state
*sstate
, enum dline dl
, int i
, int j
,
1423 enum line_state line_old
, enum line_state line_new
)
1425 game_state
*state
= sstate
->state
;
1427 /* First line in dline */
1432 if (j
> 0 && ABOVE_DOT(state
, i
, j
) == line_old
)
1433 LV_ABOVE_DOT(state
, i
, j
) = line_new
;
1437 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1438 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1441 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1442 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1446 /* Second line in dline */
1450 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1451 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1456 if (i
<= (state
)->w
&& RIGHTOF_DOT(state
, i
, j
) == line_old
)
1457 LV_RIGHTOF_DOT(state
, i
, j
) = line_new
;
1460 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1461 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1466 static void update_solver_status(solver_state
*sstate
)
1468 if (sstate
->solver_status
== SOLVER_INCOMPLETE
) {
1469 switch (loop_status(sstate
->state
)) {
1471 sstate
->solver_status
= SOLVER_INCOMPLETE
;
1474 if (sstate
->solver_status
!= SOLVER_AMBIGUOUS
)
1475 sstate
->solver_status
= SOLVER_SOLVED
;
1478 sstate
->solver_status
= SOLVER_MISTAKE
;
1485 /* This will fail an assertion if {dx,dy} are anything other than {-1,0}, {1,0}
1486 * {0,-1} or {0,1} */
1487 static int line_status_from_point(const game_state
*state
,
1488 int x
, int y
, int dx
, int dy
)
1490 if (dx
== -1 && dy
== 0)
1491 return LEFTOF_DOT(state
, x
, y
);
1492 if (dx
== 1 && dy
== 0)
1493 return RIGHTOF_DOT(state
, x
, y
);
1494 if (dx
== 0 && dy
== -1)
1495 return ABOVE_DOT(state
, x
, y
);
1496 if (dx
== 0 && dy
== 1)
1497 return BELOW_DOT(state
, x
, y
);
1499 assert(!"Illegal dx or dy in line_status_from_point");
1504 /* This will return a dynamically allocated solver_state containing the (more)
1506 static solver_state
*solve_game_rec(const solver_state
*sstate_start
, int diff
)
1509 int current_yes
, current_no
, desired
;
1510 solver_state
*sstate
, *sstate_saved
, *sstate_tmp
;
1513 solver_state
*sstate_rec_solved
;
1514 int recursive_soln_count
;
1517 printf("solve_game_rec: recursion_remaining = %d\n",
1518 sstate_start
->recursion_remaining
);
1521 sstate
= dup_solver_state((solver_state
*)sstate_start
);
1524 text
= game_text_format(sstate
->state
);
1525 printf("%s\n", text
);
1529 #define RETURN_IF_SOLVED \
1531 update_solver_status(sstate); \
1532 if (sstate->solver_status != SOLVER_INCOMPLETE) { \
1533 free_solver_state(sstate_saved); \
1538 #define FOUND_MISTAKE \
1540 sstate->solver_status = SOLVER_MISTAKE; \
1541 free_solver_state(sstate_saved); \
1546 sstate_saved
= NULL
;
1549 nonrecursive_solver
:
1552 sstate_saved
= dup_solver_state(sstate
);
1554 /* First we do the 'easy' work, that might cause concrete results */
1556 /* Per-square deductions */
1557 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1558 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1559 /* Begin rules that look at the clue (if there is one) */
1560 desired
= CLUE_AT(sstate
->state
, i
, j
);
1563 desired
= desired
- '0';
1564 current_yes
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1565 current_no
= square_order(sstate
->state
, i
, j
, LINE_NO
);
1567 if (desired
< current_yes
)
1569 if (desired
== current_yes
) {
1570 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1574 if (4 - desired
< current_no
)
1576 if (4 - desired
== current_no
) {
1577 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_YES
);
1584 /* Per-dot deductions */
1585 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1586 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1587 switch (dot_order(sstate
->state
, i
, j
, LINE_YES
)) {
1589 if (dot_order(sstate
->state
, i
, j
, LINE_NO
) == 3) {
1590 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1594 switch (dot_order(sstate
->state
, i
, j
, LINE_NO
)) {
1595 #define H1(dline, dir1_dot, dir2_dot, dot_howmany) \
1596 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1597 if (dir2_dot(sstate->state, i, j) == LINE_UNKNOWN){ \
1598 sstate->dot_howmany \
1599 [i + (sstate->state->w + 1) * j] |= 1<<dline; \
1603 if (diff
> DIFF_EASY
) {
1604 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1605 H1(dline, dir1_dot, dir2_dot, dot_atleastone)
1606 /* 1 yes, 1 no, so exactly one of unknowns is
1613 if (diff
> DIFF_EASY
) {
1614 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1615 H1(dline, dir1_dot, dir2_dot, dot_atmostone)
1616 /* 1 yes, fewer than 2 no, so at most one of
1617 * unknowns is yes */
1623 case 2: /* 1 yes, 2 no */
1624 dot_setall(sstate
->state
, i
, j
,
1625 LINE_UNKNOWN
, LINE_YES
);
1630 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1636 if (diff
> DIFF_EASY
) {
1637 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1638 if (sstate->dot_atleastone \
1639 [i + (sstate->state->w + 1) * j] & 1<<dline) { \
1640 sstate->dot_atmostone \
1641 [i + (sstate->state->w + 1) * j] |= 1<<OPP_DLINE(dline); \
1643 /* If at least one of a dline in a dot is YES, at most one
1644 * of the opposite dline to that dot must be YES. */
1651 /* More obscure per-square operations */
1652 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1653 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1654 #define H1(dline, dir1_sq, dir2_sq, a, b, dot_howmany, line_query, line_set) \
1655 if (sstate->dot_howmany[i+a + (sstate->state->w + 1) * (j+b)] &\
1657 t = dir1_sq(sstate->state, i, j); \
1658 if (t == line_query) \
1659 dir2_sq(sstate->state, i, j) = line_set; \
1661 t = dir2_sq(sstate->state, i, j); \
1662 if (t == line_query) \
1663 dir1_sq(sstate->state, i, j) = line_set; \
1666 if (diff
> DIFF_EASY
) {
1667 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1668 H1(dline, dir1_sq, dir2_sq, a, b, dot_atmostone, \
1670 /* If at most one of the DLINE is on, and one is definitely
1671 * on, set the other to definitely off */
1676 if (diff
> DIFF_EASY
) {
1677 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1678 H1(dline, dir1_sq, dir2_sq, a, b, dot_atleastone, \
1680 /* If at least one of the DLINE is on, and one is definitely
1681 * off, set the other to definitely on */
1687 switch (CLUE_AT(sstate
->state
, i
, j
)) {
1690 if (diff
> DIFF_EASY
) {
1691 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1692 /* At most one of any DLINE can be set */ \
1693 sstate->dot_atmostone \
1694 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1695 /* This DLINE provides enough YESes to solve the clue */\
1696 if (sstate->dot_atleastone \
1697 [i+a + (sstate->state->w + 1) * (j+b)] & \
1699 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1701 LINE_UNKNOWN, LINE_NO); \
1708 if (diff
> DIFF_EASY
) {
1709 #define H1(dline, dot_at1one, dot_at2one, a, b) \
1710 if (sstate->dot_at1one \
1711 [i+a + (sstate->state->w + 1) * (j+b)] & \
1713 sstate->dot_at2one \
1714 [i+(1-a) + (sstate->state->w + 1) * (j+(1-b))] |= \
1715 1<<OPP_DLINE(dline); \
1717 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1718 H1(dline, dot_atleastone, dot_atmostone, a, b); \
1719 H1(dline, dot_atmostone, dot_atleastone, a, b);
1720 /* If at least one of one DLINE is set, at most one
1721 * of the opposing one is and vice versa */
1729 if (diff
> DIFF_EASY
) {
1730 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1731 /* At least one of any DLINE can be set */ \
1732 sstate->dot_atleastone \
1733 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1734 /* This DLINE provides enough NOs to solve the clue */ \
1735 if (sstate->dot_atmostone \
1736 [i+a + (sstate->state->w + 1) * (j+b)] & \
1738 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1740 LINE_UNKNOWN, LINE_YES); \
1750 if (solver_states_equal(sstate
, sstate_saved
)) {
1751 int edgecount
= 0, clues
= 0, satclues
= 0, sm1clues
= 0;
1755 * Go through the grid and update for all the new edges.
1756 * Since merge_dots() is idempotent, the simplest way to
1757 * do this is just to update for _all_ the edges.
1759 * Also, while we're here, we count the edges, count the
1760 * clues, count the satisfied clues, and count the
1761 * satisfied-minus-one clues.
1763 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1764 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1765 if (RIGHTOF_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1766 merge_dots(sstate
, i
, j
, i
+1, j
);
1769 if (BELOW_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1770 merge_dots(sstate
, i
, j
, i
, j
+1);
1774 if (CLUE_AT(sstate
->state
, i
, j
) != ' ') {
1775 int c
= CLUE_AT(sstate
->state
, i
, j
) - '0';
1776 int o
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1787 * Now go through looking for LINE_UNKNOWN edges which
1788 * connect two dots that are already in the same
1789 * equivalence class. If we find one, test to see if the
1790 * loop it would create is a solution.
1792 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1793 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1794 for (d
= 0; d
< 2; d
++) {
1795 int i2
, j2
, eqclass
, val
;
1798 if (RIGHTOF_DOT(sstate
->state
, i
, j
) !=
1804 if (BELOW_DOT(sstate
->state
, i
, j
) !=
1811 eqclass
= dsf_canonify(sstate
->dotdsf
,
1812 j
* (sstate
->state
->w
+1) + i
);
1813 if (eqclass
!= dsf_canonify(sstate
->dotdsf
,
1814 j2
* (sstate
->state
->w
+1) +
1818 val
= LINE_NO
; /* loop is bad until proven otherwise */
1821 * This edge would form a loop. Next
1822 * question: how long would the loop be?
1823 * Would it equal the total number of edges
1824 * (plus the one we'd be adding if we added
1827 if (sstate
->looplen
[eqclass
] == edgecount
+ 1) {
1832 * This edge would form a loop which
1833 * took in all the edges in the entire
1834 * grid. So now we need to work out
1835 * whether it would be a valid solution
1836 * to the puzzle, which means we have to
1837 * check if it satisfies all the clues.
1838 * This means that every clue must be
1839 * either satisfied or satisfied-minus-
1840 * 1, and also that the number of
1841 * satisfied-minus-1 clues must be at
1842 * most two and they must lie on either
1843 * side of this edge.
1848 if (CLUE_AT(sstate
->state
, cx
,cy
) != ' ' &&
1849 square_order(sstate
->state
, cx
,cy
, LINE_YES
) ==
1850 CLUE_AT(sstate
->state
, cx
,cy
) - '0' - 1)
1852 if (CLUE_AT(sstate
->state
, i
, j
) != ' ' &&
1853 square_order(sstate
->state
, i
, j
, LINE_YES
) ==
1854 CLUE_AT(sstate
->state
, i
, j
) - '0' - 1)
1856 if (sm1clues
== sm1_nearby
&&
1857 sm1clues
+ satclues
== clues
)
1858 val
= LINE_YES
; /* loop is good! */
1862 * Right. Now we know that adding this edge
1863 * would form a loop, and we know whether
1864 * that loop would be a viable solution or
1867 * If adding this edge produces a solution,
1868 * then we know we've found _a_ solution but
1869 * we don't know that it's _the_ solution -
1870 * if it were provably the solution then
1871 * we'd have deduced this edge some time ago
1872 * without the need to do loop detection. So
1873 * in this state we return SOLVER_AMBIGUOUS,
1874 * which has the effect that hitting Solve
1875 * on a user-provided puzzle will fill in a
1876 * solution but using the solver to
1877 * construct new puzzles won't consider this
1878 * a reasonable deduction for the user to
1882 LV_RIGHTOF_DOT(sstate
->state
, i
, j
) = val
;
1884 LV_BELOW_DOT(sstate
->state
, i
, j
) = val
;
1885 if (val
== LINE_YES
) {
1886 sstate
->solver_status
= SOLVER_AMBIGUOUS
;
1887 goto finished_loop_checking
;
1893 finished_loop_checking
:
1898 if (solver_states_equal(sstate
, sstate_saved
)) {
1899 /* Solver has stopped making progress so we terminate */
1900 free_solver_state(sstate_saved
);
1904 free_solver_state(sstate_saved
);
1907 if (sstate
->solver_status
== SOLVER_SOLVED
||
1908 sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
1909 /* s/LINE_UNKNOWN/LINE_NO/g */
1910 array_setall(sstate
->state
->hl
, LINE_UNKNOWN
, LINE_NO
,
1911 HL_COUNT(sstate
->state
));
1912 array_setall(sstate
->state
->vl
, LINE_UNKNOWN
, LINE_NO
,
1913 VL_COUNT(sstate
->state
));
1917 /* Perform recursive calls */
1918 if (sstate
->recursion_remaining
) {
1919 sstate_saved
= dup_solver_state(sstate
);
1921 sstate
->recursion_remaining
--;
1923 recursive_soln_count
= 0;
1924 sstate_rec_solved
= NULL
;
1926 /* Memory management:
1927 * sstate_saved won't be modified but needs to be freed when we have
1929 * sstate is expected to contain our 'best' solution by the time we
1930 * finish this section of code. It's the thing we'll try adding lines
1931 * to, seeing if they make it more solvable.
1932 * If sstate_rec_solved is non-NULL, it will supersede sstate
1933 * eventually. sstate_tmp should not hold a value persistently.
1936 /* NB SOLVER_AMBIGUOUS is like SOLVER_SOLVED except the solver is aware
1937 * of the possibility of additional solutions. So as soon as we have a
1938 * SOLVER_AMBIGUOUS we can safely propagate it back to our caller, but
1939 * if we get a SOLVER_SOLVED we want to keep trying in case we find
1940 * further solutions and have to mark it ambiguous.
1943 #define DO_RECURSIVE_CALL(dir_dot) \
1944 if (dir_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1945 debug(("Trying " #dir_dot " at [%d,%d]\n", i, j)); \
1946 LV_##dir_dot(sstate->state, i, j) = LINE_YES; \
1947 sstate_tmp = solve_game_rec(sstate, diff); \
1948 switch (sstate_tmp->solver_status) { \
1949 case SOLVER_AMBIGUOUS: \
1950 debug(("Solver ambiguous, returning\n")); \
1951 sstate_rec_solved = sstate_tmp; \
1952 goto finished_recursion; \
1953 case SOLVER_SOLVED: \
1954 switch (++recursive_soln_count) { \
1956 debug(("One solution found\n")); \
1957 sstate_rec_solved = sstate_tmp; \
1960 debug(("Ambiguous solutions found\n")); \
1961 free_solver_state(sstate_tmp); \
1962 sstate_rec_solved->solver_status = SOLVER_AMBIGUOUS;\
1963 goto finished_recursion; \
1965 assert(!"recursive_soln_count out of range"); \
1969 case SOLVER_MISTAKE: \
1970 debug(("Non-solution found\n")); \
1971 free_solver_state(sstate_tmp); \
1972 free_solver_state(sstate_saved); \
1973 LV_##dir_dot(sstate->state, i, j) = LINE_NO; \
1974 goto nonrecursive_solver; \
1975 case SOLVER_INCOMPLETE: \
1976 debug(("Recursive step inconclusive\n")); \
1977 free_solver_state(sstate_tmp); \
1980 free_solver_state(sstate); \
1981 sstate = dup_solver_state(sstate_saved); \
1984 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1985 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1986 /* Only perform recursive calls on 'loose ends' */
1987 if (dot_order(sstate
->state
, i
, j
, LINE_YES
) == 1) {
1988 DO_RECURSIVE_CALL(LEFTOF_DOT
);
1989 DO_RECURSIVE_CALL(RIGHTOF_DOT
);
1990 DO_RECURSIVE_CALL(ABOVE_DOT
);
1991 DO_RECURSIVE_CALL(BELOW_DOT
);
1998 if (sstate_rec_solved
) {
1999 free_solver_state(sstate
);
2000 sstate
= sstate_rec_solved
;
2007 /* XXX bits of solver that may come in handy one day */
2009 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
2010 /* dline from this dot that's entirely unknown must have
2011 * both lines identical */ \
2012 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN && \
2013 dir2_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
2014 sstate->dline_identical[i + (sstate->state->w + 1) * j] |= \
2016 } else if (sstate->dline_identical[i +
2017 (sstate
->state
->w
+ 1) * j
] &\
2019 /* If they're identical and one is known do the obvious
2021 t
= dir1_dot(sstate
->state
, i
, j
); \
2022 if (t
!= LINE_UNKNOWN
) \
2023 dir2_dot(sstate
->state
, i
, j
) = t
; \
2025 t
= dir2_dot(sstate
->state
, i
, j
); \
2026 if (t
!= LINE_UNKNOWN
) \
2027 dir1_dot(sstate
->state
, i
, j
) = t
; \
2035 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
2036 if (sstate->dline_identical[i+a + \
2037 (sstate->state->w + 1) * (j+b)] &\
2039 dir1_sq(sstate->state, i, j) = LINE_YES; \
2040 dir2_sq(sstate->state, i, j) = LINE_YES; \
2042 /* If two lines are the same they must be on */
2049 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
2050 if (sstate->dot_atmostone[i+a + (sstate->state->w + 1) * (j+b)] & \
2052 if (square_order(sstate->state, i, j, LINE_UNKNOWN) - 1 == \
2053 CLUE_AT(sstate->state, i, j) - '0') { \
2054 square_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_YES); \
2055 /* XXX the following may overwrite known data! */ \
2056 dir1_sq(sstate->state, i, j) = LINE_UNKNOWN; \
2057 dir2_sq(sstate->state, i, j) = LINE_UNKNOWN; \
2065 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
2066 if (sstate->dline_identical[i+a +
2067 (sstate
->state
->w
+ 1) * (j
+b
)] &\
2069 dir1_sq(sstate
->state
, i
, j
) = LINE_NO
; \
2070 dir2_sq(sstate
->state
, i
, j
) = LINE_NO
; \
2072 /* If two lines are the same they must be off */
2077 static char *solve_game(game_state
*state
, game_state
*currstate
,
2078 char *aux
, char **error
)
2081 solver_state
*sstate
, *new_sstate
;
2083 sstate
= new_solver_state(state
);
2084 new_sstate
= solve_game_rec(sstate
, DIFFCOUNT
);
2086 if (new_sstate
->solver_status
== SOLVER_SOLVED
) {
2087 soln
= encode_solve_move(new_sstate
->state
);
2088 } else if (new_sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
2089 soln
= encode_solve_move(new_sstate
->state
);
2090 /**error = "Solver found ambiguous solutions"; */
2092 soln
= encode_solve_move(new_sstate
->state
);
2093 /**error = "Solver failed"; */
2096 free_solver_state(new_sstate
);
2097 free_solver_state(sstate
);
2102 static char *game_text_format(game_state
*state
)
2108 len
= (2 * state
->w
+ 2) * (2 * state
->h
+ 1);
2109 rp
= ret
= snewn(len
+ 1, char);
2112 switch (ABOVE_SQUARE(state, i, j)) { \
2114 rp += sprintf(rp, " -"); \
2117 rp += sprintf(rp, " x"); \
2119 case LINE_UNKNOWN: \
2120 rp += sprintf(rp, " "); \
2123 assert(!"Illegal line state for HL");\
2127 switch (LEFTOF_SQUARE(state, i, j)) {\
2129 rp += sprintf(rp, "|"); \
2132 rp += sprintf(rp, "x"); \
2134 case LINE_UNKNOWN: \
2135 rp += sprintf(rp, " "); \
2138 assert(!"Illegal line state for VL");\
2141 for (j
= 0; j
< state
->h
; ++j
) {
2142 for (i
= 0; i
< state
->w
; ++i
) {
2145 rp
+= sprintf(rp
, " \n");
2146 for (i
= 0; i
< state
->w
; ++i
) {
2148 rp
+= sprintf(rp
, "%c", (int)(CLUE_AT(state
, i
, j
)));
2151 rp
+= sprintf(rp
, "\n");
2153 for (i
= 0; i
< state
->w
; ++i
) {
2156 rp
+= sprintf(rp
, " \n");
2158 assert(strlen(ret
) == len
);
2162 static game_ui
*new_ui(game_state
*state
)
2167 static void free_ui(game_ui
*ui
)
2171 static char *encode_ui(game_ui
*ui
)
2176 static void decode_ui(game_ui
*ui
, char *encoding
)
2180 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2181 game_state
*newstate
)
2185 struct game_drawstate
{
2193 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
2194 int x
, int y
, int button
)
2199 char button_char
= ' ';
2200 enum line_state old_state
;
2202 button
&= ~MOD_MASK
;
2204 /* Around each line is a diamond-shaped region where points within that
2205 * region are closer to this line than any other. We assume any click
2206 * within a line's diamond was meant for that line. It would all be a lot
2207 * simpler if the / and % operators respected modulo arithmetic properly
2208 * for negative numbers. */
2213 /* Get the coordinates of the square the click was in */
2214 i
= (x
+ TILE_SIZE
) / TILE_SIZE
- 1;
2215 j
= (y
+ TILE_SIZE
) / TILE_SIZE
- 1;
2217 /* Get the precise position inside square [i,j] */
2218 p
= (x
+ TILE_SIZE
) % TILE_SIZE
;
2219 q
= (y
+ TILE_SIZE
) % TILE_SIZE
;
2221 /* After this bit of magic [i,j] will correspond to the point either above
2222 * or to the left of the line selected */
2224 if (TILE_SIZE
- p
> q
) {
2227 hl_selected
= FALSE
;
2231 if (TILE_SIZE
- q
> p
) {
2232 hl_selected
= FALSE
;
2243 if (i
>= state
->w
|| j
>= state
->h
+ 1)
2246 if (i
>= state
->w
+ 1 || j
>= state
->h
)
2250 /* I think it's only possible to play this game with mouse clicks, sorry */
2251 /* Maybe will add mouse drag support some time */
2253 old_state
= RIGHTOF_DOT(state
, i
, j
);
2255 old_state
= BELOW_DOT(state
, i
, j
);
2259 switch (old_state
) {
2273 switch (old_state
) {
2288 sprintf(buf
, "%d,%d%c%c", i
, j
, (int)(hl_selected ?
'h' : 'v'), (int)button_char
);
2294 static game_state
*execute_move(game_state
*state
, char *move
)
2297 game_state
*newstate
= dup_game(state
);
2299 if (move
[0] == 'S') {
2301 newstate
->cheated
= TRUE
;
2306 move
= strchr(move
, ',');
2310 move
+= strspn(move
, "1234567890");
2311 switch (*(move
++)) {
2313 if (i
>= newstate
->w
|| j
> newstate
->h
)
2315 switch (*(move
++)) {
2317 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_YES
;
2320 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_NO
;
2323 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2330 if (i
> newstate
->w
|| j
>= newstate
->h
)
2332 switch (*(move
++)) {
2334 LV_BELOW_DOT(newstate
, i
, j
) = LINE_YES
;
2337 LV_BELOW_DOT(newstate
, i
, j
) = LINE_NO
;
2340 LV_BELOW_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2352 * Check for completion.
2354 i
= 0; /* placate optimiser */
2355 for (j
= 0; j
<= newstate
->h
; j
++) {
2356 for (i
= 0; i
< newstate
->w
; i
++)
2357 if (LV_RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
)
2359 if (i
< newstate
->w
)
2362 if (j
<= newstate
->h
) {
2368 * We've found a horizontal edge at (i,j). Follow it round
2369 * to see if it's part of a loop.
2373 int order
= dot_order(newstate
, x
, y
, LINE_YES
);
2375 goto completion_check_done
;
2377 if (LEFTOF_DOT(newstate
, x
, y
) == LINE_YES
&& prevdir
!= 'L') {
2380 } else if (RIGHTOF_DOT(newstate
, x
, y
) == LINE_YES
&&
2384 } else if (ABOVE_DOT(newstate
, x
, y
) == LINE_YES
&&
2388 } else if (BELOW_DOT(newstate
, x
, y
) == LINE_YES
&&
2393 assert(!"Can't happen"); /* dot_order guarantees success */
2398 if (x
== i
&& y
== j
)
2402 if (x
!= i
|| y
!= j
|| looplen
== 0)
2403 goto completion_check_done
;
2406 * We've traced our way round a loop, and we know how many
2407 * line segments were involved. Count _all_ the line
2408 * segments in the grid, to see if the loop includes them
2412 for (j
= 0; j
<= newstate
->h
; j
++)
2413 for (i
= 0; i
<= newstate
->w
; i
++)
2414 count
+= ((RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
) +
2415 (BELOW_DOT(newstate
, i
, j
) == LINE_YES
));
2416 assert(count
>= looplen
);
2417 if (count
!= looplen
)
2418 goto completion_check_done
;
2421 * The grid contains one closed loop and nothing else.
2422 * Check that all the clues are satisfied.
2424 for (j
= 0; j
< newstate
->h
; ++j
) {
2425 for (i
= 0; i
< newstate
->w
; ++i
) {
2426 int n
= CLUE_AT(newstate
, i
, j
);
2428 if (square_order(newstate
, i
, j
, LINE_YES
) != n
- '0') {
2429 goto completion_check_done
;
2438 newstate
->solved
= TRUE
;
2441 completion_check_done
:
2445 free_game(newstate
);
2449 /* ----------------------------------------------------------------------
2453 #define SIZE(d) ((d) * TILE_SIZE + 2 * BORDER + 1)
2455 static void game_compute_size(game_params
*params
, int tilesize
,
2458 struct { int tilesize
; } ads
, *ds
= &ads
;
2459 ads
.tilesize
= tilesize
;
2461 *x
= SIZE(params
->w
);
2462 *y
= SIZE(params
->h
);
2465 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
2466 game_params
*params
, int tilesize
)
2468 ds
->tilesize
= tilesize
;
2471 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2473 float *ret
= snewn(4 * NCOLOURS
, float);
2475 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2477 ret
[COL_FOREGROUND
* 3 + 0] = 0.0F
;
2478 ret
[COL_FOREGROUND
* 3 + 1] = 0.0F
;
2479 ret
[COL_FOREGROUND
* 3 + 2] = 0.0F
;
2481 ret
[COL_HIGHLIGHT
* 3 + 0] = 1.0F
;
2482 ret
[COL_HIGHLIGHT
* 3 + 1] = 1.0F
;
2483 ret
[COL_HIGHLIGHT
* 3 + 2] = 1.0F
;
2485 ret
[COL_MISTAKE
* 3 + 0] = 1.0F
;
2486 ret
[COL_MISTAKE
* 3 + 1] = 0.0F
;
2487 ret
[COL_MISTAKE
* 3 + 2] = 0.0F
;
2489 *ncolours
= NCOLOURS
;
2493 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
2495 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2499 ds
->hl
= snewn(HL_COUNT(state
), char);
2500 ds
->vl
= snewn(VL_COUNT(state
), char);
2501 ds
->clue_error
= snewn(SQUARE_COUNT(state
), char);
2504 memset(ds
->hl
, LINE_UNKNOWN
, HL_COUNT(state
));
2505 memset(ds
->vl
, LINE_UNKNOWN
, VL_COUNT(state
));
2506 memset(ds
->clue_error
, 0, SQUARE_COUNT(state
));
2511 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2513 sfree(ds
->clue_error
);
2519 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2520 game_state
*state
, int dir
, game_ui
*ui
,
2521 float animtime
, float flashtime
)
2524 int w
= state
->w
, h
= state
->h
;
2526 int line_colour
, flash_changed
;
2531 * The initial contents of the window are not guaranteed and
2532 * can vary with front ends. To be on the safe side, all games
2533 * should start by drawing a big background-colour rectangle
2534 * covering the whole window.
2536 draw_rect(dr
, 0, 0, SIZE(state
->w
), SIZE(state
->h
), COL_BACKGROUND
);
2539 for (j
= 0; j
< h
+ 1; ++j
) {
2540 for (i
= 0; i
< w
+ 1; ++i
) {
2542 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2543 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2544 LINEWIDTH
, LINEWIDTH
, COL_FOREGROUND
);
2549 for (j
= 0; j
< h
; ++j
) {
2550 for (i
= 0; i
< w
; ++i
) {
2551 c
[0] = CLUE_AT(state
, i
, j
);
2554 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2,
2555 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2,
2556 FONT_VARIABLE
, TILE_SIZE
/2,
2557 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_FOREGROUND
, c
);
2560 draw_update(dr
, 0, 0,
2561 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2562 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
2566 if (flashtime
> 0 &&
2567 (flashtime
<= FLASH_TIME
/3 ||
2568 flashtime
>= FLASH_TIME
*2/3)) {
2569 flash_changed
= !ds
->flashing
;
2570 ds
->flashing
= TRUE
;
2571 line_colour
= COL_HIGHLIGHT
;
2573 flash_changed
= ds
->flashing
;
2574 ds
->flashing
= FALSE
;
2575 line_colour
= COL_FOREGROUND
;
2578 #define CROSS_SIZE (3 * LINEWIDTH / 2)
2580 /* Redraw clue colours if necessary */
2581 for (j
= 0; j
< h
; ++j
) {
2582 for (i
= 0; i
< w
; ++i
) {
2583 c
[0] = CLUE_AT(state
, i
, j
);
2589 assert(n
>= 0 && n
<= 4);
2591 clue_mistake
= (square_order(state
, i
, j
, LINE_YES
) > n
||
2592 square_order(state
, i
, j
, LINE_NO
) > (4-n
));
2594 if (clue_mistake
!= ds
->clue_error
[i
* w
+ j
]) {
2596 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
,
2597 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
,
2598 TILE_SIZE
- CROSS_SIZE
* 2, TILE_SIZE
- CROSS_SIZE
* 2,
2601 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2,
2602 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2,
2603 FONT_VARIABLE
, TILE_SIZE
/2,
2604 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2605 clue_mistake ? COL_MISTAKE
: COL_FOREGROUND
, c
);
2606 draw_update(dr
, i
* TILE_SIZE
+ BORDER
, j
* TILE_SIZE
+ BORDER
,
2607 TILE_SIZE
, TILE_SIZE
);
2609 ds
->clue_error
[i
* w
+ j
] = clue_mistake
;
2614 /* I've also had a request to colour lines red if they make a non-solution
2615 * loop, or if more than two lines go into any point. I think that would
2616 * be good some time. */
2618 #define CLEAR_VL(i, j) do { \
2620 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2621 BORDER + j * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, \
2623 TILE_SIZE - LINEWIDTH, \
2626 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2627 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2629 TILE_SIZE + CROSS_SIZE*2); \
2632 #define CLEAR_HL(i, j) do { \
2634 BORDER + i * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, \
2635 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2636 TILE_SIZE - LINEWIDTH, \
2640 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2641 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2642 TILE_SIZE + CROSS_SIZE*2, \
2646 /* Vertical lines */
2647 for (j
= 0; j
< h
; ++j
) {
2648 for (i
= 0; i
< w
+ 1; ++i
) {
2649 switch (BELOW_DOT(state
, i
, j
)) {
2651 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2656 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
) ||
2660 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2661 BORDER
+ j
* TILE_SIZE
+ LINEWIDTH
- LINEWIDTH
/2,
2662 LINEWIDTH
, TILE_SIZE
- LINEWIDTH
,
2667 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2670 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2671 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2672 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2673 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2676 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2677 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2678 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2679 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2684 ds
->vl
[i
+ (w
+ 1) * j
] = BELOW_DOT(state
, i
, j
);
2688 /* Horizontal lines */
2689 for (j
= 0; j
< h
+ 1; ++j
) {
2690 for (i
= 0; i
< w
; ++i
) {
2691 switch (RIGHTOF_DOT(state
, i
, j
)) {
2693 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2698 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
) ||
2702 BORDER
+ i
* TILE_SIZE
+ LINEWIDTH
- LINEWIDTH
/2,
2703 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2704 TILE_SIZE
- LINEWIDTH
, LINEWIDTH
,
2709 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2712 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2713 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2714 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2715 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2718 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2719 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2720 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2721 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2726 ds
->hl
[i
+ w
* j
] = RIGHTOF_DOT(state
, i
, j
);
2731 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2732 int dir
, game_ui
*ui
)
2737 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2738 int dir
, game_ui
*ui
)
2740 if (!oldstate
->solved
&& newstate
->solved
&&
2741 !oldstate
->cheated
&& !newstate
->cheated
) {
2748 static int game_wants_statusbar(void)
2753 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2758 static void game_print_size(game_params
*params
, float *x
, float *y
)
2763 * I'll use 7mm squares by default.
2765 game_compute_size(params
, 700, &pw
, &ph
);
2770 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2772 int w
= state
->w
, h
= state
->h
;
2773 int ink
= print_mono_colour(dr
, 0);
2775 game_drawstate ads
, *ds
= &ads
;
2776 ds
->tilesize
= tilesize
;
2779 * Dots. I'll deliberately make the dots a bit wider than the
2780 * lines, so you can still see them. (And also because it's
2781 * annoyingly tricky to make them _exactly_ the same size...)
2783 for (y
= 0; y
<= h
; y
++)
2784 for (x
= 0; x
<= w
; x
++)
2785 draw_circle(dr
, BORDER
+ x
* TILE_SIZE
, BORDER
+ y
* TILE_SIZE
,
2786 LINEWIDTH
, ink
, ink
);
2791 for (y
= 0; y
< h
; y
++)
2792 for (x
= 0; x
< w
; x
++)
2793 if (CLUE_AT(state
, x
, y
) != ' ') {
2796 c
[0] = CLUE_AT(state
, x
, y
);
2799 BORDER
+ x
* TILE_SIZE
+ TILE_SIZE
/2,
2800 BORDER
+ y
* TILE_SIZE
+ TILE_SIZE
/2,
2801 FONT_VARIABLE
, TILE_SIZE
/2,
2802 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, c
);
2806 * Lines. (At the moment, I'm not bothering with crosses.)
2808 for (y
= 0; y
<= h
; y
++)
2809 for (x
= 0; x
< w
; x
++)
2810 if (RIGHTOF_DOT(state
, x
, y
) == LINE_YES
)
2811 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
,
2812 BORDER
+ y
* TILE_SIZE
- LINEWIDTH
/2,
2813 TILE_SIZE
, (LINEWIDTH
/2) * 2 + 1, ink
);
2814 for (y
= 0; y
< h
; y
++)
2815 for (x
= 0; x
<= w
; x
++)
2816 if (BELOW_DOT(state
, x
, y
) == LINE_YES
)
2817 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
- LINEWIDTH
/2,
2818 BORDER
+ y
* TILE_SIZE
,
2819 (LINEWIDTH
/2) * 2 + 1, TILE_SIZE
, ink
);
2823 #define thegame loopy
2826 const struct game thegame
= {
2827 "Loopy", "games.loopy",
2834 TRUE
, game_configure
, custom_params
,
2842 TRUE
, game_text_format
,
2850 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2853 game_free_drawstate
,
2857 TRUE
, FALSE
, game_print_size
, game_print
,
2858 game_wants_statusbar
,
2859 FALSE
, game_timing_state
,
2860 0, /* mouse_priorities */