2 * pearl.c: Nikoli's `Masyu' puzzle. Currently this is a blank
3 * puzzle file with nothing but a test solver-generator.
9 * - The generation method appears to be fundamentally flawed. I
10 * think generating a random loop and then choosing a clue set
11 * is simply not a viable approach, because on a test run of
12 * 10,000 attempts, it generated _six_ viable puzzles. All the
13 * rest of the randomly generated loops failed to be soluble
14 * even given a maximal clue set. Also, the vast majority of the
15 * clues were white circles (straight clues); black circles
16 * (corners) seem very uncommon.
17 * + So what can we do? One possible approach would be to
18 * adjust the random loop generation so that it created loops
19 * which were in some heuristic sense more likely to be
20 * viable Masyu puzzles. Certainly a good start on that would
21 * be to arrange that black clues actually _came up_ slightly
22 * more often, but I have no idea whether that would be
24 * + A second option would be to throw the entire mechanism out
25 * and instead write a different generator from scratch which
26 * evolves the solution along with the puzzle: place a few
27 * clues, nail down a bit of the loop, place another clue,
28 * nail down some more, etc. It's unclear whether this can
29 * sensibly be done, though.
31 * - Puzzle playing UI and everything else apart from the
53 #define DX(d) ( ((d)==R) - ((d)==L) )
54 #define DY(d) ( ((d)==D) - ((d)==U) )
56 #define F(d) (((d << 2) | (d >> 2)) & 0xF)
57 #define C(d) (((d << 3) | (d >> 1)) & 0xF)
58 #define A(d) (((d << 1) | (d >> 3)) & 0xF)
87 #define bBLANK (1 << BLANK)
102 static game_params
*default_params(void)
104 game_params
*ret
= snew(game_params
);
111 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
116 static void free_params(game_params
*params
)
121 static game_params
*dup_params(game_params
*params
)
123 game_params
*ret
= snew(game_params
);
124 *ret
= *params
; /* structure copy */
128 static void decode_params(game_params
*params
, char const *string
)
132 static char *encode_params(game_params
*params
, int full
)
134 return dupstr("FIXME");
137 static config_item
*game_configure(game_params
*params
)
142 static game_params
*custom_params(config_item
*cfg
)
147 static char *validate_params(game_params
*params
, int full
)
152 /* ----------------------------------------------------------------------
156 int pearl_solve(int w
, int h
, char *clues
, char *result
)
158 int W
= 2*w
+1, H
= 2*h
+1;
165 * workspace[(2*y+1)*W+(2*x+1)] indicates the possible nature
166 * of the square (x,y), as a logical OR of bitfields.
168 * workspace[(2*y)*W+(2*x+1)], for x odd and y even, indicates
169 * whether the horizontal edge between (x,y) and (x+1,y) is
170 * connected (1), disconnected (2) or unknown (3).
172 * workspace[(2*y+1)*W+(2*x)], indicates the same about the
173 * vertical edge between (x,y) and (x,y+1).
175 * Initially, every square is considered capable of being in
176 * any of the seven possible states (two straights, four
177 * corners and empty), except those corresponding to clue
178 * squares which are more restricted.
180 * Initially, all edges are unknown, except the ones around the
181 * grid border which are known to be disconnected.
183 workspace
= snewn(W
*H
, short);
184 for (x
= 0; x
< W
*H
; x
++)
187 for (y
= 0; y
< h
; y
++)
188 for (x
= 0; x
< w
; x
++)
189 switch (clues
[y
*w
+x
]) {
191 workspace
[(2*y
+1)*W
+(2*x
+1)] = bLU
|bLD
|bRU
|bRD
;
194 workspace
[(2*y
+1)*W
+(2*x
+1)] = bLR
|bUD
;
197 workspace
[(2*y
+1)*W
+(2*x
+1)] = bLR
|bUD
|bLU
|bLD
|bRU
|bRD
|bBLANK
;
200 /* Horizontal edges */
201 for (y
= 0; y
<= h
; y
++)
202 for (x
= 0; x
< w
; x
++)
203 workspace
[(2*y
)*W
+(2*x
+1)] = (y
==0 || y
==h ?
2 : 3);
205 for (y
= 0; y
< h
; y
++)
206 for (x
= 0; x
<= w
; x
++)
207 workspace
[(2*y
+1)*W
+(2*x
)] = (x
==0 || x
==w ?
2 : 3);
210 * We maintain a dsf of connected squares, together with a
211 * count of the size of each equivalence class.
213 dsf
= snewn(w
*h
, int);
214 dsfsize
= snewn(w
*h
, int);
217 * Now repeatedly try to find something we can do.
221 #ifdef SOLVER_DIAGNOSTICS
222 for (y
= 0; y
< H
; y
++) {
223 for (x
= 0; x
< W
; x
++)
224 printf("%*x", (x
&1) ?
5 : 2, workspace
[y
*W
+x
]);
229 int done_something
= FALSE
;
232 * Go through the square state words, and discard any
233 * square state which is inconsistent with known facts
234 * about the edges around the square.
236 for (y
= 0; y
< h
; y
++)
237 for (x
= 0; x
< w
; x
++) {
238 for (b
= 0; b
< 0xD; b
++)
239 if (workspace
[(2*y
+1)*W
+(2*x
+1)] & (1<<b
)) {
241 * If any edge of this square is known to
242 * be connected when state b would require
243 * it disconnected, or vice versa, discard
246 for (d
= 1; d
<= 8; d
+= d
) {
247 int ex
= 2*x
+1 + DX(d
), ey
= 2*y
+1 + DY(d
);
248 if (workspace
[ey
*W
+ex
] ==
250 workspace
[(2*y
+1)*W
+(2*x
+1)] &= ~(1<<b
);
251 #ifdef SOLVER_DIAGNOSTICS
252 printf("edge (%d,%d)-(%d,%d) rules out state"
253 " %d for square (%d,%d)\n",
254 ex
/2, ey
/2, (ex
+1)/2, (ey
+1)/2,
257 done_something
= TRUE
;
264 * Consistency check: each square must have at
265 * least one state left!
267 if (!workspace
[(2*y
+1)*W
+(2*x
+1)]) {
268 #ifdef SOLVER_DIAGNOSTICS
269 printf("edge check at (%d,%d): inconsistency\n", x
, y
);
277 * Now go through the states array again, and nail down any
278 * unknown edge if one of its neighbouring squares makes it
281 for (y
= 0; y
< h
; y
++)
282 for (x
= 0; x
< w
; x
++) {
283 int edgeor
= 0, edgeand
= 15;
285 for (b
= 0; b
< 0xD; b
++)
286 if (workspace
[(2*y
+1)*W
+(2*x
+1)] & (1<<b
)) {
292 * Now any bit clear in edgeor marks a disconnected
293 * edge, and any bit set in edgeand marks a
297 /* First check consistency: neither bit is both! */
298 if (edgeand
& ~edgeor
) {
299 #ifdef SOLVER_DIAGNOSTICS
300 printf("square check at (%d,%d): inconsistency\n", x
, y
);
306 for (d
= 1; d
<= 8; d
+= d
) {
307 int ex
= 2*x
+1 + DX(d
), ey
= 2*y
+1 + DY(d
);
309 if (!(edgeor
& d
) && workspace
[ey
*W
+ex
] == 3) {
310 workspace
[ey
*W
+ex
] = 2;
311 done_something
= TRUE
;
312 #ifdef SOLVER_DIAGNOSTICS
313 printf("possible states of square (%d,%d) force edge"
314 " (%d,%d)-(%d,%d) to be disconnected\n",
315 x
, y
, ex
/2, ey
/2, (ex
+1)/2, (ey
+1)/2);
317 } else if ((edgeand
& d
) && workspace
[ey
*W
+ex
] == 3) {
318 workspace
[ey
*W
+ex
] = 1;
319 done_something
= TRUE
;
320 #ifdef SOLVER_DIAGNOSTICS
321 printf("possible states of square (%d,%d) force edge"
322 " (%d,%d)-(%d,%d) to be connected\n",
323 x
, y
, ex
/2, ey
/2, (ex
+1)/2, (ey
+1)/2);
333 * Now for longer-range clue-based deductions (using the
334 * rules that a corner clue must connect to two straight
335 * squares, and a straight clue must connect to at least
336 * one corner square).
338 for (y
= 0; y
< h
; y
++)
339 for (x
= 0; x
< w
; x
++)
340 switch (clues
[y
*w
+x
]) {
342 for (d
= 1; d
<= 8; d
+= d
) {
343 int ex
= 2*x
+1 + DX(d
), ey
= 2*y
+1 + DY(d
);
344 int fx
= ex
+ DX(d
), fy
= ey
+ DY(d
);
347 if (workspace
[ey
*W
+ex
] == 1) {
349 * If a corner clue is connected on any
350 * edge, then we can immediately nail
351 * down the square beyond that edge as
352 * being a straight in the appropriate
355 if (workspace
[fy
*W
+fx
] != (1<<type
)) {
356 workspace
[fy
*W
+fx
] = (1<<type
);
357 done_something
= TRUE
;
358 #ifdef SOLVER_DIAGNOSTICS
359 printf("corner clue at (%d,%d) forces square "
360 "(%d,%d) into state %d\n", x
, y
,
365 } else if (workspace
[ey
*W
+ex
] == 3) {
367 * Conversely, if a corner clue is
368 * separated by an unknown edge from a
369 * square which _cannot_ be a straight
370 * in the appropriate direction, we can
371 * mark that edge as disconnected.
373 if (!(workspace
[fy
*W
+fx
] & (1<<type
))) {
374 workspace
[ey
*W
+ex
] = 2;
375 done_something
= TRUE
;
376 #ifdef SOLVER_DIAGNOSTICS
377 printf("corner clue at (%d,%d), plus square "
378 "(%d,%d) not being state %d, "
379 "disconnects edge (%d,%d)-(%d,%d)\n",
380 x
, y
, fx
/2, fy
/2, type
,
381 ex
/2, ey
/2, (ex
+1)/2, (ey
+1)/2);
392 * If a straight clue is between two squares
393 * neither of which is capable of being a
394 * corner connected to it, then the straight
395 * clue cannot point in that direction.
397 for (d
= 1; d
<= 2; d
+= d
) {
398 int fx
= 2*x
+1 + 2*DX(d
), fy
= 2*y
+1 + 2*DY(d
);
399 int gx
= 2*x
+1 - 2*DX(d
), gy
= 2*y
+1 - 2*DY(d
);
402 if (!(workspace
[(2*y
+1)*W
+(2*x
+1)] & (1<<type
)))
405 if (!(workspace
[fy
*W
+fx
] & ((1<<(F(d
)|A(d
))) |
406 (1<<(F(d
)|C(d
))))) &&
407 !(workspace
[gy
*W
+gx
] & ((1<<( d
|A(d
))) |
409 workspace
[(2*y
+1)*W
+(2*x
+1)] &= ~(1<<type
);
410 done_something
= TRUE
;
411 #ifdef SOLVER_DIAGNOSTICS
412 printf("straight clue at (%d,%d) cannot corner at "
413 "(%d,%d) or (%d,%d) so is not state %d\n",
414 x
, y
, fx
/2, fy
/2, gx
/2, gy
/2, type
);
421 * If a straight clue with known direction is
422 * connected on one side to a known straight,
423 * then on the other side it must be a corner.
425 for (d
= 1; d
<= 8; d
+= d
) {
426 int fx
= 2*x
+1 + 2*DX(d
), fy
= 2*y
+1 + 2*DY(d
);
427 int gx
= 2*x
+1 - 2*DX(d
), gy
= 2*y
+1 - 2*DY(d
);
430 if (workspace
[(2*y
+1)*W
+(2*x
+1)] != (1<<type
))
433 if (!(workspace
[fy
*W
+fx
] &~ (bLR
|bUD
)) &&
434 (workspace
[gy
*W
+gx
] &~ (bLU
|bLD
|bRU
|bRD
))) {
435 workspace
[gy
*W
+gx
] &= (bLU
|bLD
|bRU
|bRD
);
436 done_something
= TRUE
;
437 #ifdef SOLVER_DIAGNOSTICS
438 printf("straight clue at (%d,%d) connecting to "
439 "straight at (%d,%d) makes (%d,%d) a "
440 "corner\n", x
, y
, fx
/2, fy
/2, gx
/2, gy
/2);
452 * Now detect shortcut loops.
456 int nonblanks
, loopclass
;
459 for (x
= 0; x
< w
*h
; x
++)
463 * First go through the edge entries and update the dsf
464 * of which squares are connected to which others. We
465 * also track the number of squares in each equivalence
466 * class, and count the overall number of
467 * known-non-blank squares.
469 * In the process of doing this, we must notice if a
470 * loop has already been formed. If it has, we blank
471 * out any square which isn't part of that loop
472 * (failing a consistency check if any such square does
473 * not have BLANK as one of its remaining options) and
474 * exit the deduction loop with success.
478 for (y
= 1; y
< H
-1; y
++)
479 for (x
= 1; x
< W
-1; x
++)
482 * (x,y) are the workspace coordinates of
483 * an edge field. Compute the normal-space
484 * coordinates of the squares it connects.
486 int ax
= (x
-1)/2, ay
= (y
-1)/2, ac
= ay
*w
+ax
;
487 int bx
= x
/2, by
= y
/2, bc
= by
*w
+bx
;
490 * If the edge is connected, do the dsf
493 if (workspace
[y
*W
+x
] == 1) {
496 ae
= dsf_canonify(dsf
, ac
);
497 be
= dsf_canonify(dsf
, bc
);
503 if (loopclass
!= -1) {
505 * In fact, we have two
506 * separate loops, which is
509 #ifdef SOLVER_DIAGNOSTICS
510 printf("two loops found in grid!\n");
518 * Merge the two equivalence
521 int size
= dsfsize
[ae
] + dsfsize
[be
];
522 dsf_merge(dsf
, ac
, bc
);
523 ae
= dsf_canonify(dsf
, ac
);
527 } else if ((y
& x
) & 1) {
529 * (x,y) are the workspace coordinates of a
530 * square field. If the square is
531 * definitely not blank, count it.
533 if (!(workspace
[y
*W
+x
] & bBLANK
))
538 * If we discovered an existing loop above, we must now
539 * blank every square not part of it, and exit the main
542 if (loopclass
!= -1) {
543 #ifdef SOLVER_DIAGNOSTICS
544 printf("loop found in grid!\n");
546 for (y
= 0; y
< h
; y
++)
547 for (x
= 0; x
< w
; x
++)
548 if (dsf_canonify(dsf
, y
*w
+x
) != loopclass
) {
549 if (workspace
[(y
*2+1)*W
+(x
*2+1)] & bBLANK
) {
550 workspace
[(y
*2+1)*W
+(x
*2+1)] = bBLANK
;
553 * This square is not part of the
554 * loop, but is known non-blank. We
557 #ifdef SOLVER_DIAGNOSTICS
558 printf("non-blank square (%d,%d) found outside"
573 * Now go through the workspace again and mark any edge
574 * which would cause a shortcut loop (i.e. would
575 * connect together two squares in the same equivalence
576 * class, and that equivalence class does not contain
577 * _all_ the known-non-blank squares currently in the
578 * grid) as disconnected. Also, mark any _square state_
579 * which would cause a shortcut loop as disconnected.
581 for (y
= 1; y
< H
-1; y
++)
582 for (x
= 1; x
< W
-1; x
++)
585 * (x,y) are the workspace coordinates of
586 * an edge field. Compute the normal-space
587 * coordinates of the squares it connects.
589 int ax
= (x
-1)/2, ay
= (y
-1)/2, ac
= ay
*w
+ax
;
590 int bx
= x
/2, by
= y
/2, bc
= by
*w
+bx
;
593 * If the edge is currently unknown, and
594 * sits between two squares in the same
595 * equivalence class, and the size of that
596 * class is less than nonblanks, then
597 * connecting this edge would be a shortcut
598 * loop and so we must not do so.
600 if (workspace
[y
*W
+x
] == 3) {
603 ae
= dsf_canonify(dsf
, ac
);
604 be
= dsf_canonify(dsf
, bc
);
608 * We have a loop. Is it a shortcut?
610 if (dsfsize
[ae
] < nonblanks
) {
612 * Yes! Mark this edge disconnected.
614 workspace
[y
*W
+x
] = 2;
615 done_something
= TRUE
;
616 #ifdef SOLVER_DIAGNOSTICS
617 printf("edge (%d,%d)-(%d,%d) would create"
618 " a shortcut loop, hence must be"
619 " disconnected\n", x
/2, y
/2,
625 } else if ((y
& x
) & 1) {
627 * (x,y) are the workspace coordinates of a
628 * square field. Go through its possible
629 * (non-blank) states and see if any gives
630 * rise to a shortcut loop.
632 * This is slightly fiddly, because we have
633 * to check whether this square is already
634 * part of the same equivalence class as
635 * the things it's joining.
637 int ae
= dsf_canonify(dsf
, (y
/2)*w
+(x
/2));
639 for (b
= 2; b
< 0xD; b
++)
640 if (workspace
[y
*W
+x
] & (1<<b
)) {
642 * Find the equivalence classes of
643 * the two squares this one would
644 * connect if it were in this
649 for (d
= 1; d
<= 8; d
+= d
) if (b
& d
) {
650 int xx
= x
/2 + DX(d
), yy
= y
/2 + DY(d
);
651 int ee
= dsf_canonify(dsf
, yy
*w
+xx
);
661 * This square state would form
662 * a loop on equivalence class
663 * e. Measure the size of that
664 * loop, and see if it's a
667 int loopsize
= dsfsize
[e
];
669 loopsize
++;/* add the square itself */
670 if (loopsize
< nonblanks
) {
672 * It is! Mark this square
675 workspace
[y
*W
+x
] &= ~(1<<b
);
676 done_something
= TRUE
;
677 #ifdef SOLVER_DIAGNOSTICS
678 printf("square (%d,%d) would create a "
679 "shortcut loop in state %d, "
693 * If we reach here, there is nothing left we can do.
694 * Return 2 for ambiguous puzzle.
701 * If we reach _here_, it's by `break' out of the main loop,
702 * which means we've successfully achieved a solution. This
703 * means that we expect every square to be nailed down to
704 * exactly one possibility. Transcribe those possibilities into
707 for (y
= 0; y
< h
; y
++)
708 for (x
= 0; x
< w
; x
++) {
709 for (b
= 0; b
< 0xD; b
++)
710 if (workspace
[(2*y
+1)*W
+(2*x
+1)] == (1<<b
)) {
714 assert(b
< 0xD); /* we should have had a break by now */
725 /* ----------------------------------------------------------------------
729 void pearl_loopgen(int w
, int h
, char *grid
, random_state
*rs
)
731 int *options
, *mindist
, *maxdist
, *list
;
732 int x
, y
, d
, total
, n
, area
, limit
;
735 * We're eventually going to have to return a w-by-h array
736 * containing line segment data. However, it's more convenient
737 * while actually generating the loop to consider the problem
738 * as a (w-1) by (h-1) array in which some squares are `inside'
739 * and some `outside'.
741 * I'm going to use the top left corner of my return array in
742 * the latter manner until the end of the function.
746 * To begin with, all squares are outside (0), except for one
747 * randomly selected one which is inside (1).
749 memset(grid
, 0, w
*h
);
750 x
= random_upto(rs
, w
-1);
751 y
= random_upto(rs
, h
-1);
755 * I'm also going to need an array to store the possible
756 * options for the next extension of the grid.
758 options
= snewn(w
*h
, int);
759 for (x
= 0; x
< w
*h
; x
++)
763 * And some arrays and a list for breadth-first searching.
765 mindist
= snewn(w
*h
, int);
766 maxdist
= snewn(w
*h
, int);
767 list
= snewn(w
*h
, int);
770 * Now we repeatedly scan the grid for feasible squares into
771 * which we can extend our loop, pick one, and do it.
776 #ifdef LOOPGEN_DIAGNOSTICS
777 for (y
= 0; y
< h
; y
++) {
778 for (x
= 0; x
< w
; x
++)
779 printf("%d", grid
[y
*w
+x
]);
786 * Our primary aim in growing this loop is to make it
787 * reasonably _dense_ in the target rectangle. That is, we
788 * want the maximum over all squares of the minimum
789 * distance from that square to the loop to be small.
791 * Therefore, we start with a breadth-first search of the
792 * grid to find those minimum distances.
795 int head
= 0, tail
= 0;
798 for (i
= 0; i
< w
*h
; i
++) {
806 while (head
< tail
) {
810 for (d
= 1; d
<= 8; d
+= d
) {
811 int xx
= x
+ DX(d
), yy
= y
+ DY(d
);
812 if (xx
>= 0 && xx
< w
&& yy
>= 0 && yy
< h
&&
813 mindist
[yy
*w
+xx
] < 0) {
814 mindist
[yy
*w
+xx
] = mindist
[i
] + 1;
815 list
[tail
++] = yy
*w
+xx
;
821 * Having done the BFS, we now backtrack along its path
822 * to determine the most distant square that each
823 * square is on the shortest path to. This tells us
824 * which of the loop extension candidates (all of which
825 * are squares marked 1) is most desirable to extend
826 * into in terms of minimising the maximum distance
827 * from any empty square to the nearest loop square.
829 for (head
= tail
; head
-- > 0 ;) {
838 for (d
= 1; d
<= 8; d
+= d
) {
839 int xx
= x
+ DX(d
), yy
= y
+ DY(d
);
840 if (xx
>= 0 && xx
< w
&& yy
>= 0 && yy
< h
&&
841 mindist
[yy
*w
+xx
] > mindist
[i
] &&
842 maxdist
[yy
*w
+xx
] > max
) {
843 max
= maxdist
[yy
*w
+xx
];
852 * A square is a viable candidate for extension of our loop
853 * if and only if the following conditions are all met:
854 * - It is currently labelled 0.
855 * - At least one of its four orthogonal neighbours is
857 * - If you consider its eight orthogonal and diagonal
858 * neighbours to form a ring, that ring contains at most
859 * one contiguous run of 1s. (It must also contain at
860 * _least_ one, of course, but that's already guaranteed
861 * by the previous condition so there's no need to test
865 for (y
= 0; y
< h
-1; y
++)
866 for (x
= 0; x
< w
-1; x
++) {
868 int rx
, neighbours
, runs
, dist
;
870 dist
= maxdist
[y
*w
+x
];
874 continue; /* it isn't labelled 0 */
877 for (rx
= 0, d
= 1; d
<= 8; rx
+= 2, d
+= d
) {
878 int x2
= x
+ DX(d
), y2
= y
+ DY(d
);
879 int x3
= x2
+ DX(A(d
)), y3
= y2
+ DY(A(d
));
880 int g2
= (x2
>= 0 && x2
< w
&& y2
>= 0 && y2
< h ?
882 int g3
= (x3
>= 0 && x3
< w
&& y3
>= 0 && y3
< h ?
891 continue; /* it doesn't have a 1 neighbour */
894 for (rx
= 0; rx
< 8; rx
++)
895 if (ring
[rx
] && !ring
[(rx
+1) & 7])
899 continue; /* too many runs of 1s */
902 * Now we know this square is a viable extension
903 * candidate. Mark it.
905 * FIXME: probabilistic prioritisation based on
906 * perimeter perturbation? (Wow, must keep that
909 options
[y
*w
+x
] = dist
* (4-neighbours
) * (4-neighbours
);
910 total
+= options
[y
*w
+x
];
914 break; /* nowhere to go! */
917 * Now pick a random one of the viable extension squares,
918 * and extend into it.
920 n
= random_upto(rs
, total
);
921 for (y
= 0; y
< h
-1; y
++)
922 for (x
= 0; x
< w
-1; x
++) {
924 if (options
[y
*w
+x
] > n
)
925 goto found
; /* two-level break */
928 assert(!"We shouldn't ever get here");
934 * We terminate the loop when around 7/12 of the grid area
935 * is full, but we also require that the loop has reached
938 limit
= random_upto(rs
, (w
-1)*(h
-1)) + 13*(w
-1)*(h
-1);
939 if (24 * area
> limit
) {
940 int l
= FALSE
, r
= FALSE
, u
= FALSE
, d
= FALSE
;
941 for (x
= 0; x
< w
; x
++) {
947 for (y
= 0; y
< h
; y
++) {
953 if (l
&& r
&& u
&& d
)
963 #ifdef LOOPGEN_DIAGNOSTICS
964 printf("final loop:\n");
965 for (y
= 0; y
< h
; y
++) {
966 for (x
= 0; x
< w
; x
++)
967 printf("%d", grid
[y
*w
+x
]);
974 * Now convert this array of 0s and 1s into an array of path
977 for (y
= h
; y
-- > 0 ;) {
978 for (x
= w
; x
-- > 0 ;) {
980 * Examine the four grid squares of which (x,y) are in
981 * the bottom right, to determine the output for this
984 int ul
= (x
> 0 && y
> 0 ? grid
[(y
-1)*w
+(x
-1)] : 0);
985 int ur
= (y
> 0 ? grid
[(y
-1)*w
+x
] : 0);
986 int dl
= (x
> 0 ? grid
[y
*w
+(x
-1)] : 0);
987 int dr
= grid
[y
*w
+x
];
990 if (ul
!= ur
) type
|= U
;
991 if (dl
!= dr
) type
|= D
;
992 if (ul
!= dl
) type
|= L
;
993 if (ur
!= dr
) type
|= R
;
995 assert((bLR
|bUD
|bLU
|bLD
|bRU
|bRD
|bBLANK
) & (1 << type
));
1002 #if defined LOOPGEN_DIAGNOSTICS && !defined GENERATION_DIAGNOSTICS
1003 printf("as returned:\n");
1004 for (y
= 0; y
< h
; y
++) {
1005 for (x
= 0; x
< w
; x
++) {
1006 int type
= grid
[y
*w
+x
];
1008 if (type
& L
) *p
++ = 'L';
1009 if (type
& R
) *p
++ = 'R';
1010 if (type
& U
) *p
++ = 'U';
1011 if (type
& D
) *p
++ = 'D';
1021 static char *new_game_desc(game_params
*params
, random_state
*rs
,
1022 char **aux
, int interactive
)
1027 int x
, y
, d
, ret
, i
;
1030 clues
= snewn(7*7, char);
1038 "\0\0\2\0\0\0\0", 7*7);
1039 grid
= snewn(7*7, char);
1040 printf("%d\n", pearl_solve(7, 7, clues
, grid
));
1042 clues
= snewn(10*10, char);
1044 "\0\0\2\0\2\0\0\0\0\0"
1045 "\0\0\0\0\2\0\0\0\1\0"
1046 "\0\0\1\0\1\0\2\0\0\0"
1047 "\0\0\0\2\0\0\2\0\0\0"
1048 "\1\0\0\0\0\2\0\0\0\2"
1049 "\0\0\2\0\0\0\0\2\0\0"
1050 "\0\0\1\0\0\0\2\0\0\0"
1051 "\2\0\0\0\1\0\0\0\0\2"
1052 "\0\0\0\0\0\0\2\2\0\0"
1053 "\0\0\1\0\0\0\0\0\0\1", 10*10);
1054 grid
= snewn(10*10, char);
1055 printf("%d\n", pearl_solve(10, 10, clues
, grid
));
1057 clues
= snewn(10*10, char);
1059 "\0\0\0\0\0\0\1\0\0\0"
1060 "\0\1\0\1\2\0\0\0\0\2"
1061 "\0\0\0\0\0\0\0\0\0\1"
1062 "\2\0\0\1\2\2\1\0\0\0"
1063 "\1\0\0\0\0\0\0\1\0\0"
1064 "\0\0\2\0\0\0\0\0\0\2"
1065 "\0\0\0\2\1\2\1\0\0\2"
1066 "\2\0\0\0\0\0\0\0\0\0"
1067 "\2\0\0\0\0\1\1\0\2\0"
1068 "\0\0\0\2\0\0\0\0\0\0", 10*10);
1069 grid
= snewn(10*10, char);
1070 printf("%d\n", pearl_solve(10, 10, clues
, grid
));
1073 grid
= snewn(w
*h
, char);
1074 clues
= snewn(w
*h
, char);
1075 clueorder
= snewn(w
*h
, int);
1078 pearl_loopgen(w
, h
, grid
, rs
);
1080 #ifdef GENERATION_DIAGNOSTICS
1081 printf("grid array:\n");
1082 for (y
= 0; y
< h
; y
++) {
1083 for (x
= 0; x
< w
; x
++) {
1084 int type
= grid
[y
*w
+x
];
1086 if (type
& L
) *p
++ = 'L';
1087 if (type
& R
) *p
++ = 'R';
1088 if (type
& U
) *p
++ = 'U';
1089 if (type
& D
) *p
++ = 'D';
1099 * Set up the maximal clue array.
1101 for (y
= 0; y
< h
; y
++)
1102 for (x
= 0; x
< w
; x
++) {
1103 int type
= grid
[y
*w
+x
];
1105 clues
[y
*w
+x
] = NOCLUE
;
1107 if ((bLR
|bUD
) & (1 << type
)) {
1109 * This is a straight; see if it's a viable
1110 * candidate for a straight clue. It qualifies if
1111 * at least one of the squares it connects to is a
1114 for (d
= 1; d
<= 8; d
+= d
) if (type
& d
) {
1115 int xx
= x
+ DX(d
), yy
= y
+ DY(d
);
1116 assert(xx
>= 0 && xx
< w
&& yy
>= 0 && yy
< h
);
1117 if ((bLU
|bLD
|bRU
|bRD
) & (1 << grid
[yy
*w
+xx
]))
1120 if (d
<= 8) /* we found one */
1121 clues
[y
*w
+x
] = STRAIGHT
;
1122 } else if ((bLU
|bLD
|bRU
|bRD
) & (1 << type
)) {
1124 * This is a corner; see if it's a viable candidate
1125 * for a corner clue. It qualifies if all the
1126 * squares it connects to are straights.
1128 for (d
= 1; d
<= 8; d
+= d
) if (type
& d
) {
1129 int xx
= x
+ DX(d
), yy
= y
+ DY(d
);
1130 assert(xx
>= 0 && xx
< w
&& yy
>= 0 && yy
< h
);
1131 if (!((bLR
|bUD
) & (1 << grid
[yy
*w
+xx
])))
1134 if (d
> 8) /* we didn't find a counterexample */
1135 clues
[y
*w
+x
] = CORNER
;
1139 #ifdef GENERATION_DIAGNOSTICS
1140 printf("clue array:\n");
1141 for (y
= 0; y
< h
; y
++) {
1142 for (x
= 0; x
< w
; x
++) {
1143 printf("%c", " *O"[(unsigned char)clues
[y
*w
+x
]]);
1151 * See if we can solve the puzzle just like this.
1153 ret
= pearl_solve(w
, h
, clues
, grid
);
1154 assert(ret
> 0); /* shouldn't be inconsistent! */
1156 continue; /* go round and try again */
1159 * Now shuffle the grid points and gradually remove the
1160 * clues to find a minimal set which still leaves the
1163 for (i
= 0; i
< w
*h
; i
++)
1165 shuffle(clueorder
, w
*h
, sizeof(*clueorder
), rs
);
1166 for (i
= 0; i
< w
*h
; i
++) {
1169 y
= clueorder
[i
] / w
;
1170 x
= clueorder
[i
] % w
;
1172 if (clues
[y
*w
+x
] == 0)
1175 clue
= clues
[y
*w
+x
];
1176 clues
[y
*w
+x
] = 0; /* try removing this clue */
1178 ret
= pearl_solve(w
, h
, clues
, grid
);
1181 clues
[y
*w
+x
] = clue
; /* oops, put it back again */
1184 #ifdef FINISHED_PUZZLE
1185 printf("clue array:\n");
1186 for (y
= 0; y
< h
; y
++) {
1187 for (x
= 0; x
< w
; x
++) {
1188 printf("%c", " *O"[(unsigned char)clues
[y
*w
+x
]]);
1202 return dupstr("FIXME");
1205 static char *validate_desc(game_params
*params
, char *desc
)
1210 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1212 game_state
*state
= snew(game_state
);
1219 static game_state
*dup_game(game_state
*state
)
1221 game_state
*ret
= snew(game_state
);
1223 ret
->FIXME
= state
->FIXME
;
1228 static void free_game(game_state
*state
)
1233 static char *solve_game(game_state
*state
, game_state
*currstate
,
1234 char *aux
, char **error
)
1239 static int game_can_format_as_text_now(game_params
*params
)
1244 static char *game_text_format(game_state
*state
)
1249 static game_ui
*new_ui(game_state
*state
)
1254 static void free_ui(game_ui
*ui
)
1258 static char *encode_ui(game_ui
*ui
)
1263 static void decode_ui(game_ui
*ui
, char *encoding
)
1267 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1268 game_state
*newstate
)
1272 struct game_drawstate
{
1277 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1278 int x
, int y
, int button
)
1283 static game_state
*execute_move(game_state
*state
, char *move
)
1288 /* ----------------------------------------------------------------------
1292 static void game_compute_size(game_params
*params
, int tilesize
,
1295 *x
= *y
= 10 * tilesize
; /* FIXME */
1298 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1299 game_params
*params
, int tilesize
)
1301 ds
->tilesize
= tilesize
;
1304 static float *game_colours(frontend
*fe
, int *ncolours
)
1306 float *ret
= snewn(3 * NCOLOURS
, float);
1308 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1310 *ncolours
= NCOLOURS
;
1314 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1316 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1324 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1329 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1330 game_state
*state
, int dir
, game_ui
*ui
,
1331 float animtime
, float flashtime
)
1334 * The initial contents of the window are not guaranteed and
1335 * can vary with front ends. To be on the safe side, all games
1336 * should start by drawing a big background-colour rectangle
1337 * covering the whole window.
1339 draw_rect(dr
, 0, 0, 10*ds
->tilesize
, 10*ds
->tilesize
, COL_BACKGROUND
);
1342 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1343 int dir
, game_ui
*ui
)
1348 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1349 int dir
, game_ui
*ui
)
1354 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1359 static void game_print_size(game_params
*params
, float *x
, float *y
)
1363 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1368 #define thegame pearl
1371 const struct game thegame
= {
1372 "Pearl", NULL
, NULL
,
1379 FALSE
, game_configure
, custom_params
,
1387 FALSE
, game_can_format_as_text_now
, game_text_format
,
1395 20 /* FIXME */, game_compute_size
, game_set_size
,
1398 game_free_drawstate
,
1402 FALSE
, FALSE
, game_print_size
, game_print
,
1403 FALSE
, /* wants_statusbar */
1404 FALSE
, game_timing_state
,