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 } },
433 { "10x10 Normal", { 10, 10, DIFF_NORMAL
, 0 } },
435 { "15x15 Easy", { 15, 15, DIFF_EASY
, 0 } },
436 { "15x15 Normal", { 15, 15, DIFF_NORMAL
, 0 } },
437 { "30x20 Easy", { 30, 20, DIFF_EASY
, 0 } },
438 { "30x20 Normal", { 30, 20, DIFF_NORMAL
, 0 } }
442 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
446 if (i
< 0 || i
>= lenof(loopy_presets
))
449 tmppar
= loopy_presets
[i
].params
;
450 *params
= dup_params(&tmppar
);
451 *name
= dupstr(loopy_presets
[i
].desc
);
456 static void free_params(game_params
*params
)
461 static void decode_params(game_params
*params
, char const *string
)
463 params
->h
= params
->w
= atoi(string
);
465 params
->diff
= DIFF_EASY
;
466 while (*string
&& isdigit((unsigned char)*string
)) string
++;
467 if (*string
== 'x') {
469 params
->h
= atoi(string
);
470 while (*string
&& isdigit((unsigned char)*string
)) string
++;
472 if (*string
== 'r') {
474 params
->rec
= atoi(string
);
475 while (*string
&& isdigit((unsigned char)*string
)) string
++;
477 if (*string
== 'd') {
481 for (i
= 0; i
< DIFFCOUNT
; i
++)
482 if (*string
== loopy_diffchars
[i
])
484 if (*string
) string
++;
488 static char *encode_params(game_params
*params
, int full
)
491 sprintf(str
, "%dx%d", params
->w
, params
->h
);
493 sprintf(str
+ strlen(str
), "r%dd%c", params
->rec
,
494 loopy_diffchars
[params
->diff
]);
498 static config_item
*game_configure(game_params
*params
)
503 ret
= snewn(4, config_item
);
505 ret
[0].name
= "Width";
506 ret
[0].type
= C_STRING
;
507 sprintf(buf
, "%d", params
->w
);
508 ret
[0].sval
= dupstr(buf
);
511 ret
[1].name
= "Height";
512 ret
[1].type
= C_STRING
;
513 sprintf(buf
, "%d", params
->h
);
514 ret
[1].sval
= dupstr(buf
);
517 ret
[2].name
= "Difficulty";
518 ret
[2].type
= C_CHOICES
;
519 ret
[2].sval
= DIFFCONFIG
;
520 ret
[2].ival
= params
->diff
;
530 static game_params
*custom_params(config_item
*cfg
)
532 game_params
*ret
= snew(game_params
);
534 ret
->w
= atoi(cfg
[0].sval
);
535 ret
->h
= atoi(cfg
[1].sval
);
537 ret
->diff
= cfg
[2].ival
;
542 static char *validate_params(game_params
*params
, int full
)
544 if (params
->w
< 4 || params
->h
< 4)
545 return "Width and height must both be at least 4";
547 return "Recursion depth can't be negative";
550 * This shouldn't be able to happen at all, since decode_params
551 * and custom_params will never generate anything that isn't
554 assert(params
->diff
>= 0 && params
->diff
< DIFFCOUNT
);
559 /* We're going to store a list of current candidate squares for lighting.
560 * Each square gets a 'score', which tells us how adding that square right
561 * now would affect the length of the solution loop. We're trying to
562 * maximise that quantity so will bias our random selection of squares to
563 * light towards those with high scores */
566 unsigned long random
;
570 static int get_square_cmpfn(void *v1
, void *v2
)
572 struct square
*s1
= (struct square
*)v1
;
573 struct square
*s2
= (struct square
*)v2
;
587 static int square_sort_cmpfn(void *v1
, void *v2
)
589 struct square
*s1
= (struct square
*)v1
;
590 struct square
*s2
= (struct square
*)v2
;
593 r
= s2
->score
- s1
->score
;
598 if (s1
->random
< s2
->random
)
600 else if (s1
->random
> s2
->random
)
604 * It's _just_ possible that two squares might have been given
605 * the same random value. In that situation, fall back to
606 * comparing based on the coordinates. This introduces a tiny
607 * directional bias, but not a significant one.
609 return get_square_cmpfn(v1
, v2
);
612 static void print_tree(tree234
*tree
)
617 printf("Print tree:\n");
618 while (i
< count234(tree
)) {
619 s
= (struct square
*)index234(tree
, i
);
621 printf(" [%d,%d], %d, %d\n", s
->x
, s
->y
, s
->score
, s
->random
);
627 enum { SQUARE_LIT
, SQUARE_UNLIT
};
629 #define SQUARE_STATE(i, j) \
630 (((i) < 0 || (i) >= params->w || \
631 (j) < 0 || (j) >= params->h) ? \
632 SQUARE_UNLIT : LV_SQUARE_STATE(i,j))
634 #define LV_SQUARE_STATE(i, j) board[(i) + params->w * (j)]
636 static void print_board(const game_params
*params
, const char *board
)
642 for (i
= 0; i
< params
->w
; i
++) {
646 for (j
= 0; j
< params
->h
; j
++) {
648 for (i
= 0; i
< params
->w
; i
++) {
649 printf("%c", SQUARE_STATE(i
, j
) ?
' ' : 'O');
656 static char *new_fullyclued_board(game_params
*params
, random_state
*rs
)
662 game_state
*state
= &s
;
663 int board_area
= SQUARE_COUNT(params
);
666 struct square
*square
, *tmpsquare
, *sq
;
667 struct square square_pos
;
669 /* These will contain exactly the same information, sorted into different
671 tree234
*lightable_squares_sorted
, *lightable_squares_gettable
;
673 #define SQUARE_REACHABLE(i,j) \
674 (t = (SQUARE_STATE(i-1, j) == SQUARE_LIT || \
675 SQUARE_STATE(i+1, j) == SQUARE_LIT || \
676 SQUARE_STATE(i, j-1) == SQUARE_LIT || \
677 SQUARE_STATE(i, j+1) == SQUARE_LIT), \
678 /* printf("SQUARE_REACHABLE(%d,%d) = %d\n", i, j, t), */ \
682 /* One situation in which we may not light a square is if that'll leave one
683 * square above/below and one left/right of us unlit, separated by a lit
684 * square diagnonal from us */
685 #define SQUARE_DIAGONAL_VIOLATION(i, j, h, v) \
686 (t = (SQUARE_STATE((i)+(h), (j)) == SQUARE_UNLIT && \
687 SQUARE_STATE((i), (j)+(v)) == SQUARE_UNLIT && \
688 SQUARE_STATE((i)+(h), (j)+(v)) == SQUARE_LIT), \
689 /* t ? printf("SQUARE_DIAGONAL_VIOLATION(%d, %d, %d, %d)\n",
693 /* We also may not light a square if it will form a loop of lit squares
694 * around some unlit squares, as then the game soln won't have a single
696 #define SQUARE_LOOP_VIOLATION(i, j, lit1, lit2) \
697 (SQUARE_STATE((i)+1, (j)) == lit1 && \
698 SQUARE_STATE((i)-1, (j)) == lit1 && \
699 SQUARE_STATE((i), (j)+1) == lit2 && \
700 SQUARE_STATE((i), (j)-1) == lit2)
702 #define CAN_LIGHT_SQUARE(i, j) \
703 (SQUARE_REACHABLE(i, j) && \
704 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, -1) && \
705 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, -1) && \
706 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, +1) && \
707 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, +1) && \
708 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_LIT, SQUARE_UNLIT) && \
709 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_UNLIT, SQUARE_LIT))
711 #define IS_LIGHTING_CANDIDATE(i, j) \
712 (SQUARE_STATE(i, j) == SQUARE_UNLIT && \
713 CAN_LIGHT_SQUARE(i,j))
715 /* The 'score' of a square reflects its current desirability for selection
716 * as the next square to light. We want to encourage moving into uncharted
717 * areas so we give scores according to how many of the square's neighbours
718 * are currently unlit. */
725 #define SQUARE_SCORE(i,j) \
726 (2*((SQUARE_STATE(i-1, j) == SQUARE_UNLIT) + \
727 (SQUARE_STATE(i+1, j) == SQUARE_UNLIT) + \
728 (SQUARE_STATE(i, j-1) == SQUARE_UNLIT) + \
729 (SQUARE_STATE(i, j+1) == SQUARE_UNLIT)) - 4)
731 /* When a square gets lit, this defines how far away from that square we
732 * need to go recomputing scores */
733 #define SCORE_DISTANCE 1
735 board
= snewn(board_area
, char);
736 clues
= snewn(board_area
, char);
738 state
->h
= params
->h
;
739 state
->w
= params
->w
;
740 state
->clues
= clues
;
743 memset(board
, SQUARE_UNLIT
, board_area
);
745 /* Seed the board with a single lit square near the middle */
748 if (params
->w
& 1 && random_bits(rs
, 1))
750 if (params
->h
& 1 && random_bits(rs
, 1))
753 LV_SQUARE_STATE(i
, j
) = SQUARE_LIT
;
755 /* We need a way of favouring squares that will increase our loopiness.
756 * We do this by maintaining a list of all candidate squares sorted by
757 * their score and choose randomly from that with appropriate skew.
758 * In order to avoid consistently biasing towards particular squares, we
759 * need the sort order _within_ each group of scores to be completely
760 * random. But it would be abusing the hospitality of the tree234 data
761 * structure if our comparison function were nondeterministic :-). So with
762 * each square we associate a random number that does not change during a
763 * particular run of the generator, and use that as a secondary sort key.
764 * Yes, this means we will be biased towards particular random squares in
765 * any one run but that doesn't actually matter. */
767 lightable_squares_sorted
= newtree234(square_sort_cmpfn
);
768 lightable_squares_gettable
= newtree234(get_square_cmpfn
);
769 #define ADD_SQUARE(s) \
771 /* printf("ADD SQUARE: [%d,%d], %d, %d\n",
772 s->x, s->y, s->score, s->random);*/ \
773 sq = add234(lightable_squares_sorted, s); \
775 sq = add234(lightable_squares_gettable, s); \
779 #define REMOVE_SQUARE(s) \
781 /* printf("DELETE SQUARE: [%d,%d], %d, %d\n",
782 s->x, s->y, s->score, s->random);*/ \
783 sq = del234(lightable_squares_sorted, s); \
785 sq = del234(lightable_squares_gettable, s); \
789 #define HANDLE_DIR(a, b) \
790 square = snew(struct square); \
791 square->x = (i)+(a); \
792 square->y = (j)+(b); \
794 square->random = random_bits(rs, 31); \
802 /* Light squares one at a time until the board is interesting enough */
805 /* We have count234(lightable_squares) possibilities, and in
806 * lightable_squares_sorted they are sorted with the most desirable
808 c
= count234(lightable_squares_sorted
);
811 assert(c
== count234(lightable_squares_gettable
));
813 /* Check that the best square available is any good */
814 square
= (struct square
*)index234(lightable_squares_sorted
, 0);
818 * We never want to _decrease_ the loop's perimeter. Making
819 * moves that leave the perimeter the same is occasionally
820 * useful: if it were _never_ done then the user would be
821 * able to deduce illicitly that any degree-zero vertex was
822 * on the outside of the loop. So we do it sometimes but
825 if (square
->score
< 0 || (square
->score
== 0 &&
826 random_upto(rs
, 2) == 0))
829 print_tree(lightable_squares_sorted
);
830 assert(square
->score
== SQUARE_SCORE(square
->x
, square
->y
));
831 assert(SQUARE_STATE(square
->x
, square
->y
) == SQUARE_UNLIT
);
832 assert(square
->x
>= 0 && square
->x
< params
->w
);
833 assert(square
->y
>= 0 && square
->y
< params
->h
);
834 /* printf("LIGHT SQUARE: [%d,%d], score = %d\n", square->x, square->y, square->score); */
836 /* Update data structures */
837 LV_SQUARE_STATE(square
->x
, square
->y
) = SQUARE_LIT
;
838 REMOVE_SQUARE(square
);
840 print_board(params
, board
);
842 /* We might have changed the score of any squares up to 2 units away in
844 for (b
= -SCORE_DISTANCE
; b
<= SCORE_DISTANCE
; b
++) {
845 for (a
= -SCORE_DISTANCE
; a
<= SCORE_DISTANCE
; a
++) {
848 square_pos
.x
= square
->x
+ a
;
849 square_pos
.y
= square
->y
+ b
;
850 /* printf("Refreshing score for [%d,%d]:\n", square_pos.x, square_pos.y); */
851 if (square_pos
.x
< 0 || square_pos
.x
>= params
->w
||
852 square_pos
.y
< 0 || square_pos
.y
>= params
->h
) {
853 /* printf(" Out of bounds\n"); */
856 tmpsquare
= find234(lightable_squares_gettable
, &square_pos
,
859 /* printf(" Removing\n"); */
860 assert(tmpsquare
->x
== square_pos
.x
);
861 assert(tmpsquare
->y
== square_pos
.y
);
862 assert(SQUARE_STATE(tmpsquare
->x
, tmpsquare
->y
) ==
864 REMOVE_SQUARE(tmpsquare
);
866 /* printf(" Creating\n"); */
867 tmpsquare
= snew(struct square
);
868 tmpsquare
->x
= square_pos
.x
;
869 tmpsquare
->y
= square_pos
.y
;
870 tmpsquare
->random
= random_bits(rs
, 31);
872 tmpsquare
->score
= SQUARE_SCORE(tmpsquare
->x
, tmpsquare
->y
);
874 if (IS_LIGHTING_CANDIDATE(tmpsquare
->x
, tmpsquare
->y
)) {
875 /* printf(" Adding\n"); */
876 ADD_SQUARE(tmpsquare
);
878 /* printf(" Destroying\n"); */
884 /* printf("\n\n"); */
887 while ((square
= delpos234(lightable_squares_gettable
, 0)) != NULL
)
889 freetree234(lightable_squares_gettable
);
890 freetree234(lightable_squares_sorted
);
892 /* Copy out all the clues */
893 for (j
= 0; j
< params
->h
; ++j
) {
894 for (i
= 0; i
< params
->w
; ++i
) {
895 c
= SQUARE_STATE(i
, j
);
896 LV_CLUE_AT(state
, i
, j
) = '0';
897 if (SQUARE_STATE(i
-1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
898 if (SQUARE_STATE(i
+1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
899 if (SQUARE_STATE(i
, j
-1) != c
) ++LV_CLUE_AT(state
, i
, j
);
900 if (SQUARE_STATE(i
, j
+1) != c
) ++LV_CLUE_AT(state
, i
, j
);
908 static solver_state
*solve_game_rec(const solver_state
*sstate
, int diff
);
910 static int game_has_unique_soln(const game_state
*state
, int diff
)
913 solver_state
*sstate_new
;
914 solver_state
*sstate
= new_solver_state((game_state
*)state
);
916 sstate_new
= solve_game_rec(sstate
, diff
);
918 ret
= (sstate_new
->solver_status
== SOLVER_SOLVED
);
920 free_solver_state(sstate_new
);
921 free_solver_state(sstate
);
926 /* Remove clues one at a time at random. */
927 static game_state
*remove_clues(game_state
*state
, random_state
*rs
, int diff
)
929 int *square_list
, squares
;
930 game_state
*ret
= dup_game(state
), *saved_ret
;
933 /* We need to remove some clues. We'll do this by forming a list of all
934 * available equivalence classes, shuffling it, then going along one at a
935 * time clearing every member of each equivalence class, where removing a
936 * class doesn't render the board unsolvable. */
937 squares
= state
->w
* state
->h
;
938 square_list
= snewn(squares
, int);
939 for (n
= 0; n
< squares
; ++n
) {
943 shuffle(square_list
, squares
, sizeof(int), rs
);
945 for (n
= 0; n
< squares
; ++n
) {
946 saved_ret
= dup_game(ret
);
947 LV_CLUE_AT(ret
, square_list
[n
] % state
->w
,
948 square_list
[n
] / state
->w
) = ' ';
949 if (game_has_unique_soln(ret
, diff
)) {
950 free_game(saved_ret
);
961 static char *validate_desc(game_params
*params
, char *desc
);
963 static char *new_game_desc(game_params
*params
, random_state
*rs
,
964 char **aux
, int interactive
)
966 /* solution and description both use run-length encoding in obvious ways */
968 char *description
= snewn(SQUARE_COUNT(params
) + 1, char);
969 char *dp
= description
;
972 game_state
*state
= snew(game_state
), *state_new
;
974 state
->h
= params
->h
;
975 state
->w
= params
->w
;
977 state
->hl
= snewn(HL_COUNT(params
), char);
978 state
->vl
= snewn(VL_COUNT(params
), char);
981 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
982 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
984 state
->solved
= state
->cheated
= FALSE
;
985 state
->recursion_depth
= params
->rec
;
987 /* Get a new random solvable board with all its clues filled in. Yes, this
988 * can loop for ever if the params are suitably unfavourable, but
989 * preventing games smaller than 4x4 seems to stop this happening */
992 state
->clues
= new_fullyclued_board(params
, rs
);
993 } while (!game_has_unique_soln(state
, params
->diff
));
995 state_new
= remove_clues(state
, rs
, params
->diff
);
999 if (params
->diff
> 0 && game_has_unique_soln(state
, params
->diff
-1)) {
1000 /* Board is too easy */
1001 goto newboard_please
;
1005 for (j
= 0; j
< params
->h
; ++j
) {
1006 for (i
= 0; i
< params
->w
; ++i
) {
1007 if (CLUE_AT(state
, i
, j
) == ' ') {
1008 if (empty_count
> 25) {
1009 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
1015 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
1018 dp
+= sprintf(dp
, "%c", (int)(CLUE_AT(state
, i
, j
)));
1023 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
1026 retval
= dupstr(description
);
1029 assert(!validate_desc(params
, retval
));
1034 /* We require that the params pass the test in validate_params and that the
1035 * description fills the entire game area */
1036 static char *validate_desc(game_params
*params
, char *desc
)
1040 for (; *desc
; ++desc
) {
1041 if (*desc
>= '0' && *desc
<= '9') {
1046 count
+= *desc
- 'a' + 1;
1049 return "Unknown character in description";
1052 if (count
< SQUARE_COUNT(params
))
1053 return "Description too short for board size";
1054 if (count
> SQUARE_COUNT(params
))
1055 return "Description too long for board size";
1060 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1063 game_state
*state
= snew(game_state
);
1064 int empties_to_make
= 0;
1066 const char *dp
= desc
;
1068 state
->recursion_depth
= 0; /* XXX pending removal, probably */
1070 state
->h
= params
->h
;
1071 state
->w
= params
->w
;
1073 state
->clues
= snewn(SQUARE_COUNT(params
), char);
1074 state
->hl
= snewn(HL_COUNT(params
), char);
1075 state
->vl
= snewn(VL_COUNT(params
), char);
1077 state
->solved
= state
->cheated
= FALSE
;
1079 for (j
= 0 ; j
< params
->h
; ++j
) {
1080 for (i
= 0 ; i
< params
->w
; ++i
) {
1081 if (empties_to_make
) {
1083 LV_CLUE_AT(state
, i
, j
) = ' ';
1089 if (n
>=0 && n
< 10) {
1090 LV_CLUE_AT(state
, i
, j
) = *dp
;
1094 LV_CLUE_AT(state
, i
, j
) = ' ';
1095 empties_to_make
= n
- 1;
1101 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
1102 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
1107 enum { LOOP_NONE
=0, LOOP_SOLN
, LOOP_NOT_SOLN
};
1109 /* Starting at dot [i,j] moves around 'state' removing lines until it's clear
1110 * whether or not the starting dot was on a loop. Returns boolean specifying
1111 * whether a loop was found. loop_status calls this and assumes that if state
1112 * has any lines set, this function will always remove at least one. */
1113 static int destructively_find_loop(game_state
*state
)
1115 int a
, b
, i
, j
, new_i
, new_j
, n
;
1118 lp
= (char *)memchr(state
->hl
, LINE_YES
, HL_COUNT(state
));
1120 /* We know we're going to return false but we have to fulfil our
1122 lp
= (char *)memchr(state
->vl
, LINE_YES
, VL_COUNT(state
));
1134 assert(i
+ j
* state
->w
== n
); /* because I'm feeling stupid */
1135 /* Save start position */
1139 /* Delete one line from the potential loop */
1140 if (LEFTOF_DOT(state
, i
, j
) == LINE_YES
) {
1141 LV_LEFTOF_DOT(state
, i
, j
) = LINE_NO
;
1143 } else if (ABOVE_DOT(state
, i
, j
) == LINE_YES
) {
1144 LV_ABOVE_DOT(state
, i
, j
) = LINE_NO
;
1146 } else if (RIGHTOF_DOT(state
, i
, j
) == LINE_YES
) {
1147 LV_RIGHTOF_DOT(state
, i
, j
) = LINE_NO
;
1149 } else if (BELOW_DOT(state
, i
, j
) == LINE_YES
) {
1150 LV_BELOW_DOT(state
, i
, j
) = LINE_NO
;
1157 /* From the current position of [i,j] there needs to be exactly one
1161 #define HANDLE_DIR(dir_dot, x, y) \
1162 if (dir_dot(state, i, j) == LINE_YES) { \
1163 if (new_i != -1 || new_j != -1) \
1167 LV_##dir_dot(state, i, j) = LINE_NO; \
1169 HANDLE_DIR(ABOVE_DOT
, 0, -1);
1170 HANDLE_DIR(BELOW_DOT
, 0, +1);
1171 HANDLE_DIR(LEFTOF_DOT
, -1, 0);
1172 HANDLE_DIR(RIGHTOF_DOT
, +1, 0);
1174 if (new_i
== -1 || new_j
== -1) {
1180 } while (i
!= a
|| j
!= b
);
1185 static int loop_status(game_state
*state
)
1188 game_state
*tmpstate
;
1189 int loop_found
= FALSE
, non_loop_found
= FALSE
, any_lines_found
= FALSE
;
1191 #define BAD_LOOP_FOUND \
1192 do { free_game(tmpstate); return LOOP_NOT_SOLN; } while(0)
1194 /* Repeatedly look for loops until we either run out of lines to consider
1195 * or discover for sure that the board fails on the grounds of having no
1197 tmpstate
= dup_game(state
);
1200 if (!memchr(tmpstate
->hl
, LINE_YES
, HL_COUNT(tmpstate
)) &&
1201 !memchr(tmpstate
->vl
, LINE_YES
, VL_COUNT(tmpstate
))) {
1204 any_lines_found
= TRUE
;
1208 if (destructively_find_loop(tmpstate
)) {
1213 non_loop_found
= TRUE
;
1217 free_game(tmpstate
);
1219 if (!any_lines_found
)
1222 if (non_loop_found
) {
1223 assert(!loop_found
); /* should have dealt with this already */
1227 /* Check that every clue is satisfied */
1228 for (j
= 0; j
< state
->h
; ++j
) {
1229 for (i
= 0; i
< state
->w
; ++i
) {
1230 n
= CLUE_AT(state
, i
, j
);
1232 if (square_order(state
, i
, j
, LINE_YES
) != n
- '0') {
1233 return LOOP_NOT_SOLN
;
1242 /* Sums the lengths of the numbers in range [0,n) */
1243 /* See equivalent function in solo.c for justification of this. */
1244 static int len_0_to_n(int n
)
1246 int len
= 1; /* Counting 0 as a bit of a special case */
1249 for (i
= 1; i
< n
; i
*= 10) {
1250 len
+= max(n
- i
, 0);
1256 static char *encode_solve_move(const game_state
*state
)
1260 /* This is going to return a string representing the moves needed to set
1261 * every line in a grid to be the same as the ones in 'state'. The exact
1262 * length of this string is predictable. */
1264 len
= 1; /* Count the 'S' prefix */
1265 /* Numbers in horizontal lines */
1266 /* Horizontal lines, x position */
1267 len
+= len_0_to_n(state
->w
) * (state
->h
+ 1);
1268 /* Horizontal lines, y position */
1269 len
+= len_0_to_n(state
->h
+ 1) * (state
->w
);
1270 /* Vertical lines, y position */
1271 len
+= len_0_to_n(state
->h
) * (state
->w
+ 1);
1272 /* Vertical lines, x position */
1273 len
+= len_0_to_n(state
->w
+ 1) * (state
->h
);
1274 /* For each line we also have two letters and a comma */
1275 len
+= 3 * (HL_COUNT(state
) + VL_COUNT(state
));
1277 ret
= snewn(len
+ 1, char);
1280 p
+= sprintf(p
, "S");
1282 for (j
= 0; j
< state
->h
+ 1; ++j
) {
1283 for (i
= 0; i
< state
->w
; ++i
) {
1284 switch (RIGHTOF_DOT(state
, i
, j
)) {
1286 p
+= sprintf(p
, "%d,%dhy", i
, j
);
1289 p
+= sprintf(p
, "%d,%dhn", i
, j
);
1292 /* I'm going to forgive this because I think the results
1294 /* assert(!"Solver produced incomplete solution!"); */
1299 for (j
= 0; j
< state
->h
; ++j
) {
1300 for (i
= 0; i
< state
->w
+ 1; ++i
) {
1301 switch (BELOW_DOT(state
, i
, j
)) {
1303 p
+= sprintf(p
, "%d,%dvy", i
, j
);
1306 p
+= sprintf(p
, "%d,%dvn", i
, j
);
1309 /* I'm going to forgive this because I think the results
1311 /* assert(!"Solver produced incomplete solution!"); */
1316 /* No point in doing sums like that if they're going to be wrong */
1317 assert(strlen(ret
) == (size_t)len
);
1321 /* BEGIN SOLVER IMPLEMENTATION */
1323 /* For each pair of lines through each dot we store a bit for whether
1324 * exactly one of those lines is ON, and in separate arrays we store whether
1325 * at least one is on and whether at most 1 is on. (If we know both or
1326 * neither is on that's already stored more directly.) That's six bits per
1327 * dot. Bit number n represents the lines shown in dot_type_dirs[n]. */
1338 #define OPP_DLINE(dline) (dline ^ 1)
1341 #define SQUARE_DLINES \
1342 HANDLE_DLINE(DLINE_UL, RIGHTOF_SQUARE, BELOW_SQUARE, 1, 1); \
1343 HANDLE_DLINE(DLINE_UR, LEFTOF_SQUARE, BELOW_SQUARE, 0, 1); \
1344 HANDLE_DLINE(DLINE_DL, RIGHTOF_SQUARE, ABOVE_SQUARE, 1, 0); \
1345 HANDLE_DLINE(DLINE_DR, LEFTOF_SQUARE, ABOVE_SQUARE, 0, 0);
1347 #define DOT_DLINES \
1348 HANDLE_DLINE(DLINE_VERT, ABOVE_DOT, BELOW_DOT); \
1349 HANDLE_DLINE(DLINE_HORIZ, LEFTOF_DOT, RIGHTOF_DOT); \
1350 HANDLE_DLINE(DLINE_UL, ABOVE_DOT, LEFTOF_DOT); \
1351 HANDLE_DLINE(DLINE_UR, ABOVE_DOT, RIGHTOF_DOT); \
1352 HANDLE_DLINE(DLINE_DL, BELOW_DOT, LEFTOF_DOT); \
1353 HANDLE_DLINE(DLINE_DR, BELOW_DOT, RIGHTOF_DOT);
1355 static void array_setall(char *array
, char from
, char to
, int len
)
1357 char *p
= array
, *p_old
= p
;
1358 int len_remaining
= len
;
1360 while ((p
= memchr(p
, from
, len_remaining
))) {
1362 len_remaining
-= p
- p_old
;
1368 static int game_states_equal(const game_state
*state1
,
1369 const game_state
*state2
)
1371 /* This deliberately doesn't check _all_ fields, just the ones that make a
1372 * game state 'interesting' from the POV of the solver */
1373 /* XXX review this */
1374 if (state1
== state2
)
1377 if (!state1
|| !state2
)
1380 if (state1
->w
!= state2
->w
|| state1
->h
!= state2
->h
)
1383 if (memcmp(state1
->hl
, state2
->hl
, HL_COUNT(state1
)))
1386 if (memcmp(state1
->vl
, state2
->vl
, VL_COUNT(state1
)))
1392 static int solver_states_equal(const solver_state
*sstate1
,
1393 const solver_state
*sstate2
)
1402 if (!game_states_equal(sstate1
->state
, sstate2
->state
)) {
1406 /* XXX fields missing, needs review */
1407 /* XXX we're deliberately not looking at solver_state as it's only a cache */
1409 if (memcmp(sstate1
->dot_atleastone
, sstate2
->dot_atleastone
,
1410 DOT_COUNT(sstate1
->state
))) {
1414 if (memcmp(sstate1
->dot_atmostone
, sstate2
->dot_atmostone
,
1415 DOT_COUNT(sstate1
->state
))) {
1419 /* handle dline_identical here */
1424 static void dot_setall_dlines(solver_state
*sstate
, enum dline dl
, int i
, int j
,
1425 enum line_state line_old
, enum line_state line_new
)
1427 game_state
*state
= sstate
->state
;
1429 /* First line in dline */
1434 if (j
> 0 && ABOVE_DOT(state
, i
, j
) == line_old
)
1435 LV_ABOVE_DOT(state
, i
, j
) = line_new
;
1439 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1440 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1443 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1444 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1448 /* Second line in dline */
1452 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1453 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1458 if (i
<= (state
)->w
&& RIGHTOF_DOT(state
, i
, j
) == line_old
)
1459 LV_RIGHTOF_DOT(state
, i
, j
) = line_new
;
1462 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1463 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1468 static void update_solver_status(solver_state
*sstate
)
1470 if (sstate
->solver_status
== SOLVER_INCOMPLETE
) {
1471 switch (loop_status(sstate
->state
)) {
1473 sstate
->solver_status
= SOLVER_INCOMPLETE
;
1476 if (sstate
->solver_status
!= SOLVER_AMBIGUOUS
)
1477 sstate
->solver_status
= SOLVER_SOLVED
;
1480 sstate
->solver_status
= SOLVER_MISTAKE
;
1487 /* This will fail an assertion if {dx,dy} are anything other than {-1,0}, {1,0}
1488 * {0,-1} or {0,1} */
1489 static int line_status_from_point(const game_state
*state
,
1490 int x
, int y
, int dx
, int dy
)
1492 if (dx
== -1 && dy
== 0)
1493 return LEFTOF_DOT(state
, x
, y
);
1494 if (dx
== 1 && dy
== 0)
1495 return RIGHTOF_DOT(state
, x
, y
);
1496 if (dx
== 0 && dy
== -1)
1497 return ABOVE_DOT(state
, x
, y
);
1498 if (dx
== 0 && dy
== 1)
1499 return BELOW_DOT(state
, x
, y
);
1501 assert(!"Illegal dx or dy in line_status_from_point");
1506 /* This will return a dynamically allocated solver_state containing the (more)
1508 static solver_state
*solve_game_rec(const solver_state
*sstate_start
, int diff
)
1511 int current_yes
, current_no
, desired
;
1512 solver_state
*sstate
, *sstate_saved
, *sstate_tmp
;
1515 solver_state
*sstate_rec_solved
;
1516 int recursive_soln_count
;
1519 printf("solve_game_rec: recursion_remaining = %d\n",
1520 sstate_start
->recursion_remaining
);
1523 sstate
= dup_solver_state((solver_state
*)sstate_start
);
1526 text
= game_text_format(sstate
->state
);
1527 printf("%s\n", text
);
1531 #define RETURN_IF_SOLVED \
1533 update_solver_status(sstate); \
1534 if (sstate->solver_status != SOLVER_INCOMPLETE) { \
1535 free_solver_state(sstate_saved); \
1540 #define FOUND_MISTAKE \
1542 sstate->solver_status = SOLVER_MISTAKE; \
1543 free_solver_state(sstate_saved); \
1548 sstate_saved
= NULL
;
1551 nonrecursive_solver
:
1554 sstate_saved
= dup_solver_state(sstate
);
1556 /* First we do the 'easy' work, that might cause concrete results */
1558 /* Per-square deductions */
1559 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1560 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1561 /* Begin rules that look at the clue (if there is one) */
1562 desired
= CLUE_AT(sstate
->state
, i
, j
);
1565 desired
= desired
- '0';
1566 current_yes
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1567 current_no
= square_order(sstate
->state
, i
, j
, LINE_NO
);
1569 if (desired
< current_yes
)
1571 if (desired
== current_yes
) {
1572 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1576 if (4 - desired
< current_no
)
1578 if (4 - desired
== current_no
) {
1579 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_YES
);
1586 /* Per-dot deductions */
1587 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1588 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1589 switch (dot_order(sstate
->state
, i
, j
, LINE_YES
)) {
1591 if (dot_order(sstate
->state
, i
, j
, LINE_NO
) == 3) {
1592 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1596 switch (dot_order(sstate
->state
, i
, j
, LINE_NO
)) {
1597 #define H1(dline, dir1_dot, dir2_dot, dot_howmany) \
1598 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1599 if (dir2_dot(sstate->state, i, j) == LINE_UNKNOWN){ \
1600 sstate->dot_howmany \
1601 [i + (sstate->state->w + 1) * j] |= 1<<dline; \
1605 if (diff
> DIFF_EASY
) {
1606 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1607 H1(dline, dir1_dot, dir2_dot, dot_atleastone)
1608 /* 1 yes, 1 no, so exactly one of unknowns is
1615 if (diff
> DIFF_EASY
) {
1616 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1617 H1(dline, dir1_dot, dir2_dot, dot_atmostone)
1618 /* 1 yes, fewer than 2 no, so at most one of
1619 * unknowns is yes */
1625 case 2: /* 1 yes, 2 no */
1626 dot_setall(sstate
->state
, i
, j
,
1627 LINE_UNKNOWN
, LINE_YES
);
1632 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1638 if (diff
> DIFF_EASY
) {
1639 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1640 if (sstate->dot_atleastone \
1641 [i + (sstate->state->w + 1) * j] & 1<<dline) { \
1642 sstate->dot_atmostone \
1643 [i + (sstate->state->w + 1) * j] |= 1<<OPP_DLINE(dline); \
1645 /* If at least one of a dline in a dot is YES, at most one
1646 * of the opposite dline to that dot must be YES. */
1653 /* More obscure per-square operations */
1654 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1655 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1656 #define H1(dline, dir1_sq, dir2_sq, a, b, dot_howmany, line_query, line_set) \
1657 if (sstate->dot_howmany[i+a + (sstate->state->w + 1) * (j+b)] &\
1659 t = dir1_sq(sstate->state, i, j); \
1660 if (t == line_query) \
1661 dir2_sq(sstate->state, i, j) = line_set; \
1663 t = dir2_sq(sstate->state, i, j); \
1664 if (t == line_query) \
1665 dir1_sq(sstate->state, i, j) = line_set; \
1668 if (diff
> DIFF_EASY
) {
1669 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1670 H1(dline, dir1_sq, dir2_sq, a, b, dot_atmostone, \
1672 /* If at most one of the DLINE is on, and one is definitely
1673 * on, set the other to definitely off */
1678 if (diff
> DIFF_EASY
) {
1679 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1680 H1(dline, dir1_sq, dir2_sq, a, b, dot_atleastone, \
1682 /* If at least one of the DLINE is on, and one is definitely
1683 * off, set the other to definitely on */
1689 switch (CLUE_AT(sstate
->state
, i
, j
)) {
1692 if (diff
> DIFF_EASY
) {
1693 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1694 /* At most one of any DLINE can be set */ \
1695 sstate->dot_atmostone \
1696 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1697 /* This DLINE provides enough YESes to solve the clue */\
1698 if (sstate->dot_atleastone \
1699 [i+a + (sstate->state->w + 1) * (j+b)] & \
1701 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1703 LINE_UNKNOWN, LINE_NO); \
1710 if (diff
> DIFF_EASY
) {
1711 #define H1(dline, dot_at1one, dot_at2one, a, b) \
1712 if (sstate->dot_at1one \
1713 [i+a + (sstate->state->w + 1) * (j+b)] & \
1715 sstate->dot_at2one \
1716 [i+(1-a) + (sstate->state->w + 1) * (j+(1-b))] |= \
1717 1<<OPP_DLINE(dline); \
1719 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1720 H1(dline, dot_atleastone, dot_atmostone, a, b); \
1721 H1(dline, dot_atmostone, dot_atleastone, a, b);
1722 /* If at least one of one DLINE is set, at most one
1723 * of the opposing one is and vice versa */
1731 if (diff
> DIFF_EASY
) {
1732 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1733 /* At least one of any DLINE can be set */ \
1734 sstate->dot_atleastone \
1735 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1736 /* This DLINE provides enough NOs to solve the clue */ \
1737 if (sstate->dot_atmostone \
1738 [i+a + (sstate->state->w + 1) * (j+b)] & \
1740 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1742 LINE_UNKNOWN, LINE_YES); \
1752 if (solver_states_equal(sstate
, sstate_saved
)) {
1753 int edgecount
= 0, clues
= 0, satclues
= 0, sm1clues
= 0;
1757 * Go through the grid and update for all the new edges.
1758 * Since merge_dots() is idempotent, the simplest way to
1759 * do this is just to update for _all_ the edges.
1761 * Also, while we're here, we count the edges, count the
1762 * clues, count the satisfied clues, and count the
1763 * satisfied-minus-one clues.
1765 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1766 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1767 if (RIGHTOF_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1768 merge_dots(sstate
, i
, j
, i
+1, j
);
1771 if (BELOW_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1772 merge_dots(sstate
, i
, j
, i
, j
+1);
1776 if (CLUE_AT(sstate
->state
, i
, j
) != ' ') {
1777 int c
= CLUE_AT(sstate
->state
, i
, j
) - '0';
1778 int o
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1789 * Now go through looking for LINE_UNKNOWN edges which
1790 * connect two dots that are already in the same
1791 * equivalence class. If we find one, test to see if the
1792 * loop it would create is a solution.
1794 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1795 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1796 for (d
= 0; d
< 2; d
++) {
1797 int i2
, j2
, eqclass
, val
;
1800 if (RIGHTOF_DOT(sstate
->state
, i
, j
) !=
1806 if (BELOW_DOT(sstate
->state
, i
, j
) !=
1813 eqclass
= dsf_canonify(sstate
->dotdsf
,
1814 j
* (sstate
->state
->w
+1) + i
);
1815 if (eqclass
!= dsf_canonify(sstate
->dotdsf
,
1816 j2
* (sstate
->state
->w
+1) +
1820 val
= LINE_NO
; /* loop is bad until proven otherwise */
1823 * This edge would form a loop. Next
1824 * question: how long would the loop be?
1825 * Would it equal the total number of edges
1826 * (plus the one we'd be adding if we added
1829 if (sstate
->looplen
[eqclass
] == edgecount
+ 1) {
1834 * This edge would form a loop which
1835 * took in all the edges in the entire
1836 * grid. So now we need to work out
1837 * whether it would be a valid solution
1838 * to the puzzle, which means we have to
1839 * check if it satisfies all the clues.
1840 * This means that every clue must be
1841 * either satisfied or satisfied-minus-
1842 * 1, and also that the number of
1843 * satisfied-minus-1 clues must be at
1844 * most two and they must lie on either
1845 * side of this edge.
1850 if (CLUE_AT(sstate
->state
, cx
,cy
) != ' ' &&
1851 square_order(sstate
->state
, cx
,cy
, LINE_YES
) ==
1852 CLUE_AT(sstate
->state
, cx
,cy
) - '0' - 1)
1854 if (CLUE_AT(sstate
->state
, i
, j
) != ' ' &&
1855 square_order(sstate
->state
, i
, j
, LINE_YES
) ==
1856 CLUE_AT(sstate
->state
, i
, j
) - '0' - 1)
1858 if (sm1clues
== sm1_nearby
&&
1859 sm1clues
+ satclues
== clues
)
1860 val
= LINE_YES
; /* loop is good! */
1864 * Right. Now we know that adding this edge
1865 * would form a loop, and we know whether
1866 * that loop would be a viable solution or
1869 * If adding this edge produces a solution,
1870 * then we know we've found _a_ solution but
1871 * we don't know that it's _the_ solution -
1872 * if it were provably the solution then
1873 * we'd have deduced this edge some time ago
1874 * without the need to do loop detection. So
1875 * in this state we return SOLVER_AMBIGUOUS,
1876 * which has the effect that hitting Solve
1877 * on a user-provided puzzle will fill in a
1878 * solution but using the solver to
1879 * construct new puzzles won't consider this
1880 * a reasonable deduction for the user to
1884 LV_RIGHTOF_DOT(sstate
->state
, i
, j
) = val
;
1886 LV_BELOW_DOT(sstate
->state
, i
, j
) = val
;
1887 if (val
== LINE_YES
) {
1888 sstate
->solver_status
= SOLVER_AMBIGUOUS
;
1889 goto finished_loop_checking
;
1895 finished_loop_checking
:
1900 if (solver_states_equal(sstate
, sstate_saved
)) {
1901 /* Solver has stopped making progress so we terminate */
1902 free_solver_state(sstate_saved
);
1906 free_solver_state(sstate_saved
);
1909 if (sstate
->solver_status
== SOLVER_SOLVED
||
1910 sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
1911 /* s/LINE_UNKNOWN/LINE_NO/g */
1912 array_setall(sstate
->state
->hl
, LINE_UNKNOWN
, LINE_NO
,
1913 HL_COUNT(sstate
->state
));
1914 array_setall(sstate
->state
->vl
, LINE_UNKNOWN
, LINE_NO
,
1915 VL_COUNT(sstate
->state
));
1919 /* Perform recursive calls */
1920 if (sstate
->recursion_remaining
) {
1921 sstate_saved
= dup_solver_state(sstate
);
1923 sstate
->recursion_remaining
--;
1925 recursive_soln_count
= 0;
1926 sstate_rec_solved
= NULL
;
1928 /* Memory management:
1929 * sstate_saved won't be modified but needs to be freed when we have
1931 * sstate is expected to contain our 'best' solution by the time we
1932 * finish this section of code. It's the thing we'll try adding lines
1933 * to, seeing if they make it more solvable.
1934 * If sstate_rec_solved is non-NULL, it will supersede sstate
1935 * eventually. sstate_tmp should not hold a value persistently.
1938 /* NB SOLVER_AMBIGUOUS is like SOLVER_SOLVED except the solver is aware
1939 * of the possibility of additional solutions. So as soon as we have a
1940 * SOLVER_AMBIGUOUS we can safely propagate it back to our caller, but
1941 * if we get a SOLVER_SOLVED we want to keep trying in case we find
1942 * further solutions and have to mark it ambiguous.
1945 #define DO_RECURSIVE_CALL(dir_dot) \
1946 if (dir_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1947 debug(("Trying " #dir_dot " at [%d,%d]\n", i, j)); \
1948 LV_##dir_dot(sstate->state, i, j) = LINE_YES; \
1949 sstate_tmp = solve_game_rec(sstate, diff); \
1950 switch (sstate_tmp->solver_status) { \
1951 case SOLVER_AMBIGUOUS: \
1952 debug(("Solver ambiguous, returning\n")); \
1953 sstate_rec_solved = sstate_tmp; \
1954 goto finished_recursion; \
1955 case SOLVER_SOLVED: \
1956 switch (++recursive_soln_count) { \
1958 debug(("One solution found\n")); \
1959 sstate_rec_solved = sstate_tmp; \
1962 debug(("Ambiguous solutions found\n")); \
1963 free_solver_state(sstate_tmp); \
1964 sstate_rec_solved->solver_status = SOLVER_AMBIGUOUS;\
1965 goto finished_recursion; \
1967 assert(!"recursive_soln_count out of range"); \
1971 case SOLVER_MISTAKE: \
1972 debug(("Non-solution found\n")); \
1973 free_solver_state(sstate_tmp); \
1974 free_solver_state(sstate_saved); \
1975 LV_##dir_dot(sstate->state, i, j) = LINE_NO; \
1976 goto nonrecursive_solver; \
1977 case SOLVER_INCOMPLETE: \
1978 debug(("Recursive step inconclusive\n")); \
1979 free_solver_state(sstate_tmp); \
1982 free_solver_state(sstate); \
1983 sstate = dup_solver_state(sstate_saved); \
1986 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1987 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1988 /* Only perform recursive calls on 'loose ends' */
1989 if (dot_order(sstate
->state
, i
, j
, LINE_YES
) == 1) {
1990 DO_RECURSIVE_CALL(LEFTOF_DOT
);
1991 DO_RECURSIVE_CALL(RIGHTOF_DOT
);
1992 DO_RECURSIVE_CALL(ABOVE_DOT
);
1993 DO_RECURSIVE_CALL(BELOW_DOT
);
2000 if (sstate_rec_solved
) {
2001 free_solver_state(sstate
);
2002 sstate
= sstate_rec_solved
;
2009 /* XXX bits of solver that may come in handy one day */
2011 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
2012 /* dline from this dot that's entirely unknown must have
2013 * both lines identical */ \
2014 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN && \
2015 dir2_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
2016 sstate->dline_identical[i + (sstate->state->w + 1) * j] |= \
2018 } else if (sstate->dline_identical[i +
2019 (sstate
->state
->w
+ 1) * j
] &\
2021 /* If they're identical and one is known do the obvious
2023 t
= dir1_dot(sstate
->state
, i
, j
); \
2024 if (t
!= LINE_UNKNOWN
) \
2025 dir2_dot(sstate
->state
, i
, j
) = t
; \
2027 t
= dir2_dot(sstate
->state
, i
, j
); \
2028 if (t
!= LINE_UNKNOWN
) \
2029 dir1_dot(sstate
->state
, i
, j
) = t
; \
2037 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
2038 if (sstate->dline_identical[i+a + \
2039 (sstate->state->w + 1) * (j+b)] &\
2041 dir1_sq(sstate->state, i, j) = LINE_YES; \
2042 dir2_sq(sstate->state, i, j) = LINE_YES; \
2044 /* If two lines are the same they must be on */
2051 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
2052 if (sstate->dot_atmostone[i+a + (sstate->state->w + 1) * (j+b)] & \
2054 if (square_order(sstate->state, i, j, LINE_UNKNOWN) - 1 == \
2055 CLUE_AT(sstate->state, i, j) - '0') { \
2056 square_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_YES); \
2057 /* XXX the following may overwrite known data! */ \
2058 dir1_sq(sstate->state, i, j) = LINE_UNKNOWN; \
2059 dir2_sq(sstate->state, i, j) = LINE_UNKNOWN; \
2067 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
2068 if (sstate->dline_identical[i+a +
2069 (sstate
->state
->w
+ 1) * (j
+b
)] &\
2071 dir1_sq(sstate
->state
, i
, j
) = LINE_NO
; \
2072 dir2_sq(sstate
->state
, i
, j
) = LINE_NO
; \
2074 /* If two lines are the same they must be off */
2079 static char *solve_game(game_state
*state
, game_state
*currstate
,
2080 char *aux
, char **error
)
2083 solver_state
*sstate
, *new_sstate
;
2085 sstate
= new_solver_state(state
);
2086 new_sstate
= solve_game_rec(sstate
, DIFFCOUNT
);
2088 if (new_sstate
->solver_status
== SOLVER_SOLVED
) {
2089 soln
= encode_solve_move(new_sstate
->state
);
2090 } else if (new_sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
2091 soln
= encode_solve_move(new_sstate
->state
);
2092 /**error = "Solver found ambiguous solutions"; */
2094 soln
= encode_solve_move(new_sstate
->state
);
2095 /**error = "Solver failed"; */
2098 free_solver_state(new_sstate
);
2099 free_solver_state(sstate
);
2104 static char *game_text_format(game_state
*state
)
2110 len
= (2 * state
->w
+ 2) * (2 * state
->h
+ 1);
2111 rp
= ret
= snewn(len
+ 1, char);
2114 switch (ABOVE_SQUARE(state, i, j)) { \
2116 rp += sprintf(rp, " -"); \
2119 rp += sprintf(rp, " x"); \
2121 case LINE_UNKNOWN: \
2122 rp += sprintf(rp, " "); \
2125 assert(!"Illegal line state for HL");\
2129 switch (LEFTOF_SQUARE(state, i, j)) {\
2131 rp += sprintf(rp, "|"); \
2134 rp += sprintf(rp, "x"); \
2136 case LINE_UNKNOWN: \
2137 rp += sprintf(rp, " "); \
2140 assert(!"Illegal line state for VL");\
2143 for (j
= 0; j
< state
->h
; ++j
) {
2144 for (i
= 0; i
< state
->w
; ++i
) {
2147 rp
+= sprintf(rp
, " \n");
2148 for (i
= 0; i
< state
->w
; ++i
) {
2150 rp
+= sprintf(rp
, "%c", (int)(CLUE_AT(state
, i
, j
)));
2153 rp
+= sprintf(rp
, "\n");
2155 for (i
= 0; i
< state
->w
; ++i
) {
2158 rp
+= sprintf(rp
, " \n");
2160 assert(strlen(ret
) == len
);
2164 static game_ui
*new_ui(game_state
*state
)
2169 static void free_ui(game_ui
*ui
)
2173 static char *encode_ui(game_ui
*ui
)
2178 static void decode_ui(game_ui
*ui
, char *encoding
)
2182 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2183 game_state
*newstate
)
2187 struct game_drawstate
{
2195 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
2196 int x
, int y
, int button
)
2201 char button_char
= ' ';
2202 enum line_state old_state
;
2204 button
&= ~MOD_MASK
;
2206 /* Around each line is a diamond-shaped region where points within that
2207 * region are closer to this line than any other. We assume any click
2208 * within a line's diamond was meant for that line. It would all be a lot
2209 * simpler if the / and % operators respected modulo arithmetic properly
2210 * for negative numbers. */
2215 /* Get the coordinates of the square the click was in */
2216 i
= (x
+ TILE_SIZE
) / TILE_SIZE
- 1;
2217 j
= (y
+ TILE_SIZE
) / TILE_SIZE
- 1;
2219 /* Get the precise position inside square [i,j] */
2220 p
= (x
+ TILE_SIZE
) % TILE_SIZE
;
2221 q
= (y
+ TILE_SIZE
) % TILE_SIZE
;
2223 /* After this bit of magic [i,j] will correspond to the point either above
2224 * or to the left of the line selected */
2226 if (TILE_SIZE
- p
> q
) {
2229 hl_selected
= FALSE
;
2233 if (TILE_SIZE
- q
> p
) {
2234 hl_selected
= FALSE
;
2245 if (i
>= state
->w
|| j
>= state
->h
+ 1)
2248 if (i
>= state
->w
+ 1 || j
>= state
->h
)
2252 /* I think it's only possible to play this game with mouse clicks, sorry */
2253 /* Maybe will add mouse drag support some time */
2255 old_state
= RIGHTOF_DOT(state
, i
, j
);
2257 old_state
= BELOW_DOT(state
, i
, j
);
2261 switch (old_state
) {
2275 switch (old_state
) {
2290 sprintf(buf
, "%d,%d%c%c", i
, j
, (int)(hl_selected ?
'h' : 'v'), (int)button_char
);
2296 static game_state
*execute_move(game_state
*state
, char *move
)
2299 game_state
*newstate
= dup_game(state
);
2301 if (move
[0] == 'S') {
2303 newstate
->cheated
= TRUE
;
2308 move
= strchr(move
, ',');
2312 move
+= strspn(move
, "1234567890");
2313 switch (*(move
++)) {
2315 if (i
>= newstate
->w
|| j
> newstate
->h
)
2317 switch (*(move
++)) {
2319 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_YES
;
2322 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_NO
;
2325 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2332 if (i
> newstate
->w
|| j
>= newstate
->h
)
2334 switch (*(move
++)) {
2336 LV_BELOW_DOT(newstate
, i
, j
) = LINE_YES
;
2339 LV_BELOW_DOT(newstate
, i
, j
) = LINE_NO
;
2342 LV_BELOW_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2354 * Check for completion.
2356 i
= 0; /* placate optimiser */
2357 for (j
= 0; j
<= newstate
->h
; j
++) {
2358 for (i
= 0; i
< newstate
->w
; i
++)
2359 if (LV_RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
)
2361 if (i
< newstate
->w
)
2364 if (j
<= newstate
->h
) {
2370 * We've found a horizontal edge at (i,j). Follow it round
2371 * to see if it's part of a loop.
2375 int order
= dot_order(newstate
, x
, y
, LINE_YES
);
2377 goto completion_check_done
;
2379 if (LEFTOF_DOT(newstate
, x
, y
) == LINE_YES
&& prevdir
!= 'L') {
2382 } else if (RIGHTOF_DOT(newstate
, x
, y
) == LINE_YES
&&
2386 } else if (ABOVE_DOT(newstate
, x
, y
) == LINE_YES
&&
2390 } else if (BELOW_DOT(newstate
, x
, y
) == LINE_YES
&&
2395 assert(!"Can't happen"); /* dot_order guarantees success */
2400 if (x
== i
&& y
== j
)
2404 if (x
!= i
|| y
!= j
|| looplen
== 0)
2405 goto completion_check_done
;
2408 * We've traced our way round a loop, and we know how many
2409 * line segments were involved. Count _all_ the line
2410 * segments in the grid, to see if the loop includes them
2414 for (j
= 0; j
<= newstate
->h
; j
++)
2415 for (i
= 0; i
<= newstate
->w
; i
++)
2416 count
+= ((RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
) +
2417 (BELOW_DOT(newstate
, i
, j
) == LINE_YES
));
2418 assert(count
>= looplen
);
2419 if (count
!= looplen
)
2420 goto completion_check_done
;
2423 * The grid contains one closed loop and nothing else.
2424 * Check that all the clues are satisfied.
2426 for (j
= 0; j
< newstate
->h
; ++j
) {
2427 for (i
= 0; i
< newstate
->w
; ++i
) {
2428 int n
= CLUE_AT(newstate
, i
, j
);
2430 if (square_order(newstate
, i
, j
, LINE_YES
) != n
- '0') {
2431 goto completion_check_done
;
2440 newstate
->solved
= TRUE
;
2443 completion_check_done
:
2447 free_game(newstate
);
2451 /* ----------------------------------------------------------------------
2455 #define SIZE(d) ((d) * TILE_SIZE + 2 * BORDER + 1)
2457 static void game_compute_size(game_params
*params
, int tilesize
,
2460 struct { int tilesize
; } ads
, *ds
= &ads
;
2461 ads
.tilesize
= tilesize
;
2463 *x
= SIZE(params
->w
);
2464 *y
= SIZE(params
->h
);
2467 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
2468 game_params
*params
, int tilesize
)
2470 ds
->tilesize
= tilesize
;
2473 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2475 float *ret
= snewn(4 * NCOLOURS
, float);
2477 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2479 ret
[COL_FOREGROUND
* 3 + 0] = 0.0F
;
2480 ret
[COL_FOREGROUND
* 3 + 1] = 0.0F
;
2481 ret
[COL_FOREGROUND
* 3 + 2] = 0.0F
;
2483 ret
[COL_HIGHLIGHT
* 3 + 0] = 1.0F
;
2484 ret
[COL_HIGHLIGHT
* 3 + 1] = 1.0F
;
2485 ret
[COL_HIGHLIGHT
* 3 + 2] = 1.0F
;
2487 ret
[COL_MISTAKE
* 3 + 0] = 1.0F
;
2488 ret
[COL_MISTAKE
* 3 + 1] = 0.0F
;
2489 ret
[COL_MISTAKE
* 3 + 2] = 0.0F
;
2491 *ncolours
= NCOLOURS
;
2495 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
2497 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2501 ds
->hl
= snewn(HL_COUNT(state
), char);
2502 ds
->vl
= snewn(VL_COUNT(state
), char);
2503 ds
->clue_error
= snewn(SQUARE_COUNT(state
), char);
2506 memset(ds
->hl
, LINE_UNKNOWN
, HL_COUNT(state
));
2507 memset(ds
->vl
, LINE_UNKNOWN
, VL_COUNT(state
));
2508 memset(ds
->clue_error
, 0, SQUARE_COUNT(state
));
2513 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2515 sfree(ds
->clue_error
);
2521 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2522 game_state
*state
, int dir
, game_ui
*ui
,
2523 float animtime
, float flashtime
)
2526 int w
= state
->w
, h
= state
->h
;
2528 int line_colour
, flash_changed
;
2533 * The initial contents of the window are not guaranteed and
2534 * can vary with front ends. To be on the safe side, all games
2535 * should start by drawing a big background-colour rectangle
2536 * covering the whole window.
2538 draw_rect(dr
, 0, 0, SIZE(state
->w
), SIZE(state
->h
), COL_BACKGROUND
);
2541 for (j
= 0; j
< h
+ 1; ++j
) {
2542 for (i
= 0; i
< w
+ 1; ++i
) {
2544 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2545 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2546 LINEWIDTH
, LINEWIDTH
, COL_FOREGROUND
);
2551 for (j
= 0; j
< h
; ++j
) {
2552 for (i
= 0; i
< w
; ++i
) {
2553 c
[0] = CLUE_AT(state
, i
, j
);
2556 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2,
2557 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2,
2558 FONT_VARIABLE
, TILE_SIZE
/2,
2559 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_FOREGROUND
, c
);
2562 draw_update(dr
, 0, 0,
2563 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2564 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
2568 if (flashtime
> 0 &&
2569 (flashtime
<= FLASH_TIME
/3 ||
2570 flashtime
>= FLASH_TIME
*2/3)) {
2571 flash_changed
= !ds
->flashing
;
2572 ds
->flashing
= TRUE
;
2573 line_colour
= COL_HIGHLIGHT
;
2575 flash_changed
= ds
->flashing
;
2576 ds
->flashing
= FALSE
;
2577 line_colour
= COL_FOREGROUND
;
2580 #define CROSS_SIZE (3 * LINEWIDTH / 2)
2582 /* Redraw clue colours if necessary */
2583 for (j
= 0; j
< h
; ++j
) {
2584 for (i
= 0; i
< w
; ++i
) {
2585 c
[0] = CLUE_AT(state
, i
, j
);
2591 assert(n
>= 0 && n
<= 4);
2593 clue_mistake
= (square_order(state
, i
, j
, LINE_YES
) > n
||
2594 square_order(state
, i
, j
, LINE_NO
) > (4-n
));
2596 if (clue_mistake
!= ds
->clue_error
[j
* w
+ i
]) {
2598 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
,
2599 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
,
2600 TILE_SIZE
- CROSS_SIZE
* 2, TILE_SIZE
- CROSS_SIZE
* 2,
2603 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2,
2604 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2,
2605 FONT_VARIABLE
, TILE_SIZE
/2,
2606 ALIGN_VCENTRE
| ALIGN_HCENTRE
,
2607 clue_mistake ? COL_MISTAKE
: COL_FOREGROUND
, c
);
2608 draw_update(dr
, i
* TILE_SIZE
+ BORDER
, j
* TILE_SIZE
+ BORDER
,
2609 TILE_SIZE
, TILE_SIZE
);
2611 ds
->clue_error
[j
* w
+ i
] = clue_mistake
;
2616 /* I've also had a request to colour lines red if they make a non-solution
2617 * loop, or if more than two lines go into any point. I think that would
2618 * be good some time. */
2620 #define CLEAR_VL(i, j) do { \
2622 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2623 BORDER + j * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, \
2625 TILE_SIZE - LINEWIDTH, \
2628 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2629 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2631 TILE_SIZE + CROSS_SIZE*2); \
2634 #define CLEAR_HL(i, j) do { \
2636 BORDER + i * TILE_SIZE + LINEWIDTH - LINEWIDTH/2, \
2637 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2638 TILE_SIZE - LINEWIDTH, \
2642 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2643 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2644 TILE_SIZE + CROSS_SIZE*2, \
2648 /* Vertical lines */
2649 for (j
= 0; j
< h
; ++j
) {
2650 for (i
= 0; i
< w
+ 1; ++i
) {
2651 switch (BELOW_DOT(state
, i
, j
)) {
2653 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2658 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
) ||
2662 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2663 BORDER
+ j
* TILE_SIZE
+ LINEWIDTH
- LINEWIDTH
/2,
2664 LINEWIDTH
, TILE_SIZE
- LINEWIDTH
,
2669 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2672 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2673 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2674 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2675 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2678 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2679 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2680 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2681 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2686 ds
->vl
[i
+ (w
+ 1) * j
] = BELOW_DOT(state
, i
, j
);
2690 /* Horizontal lines */
2691 for (j
= 0; j
< h
+ 1; ++j
) {
2692 for (i
= 0; i
< w
; ++i
) {
2693 switch (RIGHTOF_DOT(state
, i
, j
)) {
2695 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2700 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
) ||
2704 BORDER
+ i
* TILE_SIZE
+ LINEWIDTH
- LINEWIDTH
/2,
2705 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2706 TILE_SIZE
- LINEWIDTH
, LINEWIDTH
,
2711 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2714 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2715 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2716 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2717 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2720 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2721 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2722 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2723 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2728 ds
->hl
[i
+ w
* j
] = RIGHTOF_DOT(state
, i
, j
);
2733 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2734 int dir
, game_ui
*ui
)
2739 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2740 int dir
, game_ui
*ui
)
2742 if (!oldstate
->solved
&& newstate
->solved
&&
2743 !oldstate
->cheated
&& !newstate
->cheated
) {
2750 static int game_wants_statusbar(void)
2755 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2760 static void game_print_size(game_params
*params
, float *x
, float *y
)
2765 * I'll use 7mm squares by default.
2767 game_compute_size(params
, 700, &pw
, &ph
);
2772 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2774 int w
= state
->w
, h
= state
->h
;
2775 int ink
= print_mono_colour(dr
, 0);
2777 game_drawstate ads
, *ds
= &ads
;
2778 ds
->tilesize
= tilesize
;
2781 * Dots. I'll deliberately make the dots a bit wider than the
2782 * lines, so you can still see them. (And also because it's
2783 * annoyingly tricky to make them _exactly_ the same size...)
2785 for (y
= 0; y
<= h
; y
++)
2786 for (x
= 0; x
<= w
; x
++)
2787 draw_circle(dr
, BORDER
+ x
* TILE_SIZE
, BORDER
+ y
* TILE_SIZE
,
2788 LINEWIDTH
, ink
, ink
);
2793 for (y
= 0; y
< h
; y
++)
2794 for (x
= 0; x
< w
; x
++)
2795 if (CLUE_AT(state
, x
, y
) != ' ') {
2798 c
[0] = CLUE_AT(state
, x
, y
);
2801 BORDER
+ x
* TILE_SIZE
+ TILE_SIZE
/2,
2802 BORDER
+ y
* TILE_SIZE
+ TILE_SIZE
/2,
2803 FONT_VARIABLE
, TILE_SIZE
/2,
2804 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, c
);
2808 * Lines. (At the moment, I'm not bothering with crosses.)
2810 for (y
= 0; y
<= h
; y
++)
2811 for (x
= 0; x
< w
; x
++)
2812 if (RIGHTOF_DOT(state
, x
, y
) == LINE_YES
)
2813 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
,
2814 BORDER
+ y
* TILE_SIZE
- LINEWIDTH
/2,
2815 TILE_SIZE
, (LINEWIDTH
/2) * 2 + 1, ink
);
2816 for (y
= 0; y
< h
; y
++)
2817 for (x
= 0; x
<= w
; x
++)
2818 if (BELOW_DOT(state
, x
, y
) == LINE_YES
)
2819 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
- LINEWIDTH
/2,
2820 BORDER
+ y
* TILE_SIZE
,
2821 (LINEWIDTH
/2) * 2 + 1, TILE_SIZE
, ink
);
2825 #define thegame loopy
2828 const struct game thegame
= {
2829 "Loopy", "games.loopy",
2836 TRUE
, game_configure
, custom_params
,
2844 TRUE
, game_text_format
,
2852 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2855 game_free_drawstate
,
2859 TRUE
, FALSE
, game_print_size
, game_print
,
2860 game_wants_statusbar
,
2861 FALSE
, game_timing_state
,
2862 0, /* mouse_priorities */