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); */
233 static solver_state
*dup_solver_state(solver_state
*sstate
) {
234 game_state
*state
= dup_game(sstate
->state
);
236 solver_state
*ret
= snew(solver_state
);
238 ret
->state
= dup_game(state
);
240 ret
->dot_atmostone
= snewn(DOT_COUNT(state
), char);
241 memcpy(ret
->dot_atmostone
, sstate
->dot_atmostone
, DOT_COUNT(state
));
243 ret
->dot_atleastone
= snewn(DOT_COUNT(state
), char);
244 memcpy(ret
->dot_atleastone
, sstate
->dot_atleastone
, DOT_COUNT(state
));
247 ret
->dline_identical
= snewn((state
->w
+ 1) * (state
->h
+ 1), char);
248 memcpy(ret
->dline_identical
, state
->dot_atmostone
,
249 (state
->w
+ 1) * (state
->h
+ 1));
252 ret
->recursion_remaining
= sstate
->recursion_remaining
;
253 ret
->solver_status
= sstate
->solver_status
;
255 ret
->dotdsf
= snewn(DOT_COUNT(state
), int);
256 ret
->looplen
= snewn(DOT_COUNT(state
), int);
257 memcpy(ret
->dotdsf
, sstate
->dotdsf
, DOT_COUNT(state
) * sizeof(int));
258 memcpy(ret
->looplen
, sstate
->looplen
, DOT_COUNT(state
) * sizeof(int));
264 * Merge two dots due to the existence of an edge between them.
265 * Updates the dsf tracking equivalence classes, and keeps track of
266 * the length of path each dot is currently a part of.
268 static void merge_dots(solver_state
*sstate
, int x1
, int y1
, int x2
, int y2
)
272 i
= y1
* (sstate
->state
->w
+ 1) + x1
;
273 j
= y2
* (sstate
->state
->w
+ 1) + x2
;
275 i
= dsf_canonify(sstate
->dotdsf
, i
);
276 j
= dsf_canonify(sstate
->dotdsf
, j
);
279 len
= sstate
->looplen
[i
] + sstate
->looplen
[j
];
280 dsf_merge(sstate
->dotdsf
, i
, j
);
281 i
= dsf_canonify(sstate
->dotdsf
, i
);
282 sstate
->looplen
[i
] = len
;
286 /* Count the number of lines of a particular type currently going into the
287 * given dot. Lines going off the edge of the board are assumed fixed no. */
288 static int dot_order(const game_state
* state
, int i
, int j
, char line_type
)
293 if (LEFTOF_DOT(state
, i
, j
) == line_type
)
296 if (line_type
== LINE_NO
)
300 if (RIGHTOF_DOT(state
, i
, j
) == line_type
)
303 if (line_type
== LINE_NO
)
307 if (ABOVE_DOT(state
, i
, j
) == line_type
)
310 if (line_type
== LINE_NO
)
314 if (BELOW_DOT(state
, i
, j
) == line_type
)
317 if (line_type
== LINE_NO
)
323 /* Count the number of lines of a particular type currently surrounding the
325 static int square_order(const game_state
* state
, int i
, int j
, char line_type
)
329 if (ABOVE_SQUARE(state
, i
, j
) == line_type
)
331 if (BELOW_SQUARE(state
, i
, j
) == line_type
)
333 if (LEFTOF_SQUARE(state
, i
, j
) == line_type
)
335 if (RIGHTOF_SQUARE(state
, i
, j
) == line_type
)
341 /* Set all lines bordering a dot of type old_type to type new_type */
342 static void dot_setall(game_state
*state
, int i
, int j
,
343 char old_type
, char new_type
)
345 /* printf("dot_setall([%d,%d], %d, %d)\n", i, j, old_type, new_type); */
346 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == old_type
)
347 LV_LEFTOF_DOT(state
, i
, j
) = new_type
;
348 if (i
< state
->w
&& RIGHTOF_DOT(state
, i
, j
) == old_type
)
349 LV_RIGHTOF_DOT(state
, i
, j
) = new_type
;
350 if (j
> 0 && ABOVE_DOT(state
, i
, j
) == old_type
)
351 LV_ABOVE_DOT(state
, i
, j
) = new_type
;
352 if (j
< state
->h
&& BELOW_DOT(state
, i
, j
) == old_type
)
353 LV_BELOW_DOT(state
, i
, j
) = new_type
;
355 /* Set all lines bordering a square of type old_type to type new_type */
356 static void square_setall(game_state
*state
, int i
, int j
,
357 char old_type
, char new_type
)
359 if (ABOVE_SQUARE(state
, i
, j
) == old_type
)
360 ABOVE_SQUARE(state
, i
, j
) = new_type
;
361 if (BELOW_SQUARE(state
, i
, j
) == old_type
)
362 BELOW_SQUARE(state
, i
, j
) = new_type
;
363 if (LEFTOF_SQUARE(state
, i
, j
) == old_type
)
364 LEFTOF_SQUARE(state
, i
, j
) = new_type
;
365 if (RIGHTOF_SQUARE(state
, i
, j
) == old_type
)
366 RIGHTOF_SQUARE(state
, i
, j
) = new_type
;
369 static game_params
*default_params(void)
371 game_params
*ret
= snew(game_params
);
380 static game_params
*dup_params(game_params
*params
)
382 game_params
*ret
= snew(game_params
);
383 *ret
= *params
; /* structure copy */
387 static const struct {
390 } loopy_presets
[] = {
391 { "4x4 Easy", { 4, 4, 0 } },
392 { "4x4 Hard", { 4, 4, 2 } },
393 { "7x7 Easy", { 7, 7, 0 } },
394 { "7x7 Hard", { 7, 7, 0 } },
395 { "10x10 Easy", { 10, 10, 0 } },
396 { "10x10 Hard", { 10, 10, 2 } },
397 { "15x15 Easy", { 15, 15, 0 } },
398 { "20x30 Easy", { 20, 30, 0 } }
401 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
405 if (i
< 0 || i
>= lenof(loopy_presets
))
408 tmppar
= loopy_presets
[i
].params
;
409 *params
= dup_params(&tmppar
);
410 *name
= dupstr(loopy_presets
[i
].desc
);
415 static void free_params(game_params
*params
)
420 static void decode_params(game_params
*params
, char const *string
)
422 params
->h
= params
->w
= atoi(string
);
424 while (*string
&& isdigit((unsigned char)*string
)) string
++;
425 if (*string
== 'x') {
427 params
->h
= atoi(string
);
428 while (*string
&& isdigit((unsigned char)*string
)) string
++;
430 if (*string
== 'r') {
432 params
->rec
= atoi(string
);
433 while (*string
&& isdigit((unsigned char)*string
)) string
++;
437 static char *encode_params(game_params
*params
, int full
)
440 sprintf(str
, "%dx%d", params
->w
, params
->h
);
442 sprintf(str
+ strlen(str
), "r%d", params
->rec
);
446 static config_item
*game_configure(game_params
*params
)
451 ret
= snewn(4, config_item
);
453 ret
[0].name
= "Width";
454 ret
[0].type
= C_STRING
;
455 sprintf(buf
, "%d", params
->w
);
456 ret
[0].sval
= dupstr(buf
);
459 ret
[1].name
= "Height";
460 ret
[1].type
= C_STRING
;
461 sprintf(buf
, "%d", params
->h
);
462 ret
[1].sval
= dupstr(buf
);
465 ret
[2].name
= "Recursion depth";
466 ret
[2].type
= C_STRING
;
467 sprintf(buf
, "%d", params
->rec
);
468 ret
[2].sval
= dupstr(buf
);
479 static game_params
*custom_params(config_item
*cfg
)
481 game_params
*ret
= snew(game_params
);
483 ret
->w
= atoi(cfg
[0].sval
);
484 ret
->h
= atoi(cfg
[1].sval
);
485 ret
->rec
= atoi(cfg
[2].sval
);
490 static char *validate_params(game_params
*params
, int full
)
492 if (params
->w
< 4 || params
->h
< 4)
493 return "Width and height must both be at least 4";
495 return "Recursion depth can't be negative";
499 /* We're going to store a list of current candidate squares for lighting.
500 * Each square gets a 'score', which tells us how adding that square right
501 * now would affect the length of the solution loop. We're trying to
502 * maximise that quantity so will bias our random selection of squares to
503 * light towards those with high scores */
510 static int get_square_cmpfn(void *v1
, void *v2
)
512 struct square
*s1
= (struct square
*)v1
;
513 struct square
*s2
= (struct square
*)v2
;
527 static int square_sort_cmpfn(void *v1
, void *v2
)
529 struct square
*s1
= (struct square
*)v1
;
530 struct square
*s2
= (struct square
*)v2
;
533 r
= s2
->score
- s1
->score
;
538 r
= s1
->random
- s2
->random
;
544 * It's _just_ possible that two squares might have been given
545 * the same random value. In that situation, fall back to
546 * comparing based on the coordinates. This introduces a tiny
547 * directional bias, but not a significant one.
549 return get_square_cmpfn(v1
, v2
);
552 static void print_tree(tree234
*tree
)
557 printf("Print tree:\n");
558 while (i
< count234(tree
)) {
559 s
= (struct square
*)index234(tree
, i
);
561 printf(" [%d,%d], %d, %d\n", s
->x
, s
->y
, s
->score
, s
->random
);
567 enum { SQUARE_LIT
, SQUARE_UNLIT
};
569 #define SQUARE_STATE(i, j) \
570 (((i) < 0 || (i) >= params->w || \
571 (j) < 0 || (j) >= params->h) ? \
572 SQUARE_UNLIT : LV_SQUARE_STATE(i,j))
574 #define LV_SQUARE_STATE(i, j) board[(i) + params->w * (j)]
576 static void print_board(const game_params
*params
, const char *board
)
582 for (i
= 0; i
< params
->w
; i
++) {
586 for (j
= 0; j
< params
->h
; j
++) {
588 for (i
= 0; i
< params
->w
; i
++) {
589 printf("%c", SQUARE_STATE(i
, j
) ?
' ' : 'O');
596 static char *new_fullyclued_board(game_params
*params
, random_state
*rs
)
602 game_state
*state
= &s
;
603 int board_area
= SQUARE_COUNT(params
);
606 struct square
*square
, *tmpsquare
, *sq
;
607 struct square square_pos
;
609 /* These will contain exactly the same information, sorted into different
611 tree234
*lightable_squares_sorted
, *lightable_squares_gettable
;
613 #define SQUARE_REACHABLE(i,j) \
614 (t = (SQUARE_STATE(i-1, j) == SQUARE_LIT || \
615 SQUARE_STATE(i+1, j) == SQUARE_LIT || \
616 SQUARE_STATE(i, j-1) == SQUARE_LIT || \
617 SQUARE_STATE(i, j+1) == SQUARE_LIT), \
618 /* printf("SQUARE_REACHABLE(%d,%d) = %d\n", i, j, t), */ \
622 /* One situation in which we may not light a square is if that'll leave one
623 * square above/below and one left/right of us unlit, separated by a lit
624 * square diagnonal from us */
625 #define SQUARE_DIAGONAL_VIOLATION(i, j, h, v) \
626 (t = (SQUARE_STATE((i)+(h), (j)) == SQUARE_UNLIT && \
627 SQUARE_STATE((i), (j)+(v)) == SQUARE_UNLIT && \
628 SQUARE_STATE((i)+(h), (j)+(v)) == SQUARE_LIT), \
629 /* t ? printf("SQUARE_DIAGONAL_VIOLATION(%d, %d, %d, %d)\n",
633 /* We also may not light a square if it will form a loop of lit squares
634 * around some unlit squares, as then the game soln won't have a single
636 #define SQUARE_LOOP_VIOLATION(i, j, lit1, lit2) \
637 (SQUARE_STATE((i)+1, (j)) == lit1 && \
638 SQUARE_STATE((i)-1, (j)) == lit1 && \
639 SQUARE_STATE((i), (j)+1) == lit2 && \
640 SQUARE_STATE((i), (j)-1) == lit2)
642 #define CAN_LIGHT_SQUARE(i, j) \
643 (SQUARE_REACHABLE(i, j) && \
644 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, -1) && \
645 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, -1) && \
646 !SQUARE_DIAGONAL_VIOLATION(i, j, -1, +1) && \
647 !SQUARE_DIAGONAL_VIOLATION(i, j, +1, +1) && \
648 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_LIT, SQUARE_UNLIT) && \
649 !SQUARE_LOOP_VIOLATION(i, j, SQUARE_UNLIT, SQUARE_LIT))
651 #define IS_LIGHTING_CANDIDATE(i, j) \
652 (SQUARE_STATE(i, j) == SQUARE_UNLIT && \
653 CAN_LIGHT_SQUARE(i,j))
655 /* The 'score' of a square reflects its current desirability for selection
656 * as the next square to light. We want to encourage moving into uncharted
657 * areas so we give scores according to how many of the square's neighbours
658 * are currently unlit. */
665 #define SQUARE_SCORE(i,j) \
666 (2*((SQUARE_STATE(i-1, j) == SQUARE_UNLIT) + \
667 (SQUARE_STATE(i+1, j) == SQUARE_UNLIT) + \
668 (SQUARE_STATE(i, j-1) == SQUARE_UNLIT) + \
669 (SQUARE_STATE(i, j+1) == SQUARE_UNLIT)) - 4)
671 /* When a square gets lit, this defines how far away from that square we
672 * need to go recomputing scores */
673 #define SCORE_DISTANCE 1
675 board
= snewn(board_area
, char);
676 clues
= snewn(board_area
, char);
678 state
->h
= params
->h
;
679 state
->w
= params
->w
;
680 state
->clues
= clues
;
683 memset(board
, SQUARE_UNLIT
, board_area
);
685 /* Seed the board with a single lit square near the middle */
688 if (params
->w
& 1 && random_bits(rs
, 1))
690 if (params
->h
& 1 && random_bits(rs
, 1))
693 LV_SQUARE_STATE(i
, j
) = SQUARE_LIT
;
695 /* We need a way of favouring squares that will increase our loopiness.
696 * We do this by maintaining a list of all candidate squares sorted by
697 * their score and choose randomly from that with appropriate skew.
698 * In order to avoid consistently biasing towards particular squares, we
699 * need the sort order _within_ each group of scores to be completely
700 * random. But it would be abusing the hospitality of the tree234 data
701 * structure if our comparison function were nondeterministic :-). So with
702 * each square we associate a random number that does not change during a
703 * particular run of the generator, and use that as a secondary sort key.
704 * Yes, this means we will be biased towards particular random squares in
705 * any one run but that doesn't actually matter. */
707 lightable_squares_sorted
= newtree234(square_sort_cmpfn
);
708 lightable_squares_gettable
= newtree234(get_square_cmpfn
);
709 #define ADD_SQUARE(s) \
711 /* printf("ADD SQUARE: [%d,%d], %d, %d\n",
712 s->x, s->y, s->score, s->random);*/ \
713 sq = add234(lightable_squares_sorted, s); \
715 sq = add234(lightable_squares_gettable, s); \
719 #define REMOVE_SQUARE(s) \
721 /* printf("DELETE SQUARE: [%d,%d], %d, %d\n",
722 s->x, s->y, s->score, s->random);*/ \
723 sq = del234(lightable_squares_sorted, s); \
725 sq = del234(lightable_squares_gettable, s); \
729 #define HANDLE_DIR(a, b) \
730 square = snew(struct square); \
731 square->x = (i)+(a); \
732 square->y = (j)+(b); \
734 square->random = random_bits(rs, 31); \
742 /* Light squares one at a time until the board is interesting enough */
745 /* We have count234(lightable_squares) possibilities, and in
746 * lightable_squares_sorted they are sorted with the most desirable
748 c
= count234(lightable_squares_sorted
);
751 assert(c
== count234(lightable_squares_gettable
));
753 /* Check that the best square available is any good */
754 square
= (struct square
*)index234(lightable_squares_sorted
, 0);
757 if (square
->score
<= 0)
760 print_tree(lightable_squares_sorted
);
761 assert(square
->score
== SQUARE_SCORE(square
->x
, square
->y
));
762 assert(SQUARE_STATE(square
->x
, square
->y
) == SQUARE_UNLIT
);
763 assert(square
->x
>= 0 && square
->x
< params
->w
);
764 assert(square
->y
>= 0 && square
->y
< params
->h
);
765 /* printf("LIGHT SQUARE: [%d,%d], score = %d\n", square->x, square->y, square->score); */
767 /* Update data structures */
768 LV_SQUARE_STATE(square
->x
, square
->y
) = SQUARE_LIT
;
769 REMOVE_SQUARE(square
);
771 print_board(params
, board
);
773 /* We might have changed the score of any squares up to 2 units away in
775 for (b
= -SCORE_DISTANCE
; b
<= SCORE_DISTANCE
; b
++) {
776 for (a
= -SCORE_DISTANCE
; a
<= SCORE_DISTANCE
; a
++) {
779 square_pos
.x
= square
->x
+ a
;
780 square_pos
.y
= square
->y
+ b
;
781 /* printf("Refreshing score for [%d,%d]:\n", square_pos.x, square_pos.y); */
782 if (square_pos
.x
< 0 || square_pos
.x
>= params
->w
||
783 square_pos
.y
< 0 || square_pos
.y
>= params
->h
) {
784 /* printf(" Out of bounds\n"); */
787 tmpsquare
= find234(lightable_squares_gettable
, &square_pos
,
790 /* printf(" Removing\n"); */
791 assert(tmpsquare
->x
== square_pos
.x
);
792 assert(tmpsquare
->y
== square_pos
.y
);
793 assert(SQUARE_STATE(tmpsquare
->x
, tmpsquare
->y
) ==
795 REMOVE_SQUARE(tmpsquare
);
797 /* printf(" Creating\n"); */
798 tmpsquare
= snew(struct square
);
799 tmpsquare
->x
= square_pos
.x
;
800 tmpsquare
->y
= square_pos
.y
;
801 tmpsquare
->random
= random_bits(rs
, 31);
803 tmpsquare
->score
= SQUARE_SCORE(tmpsquare
->x
, tmpsquare
->y
);
805 if (IS_LIGHTING_CANDIDATE(tmpsquare
->x
, tmpsquare
->y
)) {
806 /* printf(" Adding\n"); */
807 ADD_SQUARE(tmpsquare
);
809 /* printf(" Destroying\n"); */
814 /* printf("\n\n"); */
817 while ((square
= delpos234(lightable_squares_gettable
, 0)) != NULL
)
819 freetree234(lightable_squares_gettable
);
820 freetree234(lightable_squares_sorted
);
822 /* Copy out all the clues */
823 for (j
= 0; j
< params
->h
; ++j
) {
824 for (i
= 0; i
< params
->w
; ++i
) {
825 c
= SQUARE_STATE(i
, j
);
826 LV_CLUE_AT(state
, i
, j
) = '0';
827 if (SQUARE_STATE(i
-1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
828 if (SQUARE_STATE(i
+1, j
) != c
) ++LV_CLUE_AT(state
, i
, j
);
829 if (SQUARE_STATE(i
, j
-1) != c
) ++LV_CLUE_AT(state
, i
, j
);
830 if (SQUARE_STATE(i
, j
+1) != c
) ++LV_CLUE_AT(state
, i
, j
);
838 static solver_state
*solve_game_rec(const solver_state
*sstate
);
840 static int game_has_unique_soln(const game_state
*state
)
843 solver_state
*sstate_new
;
844 solver_state
*sstate
= new_solver_state((game_state
*)state
);
846 sstate_new
= solve_game_rec(sstate
);
848 ret
= (sstate_new
->solver_status
== SOLVER_SOLVED
);
850 free_solver_state(sstate_new
);
851 free_solver_state(sstate
);
856 /* Remove clues one at a time at random. */
857 static game_state
*remove_clues(game_state
*state
, random_state
*rs
)
859 int *square_list
, squares
;
860 game_state
*ret
= dup_game(state
), *saved_ret
;
863 /* We need to remove some clues. We'll do this by forming a list of all
864 * available equivalence classes, shuffling it, then going along one at a
865 * time clearing every member of each equivalence class, where removing a
866 * class doesn't render the board unsolvable. */
867 squares
= state
->w
* state
->h
;
868 square_list
= snewn(squares
, int);
869 for (n
= 0; n
< squares
; ++n
) {
873 shuffle(square_list
, squares
, sizeof(int), rs
);
875 for (n
= 0; n
< squares
; ++n
) {
876 saved_ret
= dup_game(ret
);
877 LV_CLUE_AT(ret
, square_list
[n
] % state
->w
,
878 square_list
[n
] / state
->w
) = ' ';
879 if (game_has_unique_soln(ret
)) {
880 free_game(saved_ret
);
890 static char *validate_desc(game_params
*params
, char *desc
);
892 static char *new_game_desc(game_params
*params
, random_state
*rs
,
893 char **aux
, int interactive
)
895 /* solution and description both use run-length encoding in obvious ways */
897 char *description
= snewn(SQUARE_COUNT(params
) + 1, char);
898 char *dp
= description
;
901 game_state
*state
= snew(game_state
), *state_new
;
903 state
->h
= params
->h
;
904 state
->w
= params
->w
;
906 state
->hl
= snewn(HL_COUNT(params
), char);
907 state
->vl
= snewn(VL_COUNT(params
), char);
908 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
909 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
911 state
->solved
= state
->cheated
= FALSE
;
912 state
->recursion_depth
= params
->rec
;
914 /* Get a new random solvable board with all its clues filled in. Yes, this
915 * can loop for ever if the params are suitably unfavourable, but
916 * preventing games smaller than 4x4 seems to stop this happening */
918 state
->clues
= new_fullyclued_board(params
, rs
);
919 } while (!game_has_unique_soln(state
));
921 state_new
= remove_clues(state
, rs
);
926 for (j
= 0; j
< params
->h
; ++j
) {
927 for (i
= 0; i
< params
->w
; ++i
) {
928 if (CLUE_AT(state
, i
, j
) == ' ') {
929 if (empty_count
> 25) {
930 dp
+= sprintf(dp
, "%c", empty_count
+ 'a' - 1);
936 dp
+= sprintf(dp
, "%c", empty_count
+ 'a' - 1);
939 dp
+= sprintf(dp
, "%c", CLUE_AT(state
, i
, j
));
944 dp
+= sprintf(dp
, "%c", empty_count
+ 'a' - 1);
947 retval
= dupstr(description
);
950 assert(!validate_desc(params
, retval
));
955 /* We require that the params pass the test in validate_params and that the
956 * description fills the entire game area */
957 static char *validate_desc(game_params
*params
, char *desc
)
961 for (; *desc
; ++desc
) {
962 if (*desc
>= '0' && *desc
<= '9') {
967 count
+= *desc
- 'a' + 1;
970 return "Unknown character in description";
973 if (count
< SQUARE_COUNT(params
))
974 return "Description too short for board size";
975 if (count
> SQUARE_COUNT(params
))
976 return "Description too long for board size";
981 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
984 game_state
*state
= snew(game_state
);
985 int empties_to_make
= 0;
987 const char *dp
= desc
;
989 state
->recursion_depth
= params
->rec
;
991 state
->h
= params
->h
;
992 state
->w
= params
->w
;
994 state
->clues
= snewn(SQUARE_COUNT(params
), char);
995 state
->hl
= snewn(HL_COUNT(params
), char);
996 state
->vl
= snewn(VL_COUNT(params
), char);
998 state
->solved
= state
->cheated
= FALSE
;
1000 for (j
= 0 ; j
< params
->h
; ++j
) {
1001 for (i
= 0 ; i
< params
->w
; ++i
) {
1002 if (empties_to_make
) {
1004 LV_CLUE_AT(state
, i
, j
) = ' ';
1010 if (n
>=0 && n
< 10) {
1011 LV_CLUE_AT(state
, i
, j
) = *dp
;
1015 LV_CLUE_AT(state
, i
, j
) = ' ';
1016 empties_to_make
= n
- 1;
1022 memset(state
->hl
, LINE_UNKNOWN
, HL_COUNT(params
));
1023 memset(state
->vl
, LINE_UNKNOWN
, VL_COUNT(params
));
1028 enum { LOOP_NONE
=0, LOOP_SOLN
, LOOP_NOT_SOLN
};
1030 /* Starting at dot [i,j] moves around 'state' removing lines until it's clear
1031 * whether or not the starting dot was on a loop. Returns boolean specifying
1032 * whether a loop was found. loop_status calls this and assumes that if state
1033 * has any lines set, this function will always remove at least one. */
1034 static int destructively_find_loop(game_state
*state
)
1036 int a
, b
, i
, j
, new_i
, new_j
, n
;
1039 lp
= (char *)memchr(state
->hl
, LINE_YES
, HL_COUNT(state
));
1041 /* We know we're going to return false but we have to fulfil our
1043 lp
= (char *)memchr(state
->vl
, LINE_YES
, VL_COUNT(state
));
1055 assert(i
+ j
* state
->w
== n
); /* because I'm feeling stupid */
1056 /* Save start position */
1060 /* Delete one line from the potential loop */
1061 if (LEFTOF_DOT(state
, i
, j
) == LINE_YES
) {
1062 LV_LEFTOF_DOT(state
, i
, j
) = LINE_NO
;
1064 } else if (ABOVE_DOT(state
, i
, j
) == LINE_YES
) {
1065 LV_ABOVE_DOT(state
, i
, j
) = LINE_NO
;
1067 } else if (RIGHTOF_DOT(state
, i
, j
) == LINE_YES
) {
1068 LV_RIGHTOF_DOT(state
, i
, j
) = LINE_NO
;
1070 } else if (BELOW_DOT(state
, i
, j
) == LINE_YES
) {
1071 LV_BELOW_DOT(state
, i
, j
) = LINE_NO
;
1078 /* From the current position of [i,j] there needs to be exactly one
1082 #define HANDLE_DIR(dir_dot, x, y) \
1083 if (dir_dot(state, i, j) == LINE_YES) { \
1084 if (new_i != -1 || new_j != -1) \
1088 LV_##dir_dot(state, i, j) = LINE_NO; \
1090 HANDLE_DIR(ABOVE_DOT
, 0, -1);
1091 HANDLE_DIR(BELOW_DOT
, 0, +1);
1092 HANDLE_DIR(LEFTOF_DOT
, -1, 0);
1093 HANDLE_DIR(RIGHTOF_DOT
, +1, 0);
1095 if (new_i
== -1 || new_j
== -1) {
1101 } while (i
!= a
|| j
!= b
);
1106 static int loop_status(game_state
*state
)
1109 game_state
*tmpstate
;
1110 int loop_found
= FALSE
, non_loop_found
= FALSE
, any_lines_found
= FALSE
;
1112 #define BAD_LOOP_FOUND \
1113 do { free_game(tmpstate); return LOOP_NOT_SOLN; } while(0)
1115 /* Repeatedly look for loops until we either run out of lines to consider
1116 * or discover for sure that the board fails on the grounds of having no
1118 tmpstate
= dup_game(state
);
1121 if (!memchr(tmpstate
->hl
, LINE_YES
, HL_COUNT(tmpstate
)) &&
1122 !memchr(tmpstate
->vl
, LINE_YES
, VL_COUNT(tmpstate
))) {
1125 any_lines_found
= TRUE
;
1129 if (destructively_find_loop(tmpstate
)) {
1134 non_loop_found
= TRUE
;
1138 free_game(tmpstate
);
1140 if (!any_lines_found
)
1143 if (non_loop_found
) {
1144 assert(!loop_found
); /* should have dealt with this already */
1148 /* Check that every clue is satisfied */
1149 for (j
= 0; j
< state
->h
; ++j
) {
1150 for (i
= 0; i
< state
->w
; ++i
) {
1151 n
= CLUE_AT(state
, i
, j
);
1153 if (square_order(state
, i
, j
, LINE_YES
) != n
- '0') {
1154 return LOOP_NOT_SOLN
;
1163 /* Sums the lengths of the numbers in range [0,n) */
1164 /* See equivalent function in solo.c for justification of this. */
1165 int len_0_to_n(int n
)
1167 int len
= 1; /* Counting 0 as a bit of a special case */
1170 for (i
= 1; i
< n
; i
*= 10) {
1171 len
+= max(n
- i
, 0);
1177 static char *encode_solve_move(const game_state
*state
)
1181 /* This is going to return a string representing the moves needed to set
1182 * every line in a grid to be the same as the ones in 'state'. The exact
1183 * length of this string is predictable. */
1185 len
= 1; /* Count the 'S' prefix */
1186 /* Numbers in horizontal lines */
1187 /* Horizontal lines, x position */
1188 len
+= len_0_to_n(state
->w
) * (state
->h
+ 1);
1189 /* Horizontal lines, y position */
1190 len
+= len_0_to_n(state
->h
+ 1) * (state
->w
);
1191 /* Vertical lines, y position */
1192 len
+= len_0_to_n(state
->h
) * (state
->w
+ 1);
1193 /* Vertical lines, x position */
1194 len
+= len_0_to_n(state
->w
+ 1) * (state
->h
);
1195 /* For each line we also have two letters and a comma */
1196 len
+= 3 * (HL_COUNT(state
) + VL_COUNT(state
));
1198 ret
= snewn(len
+ 1, char);
1201 p
+= sprintf(p
, "S");
1203 for (j
= 0; j
< state
->h
+ 1; ++j
) {
1204 for (i
= 0; i
< state
->w
; ++i
) {
1205 switch (RIGHTOF_DOT(state
, i
, j
)) {
1207 p
+= sprintf(p
, "%d,%dhy", i
, j
);
1210 p
+= sprintf(p
, "%d,%dhn", i
, j
);
1213 /* I'm going to forgive this because I think the results
1215 /* assert(!"Solver produced incomplete solution!"); */
1220 for (j
= 0; j
< state
->h
; ++j
) {
1221 for (i
= 0; i
< state
->w
+ 1; ++i
) {
1222 switch (BELOW_DOT(state
, i
, j
)) {
1224 p
+= sprintf(p
, "%d,%dvy", i
, j
);
1227 p
+= sprintf(p
, "%d,%dvn", i
, j
);
1230 /* I'm going to forgive this because I think the results
1232 /* assert(!"Solver produced incomplete solution!"); */
1237 /* No point in doing sums like that if they're going to be wrong */
1238 assert(strlen(ret
) <= (size_t)len
);
1242 /* BEGIN SOLVER IMPLEMENTATION */
1244 /* For each pair of lines through each dot we store a bit for whether
1245 * exactly one of those lines is ON, and in separate arrays we store whether
1246 * at least one is on and whether at most 1 is on. (If we know both or
1247 * neither is on that's already stored more directly.) That's six bits per
1248 * dot. Bit number n represents the lines shown in dot_type_dirs[n]. */
1259 #define OPP_DLINE(dline) (dline ^ 1)
1262 #define SQUARE_DLINES \
1263 HANDLE_DLINE(DLINE_UL, RIGHTOF_SQUARE, BELOW_SQUARE, 1, 1); \
1264 HANDLE_DLINE(DLINE_UR, LEFTOF_SQUARE, BELOW_SQUARE, 0, 1); \
1265 HANDLE_DLINE(DLINE_DL, RIGHTOF_SQUARE, ABOVE_SQUARE, 1, 0); \
1266 HANDLE_DLINE(DLINE_DR, LEFTOF_SQUARE, ABOVE_SQUARE, 0, 0);
1268 #define DOT_DLINES \
1269 HANDLE_DLINE(DLINE_VERT, ABOVE_DOT, BELOW_DOT); \
1270 HANDLE_DLINE(DLINE_HORIZ, LEFTOF_DOT, RIGHTOF_DOT); \
1271 HANDLE_DLINE(DLINE_UL, ABOVE_DOT, LEFTOF_DOT); \
1272 HANDLE_DLINE(DLINE_UR, ABOVE_DOT, RIGHTOF_DOT); \
1273 HANDLE_DLINE(DLINE_DL, BELOW_DOT, LEFTOF_DOT); \
1274 HANDLE_DLINE(DLINE_DR, BELOW_DOT, RIGHTOF_DOT);
1276 static void array_setall(char *array
, char from
, char to
, int len
)
1278 char *p
= array
, *p_old
= p
;
1279 int len_remaining
= len
;
1281 while ((p
= memchr(p
, from
, len_remaining
))) {
1283 len_remaining
-= p
- p_old
;
1289 static int game_states_equal(const game_state
*state1
,
1290 const game_state
*state2
)
1292 /* This deliberately doesn't check _all_ fields, just the ones that make a
1293 * game state 'interesting' from the POV of the solver */
1294 /* XXX review this */
1295 if (state1
== state2
)
1298 if (!state1
|| !state2
)
1301 if (state1
->w
!= state2
->w
|| state1
->h
!= state2
->h
)
1304 if (memcmp(state1
->hl
, state2
->hl
, HL_COUNT(state1
)))
1307 if (memcmp(state1
->vl
, state2
->vl
, VL_COUNT(state1
)))
1313 static int solver_states_equal(const solver_state
*sstate1
,
1314 const solver_state
*sstate2
)
1323 if (!game_states_equal(sstate1
->state
, sstate2
->state
)) {
1327 /* XXX fields missing, needs review */
1328 /* XXX we're deliberately not looking at solver_state as it's only a cache */
1330 if (memcmp(sstate1
->dot_atleastone
, sstate2
->dot_atleastone
,
1331 DOT_COUNT(sstate1
->state
))) {
1335 if (memcmp(sstate1
->dot_atmostone
, sstate2
->dot_atmostone
,
1336 DOT_COUNT(sstate1
->state
))) {
1340 /* handle dline_identical here */
1345 static void dot_setall_dlines(solver_state
*sstate
, enum dline dl
, int i
, int j
,
1346 enum line_state line_old
, enum line_state line_new
)
1348 game_state
*state
= sstate
->state
;
1350 /* First line in dline */
1355 if (j
> 0 && ABOVE_DOT(state
, i
, j
) == line_old
)
1356 LV_ABOVE_DOT(state
, i
, j
) = line_new
;
1360 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1361 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1364 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1365 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1369 /* Second line in dline */
1373 if (i
> 0 && LEFTOF_DOT(state
, i
, j
) == line_old
)
1374 LV_LEFTOF_DOT(state
, i
, j
) = line_new
;
1379 if (i
<= (state
)->w
&& RIGHTOF_DOT(state
, i
, j
) == line_old
)
1380 LV_RIGHTOF_DOT(state
, i
, j
) = line_new
;
1383 if (j
<= (state
)->h
&& BELOW_DOT(state
, i
, j
) == line_old
)
1384 LV_BELOW_DOT(state
, i
, j
) = line_new
;
1389 static void update_solver_status(solver_state
*sstate
)
1391 if (sstate
->solver_status
== SOLVER_INCOMPLETE
) {
1392 switch (loop_status(sstate
->state
)) {
1394 sstate
->solver_status
= SOLVER_INCOMPLETE
;
1397 if (sstate
->solver_status
!= SOLVER_AMBIGUOUS
)
1398 sstate
->solver_status
= SOLVER_SOLVED
;
1401 sstate
->solver_status
= SOLVER_MISTAKE
;
1408 /* This will return a dynamically allocated solver_state containing the (more)
1410 static solver_state
*solve_game_rec(const solver_state
*sstate_start
)
1413 int current_yes
, current_no
, desired
;
1414 solver_state
*sstate
, *sstate_saved
, *sstate_tmp
;
1417 solver_state
*sstate_rec_solved
;
1418 int recursive_soln_count
;
1421 printf("solve_game_rec: recursion_remaining = %d\n",
1422 sstate_start
->recursion_remaining
);
1425 sstate
= dup_solver_state((solver_state
*)sstate_start
);
1428 text
= game_text_format(sstate
->state
);
1429 printf("%s\n", text
);
1433 #define RETURN_IF_SOLVED \
1435 update_solver_status(sstate); \
1436 if (sstate->solver_status != SOLVER_INCOMPLETE) { \
1437 free_solver_state(sstate_saved); \
1442 sstate_saved
= NULL
;
1445 nonrecursive_solver
:
1448 sstate_saved
= dup_solver_state(sstate
);
1450 /* First we do the 'easy' work, that might cause concrete results */
1452 /* Per-square deductions */
1453 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1454 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1455 /* Begin rules that look at the clue (if there is one) */
1456 desired
= CLUE_AT(sstate
->state
, i
, j
);
1459 desired
= desired
- '0';
1460 current_yes
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1461 current_no
= square_order(sstate
->state
, i
, j
, LINE_NO
);
1463 if (desired
<= current_yes
) {
1464 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1468 if (4 - desired
<= current_no
) {
1469 square_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_YES
);
1476 /* Per-dot deductions */
1477 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1478 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1479 switch (dot_order(sstate
->state
, i
, j
, LINE_YES
)) {
1481 if (dot_order(sstate
->state
, i
, j
, LINE_NO
) == 3) {
1482 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1486 switch (dot_order(sstate
->state
, i
, j
, LINE_NO
)) {
1487 #define H1(dline, dir1_dot, dir2_dot, dot_howmany) \
1488 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1489 if (dir2_dot(sstate->state, i, j) == LINE_UNKNOWN){ \
1490 sstate->dot_howmany \
1491 [i + (sstate->state->w + 1) * j] |= 1<<dline; \
1495 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1496 H1(dline, dir1_dot, dir2_dot, dot_atleastone)
1497 /* 1 yes, 1 no, so exactly one of unknowns is yes */
1502 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1503 H1(dline, dir1_dot, dir2_dot, dot_atmostone)
1504 /* 1 yes, fewer than 2 no, so at most one of
1505 * unknowns is yes */
1510 case 2: /* 1 yes, 2 no */
1511 dot_setall(sstate
->state
, i
, j
,
1512 LINE_UNKNOWN
, LINE_YES
);
1518 dot_setall(sstate
->state
, i
, j
, LINE_UNKNOWN
, LINE_NO
);
1520 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1521 if (sstate->dot_atleastone \
1522 [i + (sstate->state->w + 1) * j] & 1<<dline) { \
1523 sstate->dot_atmostone \
1524 [i + (sstate->state->w + 1) * j] |= 1<<OPP_DLINE(dline); \
1526 /* If at least one of a dline in a dot is YES, at most one of
1527 * the opposite dline to that dot must be YES. */
1533 /* More obscure per-square operations */
1534 for (j
= 0; j
< sstate
->state
->h
; ++j
) {
1535 for (i
= 0; i
< sstate
->state
->w
; ++i
) {
1536 #define H1(dline, dir1_sq, dir2_sq, a, b, dot_howmany, line_query, line_set) \
1537 if (sstate->dot_howmany[i+a + (sstate->state->w + 1) * (j+b)] &\
1539 t = dir1_sq(sstate->state, i, j); \
1540 if (t == line_query) \
1541 dir2_sq(sstate->state, i, j) = line_set; \
1543 t = dir2_sq(sstate->state, i, j); \
1544 if (t == line_query) \
1545 dir1_sq(sstate->state, i, j) = line_set; \
1548 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1549 H1(dline, dir1_sq, dir2_sq, a, b, dot_atmostone, \
1551 /* If at most one of the DLINE is on, and one is definitely on,
1552 * set the other to definitely off */
1556 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1557 H1(dline, dir1_sq, dir2_sq, a, b, dot_atleastone, \
1559 /* If at least one of the DLINE is on, and one is definitely
1560 * off, set the other to definitely on */
1565 switch (CLUE_AT(sstate
->state
, i
, j
)) {
1568 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1569 /* At most one of any DLINE can be set */ \
1570 sstate->dot_atmostone \
1571 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1572 /* This DLINE provides enough YESes to solve the clue */\
1573 if (sstate->dot_atleastone \
1574 [i+a + (sstate->state->w + 1) * (j+b)] & \
1576 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1578 LINE_UNKNOWN, LINE_NO); \
1584 #define H1(dline, dot_at1one, dot_at2one, a, b) \
1585 if (sstate->dot_at1one \
1586 [i+a + (sstate->state->w + 1) * (j+b)] & \
1588 sstate->dot_at2one \
1589 [i+(1-a) + (sstate->state->w + 1) * (j+(1-b))] |= \
1590 1<<OPP_DLINE(dline); \
1592 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1593 H1(dline, dot_atleastone, dot_atmostone, a, b); \
1594 H1(dline, dot_atmostone, dot_atleastone, a, b);
1595 /* If at least one of one DLINE is set, at most one of
1596 * the opposing one is and vice versa */
1603 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1604 /* At least one of any DLINE can be set */ \
1605 sstate->dot_atleastone \
1606 [i+a + (sstate->state->w + 1) * (j+b)] |= 1<<dline; \
1607 /* This DLINE provides enough NOs to solve the clue */ \
1608 if (sstate->dot_atmostone \
1609 [i+a + (sstate->state->w + 1) * (j+b)] & \
1611 dot_setall_dlines(sstate, OPP_DLINE(dline), \
1613 LINE_UNKNOWN, LINE_YES); \
1622 if (solver_states_equal(sstate
, sstate_saved
)) {
1623 int edgecount
= 0, clues
= 0, satclues
= 0, sm1clues
= 0;
1627 * Go through the grid and update for all the new edges.
1628 * Since merge_dots() is idempotent, the simplest way to
1629 * do this is just to update for _all_ the edges.
1631 * Also, while we're here, we count the edges, count the
1632 * clues, count the satisfied clues, and count the
1633 * satisfied-minus-one clues.
1635 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1636 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1637 if (RIGHTOF_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1638 merge_dots(sstate
, i
, j
, i
+1, j
);
1641 if (BELOW_DOT(sstate
->state
, i
, j
) == LINE_YES
) {
1642 merge_dots(sstate
, i
, j
, i
, j
+1);
1646 if (CLUE_AT(sstate
->state
, i
, j
) != ' ') {
1647 int c
= CLUE_AT(sstate
->state
, i
, j
) - '0';
1648 int o
= square_order(sstate
->state
, i
, j
, LINE_YES
);
1659 * Now go through looking for LINE_UNKNOWN edges which
1660 * connect two dots that are already in the same
1661 * equivalence class. If we find one, test to see if the
1662 * loop it would create is a solution.
1664 for (j
= 0; j
<= sstate
->state
->h
; ++j
) {
1665 for (i
= 0; i
<= sstate
->state
->w
; ++i
) {
1666 for (d
= 0; d
< 2; d
++) {
1667 int i2
, j2
, eqclass
, val
;
1670 if (RIGHTOF_DOT(sstate
->state
, i
, j
) !=
1676 if (BELOW_DOT(sstate
->state
, i
, j
) !=
1683 eqclass
= dsf_canonify(sstate
->dotdsf
,
1684 j
* (sstate
->state
->w
+1) + i
);
1685 if (eqclass
!= dsf_canonify(sstate
->dotdsf
,
1686 j2
* (sstate
->state
->w
+1) +
1690 val
= LINE_NO
; /* loop is bad until proven otherwise */
1693 * This edge would form a loop. Next
1694 * question: how long would the loop be?
1695 * Would it equal the total number of edges
1696 * (plus the one we'd be adding if we added
1699 if (sstate
->looplen
[eqclass
] == edgecount
+ 1) {
1704 * This edge would form a loop which
1705 * took in all the edges in the entire
1706 * grid. So now we need to work out
1707 * whether it would be a valid solution
1708 * to the puzzle, which means we have to
1709 * check if it satisfies all the clues.
1710 * This means that every clue must be
1711 * either satisfied or satisfied-minus-
1712 * 1, and also that the number of
1713 * satisfied-minus-1 clues must be at
1714 * most two and they must lie on either
1715 * side of this edge.
1720 if (CLUE_AT(sstate
->state
, cx
,cy
) != ' ' &&
1721 square_order(sstate
->state
, cx
,cy
, LINE_YES
) ==
1722 CLUE_AT(sstate
->state
, cx
,cy
) - '0' - 1)
1724 if (CLUE_AT(sstate
->state
, i
, j
) != ' ' &&
1725 square_order(sstate
->state
, i
, j
, LINE_YES
) ==
1726 CLUE_AT(sstate
->state
, i
, j
) - '0' - 1)
1728 if (sm1clues
== sm1_nearby
&&
1729 sm1clues
+ satclues
== clues
)
1730 val
= LINE_YES
; /* loop is good! */
1734 * Right. Now we know that adding this edge
1735 * would form a loop, and we know whether
1736 * that loop would be a viable solution or
1739 * If adding this edge produces a solution,
1740 * then we know we've found _a_ solution but
1741 * we don't know that it's _the_ solution -
1742 * if it were provably the solution then
1743 * we'd have deduced this edge some time ago
1744 * without the need to do loop detection. So
1745 * in this state we return SOLVER_AMBIGUOUS,
1746 * which has the effect that hitting Solve
1747 * on a user-provided puzzle will fill in a
1748 * solution but using the solver to
1749 * construct new puzzles won't consider this
1750 * a reasonable deduction for the user to
1754 LV_RIGHTOF_DOT(sstate
->state
, i
, j
) = val
;
1756 LV_BELOW_DOT(sstate
->state
, i
, j
) = val
;
1757 if (val
== LINE_YES
) {
1758 sstate
->solver_status
= SOLVER_AMBIGUOUS
;
1759 goto finished_loop_checking
;
1765 finished_loop_checking
:
1770 if (solver_states_equal(sstate
, sstate_saved
)) {
1771 /* Solver has stopped making progress so we terminate */
1772 free_solver_state(sstate_saved
);
1776 free_solver_state(sstate_saved
);
1779 if (sstate
->solver_status
== SOLVER_SOLVED
||
1780 sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
1781 /* s/LINE_UNKNOWN/LINE_NO/g */
1782 array_setall(sstate
->state
->hl
, LINE_UNKNOWN
, LINE_NO
,
1783 HL_COUNT(sstate
->state
));
1784 array_setall(sstate
->state
->vl
, LINE_UNKNOWN
, LINE_NO
,
1785 VL_COUNT(sstate
->state
));
1789 /* Perform recursive calls */
1790 if (sstate
->recursion_remaining
) {
1791 sstate
->recursion_remaining
--;
1793 sstate_saved
= dup_solver_state(sstate
);
1795 recursive_soln_count
= 0;
1796 sstate_rec_solved
= NULL
;
1798 /* Memory management:
1799 * sstate_saved won't be modified but needs to be freed when we have
1801 * sstate is expected to contain our 'best' solution by the time we
1802 * finish this section of code. It's the thing we'll try adding lines
1803 * to, seeing if they make it more solvable.
1804 * If sstate_rec_solved is non-NULL, it will supersede sstate
1805 * eventually. sstate_tmp should not hold a value persistently.
1808 /* NB SOLVER_AMBIGUOUS is like SOLVER_SOLVED except the solver is aware
1809 * of the possibility of additional solutions. So as soon as we have a
1810 * SOLVER_AMBIGUOUS we can safely propagate it back to our caller, but
1811 * if we get a SOLVER_SOLVED we want to keep trying in case we find
1812 * further solutions and have to mark it ambiguous.
1815 #define DO_RECURSIVE_CALL(dir_dot) \
1816 if (dir_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1817 debug(("Trying " #dir_dot " at [%d,%d]\n", i, j)); \
1818 LV_##dir_dot(sstate->state, i, j) = LINE_YES; \
1819 sstate_tmp = solve_game_rec(sstate); \
1820 switch (sstate_tmp->solver_status) { \
1821 case SOLVER_AMBIGUOUS: \
1822 debug(("Solver ambiguous, returning\n")); \
1823 sstate_rec_solved = sstate_tmp; \
1824 goto finished_recursion; \
1825 case SOLVER_SOLVED: \
1826 switch (++recursive_soln_count) { \
1828 debug(("One solution found\n")); \
1829 sstate_rec_solved = sstate_tmp; \
1832 debug(("Ambiguous solutions found\n")); \
1833 free_solver_state(sstate_tmp); \
1834 sstate_rec_solved->solver_status = SOLVER_AMBIGUOUS;\
1835 goto finished_recursion; \
1837 assert(!"recursive_soln_count out of range"); \
1841 case SOLVER_MISTAKE: \
1842 debug(("Non-solution found\n")); \
1843 free_solver_state(sstate_tmp); \
1844 free_solver_state(sstate_saved); \
1845 LV_##dir_dot(sstate->state, i, j) = LINE_NO; \
1846 goto nonrecursive_solver; \
1847 case SOLVER_INCOMPLETE: \
1848 debug(("Recursive step inconclusive\n")); \
1849 free_solver_state(sstate_tmp); \
1852 free_solver_state(sstate); \
1853 sstate = dup_solver_state(sstate_saved); \
1856 for (j
= 0; j
< sstate
->state
->h
+ 1; ++j
) {
1857 for (i
= 0; i
< sstate
->state
->w
+ 1; ++i
) {
1858 /* Only perform recursive calls on 'loose ends' */
1859 if (dot_order(sstate
->state
, i
, j
, LINE_YES
) == 1) {
1860 if (LEFTOF_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1861 DO_RECURSIVE_CALL(LEFTOF_DOT
);
1862 if (RIGHTOF_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1863 DO_RECURSIVE_CALL(RIGHTOF_DOT
);
1864 if (ABOVE_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1865 DO_RECURSIVE_CALL(ABOVE_DOT
);
1866 if (BELOW_DOT(sstate
->state
, i
, j
) == LINE_UNKNOWN
)
1867 DO_RECURSIVE_CALL(BELOW_DOT
);
1874 if (sstate_rec_solved
) {
1875 free_solver_state(sstate
);
1876 sstate
= sstate_rec_solved
;
1883 /* XXX bits of solver that may come in handy one day */
1885 #define HANDLE_DLINE(dline, dir1_dot, dir2_dot) \
1886 /* dline from this dot that's entirely unknown must have
1887 * both lines identical */ \
1888 if (dir1_dot(sstate->state, i, j) == LINE_UNKNOWN && \
1889 dir2_dot(sstate->state, i, j) == LINE_UNKNOWN) { \
1890 sstate->dline_identical[i + (sstate->state->w + 1) * j] |= \
1892 } else if (sstate->dline_identical[i +
1893 (sstate
->state
->w
+ 1) * j
] &\
1895 /* If they're identical and one is known do the obvious
1897 t
= dir1_dot(sstate
->state
, i
, j
); \
1898 if (t
!= LINE_UNKNOWN
) \
1899 dir2_dot(sstate
->state
, i
, j
) = t
; \
1901 t
= dir2_dot(sstate
->state
, i
, j
); \
1902 if (t
!= LINE_UNKNOWN
) \
1903 dir1_dot(sstate
->state
, i
, j
) = t
; \
1911 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1912 if (sstate->dline_identical[i+a + \
1913 (sstate->state->w + 1) * (j+b)] &\
1915 dir1_sq(sstate->state, i, j) = LINE_YES; \
1916 dir2_sq(sstate->state, i, j) = LINE_YES; \
1918 /* If two lines are the same they must be on */
1925 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1926 if (sstate->dot_atmostone[i+a + (sstate->state->w + 1) * (j+b)] & \
1928 if (square_order(sstate->state, i, j, LINE_UNKNOWN) - 1 == \
1929 CLUE_AT(sstate->state, i, j) - '0') { \
1930 square_setall(sstate->state, i, j, LINE_UNKNOWN, LINE_YES); \
1931 /* XXX the following may overwrite known data! */ \
1932 dir1_sq(sstate->state, i, j) = LINE_UNKNOWN; \
1933 dir2_sq(sstate->state, i, j) = LINE_UNKNOWN; \
1941 #define HANDLE_DLINE(dline, dir1_sq, dir2_sq, a, b) \
1942 if (sstate->dline_identical[i+a +
1943 (sstate
->state
->w
+ 1) * (j
+b
)] &\
1945 dir1_sq(sstate
->state
, i
, j
) = LINE_NO
; \
1946 dir2_sq(sstate
->state
, i
, j
) = LINE_NO
; \
1948 /* If two lines are the same they must be off */
1953 static char *solve_game(game_state
*state
, game_state
*currstate
,
1954 char *aux
, char **error
)
1957 solver_state
*sstate
, *new_sstate
;
1959 sstate
= new_solver_state(state
);
1960 new_sstate
= solve_game_rec(sstate
);
1962 if (new_sstate
->solver_status
== SOLVER_SOLVED
) {
1963 soln
= encode_solve_move(new_sstate
->state
);
1964 } else if (new_sstate
->solver_status
== SOLVER_AMBIGUOUS
) {
1965 soln
= encode_solve_move(new_sstate
->state
);
1966 /**error = "Solver found ambiguous solutions"; */
1968 soln
= encode_solve_move(new_sstate
->state
);
1969 /**error = "Solver failed"; */
1972 free_solver_state(new_sstate
);
1973 free_solver_state(sstate
);
1978 static char *game_text_format(game_state
*state
)
1984 len
= (2 * state
->w
+ 2) * (2 * state
->h
+ 1);
1985 rp
= ret
= snewn(len
+ 1, char);
1988 switch (ABOVE_SQUARE(state, i, j)) { \
1990 rp += sprintf(rp, " -"); \
1993 rp += sprintf(rp, " x"); \
1995 case LINE_UNKNOWN: \
1996 rp += sprintf(rp, " "); \
1999 assert(!"Illegal line state for HL");\
2003 switch (LEFTOF_SQUARE(state, i, j)) {\
2005 rp += sprintf(rp, "|"); \
2008 rp += sprintf(rp, "x"); \
2010 case LINE_UNKNOWN: \
2011 rp += sprintf(rp, " "); \
2014 assert(!"Illegal line state for VL");\
2017 for (j
= 0; j
< state
->h
; ++j
) {
2018 for (i
= 0; i
< state
->w
; ++i
) {
2021 rp
+= sprintf(rp
, " \n");
2022 for (i
= 0; i
< state
->w
; ++i
) {
2024 rp
+= sprintf(rp
, "%c", CLUE_AT(state
, i
, j
));
2027 rp
+= sprintf(rp
, "\n");
2029 for (i
= 0; i
< state
->w
; ++i
) {
2032 rp
+= sprintf(rp
, " \n");
2034 assert(strlen(ret
) == len
);
2038 static game_ui
*new_ui(game_state
*state
)
2043 static void free_ui(game_ui
*ui
)
2047 static char *encode_ui(game_ui
*ui
)
2052 static void decode_ui(game_ui
*ui
, char *encoding
)
2056 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2057 game_state
*newstate
)
2061 struct game_drawstate
{
2068 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
2069 int x
, int y
, int button
)
2074 char button_char
= ' ';
2075 enum line_state old_state
;
2077 button
&= ~MOD_MASK
;
2079 /* Around each line is a diamond-shaped region where points within that
2080 * region are closer to this line than any other. We assume any click
2081 * within a line's diamond was meant for that line. It would all be a lot
2082 * simpler if the / and % operators respected modulo arithmetic properly
2083 * for negative numbers. */
2088 /* Get the coordinates of the square the click was in */
2089 i
= (x
+ TILE_SIZE
) / TILE_SIZE
- 1;
2090 j
= (y
+ TILE_SIZE
) / TILE_SIZE
- 1;
2092 /* Get the precise position inside square [i,j] */
2093 p
= (x
+ TILE_SIZE
) % TILE_SIZE
;
2094 q
= (y
+ TILE_SIZE
) % TILE_SIZE
;
2096 /* After this bit of magic [i,j] will correspond to the point either above
2097 * or to the left of the line selected */
2099 if (TILE_SIZE
- p
> q
) {
2102 hl_selected
= FALSE
;
2106 if (TILE_SIZE
- q
> p
) {
2107 hl_selected
= FALSE
;
2118 if (i
>= state
->w
|| j
>= state
->h
+ 1)
2121 if (i
>= state
->w
+ 1 || j
>= state
->h
)
2125 /* I think it's only possible to play this game with mouse clicks, sorry */
2126 /* Maybe will add mouse drag support some time */
2128 old_state
= RIGHTOF_DOT(state
, i
, j
);
2130 old_state
= BELOW_DOT(state
, i
, j
);
2134 switch (old_state
) {
2148 switch (old_state
) {
2163 sprintf(buf
, "%d,%d%c%c", i
, j
, hl_selected ?
'h' : 'v', button_char
);
2169 static game_state
*execute_move(game_state
*state
, char *move
)
2172 game_state
*newstate
= dup_game(state
);
2174 if (move
[0] == 'S') {
2176 newstate
->cheated
= TRUE
;
2181 move
= strchr(move
, ',');
2185 move
+= strspn(move
, "1234567890");
2186 switch (*(move
++)) {
2188 if (i
>= newstate
->w
|| j
> newstate
->h
)
2190 switch (*(move
++)) {
2192 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_YES
;
2195 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_NO
;
2198 LV_RIGHTOF_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2205 if (i
> newstate
->w
|| j
>= newstate
->h
)
2207 switch (*(move
++)) {
2209 LV_BELOW_DOT(newstate
, i
, j
) = LINE_YES
;
2212 LV_BELOW_DOT(newstate
, i
, j
) = LINE_NO
;
2215 LV_BELOW_DOT(newstate
, i
, j
) = LINE_UNKNOWN
;
2227 * Check for completion.
2229 i
= 0; /* placate optimiser */
2230 for (j
= 0; j
<= newstate
->h
; j
++) {
2231 for (i
= 0; i
< newstate
->w
; i
++)
2232 if (LV_RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
)
2234 if (i
< newstate
->w
)
2237 if (j
<= newstate
->h
) {
2243 * We've found a horizontal edge at (i,j). Follow it round
2244 * to see if it's part of a loop.
2248 int order
= dot_order(newstate
, x
, y
, LINE_YES
);
2250 goto completion_check_done
;
2252 if (LEFTOF_DOT(newstate
, x
, y
) == LINE_YES
&& prevdir
!= 'L') {
2255 } else if (RIGHTOF_DOT(newstate
, x
, y
) == LINE_YES
&&
2259 } else if (ABOVE_DOT(newstate
, x
, y
) == LINE_YES
&&
2263 } else if (BELOW_DOT(newstate
, x
, y
) == LINE_YES
&&
2268 assert(!"Can't happen"); /* dot_order guarantees success */
2273 if (x
== i
&& y
== j
)
2277 if (x
!= i
|| y
!= j
|| looplen
== 0)
2278 goto completion_check_done
;
2281 * We've traced our way round a loop, and we know how many
2282 * line segments were involved. Count _all_ the line
2283 * segments in the grid, to see if the loop includes them
2287 for (j
= 0; j
<= newstate
->h
; j
++)
2288 for (i
= 0; i
<= newstate
->w
; i
++)
2289 count
+= ((RIGHTOF_DOT(newstate
, i
, j
) == LINE_YES
) +
2290 (BELOW_DOT(newstate
, i
, j
) == LINE_YES
));
2291 assert(count
>= looplen
);
2292 if (count
!= looplen
)
2293 goto completion_check_done
;
2296 * The grid contains one closed loop and nothing else.
2297 * Check that all the clues are satisfied.
2299 for (j
= 0; j
< newstate
->h
; ++j
) {
2300 for (i
= 0; i
< newstate
->w
; ++i
) {
2301 int n
= CLUE_AT(newstate
, i
, j
);
2303 if (square_order(newstate
, i
, j
, LINE_YES
) != n
- '0') {
2304 goto completion_check_done
;
2313 newstate
->solved
= TRUE
;
2316 completion_check_done
:
2320 free_game(newstate
);
2324 /* ----------------------------------------------------------------------
2328 #define SIZE(d) ((d) * TILE_SIZE + 2 * BORDER + 1)
2330 static void game_compute_size(game_params
*params
, int tilesize
,
2333 struct { int tilesize
; } ads
, *ds
= &ads
;
2334 ads
.tilesize
= tilesize
;
2336 *x
= SIZE(params
->w
);
2337 *y
= SIZE(params
->h
);
2340 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
2341 game_params
*params
, int tilesize
)
2343 ds
->tilesize
= tilesize
;
2346 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2348 float *ret
= snewn(4 * NCOLOURS
, float);
2350 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2352 ret
[COL_FOREGROUND
* 3 + 0] = 0.0F
;
2353 ret
[COL_FOREGROUND
* 3 + 1] = 0.0F
;
2354 ret
[COL_FOREGROUND
* 3 + 2] = 0.0F
;
2356 ret
[COL_HIGHLIGHT
* 3 + 0] = 1.0F
;
2357 ret
[COL_HIGHLIGHT
* 3 + 1] = 1.0F
;
2358 ret
[COL_HIGHLIGHT
* 3 + 2] = 1.0F
;
2360 *ncolours
= NCOLOURS
;
2364 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
2366 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2370 ds
->hl
= snewn(HL_COUNT(state
), char);
2371 ds
->vl
= snewn(VL_COUNT(state
), char);
2374 memset(ds
->hl
, LINE_UNKNOWN
, HL_COUNT(state
));
2375 memset(ds
->vl
, LINE_UNKNOWN
, VL_COUNT(state
));
2380 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2387 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2388 game_state
*state
, int dir
, game_ui
*ui
,
2389 float animtime
, float flashtime
)
2392 int w
= state
->w
, h
= state
->h
;
2394 int line_colour
, flash_changed
;
2398 * The initial contents of the window are not guaranteed and
2399 * can vary with front ends. To be on the safe side, all games
2400 * should start by drawing a big background-colour rectangle
2401 * covering the whole window.
2403 draw_rect(dr
, 0, 0, SIZE(state
->w
), SIZE(state
->h
), COL_BACKGROUND
);
2406 for (j
= 0; j
< h
+ 1; ++j
) {
2407 for (i
= 0; i
< w
+ 1; ++i
) {
2409 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2410 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2411 LINEWIDTH
, LINEWIDTH
, COL_FOREGROUND
);
2416 for (j
= 0; j
< h
; ++j
) {
2417 for (i
= 0; i
< w
; ++i
) {
2418 c
[0] = CLUE_AT(state
, i
, j
);
2421 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2,
2422 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2,
2423 FONT_VARIABLE
, TILE_SIZE
/2,
2424 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_FOREGROUND
, c
);
2427 draw_update(dr
, 0, 0,
2428 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2429 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
2433 if (flashtime
> 0 &&
2434 (flashtime
<= FLASH_TIME
/3 ||
2435 flashtime
>= FLASH_TIME
*2/3)) {
2436 flash_changed
= !ds
->flashing
;
2437 ds
->flashing
= TRUE
;
2438 line_colour
= COL_HIGHLIGHT
;
2440 flash_changed
= ds
->flashing
;
2441 ds
->flashing
= FALSE
;
2442 line_colour
= COL_FOREGROUND
;
2445 #define CROSS_SIZE (3 * LINEWIDTH / 2)
2447 #define CLEAR_VL(i, j) do { \
2449 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2450 BORDER + j * TILE_SIZE + LINEWIDTH/2, \
2452 TILE_SIZE - LINEWIDTH, \
2455 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2456 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2458 TILE_SIZE + CROSS_SIZE*2); \
2461 #define CLEAR_HL(i, j) do { \
2463 BORDER + i * TILE_SIZE + LINEWIDTH/2, \
2464 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2465 TILE_SIZE - LINEWIDTH, \
2469 BORDER + i * TILE_SIZE - CROSS_SIZE, \
2470 BORDER + j * TILE_SIZE - CROSS_SIZE, \
2471 TILE_SIZE + CROSS_SIZE*2, \
2475 /* Vertical lines */
2476 for (j
= 0; j
< h
; ++j
) {
2477 for (i
= 0; i
< w
+ 1; ++i
) {
2478 switch (BELOW_DOT(state
, i
, j
)) {
2480 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2485 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
) ||
2489 BORDER
+ i
* TILE_SIZE
- LINEWIDTH
/2,
2490 BORDER
+ j
* TILE_SIZE
+ LINEWIDTH
/2,
2491 LINEWIDTH
, TILE_SIZE
- LINEWIDTH
,
2496 if (ds
->vl
[i
+ (w
+ 1) * j
] != BELOW_DOT(state
, i
, j
)) {
2499 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2500 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2501 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2502 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2505 BORDER
+ i
* TILE_SIZE
+ CROSS_SIZE
- 1,
2506 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2507 BORDER
+ i
* TILE_SIZE
- CROSS_SIZE
,
2508 BORDER
+ j
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2513 ds
->vl
[i
+ (w
+ 1) * j
] = BELOW_DOT(state
, i
, j
);
2517 /* Horizontal lines */
2518 for (j
= 0; j
< h
+ 1; ++j
) {
2519 for (i
= 0; i
< w
; ++i
) {
2520 switch (RIGHTOF_DOT(state
, i
, j
)) {
2522 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2527 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
) ||
2531 BORDER
+ i
* TILE_SIZE
+ LINEWIDTH
/2,
2532 BORDER
+ j
* TILE_SIZE
- LINEWIDTH
/2,
2533 TILE_SIZE
- LINEWIDTH
, LINEWIDTH
,
2538 if (ds
->hl
[i
+ w
* j
] != RIGHTOF_DOT(state
, i
, j
)) {
2541 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2542 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2543 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2544 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2547 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 - CROSS_SIZE
,
2548 BORDER
+ j
* TILE_SIZE
- CROSS_SIZE
,
2549 BORDER
+ i
* TILE_SIZE
+ TILE_SIZE
/2 + CROSS_SIZE
- 1,
2550 BORDER
+ j
* TILE_SIZE
+ CROSS_SIZE
- 1,
2555 ds
->hl
[i
+ w
* j
] = RIGHTOF_DOT(state
, i
, j
);
2560 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
2561 int dir
, game_ui
*ui
)
2566 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
2567 int dir
, game_ui
*ui
)
2569 if (!oldstate
->solved
&& newstate
->solved
&&
2570 !oldstate
->cheated
&& !newstate
->cheated
) {
2577 static int game_wants_statusbar(void)
2582 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2587 static void game_print_size(game_params
*params
, float *x
, float *y
)
2592 * I'll use 7mm squares by default.
2594 game_compute_size(params
, 700, &pw
, &ph
);
2599 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2601 int w
= state
->w
, h
= state
->h
;
2602 int ink
= print_mono_colour(dr
, 0);
2604 game_drawstate ads
, *ds
= &ads
;
2605 ds
->tilesize
= tilesize
;
2608 * Dots. I'll deliberately make the dots a bit wider than the
2609 * lines, so you can still see them. (And also because it's
2610 * annoyingly tricky to make them _exactly_ the same size...)
2612 for (y
= 0; y
<= h
; y
++)
2613 for (x
= 0; x
<= w
; x
++)
2614 draw_circle(dr
, BORDER
+ x
* TILE_SIZE
, BORDER
+ y
* TILE_SIZE
,
2615 LINEWIDTH
, ink
, ink
);
2620 for (y
= 0; y
< h
; y
++)
2621 for (x
= 0; x
< w
; x
++)
2622 if (CLUE_AT(state
, x
, y
) != ' ') {
2625 c
[0] = CLUE_AT(state
, x
, y
);
2628 BORDER
+ x
* TILE_SIZE
+ TILE_SIZE
/2,
2629 BORDER
+ y
* TILE_SIZE
+ TILE_SIZE
/2,
2630 FONT_VARIABLE
, TILE_SIZE
/2,
2631 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, c
);
2635 * Lines. (At the moment, I'm not bothering with crosses.)
2637 for (y
= 0; y
<= h
; y
++)
2638 for (x
= 0; x
< w
; x
++)
2639 if (RIGHTOF_DOT(state
, x
, y
) == LINE_YES
)
2640 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
,
2641 BORDER
+ y
* TILE_SIZE
- LINEWIDTH
/2,
2642 TILE_SIZE
, (LINEWIDTH
/2) * 2 + 1, ink
);
2643 for (y
= 0; y
< h
; y
++)
2644 for (x
= 0; x
<= w
; x
++)
2645 if (BELOW_DOT(state
, x
, y
) == LINE_YES
)
2646 draw_rect(dr
, BORDER
+ x
* TILE_SIZE
- LINEWIDTH
/2,
2647 BORDER
+ y
* TILE_SIZE
,
2648 (LINEWIDTH
/2) * 2 + 1, TILE_SIZE
, ink
);
2652 #define thegame loopy
2655 const struct game thegame
= {
2656 "Loopy", "games.loopy",
2663 TRUE
, game_configure
, custom_params
,
2671 TRUE
, game_text_format
,
2679 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2682 game_free_drawstate
,
2686 TRUE
, FALSE
, game_print_size
, game_print
,
2687 game_wants_statusbar
,
2688 FALSE
, game_timing_state
,
2689 0, /* mouse_priorities */