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!)
57 #define PREFERRED_TILE_SIZE 32
58 #define TILE_SIZE (ds->tilesize)
59 #define LINEWIDTH TILE_SIZE / 16
60 #define BORDER (TILE_SIZE / 2)
62 #define FLASH_TIME 0.4F
64 #define HL_COUNT(state) ((state)->w * ((state)->h + 1))
65 #define VL_COUNT(state) (((state)->w + 1) * (state)->h)
66 #define DOT_COUNT(state) (((state)->w + 1) * ((state)->h + 1))
67 #define SQUARE_COUNT(state) ((state)->w * (state)->h)
69 #define ABOVE_SQUARE(state, i, j) ((state)->hl[(i) + (state)->w * (j)])
70 #define BELOW_SQUARE(state, i, j) ABOVE_SQUARE(state, i, (j)+1)
72 #define LEFTOF_SQUARE(state, i, j) ((state)->vl[(i) + ((state)->w + 1) * (j)])
73 #define RIGHTOF_SQUARE(state, i, j) LEFTOF_SQUARE(state, (i)+1, j)
75 #define LEGAL_DOT(state, i, j) ((i) >= 0 && (j) >= 0 && \
76 (i) <= (state)->w && (j) <= (state)->h)
79 * These macros return rvalues only, but can cope with being passed
80 * out-of-range coordinates.
82 #define ABOVE_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || j <= 0) ? \
83 LINE_NO : LV_ABOVE_DOT(state, i, j))
84 #define BELOW_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || j >= (state)->h) ? \
85 LINE_NO : LV_BELOW_DOT(state, i, j))
87 #define LEFTOF_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || i <= 0) ? \
88 LINE_NO : LV_LEFTOF_DOT(state, i, j))
89 #define RIGHTOF_DOT(state, i, j) ((!LEGAL_DOT(state, i, j) || i >= (state)->w)?\
90 LINE_NO : LV_RIGHTOF_DOT(state, i, j))
93 * These macros expect to be passed valid coordinates, and return
96 #define LV_BELOW_DOT(state, i, j) ((state)->vl[(i) + ((state)->w + 1) * (j)])
97 #define LV_ABOVE_DOT(state, i, j) LV_BELOW_DOT(state, i, (j)-1)
99 #define LV_RIGHTOF_DOT(state, i, j) ((state)->hl[(i) + (state)->w * (j)])
100 #define LV_LEFTOF_DOT(state, i, j) LV_RIGHTOF_DOT(state, (i)-1, j)
102 #define CLUE_AT(state, i, j) ((i < 0 || i >= (state)->w || \
103 j < 0 || j >= (state)->h) ? \
104 ' ' : LV_CLUE_AT(state, i, j))
106 #define LV_CLUE_AT(state, i, j) ((state)->clues[(i) + (state)->w * (j)])
108 #define OPP(dir) (dir == LINE_UNKNOWN ? LINE_UNKNOWN : \
109 dir == LINE_YES ? LINE_NO : LINE_YES)
111 static char *game_text_format(game_state
*state
);
120 enum line_state
{ LINE_UNKNOWN
, LINE_YES
, LINE_NO
};
122 enum direction
{ UP
, DOWN
, LEFT
, RIGHT
};
131 /* Put ' ' in a square that doesn't get a clue */
134 /* Arrays of line states, stored left-to-right, top-to-bottom */
143 static game_state
*dup_game(game_state
*state
)
145 game_state
*ret
= snew(game_state
);
149 ret
->solved
= state
->solved
;
150 ret
->cheated
= state
->cheated
;
152 ret
->clues
= snewn(SQUARE_COUNT(state
), char);
153 memcpy(ret
->clues
, state
->clues
, SQUARE_COUNT(state
));
155 ret
->hl
= snewn(HL_COUNT(state
), char);
156 memcpy(ret
->hl
, state
->hl
, HL_COUNT(state
));
158 ret
->vl
= snewn(VL_COUNT(state
), char);
159 memcpy(ret
->vl
, state
->vl
, VL_COUNT(state
));
161 ret
->recursion_depth
= state
->recursion_depth
;
166 static void free_game(game_state
*state
)
177 SOLVER_SOLVED
, /* This is the only solution the solver could find */
178 SOLVER_MISTAKE
, /* This is definitely not a solution */
179 SOLVER_AMBIGUOUS
, /* This _might_ be an ambiguous solution */
180 SOLVER_INCOMPLETE
/* This may be a partial solution */
183 typedef struct solver_state
{
185 /* XXX dot_atleastone[i,j, dline] is equivalent to */
186 /* dot_atmostone[i,j,OPP_DLINE(dline)] */
187 char *dot_atleastone
;
189 /* char *dline_identical; */
190 int recursion_remaining
;
191 enum solver_status solver_status
;
192 int *dotdsf
, *looplen
;
195 static solver_state
*new_solver_state(game_state
*state
) {
196 solver_state
*ret
= snew(solver_state
);
199 ret
->state
= dup_game(state
);
201 ret
->dot_atmostone
= snewn(DOT_COUNT(state
), char);
202 memset(ret
->dot_atmostone
, 0, DOT_COUNT(state
));
203 ret
->dot_atleastone
= snewn(DOT_COUNT(state
), char);
204 memset(ret
->dot_atleastone
, 0, DOT_COUNT(state
));
207 dline_identical
= snewn(DOT_COUNT(state
), char);
208 memset(dline_identical
, 0, DOT_COUNT(state
));
211 ret
->recursion_remaining
= state
->recursion_depth
;
212 ret
->solver_status
= SOLVER_INCOMPLETE
; /* XXX This may be a lie */
214 ret
->dotdsf
= snewn(DOT_COUNT(state
), int);
215 ret
->looplen
= snewn(DOT_COUNT(state
), int);
216 for (i
= 0; i
< DOT_COUNT(state
); i
++) {
224 static void free_solver_state(solver_state
*sstate
) {
226 free_game(sstate
->state
);
227 sfree(sstate
->dot_atleastone
);
228 sfree(sstate
->dot_atmostone
);
229 /* sfree(sstate->dline_identical); */
230 sfree(sstate
->dotdsf
);
231 sfree(sstate
->looplen
);
236 static solver_state
*dup_solver_state(solver_state
*sstate
) {
239 solver_state
*ret
= snew(solver_state
);
241 ret
->state
= state
= dup_game(sstate
->state
);
243 ret
->dot_atmostone
= snewn(DOT_COUNT(state
), char);
244 memcpy(ret
->dot_atmostone
, sstate
->dot_atmostone
, DOT_COUNT(state
));
246 ret
->dot_atleastone
= snewn(DOT_COUNT(state
), char);
247 memcpy(ret
->dot_atleastone
, sstate
->dot_atleastone
, DOT_COUNT(state
));
250 ret
->dline_identical
= snewn((state
->w
+ 1) * (state
->h
+ 1), char);
251 memcpy(ret
->dline_identical
, state
->dot_atmostone
,
252 (state
->w
+ 1) * (state
->h
+ 1));
255 ret
->recursion_remaining
= sstate
->recursion_remaining
;
256 ret
->solver_status
= sstate
->solver_status
;
258 ret
->dotdsf
= snewn(DOT_COUNT(state
), int);
259 ret
->looplen
= snewn(DOT_COUNT(state
), int);
260 memcpy(ret
->dotdsf
, sstate
->dotdsf
, DOT_COUNT(state
) * sizeof(int));
261 memcpy(ret
->looplen
, sstate
->looplen
, DOT_COUNT(state
) * sizeof(int));
267 * Merge two dots due to the existence of an edge between them.
268 * Updates the dsf tracking equivalence classes, and keeps track of
269 * the length of path each dot is currently a part of.
271 static void merge_dots(solver_state
*sstate
, int x1
, int y1
, int x2
, int y2
)
275 i
= y1
* (sstate
->state
->w
+ 1) + x1
;
276 j
= y2
* (sstate
->state
->w
+ 1) + x2
;
278 i
= dsf_canonify(sstate
->dotdsf
, i
);
279 j
= dsf_canonify(sstate
->dotdsf
, j
);
282 len
= sstate
->looplen
[i
] + sstate
->looplen
[j
];
283 dsf_merge(sstate
->dotdsf
, i
, j
);
284 i
= dsf_canonify(sstate
->dotdsf
, i
);
285 sstate
->looplen
[i
] = len
;
289 /* Count the number of lines of a particular type currently going into the
290 * given dot. Lines going off the edge of the board are assumed fixed no. */
291 static int dot_order(const game_state
* state
, int i
, int j
, char line_type
)
296 if (LEFTOF_DOT(state
, i
, j
) == line_type
)
299 if (line_type
== LINE_NO
)
303 if (RIGHTOF_DOT(state
, i
, j
) == line_type
)
306 if (line_type
== LINE_NO
)
310 if (ABOVE_DOT(state
, i
, j
) == line_type
)
313 if (line_type
== LINE_NO
)
317 if (BELOW_DOT(state
, i
, j
) == line_type
)
320 if (line_type
== LINE_NO
)
326 /* Count the number of lines of a particular type currently surrounding the
328 static int square_order(const game_state
* state
, int i
, int j
, char line_type
)
332 if (ABOVE_SQUARE(state
, i
, j
) == line_type
)
334 if (BELOW_SQUARE(state
, i
, j
) == line_type
)
336 if (LEFTOF_SQUARE(state
, i
, j
) == line_type
)
338 if (RIGHTOF_SQUARE(state
, i
, j
) == line_type
)
344 /* Set all lines bordering a dot of type old_type to type new_type */
345 static void dot_setall(game_state
*state
, int i
, int j
,
346 char old_type
, char new_type
)
348 /* printf("dot_setall([%d,%d], %d, %d)\n", i, j, old_type, new_type); */
349 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == old_type
)
350 LV_LEFTOF_DOT(state
, i
, j
) = new_type
;
351 if (i
< state
->w
&& RIGHTOF_DOT(state
, i
, j
) == old_type
)
352 LV_RIGHTOF_DOT(state
, i
, j
) = new_type
;
353 if (j
> 0 && ABOVE_DOT(state
, i
, j
) == old_type
)
354 LV_ABOVE_DOT(state
, i
, j
) = new_type
;
355 if (j
< state
->h
&& BELOW_DOT(state
, i
, j
) == old_type
)
356 LV_BELOW_DOT(state
, i
, j
) = new_type
;
358 /* Set all lines bordering a square of type old_type to type new_type */
359 static void square_setall(game_state
*state
, int i
, int j
,
360 char old_type
, char new_type
)
362 if (ABOVE_SQUARE(state
, i
, j
) == old_type
)
363 ABOVE_SQUARE(state
, i
, j
) = new_type
;
364 if (BELOW_SQUARE(state
, i
, j
) == old_type
)
365 BELOW_SQUARE(state
, i
, j
) = new_type
;
366 if (LEFTOF_SQUARE(state
, i
, j
) == old_type
)
367 LEFTOF_SQUARE(state
, i
, j
) = new_type
;
368 if (RIGHTOF_SQUARE(state
, i
, j
) == old_type
)
369 RIGHTOF_SQUARE(state
, i
, j
) = new_type
;
372 static game_params
*default_params(void)
374 game_params
*ret
= snew(game_params
);
383 static game_params
*dup_params(game_params
*params
)
385 game_params
*ret
= snew(game_params
);
386 *ret
= *params
; /* structure copy */
390 static const struct {
393 } loopy_presets
[] = {
394 { "4x4 Easy", { 4, 4, 0 } },
395 { "4x4 Hard", { 4, 4, 2 } },
396 { "7x7 Easy", { 7, 7, 0 } },
397 { "7x7 Hard", { 7, 7, 2 } },
398 { "10x10 Easy", { 10, 10, 0 } },
399 { "10x10 Hard", { 10, 10, 2 } },
400 { "15x15 Easy", { 15, 15, 0 } },
401 { "30x20 Easy", { 30, 20, 0 } }
404 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
408 if (i
< 0 || i
>= lenof(loopy_presets
))
411 tmppar
= loopy_presets
[i
].params
;
412 *params
= dup_params(&tmppar
);
413 *name
= dupstr(loopy_presets
[i
].desc
);
418 static void free_params(game_params
*params
)
423 static void decode_params(game_params
*params
, char const *string
)
425 params
->h
= params
->w
= atoi(string
);
427 while (*string
&& isdigit((unsigned char)*string
)) string
++;
428 if (*string
== 'x') {
430 params
->h
= atoi(string
);
431 while (*string
&& isdigit((unsigned char)*string
)) string
++;
433 if (*string
== 'r') {
435 params
->rec
= atoi(string
);
436 while (*string
&& isdigit((unsigned char)*string
)) string
++;
440 static char *encode_params(game_params
*params
, int full
)
443 sprintf(str
, "%dx%d", params
->w
, params
->h
);
445 sprintf(str
+ strlen(str
), "r%d", params
->rec
);
449 static config_item
*game_configure(game_params
*params
)
454 ret
= snewn(4, config_item
);
456 ret
[0].name
= "Width";
457 ret
[0].type
= C_STRING
;
458 sprintf(buf
, "%d", params
->w
);
459 ret
[0].sval
= dupstr(buf
);
462 ret
[1].name
= "Height";
463 ret
[1].type
= C_STRING
;
464 sprintf(buf
, "%d", params
->h
);
465 ret
[1].sval
= dupstr(buf
);
468 ret
[2].name
= "Recursion depth";
469 ret
[2].type
= C_STRING
;
470 sprintf(buf
, "%d", params
->rec
);
471 ret
[2].sval
= dupstr(buf
);
482 static game_params
*custom_params(config_item
*cfg
)
484 game_params
*ret
= snew(game_params
);
486 ret
->w
= atoi(cfg
[0].sval
);
487 ret
->h
= atoi(cfg
[1].sval
);
488 ret
->rec
= atoi(cfg
[2].sval
);
493 static char *validate_params(game_params
*params
, int full
)
495 if (params
->w
< 4 || params
->h
< 4)
496 return "Width and height must both be at least 4";
498 return "Recursion depth can't be negative";
502 /* We're going to store a list of current candidate squares for lighting.
503 * Each square gets a 'score', which tells us how adding that square right
504 * now would affect the length of the solution loop. We're trying to
505 * maximise that quantity so will bias our random selection of squares to
506 * light towards those with high scores */
513 static int get_square_cmpfn(void *v1
, void *v2
)
515 struct square
*s1
= (struct square
*)v1
;
516 struct square
*s2
= (struct square
*)v2
;
530 static int square_sort_cmpfn(void *v1
, void *v2
)
532 struct square
*s1
= (struct square
*)v1
;
533 struct square
*s2
= (struct square
*)v2
;
536 r
= s2
->score
- s1
->score
;
541 r
= s1
->random
- s2
->random
;
547 * It's _just_ possible that two squares might have been given
548 * the same random value. In that situation, fall back to
549 * comparing based on the coordinates. This introduces a tiny
550 * directional bias, but not a significant one.
552 return get_square_cmpfn(v1
, v2
);
555 static void print_tree(tree234
*tree
)
560 printf("Print tree:\n");
561 while (i
< count234(tree
)) {
562 s
= (struct square
*)index234(tree
, i
);
564 printf(" [%d,%d], %d, %d\n", s
->x
, s
->y
, s
->score
, s
->random
);
570 enum { SQUARE_LIT
, SQUARE_UNLIT
};
572 #define SQUARE_STATE(i, j) \
573 (((i) < 0 || (i) >= params->w || \
574 (j) < 0 || (j) >= params->h) ? \
575 SQUARE_UNLIT : LV_SQUARE_STATE(i,j))
577 #define LV_SQUARE_STATE(i, j) board[(i) + params->w * (j)]
579 static void print_board(const game_params
*params
, const char *board
)
585 for (i
= 0; i
< params
->w
; i
++) {
589 for (j
= 0; j
< params
->h
; j
++) {
591 for (i
= 0; i
< params
->w
; i
++) {
592 printf("%c", SQUARE_STATE(i
, j
) ?
' ' : 'O');
599 static char *new_fullyclued_board(game_params
*params
, random_state
*rs
)
605 game_state
*state
= &s
;
606 int board_area
= SQUARE_COUNT(params
);
609 struct square
*square
, *tmpsquare
, *sq
;
610 struct square square_pos
;
612 /* These will contain exactly the same information, sorted into different
614 tree234
*lightable_squares_sorted
, *lightable_squares_gettable
;
616 #define SQUARE_REACHABLE(i,j) \
617 (t = (SQUARE_STATE(i-1, j) == SQUARE_LIT || \
618 SQUARE_STATE(i+1, j) == SQUARE_LIT || \
619 SQUARE_STATE(i, j-1) == SQUARE_LIT || \
620 SQUARE_STATE(i, j+1) == SQUARE_LIT), \
621 /* printf("SQUARE_REACHABLE(%d,%d) = %d\n", i, j, t), */ \
625 /* One situation in which we may not light a square is if that'll leave one
626 * square above/below and one left/right of us unlit, separated by a lit
627 * square diagnonal from us */
628 #define SQUARE_DIAGONAL_VIOLATION(i, j, h, v) \
629 (t = (SQUARE_STATE((i)+(h), (j)) == SQUARE_UNLIT && \
630 SQUARE_STATE((i), (j)+(v)) == SQUARE_UNLIT && \
631 SQUARE_STATE((i)+(h), (j)+(v)) == SQUARE_LIT), \
632 /* t ? printf("SQUARE_DIAGONAL_VIOLATION(%d, %d, %d, %d)\n",
636 /* We also may not light a square if it will form a loop of lit squares
637 * around some unlit squares, as then the game soln won't have a single
639 #define SQUARE_LOOP_VIOLATION(i, j, lit1, lit2) \
640 (SQUARE_STATE((i)+1, (j)) == lit1 && \
641 SQUARE_STATE((i)-1, (j)) == lit1 && \
642 SQUARE_STATE((i), (j)+1) == lit2 && \
643 SQUARE_STATE((i), (j)-1) == lit2)
645 #define CAN_LIGHT_SQUARE(i, j) \
646 (SQUARE_REACHABLE(i, j) && \
647 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, -1) && \
648 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, -1) && \
649 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, +1) && \
650 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, +1) && \
651 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_LIT, SQUARE_UNLIT) && \
652 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_UNLIT, SQUARE_LIT))
654 #define IS_LIGHTING_CANDIDATE(i, j) \
655 (SQUARE_STATE(i, j) == SQUARE_UNLIT && \
656 CAN_LIGHT_SQUARE(i,j))
658 /* The 'score' of a square reflects its current desirability for selection
659 * as the next square to light. We want to encourage moving into uncharted
660 * areas so we give scores according to how many of the square's neighbours
661 * are currently unlit. */
668 #define SQUARE_SCORE(i,j) \
669 (2*((SQUARE_STATE(i-1, j) == SQUARE_UNLIT) + \
670 (SQUARE_STATE(i+1, j) == SQUARE_UNLIT) + \
671 (SQUARE_STATE(i, j-1) == SQUARE_UNLIT) + \
672 (SQUARE_STATE(i, j+1) == SQUARE_UNLIT)) - 4)
674 /* When a square gets lit, this defines how far away from that square we
675 * need to go recomputing scores */
676 #define SCORE_DISTANCE 1
678 board
= snewn(board_area
, char);
679 clues
= snewn(board_area
, char);
681 state
->h
= params
->h
;
682 state
->w
= params
->w
;
683 state
->clues
= clues
;
686 memset(board
, SQUARE_UNLIT
, board_area
);
688 /* Seed the board with a single lit square near the middle */
691 if (params
->w
& 1 && random_bits(rs
, 1))
693 if (params
->h
& 1 && random_bits(rs
, 1))
696 LV_SQUARE_STATE(i
, j
) = SQUARE_LIT
;
698 /* We need a way of favouring squares that will increase our loopiness.
699 * We do this by maintaining a list of all candidate squares sorted by
700 * their score and choose randomly from that with appropriate skew.
701 * In order to avoid consistently biasing towards particular squares, we
702 * need the sort order _within_ each group of scores to be completely
703 * random. But it would be abusing the hospitality of the tree234 data
704 * structure if our comparison function were nondeterministic :-). So with
705 * each square we associate a random number that does not change during a
706 * particular run of the generator, and use that as a secondary sort key.
707 * Yes, this means we will be biased towards particular random squares in
708 * any one run but that doesn't actually matter. */
710 lightable_squares_sorted
= newtree234(square_sort_cmpfn
);
711 lightable_squares_gettable
= newtree234(get_square_cmpfn
);
712 #define ADD_SQUARE(s) \
714 /* printf("ADD SQUARE: [%d,%d], %d, %d\n",
715 s->x, s->y, s->score, s->random);*/ \
716 sq = add234(lightable_squares_sorted, s); \
718 sq = add234(lightable_squares_gettable, s); \
722 #define REMOVE_SQUARE(s) \
724 /* printf("DELETE SQUARE: [%d,%d], %d, %d\n",
725 s->x, s->y, s->score, s->random);*/ \
726 sq = del234(lightable_squares_sorted, s); \
728 sq = del234(lightable_squares_gettable, s); \
732 #define HANDLE_DIR(a, b) \
733 square = snew(struct square); \
734 square->x = (i)+(a); \
735 square->y = (j)+(b); \
737 square->random = random_bits(rs, 31); \
745 /* Light squares one at a time until the board is interesting enough */
748 /* We have count234(lightable_squares) possibilities, and in
749 * lightable_squares_sorted they are sorted with the most desirable
751 c
= count234(lightable_squares_sorted
);
754 assert(c
== count234(lightable_squares_gettable
));
756 /* Check that the best square available is any good */
757 square
= (struct square
*)index234(lightable_squares_sorted
, 0);
760 if (square
->score
<= 0)
763 print_tree(lightable_squares_sorted
);
764 assert(square
->score
== SQUARE_SCORE(square
->x
, square
->y
));
765 assert(SQUARE_STATE(square
->x
, square
->y
) == SQUARE_UNLIT
);
766 assert(square
->x
>= 0 && square
->x
< params
->w
);
767 assert(square
->y
>= 0 && square
->y
< params
->h
);
768 /* printf("LIGHT SQUARE: [%d,%d], score = %d\n", square->x, square->y, square->score); */
770 /* Update data structures */
771 LV_SQUARE_STATE(square
->x
, square
->y
) = SQUARE_LIT
;
772 REMOVE_SQUARE(square
);
774 print_board(params
, board
);
776 /* We might have changed the score of any squares up to 2 units away in
778 for (b
= -SCORE_DISTANCE
; b
<= SCORE_DISTANCE
; b
++) {
779 for (a
= -SCORE_DISTANCE
; a
<= SCORE_DISTANCE
; a
++) {
782 square_pos
.x
= square
->x
+ a
;
783 square_pos
.y
= square
->y
+ b
;
784 /* printf("Refreshing score for [%d,%d]:\n", square_pos.x, square_pos.y); */
785 if (square_pos
.x
< 0 || square_pos
.x
>= params
->w
||
786 square_pos
.y
< 0 || square_pos
.y
>= params
->h
) {
787 /* printf(" Out of bounds\n"); */
790 tmpsquare
= find234(lightable_squares_gettable
, &square_pos
,
793 /* printf(" Removing\n"); */
794 assert(tmpsquare
->x
== square_pos
.x
);
795 assert(tmpsquare
->y
== square_pos
.y
);
796 assert(SQUARE_STATE(tmpsquare
->x
, tmpsquare
->y
) ==
798 REMOVE_SQUARE(tmpsquare
);
800 /* printf(" Creating\n"); */
801 tmpsquare
= snew(struct square
);
802 tmpsquare
->x
= square_pos
.x
;
803 tmpsquare
->y
= square_pos
.y
;
804 tmpsquare
->random
= random_bits(rs
, 31);
806 tmpsquare
->score
= SQUARE_SCORE(tmpsquare
->x
, tmpsquare
->y
);
808 if (IS_LIGHTING_CANDIDATE(tmpsquare
->x
, tmpsquare
->y
)) {
809 /* printf(" Adding\n"); */
810 ADD_SQUARE(tmpsquare
);
812 /* printf(" Destroying\n"); */
818 /* printf("\n\n"); */
821 while ((square
= delpos234(lightable_squares_gettable
, 0)) != NULL
)
823 freetree234(lightable_squares_gettable
);
824 freetree234(lightable_squares_sorted
);
826 /* Copy out all the clues */
827 for (j
= 0; j
< params
->h
; ++j
) {
828 for (i
= 0; i
< params
->w
; ++i
) {
829 c
= SQUARE_STATE(i
, j
);
830 LV_CLUE_AT(state
, i
, j
) = '0';
831 if (SQUARE_STATE(i
-1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
832 if (SQUARE_STATE(i
+1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
833 if (SQUARE_STATE(i
, j
-1) != c
) ++LV_CLUE_AT(state
, i
, j
);
834 if (SQUARE_STATE(i
, j
+1) != c
) ++LV_CLUE_AT(state
, i
, j
);
842 static solver_state
*solve_game_rec(const solver_state
*sstate
);
844 static int game_has_unique_soln(const game_state
*state
)
847 solver_state
*sstate_new
;
848 solver_state
*sstate
= new_solver_state((game_state
*)state
);
850 sstate_new
= solve_game_rec(sstate
);
852 ret
= (sstate_new
->solver_status
== SOLVER_SOLVED
);
854 free_solver_state(sstate_new
);
855 free_solver_state(sstate
);
860 /* Remove clues one at a time at random. */
861 static game_state
*remove_clues(game_state
*state
, random_state
*rs
)
863 int *square_list
, squares
;
864 game_state
*ret
= dup_game(state
), *saved_ret
;
867 /* We need to remove some clues. We'll do this by forming a list of all
868 * available equivalence classes, shuffling it, then going along one at a
869 * time clearing every member of each equivalence class, where removing a
870 * class doesn't render the board unsolvable. */
871 squares
= state
->w
* state
->h
;
872 square_list
= snewn(squares
, int);
873 for (n
= 0; n
< squares
; ++n
) {
877 shuffle(square_list
, squares
, sizeof(int), rs
);
879 for (n
= 0; n
< squares
; ++n
) {
880 saved_ret
= dup_game(ret
);
881 LV_CLUE_AT(ret
, square_list
[n
] % state
->w
,
882 square_list
[n
] / state
->w
) = ' ';
883 if (game_has_unique_soln(ret
)) {
884 free_game(saved_ret
);
895 static char *validate_desc(game_params
*params
, char *desc
);
897 static char *new_game_desc(game_params
*params
, random_state
*rs
,
898 char **aux
, int interactive
)
900 /* solution and description both use run-length encoding in obvious ways */
902 char *description
= snewn(SQUARE_COUNT(params
) + 1, char);
903 char *dp
= description
;
906 game_state
*state
= snew(game_state
), *state_new
;
908 state
->h
= params
->h
;
909 state
->w
= params
->w
;
911 state
->hl
= snewn(HL_COUNT(params
), char);
912 state
->vl
= snewn(VL_COUNT(params
), char);
913 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
914 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
916 state
->solved
= state
->cheated
= FALSE
;
917 state
->recursion_depth
= params
->rec
;
919 /* Get a new random solvable board with all its clues filled in. Yes, this
920 * can loop for ever if the params are suitably unfavourable, but
921 * preventing games smaller than 4x4 seems to stop this happening */
923 state
->clues
= new_fullyclued_board(params
, rs
);
924 } while (!game_has_unique_soln(state
));
926 state_new
= remove_clues(state
, rs
);
931 for (j
= 0; j
< params
->h
; ++j
) {
932 for (i
= 0; i
< params
->w
; ++i
) {
933 if (CLUE_AT(state
, i
, j
) == ' ') {
934 if (empty_count
> 25) {
935 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
941 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
944 dp
+= sprintf(dp
, "%c", (int)(CLUE_AT(state
, i
, j
)));
949 dp
+= sprintf(dp
, "%c", (int)(empty_count
+ 'a' - 1));
952 retval
= dupstr(description
);
955 assert(!validate_desc(params
, retval
));
960 /* We require that the params pass the test in validate_params and that the
961 * description fills the entire game area */
962 static char *validate_desc(game_params
*params
, char *desc
)
966 for (; *desc
; ++desc
) {
967 if (*desc
>= '0' && *desc
<= '9') {
972 count
+= *desc
- 'a' + 1;
975 return "Unknown character in description";
978 if (count
< SQUARE_COUNT(params
))
979 return "Description too short for board size";
980 if (count
> SQUARE_COUNT(params
))
981 return "Description too long for board size";
986 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
989 game_state
*state
= snew(game_state
);
990 int empties_to_make
= 0;
992 const char *dp
= desc
;
994 state
->recursion_depth
= params
->rec
;
996 state
->h
= params
->h
;
997 state
->w
= params
->w
;
999 state
->clues
= snewn(SQUARE_COUNT(params
), char);
1000 state
->hl
= snewn(HL_COUNT(params
), char);
1001 state
->vl
= snewn(VL_COUNT(params
), char);
1003 state
->solved
= state
->cheated
= FALSE
;
1005 for (j
= 0 ; j
< params
->h
; ++j
) {
1006 for (i
= 0 ; i
< params
->w
; ++i
) {
1007 if (empties_to_make
) {
1009 LV_CLUE_AT(state
, i
, j
) = ' ';
1015 if (n
>=0 && n
< 10) {
1016 LV_CLUE_AT(state
, i
, j
) = *dp
;
1020 LV_CLUE_AT(state
, i
, j
) = ' ';
1021 empties_to_make
= n
- 1;
1027 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
1028 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
1033 enum { LOOP_NONE
=0, LOOP_SOLN
, LOOP_NOT_SOLN
};
1035 /* Starting at dot [i,j] moves around 'state' removing lines until it's clear
1036 * whether or not the starting dot was on a loop. Returns boolean specifying
1037 * whether a loop was found. loop_status calls this and assumes that if state
1038 * has any lines set, this function will always remove at least one. */
1039 static int destructively_find_loop(game_state
*state
)
1041 int a
, b
, i
, j
, new_i
, new_j
, n
;
1044 lp
= (char *)memchr(state
->hl
, LINE_YES
, HL_COUNT(state
));
1046 /* We know we're going to return false but we have to fulfil our
1048 lp
= (char *)memchr(state
->vl
, LINE_YES
, VL_COUNT(state
));
1060 assert(i
+ j
* state
->w
== n
); /* because I'm feeling stupid */
1061 /* Save start position */
1065 /* Delete one line from the potential loop */
1066 if (LEFTOF_DOT(state
, i
, j
) == LINE_YES
) {
1067 LV_LEFTOF_DOT(state
, i
, j
) = LINE_NO
;
1069 } else if (ABOVE_DOT(state
, i
, j
) == LINE_YES
) {
1070 LV_ABOVE_DOT(state
, i
, j
) = LINE_NO
;
1072 } else if (RIGHTOF_DOT(state
, i
, j
) == LINE_YES
) {
1073 LV_RIGHTOF_DOT(state
, i
, j
) = LINE_NO
;
1075 } else if (BELOW_DOT(state
, i
, j
) == LINE_YES
) {
1076 LV_BELOW_DOT(state
, i
, j
) = LINE_NO
;
1083 /* From the current position of [i,j] there needs to be exactly one
1087 #define HANDLE_DIR(dir_dot, x, y) \
1088 if (dir_dot(state, i, j) == LINE_YES) { \
1089 if (new_i != -1 || new_j != -1) \
1093 LV_##dir_dot(state, i, j) = LINE_NO; \
1095 HANDLE_DIR(ABOVE_DOT
, 0, -1);
1096 HANDLE_DIR(BELOW_DOT
, 0, +1);
1097 HANDLE_DIR(LEFTOF_DOT
, -1, 0);
1098 HANDLE_DIR(RIGHTOF_DOT
, +1, 0);
1100 if (new_i
== -1 || new_j
== -1) {
1106 } while (i
!= a
|| j
!= b
);
1111 static int loop_status(game_state
*state
)
1114 game_state
*tmpstate
;
1115 int loop_found
= FALSE
, non_loop_found
= FALSE
, any_lines_found
= FALSE
;
1117 #define BAD_LOOP_FOUND \
1118 do { free_game(tmpstate); return LOOP_NOT_SOLN; } while(0)
1120 /* Repeatedly look for loops until we either run out of lines to consider
1121 * or discover for sure that the board fails on the grounds of having no
1123 tmpstate
= dup_game(state
);
1126 if (!memchr(tmpstate
->hl
, LINE_YES
, HL_COUNT(tmpstate
)) &&
1127 !memchr(tmpstate
->vl
, LINE_YES
, VL_COUNT(tmpstate
))) {
1130 any_lines_found
= TRUE
;
1134 if (destructively_find_loop(tmpstate
)) {
1139 non_loop_found
= TRUE
;
1143 free_game(tmpstate
);
1145 if (!any_lines_found
)
1148 if (non_loop_found
) {
1149 assert(!loop_found
); /* should have dealt with this already */
1153 /* Check that every clue is satisfied */
1154 for (j
= 0; j
< state
->h
; ++j
) {
1155 for (i
= 0; i
< state
->w
; ++i
) {
1156 n
= CLUE_AT(state
, i
, j
);
1158 if (square_order(state
, i
, j
, LINE_YES
) != n
- '0') {
1159 return LOOP_NOT_SOLN
;
1168 /* Sums the lengths of the numbers in range [0,n) */
1169 /* See equivalent function in solo.c for justification of this. */
1170 static int len_0_to_n(int n
)
1172 int len
= 1; /* Counting 0 as a bit of a special case */
1175 for (i
= 1; i
< n
; i
*= 10) {
1176 len
+= max(n
- i
, 0);
1182 static char *encode_solve_move(const game_state
*state
)
1186 /* This is going to return a string representing the moves needed to set
1187 * every line in a grid to be the same as the ones in 'state'. The exact
1188 * length of this string is predictable. */
1190 len
= 1; /* Count the 'S' prefix */
1191 /* Numbers in horizontal lines */
1192 /* Horizontal lines, x position */
1193 len
+= len_0_to_n(state
->w
) * (state
->h
+ 1);
1194 /* Horizontal lines, y position */
1195 len
+= len_0_to_n(state
->h
+ 1) * (state
->w
);
1196 /* Vertical lines, y position */
1197 len
+= len_0_to_n(state
->h
) * (state
->w
+ 1);
1198 /* Vertical lines, x position */
1199 len
+= len_0_to_n(state
->w
+ 1) * (state
->h
);
1200 /* For each line we also have two letters and a comma */
1201 len
+= 3 * (HL_COUNT(state
) + VL_COUNT(state
));
1203 ret
= snewn(len
+ 1, char);
1206 p
+= sprintf(p
, "S");
1208 for (j
= 0; j
< state
->h
+ 1; ++j
) {
1209 for (i
= 0; i
< state
->w
; ++i
) {
1210 switch (RIGHTOF_DOT(state
, i
, j
)) {
1212 p
+= sprintf(p
, "%d,%dhy", i
, j
);
1215 p
+= sprintf(p
, "%d,%dhn", i
, j
);
1218 /* I'm going to forgive this because I think the results
1220 /* assert(!"Solver produced incomplete solution!"); */
1225 for (j
= 0; j
< state
->h
; ++j
) {
1226 for (i
= 0; i
< state
->w
+ 1; ++i
) {
1227 switch (BELOW_DOT(state
, i
, j
)) {
1229 p
+= sprintf(p
, "%d,%dvy", i
, j
);
1232 p
+= sprintf(p
, "%d,%dvn", i
, j
);
1235 /* I'm going to forgive this because I think the results
1237 /* assert(!"Solver produced incomplete solution!"); */
1242 /* No point in doing sums like that if they're going to be wrong */
1243 assert(strlen(ret
) <= (size_t)len
);
1247 /* BEGIN SOLVER IMPLEMENTATION */
1249 /* For each pair of lines through each dot we store a bit for whether
1250 * exactly one of those lines is ON, and in separate arrays we store whether
1251 * at least one is on and whether at most 1 is on. (If we know both or
1252 * neither is on that's already stored more directly.) That's six bits per
1253 * dot. Bit number n represents the lines shown in dot_type_dirs[n]. */
1264 #define OPP_DLINE(dline) (dline ^ 1)
1267 #define SQUARE_DLINES \
1268 HANDLE_DLINE(DLINE_UL, RIGHTOF_SQUARE, BELOW_SQUARE, 1, 1); \
1269 HANDLE_DLINE(DLINE_UR, LEFTOF_SQUARE, BELOW_SQUARE, 0, 1); \
1270 HANDLE_DLINE(DLINE_DL, RIGHTOF_SQUARE, ABOVE_SQUARE, 1, 0); \
1271 HANDLE_DLINE(DLINE_DR, LEFTOF_SQUARE, ABOVE_SQUARE, 0, 0);
1273 #define DOT_DLINES \
1274 HANDLE_DLINE(DLINE_VERT, ABOVE_DOT, BELOW_DOT); \
1275 HANDLE_DLINE(DLINE_HORIZ, LEFTOF_DOT, RIGHTOF_DOT); \
1276 HANDLE_DLINE(DLINE_UL, ABOVE_DOT, LEFTOF_DOT); \
1277 HANDLE_DLINE(DLINE_UR, ABOVE_DOT, RIGHTOF_DOT); \
1278 HANDLE_DLINE(DLINE_DL, BELOW_DOT, LEFTOF_DOT); \
1279 HANDLE_DLINE(DLINE_DR, BELOW_DOT, RIGHTOF_DOT);
1281 static void array_setall(char *array
, char from
, char to
, int len
)
1283 char *p
= array
, *p_old
= p
;
1284 int len_remaining
= len
;
1286 while ((p
= memchr(p
, from
, len_remaining
))) {
1288 len_remaining
-= p
- p_old
;
1294 static int game_states_equal(const game_state
*state1
,
1295 const game_state
*state2
)
1297 /* This deliberately doesn't check _all_ fields, just the ones that make a
1298 * game state 'interesting' from the POV of the solver */
1299 /* XXX review this */
1300 if (state1
== state2
)
1303 if (!state1
|| !state2
)
1306 if (state1
->w
!= state2
->w
|| state1
->h
!= state2
->h
)
1309 if (memcmp(state1
->hl
, state2
->hl
, HL_COUNT(state1
)))
1312 if (memcmp(state1
->vl
, state2
->vl
, VL_COUNT(state1
)))
1318 static int solver_states_equal(const solver_state
*sstate1
,
1319 const solver_state
*sstate2
)
1328 if (!game_states_equal(sstate1
->state
, sstate2
->state
)) {
1332 /* XXX fields missing, needs review */
1333 /* XXX we're deliberately not looking at solver_state as it's only a cache */
1335 if (memcmp(sstate1
->dot_atleastone
, sstate2
->dot_atleastone
,
1336 DOT_COUNT(sstate1
->state
))) {
1340 if (memcmp(sstate1
->dot_atmostone
, sstate2
->dot_atmostone
,
1341 DOT_COUNT(sstate1
->state
))) {
1345 /* handle dline_identical here */
1350 static void dot_setall_dlines(solver_state
*sstate
, enum dline dl
, int i
, int j
,
1351 enum line_state line_old
, enum line_state line_new
)
1353 game_state
*state
= sstate
->state
;
1355 /* First line in dline */
1360 if (j
> 0 && ABOVE_DOT(state
, i
, j
) == line_old
)
1361 LV_ABOVE_DOT(state
, i
, j
) = line_new
;
1365 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1366 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1369 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1370 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1374 /* Second line in dline */
1378 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1379 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1384 if (i
<= (state
)->w
&& RIGHTOF_DOT(state
, i
, j
) == line_old
)
1385 LV_RIGHTOF_DOT(state
, i
, j
) = line_new
;
1388 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1389 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1394 static void update_solver_status(solver_state
*sstate
)
1396 if (sstate
->solver_status
== SOLVER_INCOMPLETE
) {
1397 switch (loop_status(sstate
->state
)) {
1399 sstate
->solver_status
= SOLVER_INCOMPLETE
;
1402 if (sstate
->solver_status
!= SOLVER_AMBIGUOUS
)
1403 sstate
->solver_status
= SOLVER_SOLVED
;
1406 sstate
->solver_status
= SOLVER_MISTAKE
;
1413 /* This will return a dynamically allocated solver_state containing the (more)
1415 static solver_state
*solve_game_rec(const solver_state
*sstate_start
)
1418 int current_yes
, current_no
, desired
;
1419 solver_state
*sstate
, *sstate_saved
, *sstate_tmp
;
1422 solver_state
*sstate_rec_solved
;
1423 int recursive_soln_count
;
1426 printf("solve_game_rec: recursion_remaining = %d\n",
1427 sstate_start
->recursion_remaining
);
1430 sstate
= dup_solver_state((solver_state
*)sstate_start
);
1433 text
= game_text_format(sstate
->state
);
1434 printf("%s\n", text
);
1438 #define RETURN_IF_SOLVED \
1440 update_solver_status(sstate); \
1441 if (sstate->solver_status != SOLVER_INCOMPLETE) { \
1442 free_solver_state(sstate_saved); \
1447 sstate_saved
= NULL
;
1450 nonrecursive_solver
:
1453 sstate_saved
= dup_solver_state(sstate
);
1455 /* First we do the 'easy' work, that might cause concrete results */
1457 /* Per-square deductions */
1458 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1459 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1460 /* Begin rules that look at the clue (if there is one) */
1461 desired
= CLUE_AT(sstate
->state
, i
, j
);
1464 desired
= desired
- '0';
1465 current_yes
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1466 current_no
= square_order(sstate
->state
, i
, j
, LINE_NO
);
1468 if (desired
<= current_yes
) {
1469 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1473 if (4 - desired
<= current_no
) {
1474 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_YES
);
1481 /* Per-dot deductions */
1482 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1483 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1484 switch (dot_order(sstate
->state
, i
, j
, LINE_YES
)) {
1486 if (dot_order(sstate
->state
, i
, j
, LINE_NO
) == 3) {
1487 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1491 switch (dot_order(sstate
->state
, i
, j
, LINE_NO
)) {
1492 #define H1(dline, dir1_dot, dir2_dot, dot_howmany) \
1493 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1494 if (dir2_dot(sstate->state, i, j) == LINE_UNKNOWN){ \
1495 sstate->dot_howmany \
1496 [i + (sstate->state->w + 1) * j] |= 1<<dline; \
1500 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1501 H1(dline, dir1_dot, dir2_dot, dot_atleastone)
1502 /* 1 yes, 1 no, so exactly one of unknowns is yes */
1507 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1508 H1(dline, dir1_dot, dir2_dot, dot_atmostone)
1509 /* 1 yes, fewer than 2 no, so at most one of
1510 * unknowns is yes */
1515 case 2: /* 1 yes, 2 no */
1516 dot_setall(sstate
->state
, i
, j
,
1517 LINE_UNKNOWN
, LINE_YES
);
1523 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1525 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1526 if (sstate->dot_atleastone \
1527 [i + (sstate->state->w + 1) * j] & 1<<dline) { \
1528 sstate->dot_atmostone \
1529 [i + (sstate->state->w + 1) * j] |= 1<<OPP_DLINE(dline); \
1531 /* If at least one of a dline in a dot is YES, at most one of
1532 * the opposite dline to that dot must be YES. */
1538 /* More obscure per-square operations */
1539 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1540 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1541 #define H1(dline, dir1_sq, dir2_sq, a, b, dot_howmany, line_query, line_set) \
1542 if (sstate->dot_howmany[i+a + (sstate->state->w + 1) * (j+b)] &\
1544 t = dir1_sq(sstate->state, i, j); \
1545 if (t == line_query) \
1546 dir2_sq(sstate->state, i, j) = line_set; \
1548 t = dir2_sq(sstate->state, i, j); \
1549 if (t == line_query) \
1550 dir1_sq(sstate->state, i, j) = line_set; \
1553 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1554 H1(dline, dir1_sq, dir2_sq, a, b, dot_atmostone, \
1556 /* If at most one of the DLINE is on, and one is definitely on,
1557 * set the other to definitely off */
1561 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1562 H1(dline, dir1_sq, dir2_sq, a, b, dot_atleastone, \
1564 /* If at least one of the DLINE is on, and one is definitely
1565 * off, set the other to definitely on */
1570 switch (CLUE_AT(sstate
->state
, i
, j
)) {
1573 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1574 /* At most one of any DLINE can be set */ \
1575 sstate->dot_atmostone \
1576 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1577 /* This DLINE provides enough YESes to solve the clue */\
1578 if (sstate->dot_atleastone \
1579 [i+a + (sstate->state->w + 1) * (j+b)] & \
1581 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1583 LINE_UNKNOWN, LINE_NO); \
1589 #define H1(dline, dot_at1one, dot_at2one, a, b) \
1590 if (sstate->dot_at1one \
1591 [i+a + (sstate->state->w + 1) * (j+b)] & \
1593 sstate->dot_at2one \
1594 [i+(1-a) + (sstate->state->w + 1) * (j+(1-b))] |= \
1595 1<<OPP_DLINE(dline); \
1597 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1598 H1(dline, dot_atleastone, dot_atmostone, a, b); \
1599 H1(dline, dot_atmostone, dot_atleastone, a, b);
1600 /* If at least one of one DLINE is set, at most one of
1601 * the opposing one is and vice versa */
1608 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1609 /* At least one of any DLINE can be set */ \
1610 sstate->dot_atleastone \
1611 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1612 /* This DLINE provides enough NOs to solve the clue */ \
1613 if (sstate->dot_atmostone \
1614 [i+a + (sstate->state->w + 1) * (j+b)] & \
1616 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1618 LINE_UNKNOWN, LINE_YES); \
1627 if (solver_states_equal(sstate
, sstate_saved
)) {
1628 int edgecount
= 0, clues
= 0, satclues
= 0, sm1clues
= 0;
1632 * Go through the grid and update for all the new edges.
1633 * Since merge_dots() is idempotent, the simplest way to
1634 * do this is just to update for _all_ the edges.
1636 * Also, while we're here, we count the edges, count the
1637 * clues, count the satisfied clues, and count the
1638 * satisfied-minus-one clues.
1640 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1641 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1642 if (RIGHTOF_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1643 merge_dots(sstate
, i
, j
, i
+1, j
);
1646 if (BELOW_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1647 merge_dots(sstate
, i
, j
, i
, j
+1);
1651 if (CLUE_AT(sstate
->state
, i
, j
) != ' ') {
1652 int c
= CLUE_AT(sstate
->state
, i
, j
) - '0';
1653 int o
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1664 * Now go through looking for LINE_UNKNOWN edges which
1665 * connect two dots that are already in the same
1666 * equivalence class. If we find one, test to see if the
1667 * loop it would create is a solution.
1669 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1670 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1671 for (d
= 0; d
< 2; d
++) {
1672 int i2
, j2
, eqclass
, val
;
1675 if (RIGHTOF_DOT(sstate
->state
, i
, j
) !=
1681 if (BELOW_DOT(sstate
->state
, i
, j
) !=
1688 eqclass
= dsf_canonify(sstate
->dotdsf
,
1689 j
* (sstate
->state
->w
+1) + i
);
1690 if (eqclass
!= dsf_canonify(sstate
->dotdsf
,
1691 j2
* (sstate
->state
->w
+1) +
1695 val
= LINE_NO
; /* loop is bad until proven otherwise */
1698 * This edge would form a loop. Next
1699 * question: how long would the loop be?
1700 * Would it equal the total number of edges
1701 * (plus the one we'd be adding if we added
1704 if (sstate
->looplen
[eqclass
] == edgecount
+ 1) {
1709 * This edge would form a loop which
1710 * took in all the edges in the entire
1711 * grid. So now we need to work out
1712 * whether it would be a valid solution
1713 * to the puzzle, which means we have to
1714 * check if it satisfies all the clues.
1715 * This means that every clue must be
1716 * either satisfied or satisfied-minus-
1717 * 1, and also that the number of
1718 * satisfied-minus-1 clues must be at
1719 * most two and they must lie on either
1720 * side of this edge.
1725 if (CLUE_AT(sstate
->state
, cx
,cy
) != ' ' &&
1726 square_order(sstate
->state
, cx
,cy
, LINE_YES
) ==
1727 CLUE_AT(sstate
->state
, cx
,cy
) - '0' - 1)
1729 if (CLUE_AT(sstate
->state
, i
, j
) != ' ' &&
1730 square_order(sstate
->state
, i
, j
, LINE_YES
) ==
1731 CLUE_AT(sstate
->state
, i
, j
) - '0' - 1)
1733 if (sm1clues
== sm1_nearby
&&
1734 sm1clues
+ satclues
== clues
)
1735 val
= LINE_YES
; /* loop is good! */
1739 * Right. Now we know that adding this edge
1740 * would form a loop, and we know whether
1741 * that loop would be a viable solution or
1744 * If adding this edge produces a solution,
1745 * then we know we've found _a_ solution but
1746 * we don't know that it's _the_ solution -
1747 * if it were provably the solution then
1748 * we'd have deduced this edge some time ago
1749 * without the need to do loop detection. So
1750 * in this state we return SOLVER_AMBIGUOUS,
1751 * which has the effect that hitting Solve
1752 * on a user-provided puzzle will fill in a
1753 * solution but using the solver to
1754 * construct new puzzles won't consider this
1755 * a reasonable deduction for the user to
1759 LV_RIGHTOF_DOT(sstate
->state
, i
, j
) = val
;
1761 LV_BELOW_DOT(sstate
->state
, i
, j
) = val
;
1762 if (val
== LINE_YES
) {
1763 sstate
->solver_status
= SOLVER_AMBIGUOUS
;
1764 goto finished_loop_checking
;
1770 finished_loop_checking
:
1775 if (solver_states_equal(sstate
, sstate_saved
)) {
1776 /* Solver has stopped making progress so we terminate */
1777 free_solver_state(sstate_saved
);
1781 free_solver_state(sstate_saved
);
1784 if (sstate
->solver_status
== SOLVER_SOLVED
||
1785 sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
1786 /* s/LINE_UNKNOWN/LINE_NO/g */
1787 array_setall(sstate
->state
->hl
, LINE_UNKNOWN
, LINE_NO
,
1788 HL_COUNT(sstate
->state
));
1789 array_setall(sstate
->state
->vl
, LINE_UNKNOWN
, LINE_NO
,
1790 VL_COUNT(sstate
->state
));
1794 /* Perform recursive calls */
1795 if (sstate
->recursion_remaining
) {
1796 sstate
->recursion_remaining
--;
1798 sstate_saved
= dup_solver_state(sstate
);
1800 recursive_soln_count
= 0;
1801 sstate_rec_solved
= NULL
;
1803 /* Memory management:
1804 * sstate_saved won't be modified but needs to be freed when we have
1806 * sstate is expected to contain our 'best' solution by the time we
1807 * finish this section of code. It's the thing we'll try adding lines
1808 * to, seeing if they make it more solvable.
1809 * If sstate_rec_solved is non-NULL, it will supersede sstate
1810 * eventually. sstate_tmp should not hold a value persistently.
1813 /* NB SOLVER_AMBIGUOUS is like SOLVER_SOLVED except the solver is aware
1814 * of the possibility of additional solutions. So as soon as we have a
1815 * SOLVER_AMBIGUOUS we can safely propagate it back to our caller, but
1816 * if we get a SOLVER_SOLVED we want to keep trying in case we find
1817 * further solutions and have to mark it ambiguous.
1820 #define DO_RECURSIVE_CALL(dir_dot) \
1821 if (dir_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1822 debug(("Trying " #dir_dot " at [%d,%d]\n", i, j)); \
1823 LV_##dir_dot(sstate->state, i, j) = LINE_YES; \
1824 sstate_tmp = solve_game_rec(sstate); \
1825 switch (sstate_tmp->solver_status) { \
1826 case SOLVER_AMBIGUOUS: \
1827 debug(("Solver ambiguous, returning\n")); \
1828 sstate_rec_solved = sstate_tmp; \
1829 goto finished_recursion; \
1830 case SOLVER_SOLVED: \
1831 switch (++recursive_soln_count) { \
1833 debug(("One solution found\n")); \
1834 sstate_rec_solved = sstate_tmp; \
1837 debug(("Ambiguous solutions found\n")); \
1838 free_solver_state(sstate_tmp); \
1839 sstate_rec_solved->solver_status = SOLVER_AMBIGUOUS;\
1840 goto finished_recursion; \
1842 assert(!"recursive_soln_count out of range"); \
1846 case SOLVER_MISTAKE: \
1847 debug(("Non-solution found\n")); \
1848 free_solver_state(sstate_tmp); \
1849 free_solver_state(sstate_saved); \
1850 LV_##dir_dot(sstate->state, i, j) = LINE_NO; \
1851 goto nonrecursive_solver; \
1852 case SOLVER_INCOMPLETE: \
1853 debug(("Recursive step inconclusive\n")); \
1854 free_solver_state(sstate_tmp); \
1857 free_solver_state(sstate); \
1858 sstate = dup_solver_state(sstate_saved); \
1861 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1862 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1863 /* Only perform recursive calls on 'loose ends' */
1864 if (dot_order(sstate
->state
, i
, j
, LINE_YES
) == 1) {
1865 if (LEFTOF_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1866 DO_RECURSIVE_CALL(LEFTOF_DOT
);
1867 if (RIGHTOF_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1868 DO_RECURSIVE_CALL(RIGHTOF_DOT
);
1869 if (ABOVE_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1870 DO_RECURSIVE_CALL(ABOVE_DOT
);
1871 if (BELOW_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1872 DO_RECURSIVE_CALL(BELOW_DOT
);
1879 if (sstate_rec_solved
) {
1880 free_solver_state(sstate
);
1881 sstate
= sstate_rec_solved
;
1888 /* XXX bits of solver that may come in handy one day */
1890 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1891 /* dline from this dot that's entirely unknown must have
1892 * both lines identical */ \
1893 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN && \
1894 dir2_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1895 sstate->dline_identical[i + (sstate->state->w + 1) * j] |= \
1897 } else if (sstate->dline_identical[i +
1898 (sstate
->state
->w
+ 1) * j
] &\
1900 /* If they're identical and one is known do the obvious
1902 t
= dir1_dot(sstate
->state
, i
, j
); \
1903 if (t
!= LINE_UNKNOWN
) \
1904 dir2_dot(sstate
->state
, i
, j
) = t
; \
1906 t
= dir2_dot(sstate
->state
, i
, j
); \
1907 if (t
!= LINE_UNKNOWN
) \
1908 dir1_dot(sstate
->state
, i
, j
) = t
; \
1916 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1917 if (sstate->dline_identical[i+a + \
1918 (sstate->state->w + 1) * (j+b)] &\
1920 dir1_sq(sstate->state, i, j) = LINE_YES; \
1921 dir2_sq(sstate->state, i, j) = LINE_YES; \
1923 /* If two lines are the same they must be on */
1930 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1931 if (sstate->dot_atmostone[i+a + (sstate->state->w + 1) * (j+b)] & \
1933 if (square_order(sstate->state, i, j, LINE_UNKNOWN) - 1 == \
1934 CLUE_AT(sstate->state, i, j) - '0') { \
1935 square_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_YES); \
1936 /* XXX the following may overwrite known data! */ \
1937 dir1_sq(sstate->state, i, j) = LINE_UNKNOWN; \
1938 dir2_sq(sstate->state, i, j) = LINE_UNKNOWN; \
1946 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1947 if (sstate->dline_identical[i+a +
1948 (sstate
->state
->w
+ 1) * (j
+b
)] &\
1950 dir1_sq(sstate
->state
, i
, j
) = LINE_NO
; \
1951 dir2_sq(sstate
->state
, i
, j
) = LINE_NO
; \
1953 /* If two lines are the same they must be off */
1958 static char *solve_game(game_state
*state
, game_state
*currstate
,
1959 char *aux
, char **error
)
1962 solver_state
*sstate
, *new_sstate
;
1964 sstate
= new_solver_state(state
);
1965 new_sstate
= solve_game_rec(sstate
);
1967 if (new_sstate
->solver_status
== SOLVER_SOLVED
) {
1968 soln
= encode_solve_move(new_sstate
->state
);
1969 } else if (new_sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
1970 soln
= encode_solve_move(new_sstate
->state
);
1971 /**error = "Solver found ambiguous solutions"; */
1973 soln
= encode_solve_move(new_sstate
->state
);
1974 /**error = "Solver failed"; */
1977 free_solver_state(new_sstate
);
1978 free_solver_state(sstate
);
1983 static char *game_text_format(game_state
*state
)
1989 len
= (2 * state
->w
+ 2) * (2 * state
->h
+ 1);
1990 rp
= ret
= snewn(len
+ 1, char);
1993 switch (ABOVE_SQUARE(state, i, j)) { \
1995 rp += sprintf(rp, " -"); \
1998 rp += sprintf(rp, " x"); \
2000 case LINE_UNKNOWN: \
2001 rp += sprintf(rp, " "); \
2004 assert(!"Illegal line state for HL");\
2008 switch (LEFTOF_SQUARE(state, i, j)) {\
2010 rp += sprintf(rp, "|"); \
2013 rp += sprintf(rp, "x"); \
2015 case LINE_UNKNOWN: \
2016 rp += sprintf(rp, " "); \
2019 assert(!"Illegal line state for VL");\
2022 for (j
= 0; j
< state
->h
; ++j
) {
2023 for (i
= 0; i
< state
->w
; ++i
) {
2026 rp
+= sprintf(rp
, " \n");
2027 for (i
= 0; i
< state
->w
; ++i
) {
2029 rp
+= sprintf(rp
, "%c", (int)(CLUE_AT(state
, i
, j
)));
2032 rp
+= sprintf(rp
, "\n");
2034 for (i
= 0; i
< state
->w
; ++i
) {
2037 rp
+= sprintf(rp
, " \n");
2039 assert(strlen(ret
) == len
);
2043 static game_ui
*new_ui(game_state
*state
)
2048 static void free_ui(game_ui
*ui
)
2052 static char *encode_ui(game_ui
*ui
)
2057 static void decode_ui(game_ui
*ui
, char *encoding
)
2061 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2062 game_state
*newstate
)
2066 struct game_drawstate
{
2073 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
2074 int x
, int y
, int button
)
2079 char button_char
= ' ';
2080 enum line_state old_state
;
2082 button
&= ~MOD_MASK
;
2084 /* Around each line is a diamond-shaped region where points within that
2085 * region are closer to this line than any other. We assume any click
2086 * within a line's diamond was meant for that line. It would all be a lot
2087 * simpler if the / and % operators respected modulo arithmetic properly
2088 * for negative numbers. */
2093 /* Get the coordinates of the square the click was in */
2094 i
= (x
+ TILE_SIZE
) / TILE_SIZE
- 1;
2095 j
= (y
+ TILE_SIZE
) / TILE_SIZE
- 1;
2097 /* Get the precise position inside square [i,j] */
2098 p
= (x
+ TILE_SIZE
) % TILE_SIZE
;
2099 q
= (y
+ TILE_SIZE
) % TILE_SIZE
;
2101 /* After this bit of magic [i,j] will correspond to the point either above
2102 * or to the left of the line selected */
2104 if (TILE_SIZE
- p
> q
) {
2107 hl_selected
= FALSE
;
2111 if (TILE_SIZE
- q
> p
) {
2112 hl_selected
= FALSE
;
2123 if (i
>= state
->w
|| j
>= state
->h
+ 1)
2126 if (i
>= state
->w
+ 1 || j
>= state
->h
)
2130 /* I think it's only possible to play this game with mouse clicks, sorry */
2131 /* Maybe will add mouse drag support some time */
2133 old_state
= RIGHTOF_DOT(state
, i
, j
);
2135 old_state
= BELOW_DOT(state
, i
, j
);
2139 switch (old_state
) {
2153 switch (old_state
) {
2168 sprintf(buf
, "%d,%d%c%c", i
, j
, (int)(hl_selected ?
'h' : 'v'), (int)button_char
);
2174 static game_state
*execute_move(game_state
*state
, char *move
)
2177 game_state
*newstate
= dup_game(state
);
2179 if (move
[0] == 'S') {
2181 newstate
->cheated
= TRUE
;
2186 move
= strchr(move
, ',');
2190 move
+= strspn(move
, "1234567890");
2191 switch (*(move
++)) {
2193 if (i
>= newstate
->w
|| j
> newstate
->h
)
2195 switch (*(move
++)) {
2197 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_YES
;
2200 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_NO
;
2203 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2210 if (i
> newstate
->w
|| j
>= newstate
->h
)
2212 switch (*(move
++)) {
2214 LV_BELOW_DOT(newstate
, i
, j
) = LINE_YES
;
2217 LV_BELOW_DOT(newstate
, i
, j
) = LINE_NO
;
2220 LV_BELOW_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2232 * Check for completion.
2234 i
= 0; /* placate optimiser */
2235 for (j
= 0; j
<= newstate
->h
; j
++) {
2236 for (i
= 0; i
< newstate
->w
; i
++)
2237 if (LV_RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
)
2239 if (i
< newstate
->w
)
2242 if (j
<= newstate
->h
) {
2248 * We've found a horizontal edge at (i,j). Follow it round
2249 * to see if it's part of a loop.
2253 int order
= dot_order(newstate
, x
, y
, LINE_YES
);
2255 goto completion_check_done
;
2257 if (LEFTOF_DOT(newstate
, x
, y
) == LINE_YES
&& prevdir
!= 'L') {
2260 } else if (RIGHTOF_DOT(newstate
, x
, y
) == LINE_YES
&&
2264 } else if (ABOVE_DOT(newstate
, x
, y
) == LINE_YES
&&
2268 } else if (BELOW_DOT(newstate
, x
, y
) == LINE_YES
&&
2273 assert(!"Can't happen"); /* dot_order guarantees success */
2278 if (x
== i
&& y
== j
)
2282 if (x
!= i
|| y
!= j
|| looplen
== 0)
2283 goto completion_check_done
;
2286 * We've traced our way round a loop, and we know how many
2287 * line segments were involved. Count _all_ the line
2288 * segments in the grid, to see if the loop includes them
2292 for (j
= 0; j
<= newstate
->h
; j
++)
2293 for (i
= 0; i
<= newstate
->w
; i
++)
2294 count
+= ((RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
) +
2295 (BELOW_DOT(newstate
, i
, j
) == LINE_YES
));
2296 assert(count
>= looplen
);
2297 if (count
!= looplen
)
2298 goto completion_check_done
;
2301 * The grid contains one closed loop and nothing else.
2302 * Check that all the clues are satisfied.
2304 for (j
= 0; j
< newstate
->h
; ++j
) {
2305 for (i
= 0; i
< newstate
->w
; ++i
) {
2306 int n
= CLUE_AT(newstate
, i
, j
);
2308 if (square_order(newstate
, i
, j
, LINE_YES
) != n
- '0') {
2309 goto completion_check_done
;
2318 newstate
->solved
= TRUE
;
2321 completion_check_done
:
2325 free_game(newstate
);
2329 /* ----------------------------------------------------------------------
2333 #define SIZE(d) ((d) * TILE_SIZE + 2 * BORDER + 1)
2335 static void game_compute_size(game_params
*params
, int tilesize
,
2338 struct { int tilesize
; } ads
, *ds
= &ads
;
2339 ads
.tilesize
= tilesize
;
2341 *x
= SIZE(params
->w
);
2342 *y
= SIZE(params
->h
);
2345 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
2346 game_params
*params
, int tilesize
)
2348 ds
->tilesize
= tilesize
;
2351 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2353 float *ret
= snewn(4 * NCOLOURS
, float);
2355 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2357 ret
[COL_FOREGROUND
* 3 + 0] = 0.0F
;
2358 ret
[COL_FOREGROUND
* 3 + 1] = 0.0F
;
2359 ret
[COL_FOREGROUND
* 3 + 2] = 0.0F
;
2361 ret
[COL_HIGHLIGHT
* 3 + 0] = 1.0F
;
2362 ret
[COL_HIGHLIGHT
* 3 + 1] = 1.0F
;
2363 ret
[COL_HIGHLIGHT
* 3 + 2] = 1.0F
;
2365 *ncolours
= NCOLOURS
;
2369 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
2371 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2375 ds
->hl
= snewn(HL_COUNT(state
), char);
2376 ds
->vl
= snewn(VL_COUNT(state
), char);
2379 memset(ds
->hl
, LINE_UNKNOWN
, HL_COUNT(state
));
2380 memset(ds
->vl
, LINE_UNKNOWN
, VL_COUNT(state
));
2385 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2392 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2393 game_state
*state
, int dir
, game_ui
*ui
,
2394 float animtime
, float flashtime
)
2397 int w
= state
->w
, h
= state
->h
;
2399 int line_colour
, flash_changed
;
2403 * The initial contents of the window are not guaranteed and
2404 * can vary with front ends. To be on the safe side, all games
2405 * should start by drawing a big background-colour rectangle
2406 * covering the whole window.
2408 draw_rect(dr
, 0, 0, SIZE(state
->w
), SIZE(state
->h
), COL_BACKGROUND
);
2411 for (j
= 0; j
< h
+ 1; ++j
) {
2412 for (i
= 0; i
< w
+ 1; ++i
) {
2414 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2415 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2416 LINEWIDTH
, LINEWIDTH
, COL_FOREGROUND
);
2421 for (j
= 0; j
< h
; ++j
) {
2422 for (i
= 0; i
< w
; ++i
) {
2423 c
[0] = CLUE_AT(state
, i
, j
);
2426 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2,
2427 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2,
2428 FONT_VARIABLE
, TILE_SIZE
/2,
2429 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_FOREGROUND
, c
);
2432 draw_update(dr
, 0, 0,
2433 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2434 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
2438 if (flashtime
> 0 &&
2439 (flashtime
<= FLASH_TIME
/3 ||
2440 flashtime
>= FLASH_TIME
*2/3)) {
2441 flash_changed
= !ds
->flashing
;
2442 ds
->flashing
= TRUE
;
2443 line_colour
= COL_HIGHLIGHT
;
2445 flash_changed
= ds
->flashing
;
2446 ds
->flashing
= FALSE
;
2447 line_colour
= COL_FOREGROUND
;
2450 #define CROSS_SIZE (3 * LINEWIDTH / 2)
2452 #define CLEAR_VL(i, j) do { \
2454 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2455 BORDER + j * TILE_SIZE + LINEWIDTH/2, \
2457 TILE_SIZE - LINEWIDTH, \
2460 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2461 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2463 TILE_SIZE + CROSS_SIZE*2); \
2466 #define CLEAR_HL(i, j) do { \
2468 BORDER + i * TILE_SIZE + LINEWIDTH/2, \
2469 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2470 TILE_SIZE - LINEWIDTH, \
2474 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2475 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2476 TILE_SIZE + CROSS_SIZE*2, \
2480 /* Vertical lines */
2481 for (j
= 0; j
< h
; ++j
) {
2482 for (i
= 0; i
< w
+ 1; ++i
) {
2483 switch (BELOW_DOT(state
, i
, j
)) {
2485 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2490 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
) ||
2494 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2495 BORDER
+ j
* TILE_SIZE
+ LINEWIDTH
/2,
2496 LINEWIDTH
, TILE_SIZE
- LINEWIDTH
,
2501 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2504 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2505 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2506 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2507 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2510 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2511 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2512 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2513 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2518 ds
->vl
[i
+ (w
+ 1) * j
] = BELOW_DOT(state
, i
, j
);
2522 /* Horizontal lines */
2523 for (j
= 0; j
< h
+ 1; ++j
) {
2524 for (i
= 0; i
< w
; ++i
) {
2525 switch (RIGHTOF_DOT(state
, i
, j
)) {
2527 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2532 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
) ||
2536 BORDER
+ i
* TILE_SIZE
+ LINEWIDTH
/2,
2537 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2538 TILE_SIZE
- LINEWIDTH
, LINEWIDTH
,
2543 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2546 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2547 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2548 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2549 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2552 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2553 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2554 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2555 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2560 ds
->hl
[i
+ w
* j
] = RIGHTOF_DOT(state
, i
, j
);
2565 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2566 int dir
, game_ui
*ui
)
2571 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2572 int dir
, game_ui
*ui
)
2574 if (!oldstate
->solved
&& newstate
->solved
&&
2575 !oldstate
->cheated
&& !newstate
->cheated
) {
2582 static int game_wants_statusbar(void)
2587 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2592 static void game_print_size(game_params
*params
, float *x
, float *y
)
2597 * I'll use 7mm squares by default.
2599 game_compute_size(params
, 700, &pw
, &ph
);
2604 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2606 int w
= state
->w
, h
= state
->h
;
2607 int ink
= print_mono_colour(dr
, 0);
2609 game_drawstate ads
, *ds
= &ads
;
2610 ds
->tilesize
= tilesize
;
2613 * Dots. I'll deliberately make the dots a bit wider than the
2614 * lines, so you can still see them. (And also because it's
2615 * annoyingly tricky to make them _exactly_ the same size...)
2617 for (y
= 0; y
<= h
; y
++)
2618 for (x
= 0; x
<= w
; x
++)
2619 draw_circle(dr
, BORDER
+ x
* TILE_SIZE
, BORDER
+ y
* TILE_SIZE
,
2620 LINEWIDTH
, ink
, ink
);
2625 for (y
= 0; y
< h
; y
++)
2626 for (x
= 0; x
< w
; x
++)
2627 if (CLUE_AT(state
, x
, y
) != ' ') {
2630 c
[0] = CLUE_AT(state
, x
, y
);
2633 BORDER
+ x
* TILE_SIZE
+ TILE_SIZE
/2,
2634 BORDER
+ y
* TILE_SIZE
+ TILE_SIZE
/2,
2635 FONT_VARIABLE
, TILE_SIZE
/2,
2636 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, c
);
2640 * Lines. (At the moment, I'm not bothering with crosses.)
2642 for (y
= 0; y
<= h
; y
++)
2643 for (x
= 0; x
< w
; x
++)
2644 if (RIGHTOF_DOT(state
, x
, y
) == LINE_YES
)
2645 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
,
2646 BORDER
+ y
* TILE_SIZE
- LINEWIDTH
/2,
2647 TILE_SIZE
, (LINEWIDTH
/2) * 2 + 1, ink
);
2648 for (y
= 0; y
< h
; y
++)
2649 for (x
= 0; x
<= w
; x
++)
2650 if (BELOW_DOT(state
, x
, y
) == LINE_YES
)
2651 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
- LINEWIDTH
/2,
2652 BORDER
+ y
* TILE_SIZE
,
2653 (LINEWIDTH
/2) * 2 + 1, TILE_SIZE
, ink
);
2657 #define thegame loopy
2660 const struct game thegame
= {
2661 "Loopy", "games.loopy",
2668 TRUE
, game_configure
, custom_params
,
2676 TRUE
, game_text_format
,
2684 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2687 game_free_drawstate
,
2691 TRUE
, FALSE
, game_print_size
, game_print
,
2692 game_wants_statusbar
,
2693 FALSE
, game_timing_state
,
2694 0, /* mouse_priorities */