2 * rect.c: Puzzle from nikoli.co.jp. You have a square grid with
3 * numbers in some squares; you must divide the square grid up into
4 * variously sized rectangles, such that every rectangle contains
5 * exactly one numbered square and the area of each rectangle is
6 * equal to the number contained in it.
12 * - Improve singleton removal.
13 * + It would be nice to limit the size of the generated
14 * rectangles in accordance with existing constraints such as
15 * the maximum rectangle size and the one about not
16 * generating a rectangle the full width or height of the
18 * + This could be achieved by making a less random choice
19 * about which of the available options to use.
20 * + Alternatively, we could create our rectangle and then
49 #define INDEX(state, x, y) (((y) * (state)->w) + (x))
50 #define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
51 #define grid(state,x,y) index(state, (state)->grid, x, y)
52 #define vedge(state,x,y) index(state, (state)->vedge, x, y)
53 #define hedge(state,x,y) index(state, (state)->hedge, x, y)
55 #define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
56 (y) >= dy && (y) < (state)->h )
57 #define RANGE(state,x,y) CRANGE(state,x,y,0,0)
58 #define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
59 #define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
61 #define PREFERRED_TILE_SIZE 24
62 #define TILE_SIZE (ds->tilesize)
63 #define BORDER (TILE_SIZE * 3 / 4)
65 #define CORNER_TOLERANCE 0.15F
66 #define CENTRE_TOLERANCE 0.15F
68 #define FLASH_TIME 0.13F
70 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
71 #define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
75 int *grid
; /* contains the numbers */
76 unsigned char *vedge
; /* (w+1) x h */
77 unsigned char *hedge
; /* w x (h+1) */
78 int completed
, cheated
;
79 unsigned char *correct
;
82 static game_params
*default_params(void)
84 game_params
*ret
= snew(game_params
);
87 ret
->expandfactor
= 0.0F
;
93 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
100 case 0: w
= 7, h
= 7; break;
101 case 1: w
= 9, h
= 9; break;
102 case 2: w
= 11, h
= 11; break;
103 case 3: w
= 13, h
= 13; break;
104 case 4: w
= 15, h
= 15; break;
105 case 5: w
= 17, h
= 17; break;
106 case 6: w
= 19, h
= 19; break;
107 default: return FALSE
;
110 sprintf(buf
, "%dx%d", w
, h
);
112 *params
= ret
= snew(game_params
);
115 ret
->expandfactor
= 0.0F
;
120 static void free_params(game_params
*params
)
125 static game_params
*dup_params(game_params
*params
)
127 game_params
*ret
= snew(game_params
);
128 *ret
= *params
; /* structure copy */
132 static void decode_params(game_params
*ret
, char const *string
)
134 ret
->w
= ret
->h
= atoi(string
);
135 while (*string
&& isdigit((unsigned char)*string
)) string
++;
136 if (*string
== 'x') {
138 ret
->h
= atoi(string
);
139 while (*string
&& isdigit((unsigned char)*string
)) string
++;
141 if (*string
== 'e') {
143 ret
->expandfactor
= atof(string
);
145 (*string
== '.' || isdigit((unsigned char)*string
))) string
++;
147 if (*string
== 'a') {
153 static char *encode_params(game_params
*params
, int full
)
157 sprintf(data
, "%dx%d", params
->w
, params
->h
);
158 if (full
&& params
->expandfactor
)
159 sprintf(data
+ strlen(data
), "e%g", params
->expandfactor
);
160 if (full
&& !params
->unique
)
166 static config_item
*game_configure(game_params
*params
)
171 ret
= snewn(5, config_item
);
173 ret
[0].name
= "Width";
174 ret
[0].type
= C_STRING
;
175 sprintf(buf
, "%d", params
->w
);
176 ret
[0].sval
= dupstr(buf
);
179 ret
[1].name
= "Height";
180 ret
[1].type
= C_STRING
;
181 sprintf(buf
, "%d", params
->h
);
182 ret
[1].sval
= dupstr(buf
);
185 ret
[2].name
= "Expansion factor";
186 ret
[2].type
= C_STRING
;
187 sprintf(buf
, "%g", params
->expandfactor
);
188 ret
[2].sval
= dupstr(buf
);
191 ret
[3].name
= "Ensure unique solution";
192 ret
[3].type
= C_BOOLEAN
;
194 ret
[3].ival
= params
->unique
;
204 static game_params
*custom_params(config_item
*cfg
)
206 game_params
*ret
= snew(game_params
);
208 ret
->w
= atoi(cfg
[0].sval
);
209 ret
->h
= atoi(cfg
[1].sval
);
210 ret
->expandfactor
= atof(cfg
[2].sval
);
211 ret
->unique
= cfg
[3].ival
;
216 static char *validate_params(game_params
*params
, int full
)
218 if (params
->w
<= 0 || params
->h
<= 0)
219 return "Width and height must both be greater than zero";
220 if (params
->w
*params
->h
< 2)
221 return "Grid area must be greater than one";
222 if (params
->expandfactor
< 0.0F
)
223 return "Expansion factor may not be negative";
244 struct point
*points
;
247 /* ----------------------------------------------------------------------
248 * Solver for Rectangles games.
250 * This solver is souped up beyond the needs of actually _solving_
251 * a puzzle. It is also designed to cope with uncertainty about
252 * where the numbers have been placed. This is because I run it on
253 * my generated grids _before_ placing the numbers, and have it
254 * tell me where I need to place the numbers to ensure a unique
258 static void remove_rect_placement(int w
, int h
,
259 struct rectlist
*rectpositions
,
261 int rectnum
, int placement
)
265 #ifdef SOLVER_DIAGNOSTICS
266 printf("ruling out rect %d placement at %d,%d w=%d h=%d\n", rectnum
,
267 rectpositions
[rectnum
].rects
[placement
].x
,
268 rectpositions
[rectnum
].rects
[placement
].y
,
269 rectpositions
[rectnum
].rects
[placement
].w
,
270 rectpositions
[rectnum
].rects
[placement
].h
);
274 * Decrement each entry in the overlaps array to reflect the
275 * removal of this rectangle placement.
277 for (yy
= 0; yy
< rectpositions
[rectnum
].rects
[placement
].h
; yy
++) {
278 y
= yy
+ rectpositions
[rectnum
].rects
[placement
].y
;
279 for (xx
= 0; xx
< rectpositions
[rectnum
].rects
[placement
].w
; xx
++) {
280 x
= xx
+ rectpositions
[rectnum
].rects
[placement
].x
;
282 assert(overlaps
[(rectnum
* h
+ y
) * w
+ x
] != 0);
284 if (overlaps
[(rectnum
* h
+ y
) * w
+ x
] > 0)
285 overlaps
[(rectnum
* h
+ y
) * w
+ x
]--;
290 * Remove the placement from the list of positions for that
291 * rectangle, by interchanging it with the one on the end.
293 if (placement
< rectpositions
[rectnum
].n
- 1) {
296 t
= rectpositions
[rectnum
].rects
[rectpositions
[rectnum
].n
- 1];
297 rectpositions
[rectnum
].rects
[rectpositions
[rectnum
].n
- 1] =
298 rectpositions
[rectnum
].rects
[placement
];
299 rectpositions
[rectnum
].rects
[placement
] = t
;
301 rectpositions
[rectnum
].n
--;
304 static void remove_number_placement(int w
, int h
, struct numberdata
*number
,
305 int index
, int *rectbyplace
)
308 * Remove the entry from the rectbyplace array.
310 rectbyplace
[number
->points
[index
].y
* w
+ number
->points
[index
].x
] = -1;
313 * Remove the placement from the list of candidates for that
314 * number, by interchanging it with the one on the end.
316 if (index
< number
->npoints
- 1) {
319 t
= number
->points
[number
->npoints
- 1];
320 number
->points
[number
->npoints
- 1] = number
->points
[index
];
321 number
->points
[index
] = t
;
326 static int rect_solver(int w
, int h
, int nrects
, struct numberdata
*numbers
,
327 unsigned char *hedge
, unsigned char *vedge
,
330 struct rectlist
*rectpositions
;
331 int *overlaps
, *rectbyplace
, *workspace
;
335 * Start by setting up a list of candidate positions for each
338 rectpositions
= snewn(nrects
, struct rectlist
);
339 for (i
= 0; i
< nrects
; i
++) {
340 int rw
, rh
, area
= numbers
[i
].area
;
341 int j
, minx
, miny
, maxx
, maxy
;
343 int rlistn
, rlistsize
;
346 * For each rectangle, begin by finding the bounding
347 * rectangle of its candidate number placements.
352 for (j
= 0; j
< numbers
[i
].npoints
; j
++) {
353 if (minx
> numbers
[i
].points
[j
].x
) minx
= numbers
[i
].points
[j
].x
;
354 if (miny
> numbers
[i
].points
[j
].y
) miny
= numbers
[i
].points
[j
].y
;
355 if (maxx
< numbers
[i
].points
[j
].x
) maxx
= numbers
[i
].points
[j
].x
;
356 if (maxy
< numbers
[i
].points
[j
].y
) maxy
= numbers
[i
].points
[j
].y
;
360 * Now loop over all possible rectangle placements
361 * overlapping a point within that bounding rectangle;
362 * ensure each one actually contains a candidate number
363 * placement, and add it to the list.
366 rlistn
= rlistsize
= 0;
368 for (rw
= 1; rw
<= area
&& rw
<= w
; rw
++) {
377 for (y
= miny
- rh
+ 1; y
<= maxy
; y
++) {
378 if (y
< 0 || y
+rh
> h
)
381 for (x
= minx
- rw
+ 1; x
<= maxx
; x
++) {
382 if (x
< 0 || x
+rw
> w
)
386 * See if we can find a candidate number
387 * placement within this rectangle.
389 for (j
= 0; j
< numbers
[i
].npoints
; j
++)
390 if (numbers
[i
].points
[j
].x
>= x
&&
391 numbers
[i
].points
[j
].x
< x
+rw
&&
392 numbers
[i
].points
[j
].y
>= y
&&
393 numbers
[i
].points
[j
].y
< y
+rh
)
396 if (j
< numbers
[i
].npoints
) {
398 * Add this to the list of candidate
399 * placements for this rectangle.
401 if (rlistn
>= rlistsize
) {
402 rlistsize
= rlistn
+ 32;
403 rlist
= sresize(rlist
, rlistsize
, struct rect
);
407 rlist
[rlistn
].w
= rw
;
408 rlist
[rlistn
].h
= rh
;
409 #ifdef SOLVER_DIAGNOSTICS
410 printf("rect %d [area %d]: candidate position at"
411 " %d,%d w=%d h=%d\n",
412 i
, area
, x
, y
, rw
, rh
);
420 rectpositions
[i
].rects
= rlist
;
421 rectpositions
[i
].n
= rlistn
;
425 * Next, construct a multidimensional array tracking how many
426 * candidate positions for each rectangle overlap each square.
428 * Indexing of this array is by the formula
430 * overlaps[(rectindex * h + y) * w + x]
432 overlaps
= snewn(nrects
* w
* h
, int);
433 memset(overlaps
, 0, nrects
* w
* h
* sizeof(int));
434 for (i
= 0; i
< nrects
; i
++) {
437 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
440 for (yy
= 0; yy
< rectpositions
[i
].rects
[j
].h
; yy
++)
441 for (xx
= 0; xx
< rectpositions
[i
].rects
[j
].w
; xx
++)
442 overlaps
[(i
* h
+ yy
+rectpositions
[i
].rects
[j
].y
) * w
+
443 xx
+rectpositions
[i
].rects
[j
].x
]++;
448 * Also we want an array covering the grid once, to make it
449 * easy to figure out which squares are candidate number
450 * placements for which rectangles. (The existence of this
451 * single array assumes that no square starts off as a
452 * candidate number placement for more than one rectangle. This
453 * assumption is justified, because this solver is _either_
454 * used to solve real problems - in which case there is a
455 * single placement for every number - _or_ used to decide on
456 * number placements for a new puzzle, in which case each
457 * number's placements are confined to the intended position of
458 * the rectangle containing that number.)
460 rectbyplace
= snewn(w
* h
, int);
461 for (i
= 0; i
< w
*h
; i
++)
464 for (i
= 0; i
< nrects
; i
++) {
467 for (j
= 0; j
< numbers
[i
].npoints
; j
++) {
468 int x
= numbers
[i
].points
[j
].x
;
469 int y
= numbers
[i
].points
[j
].y
;
471 assert(rectbyplace
[y
* w
+ x
] == -1);
472 rectbyplace
[y
* w
+ x
] = i
;
476 workspace
= snewn(nrects
, int);
479 * Now run the actual deduction loop.
482 int done_something
= FALSE
;
484 #ifdef SOLVER_DIAGNOSTICS
485 printf("starting deduction loop\n");
487 for (i
= 0; i
< nrects
; i
++) {
488 printf("rect %d overlaps:\n", i
);
491 for (y
= 0; y
< h
; y
++) {
492 for (x
= 0; x
< w
; x
++) {
493 printf("%3d", overlaps
[(i
* h
+ y
) * w
+ x
]);
499 printf("rectbyplace:\n");
502 for (y
= 0; y
< h
; y
++) {
503 for (x
= 0; x
< w
; x
++) {
504 printf("%3d", rectbyplace
[y
* w
+ x
]);
512 * Housekeeping. Look for rectangles whose number has only
513 * one candidate position left, and mark that square as
514 * known if it isn't already.
516 for (i
= 0; i
< nrects
; i
++) {
517 if (numbers
[i
].npoints
== 1) {
518 int x
= numbers
[i
].points
[0].x
;
519 int y
= numbers
[i
].points
[0].y
;
520 if (overlaps
[(i
* h
+ y
) * w
+ x
] >= -1) {
523 assert(overlaps
[(i
* h
+ y
) * w
+ x
] > 0);
524 #ifdef SOLVER_DIAGNOSTICS
525 printf("marking %d,%d as known for rect %d"
526 " (sole remaining number position)\n", x
, y
, i
);
529 for (j
= 0; j
< nrects
; j
++)
530 overlaps
[(j
* h
+ y
) * w
+ x
] = -1;
532 overlaps
[(i
* h
+ y
) * w
+ x
] = -2;
538 * Now look at the intersection of all possible placements
539 * for each rectangle, and mark all squares in that
540 * intersection as known for that rectangle if they aren't
543 for (i
= 0; i
< nrects
; i
++) {
544 int minx
, miny
, maxx
, maxy
, xx
, yy
, j
;
550 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
551 int x
= rectpositions
[i
].rects
[j
].x
;
552 int y
= rectpositions
[i
].rects
[j
].y
;
553 int w
= rectpositions
[i
].rects
[j
].w
;
554 int h
= rectpositions
[i
].rects
[j
].h
;
556 if (minx
< x
) minx
= x
;
557 if (miny
< y
) miny
= y
;
558 if (maxx
> x
+w
) maxx
= x
+w
;
559 if (maxy
> y
+h
) maxy
= y
+h
;
562 for (yy
= miny
; yy
< maxy
; yy
++)
563 for (xx
= minx
; xx
< maxx
; xx
++)
564 if (overlaps
[(i
* h
+ yy
) * w
+ xx
] >= -1) {
565 assert(overlaps
[(i
* h
+ yy
) * w
+ xx
] > 0);
566 #ifdef SOLVER_DIAGNOSTICS
567 printf("marking %d,%d as known for rect %d"
568 " (intersection of all placements)\n",
572 for (j
= 0; j
< nrects
; j
++)
573 overlaps
[(j
* h
+ yy
) * w
+ xx
] = -1;
575 overlaps
[(i
* h
+ yy
) * w
+ xx
] = -2;
580 * Rectangle-focused deduction. Look at each rectangle in
581 * turn and try to rule out some of its candidate
584 for (i
= 0; i
< nrects
; i
++) {
587 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
591 for (k
= 0; k
< nrects
; k
++)
594 for (yy
= 0; yy
< rectpositions
[i
].rects
[j
].h
; yy
++) {
595 int y
= yy
+ rectpositions
[i
].rects
[j
].y
;
596 for (xx
= 0; xx
< rectpositions
[i
].rects
[j
].w
; xx
++) {
597 int x
= xx
+ rectpositions
[i
].rects
[j
].x
;
599 if (overlaps
[(i
* h
+ y
) * w
+ x
] == -1) {
601 * This placement overlaps a square
602 * which is _known_ to be part of
603 * another rectangle. Therefore we must
606 #ifdef SOLVER_DIAGNOSTICS
607 printf("rect %d placement at %d,%d w=%d h=%d "
608 "contains %d,%d which is known-other\n", i
,
609 rectpositions
[i
].rects
[j
].x
,
610 rectpositions
[i
].rects
[j
].y
,
611 rectpositions
[i
].rects
[j
].w
,
612 rectpositions
[i
].rects
[j
].h
,
618 if (rectbyplace
[y
* w
+ x
] != -1) {
620 * This placement overlaps one of the
621 * candidate number placements for some
622 * rectangle. Count it.
624 workspace
[rectbyplace
[y
* w
+ x
]]++;
631 * If we haven't ruled this placement out
632 * already, see if it overlaps _all_ of the
633 * candidate number placements for any
634 * rectangle. If so, we can rule it out.
636 for (k
= 0; k
< nrects
; k
++)
637 if (k
!= i
&& workspace
[k
] == numbers
[k
].npoints
) {
638 #ifdef SOLVER_DIAGNOSTICS
639 printf("rect %d placement at %d,%d w=%d h=%d "
640 "contains all number points for rect %d\n",
642 rectpositions
[i
].rects
[j
].x
,
643 rectpositions
[i
].rects
[j
].y
,
644 rectpositions
[i
].rects
[j
].w
,
645 rectpositions
[i
].rects
[j
].h
,
653 * Failing that, see if it overlaps at least
654 * one of the candidate number placements for
655 * itself! (This might not be the case if one
656 * of those number placements has been removed
659 if (!del
&& workspace
[i
] == 0) {
660 #ifdef SOLVER_DIAGNOSTICS
661 printf("rect %d placement at %d,%d w=%d h=%d "
662 "contains none of its own number points\n",
664 rectpositions
[i
].rects
[j
].x
,
665 rectpositions
[i
].rects
[j
].y
,
666 rectpositions
[i
].rects
[j
].w
,
667 rectpositions
[i
].rects
[j
].h
);
674 remove_rect_placement(w
, h
, rectpositions
, overlaps
, i
, j
);
676 j
--; /* don't skip over next placement */
678 done_something
= TRUE
;
684 * Square-focused deduction. Look at each square not marked
685 * as known, and see if there are any which can only be
686 * part of a single rectangle.
690 for (y
= 0; y
< h
; y
++) for (x
= 0; x
< w
; x
++) {
691 /* Known squares are marked as <0 everywhere, so we only need
692 * to check the overlaps entry for rect 0. */
693 if (overlaps
[y
* w
+ x
] < 0)
694 continue; /* known already */
698 for (i
= 0; i
< nrects
; i
++)
699 if (overlaps
[(i
* h
+ y
) * w
+ x
] > 0)
706 * Now we can rule out all placements for
707 * rectangle `index' which _don't_ contain
710 #ifdef SOLVER_DIAGNOSTICS
711 printf("square %d,%d can only be in rectangle %d\n",
714 for (j
= 0; j
< rectpositions
[index
].n
; j
++) {
715 struct rect
*r
= &rectpositions
[index
].rects
[j
];
716 if (x
>= r
->x
&& x
< r
->x
+ r
->w
&&
717 y
>= r
->y
&& y
< r
->y
+ r
->h
)
718 continue; /* this one is OK */
719 remove_rect_placement(w
, h
, rectpositions
, overlaps
,
721 j
--; /* don't skip over next placement */
722 done_something
= TRUE
;
729 * If we've managed to deduce anything by normal means,
730 * loop round again and see if there's more to be done.
731 * Only if normal deduction has completely failed us should
732 * we now move on to narrowing down the possible number
739 * Now we have done everything we can with the current set
740 * of number placements. So we need to winnow the number
741 * placements so as to narrow down the possibilities. We do
742 * this by searching for a candidate placement (of _any_
743 * rectangle) which overlaps a candidate placement of the
744 * number for some other rectangle.
752 size_t nrpns
= 0, rpnsize
= 0;
755 for (i
= 0; i
< nrects
; i
++) {
756 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
759 for (yy
= 0; yy
< rectpositions
[i
].rects
[j
].h
; yy
++) {
760 int y
= yy
+ rectpositions
[i
].rects
[j
].y
;
761 for (xx
= 0; xx
< rectpositions
[i
].rects
[j
].w
; xx
++) {
762 int x
= xx
+ rectpositions
[i
].rects
[j
].x
;
764 if (rectbyplace
[y
* w
+ x
] >= 0 &&
765 rectbyplace
[y
* w
+ x
] != i
) {
767 * Add this to the list of
768 * winnowing possibilities.
770 if (nrpns
>= rpnsize
) {
771 rpnsize
= rpnsize
* 3 / 2 + 32;
772 rpns
= sresize(rpns
, rpnsize
, struct rpn
);
774 rpns
[nrpns
].rect
= i
;
775 rpns
[nrpns
].placement
= j
;
776 rpns
[nrpns
].number
= rectbyplace
[y
* w
+ x
];
785 #ifdef SOLVER_DIAGNOSTICS
786 printf("%d candidate rect placements we could eliminate\n", nrpns
);
790 * Now choose one of these unwanted rectangle
791 * placements, and eliminate it.
793 int index
= random_upto(rs
, nrpns
);
795 struct rpn rpn
= rpns
[index
];
802 r
= rectpositions
[i
].rects
[j
];
805 * We rule out placement j of rectangle i by means
806 * of removing all of rectangle k's candidate
807 * number placements which do _not_ overlap it.
808 * This will ensure that it is eliminated during
809 * the next pass of rectangle-focused deduction.
811 #ifdef SOLVER_DIAGNOSTICS
812 printf("ensuring number for rect %d is within"
813 " rect %d's placement at %d,%d w=%d h=%d\n",
814 k
, i
, r
.x
, r
.y
, r
.w
, r
.h
);
817 for (m
= 0; m
< numbers
[k
].npoints
; m
++) {
818 int x
= numbers
[k
].points
[m
].x
;
819 int y
= numbers
[k
].points
[m
].y
;
821 if (x
< r
.x
|| x
>= r
.x
+ r
.w
||
822 y
< r
.y
|| y
>= r
.y
+ r
.h
) {
823 #ifdef SOLVER_DIAGNOSTICS
824 printf("eliminating number for rect %d at %d,%d\n",
827 remove_number_placement(w
, h
, &numbers
[k
],
829 m
--; /* don't skip the next one */
830 done_something
= TRUE
;
836 if (!done_something
) {
837 #ifdef SOLVER_DIAGNOSTICS
838 printf("terminating deduction loop\n");
845 for (i
= 0; i
< nrects
; i
++) {
846 #ifdef SOLVER_DIAGNOSTICS
847 printf("rect %d has %d possible placements\n",
848 i
, rectpositions
[i
].n
);
850 assert(rectpositions
[i
].n
> 0);
851 if (rectpositions
[i
].n
> 1) {
853 } else if (hedge
&& vedge
) {
855 * Place the rectangle in its only possible position.
858 struct rect
*r
= &rectpositions
[i
].rects
[0];
860 for (y
= 0; y
< r
->h
; y
++) {
862 vedge
[(r
->y
+y
) * w
+ r
->x
] = 1;
864 vedge
[(r
->y
+y
) * w
+ r
->x
+r
->w
] = 1;
866 for (x
= 0; x
< r
->w
; x
++) {
868 hedge
[r
->y
* w
+ r
->x
+x
] = 1;
870 hedge
[(r
->y
+r
->h
) * w
+ r
->x
+x
] = 1;
876 * Free up all allocated storage.
881 for (i
= 0; i
< nrects
; i
++)
882 sfree(rectpositions
[i
].rects
);
883 sfree(rectpositions
);
888 /* ----------------------------------------------------------------------
889 * Grid generation code.
893 * This function does one of two things. If passed r==NULL, it
894 * counts the number of possible rectangles which cover the given
895 * square, and returns it in *n. If passed r!=NULL then it _reads_
896 * *n to find an index, counts the possible rectangles until it
897 * reaches the nth, and writes it into r.
899 * `scratch' is expected to point to an array of 2 * params->w
900 * ints, used internally as scratch space (and passed in like this
901 * to avoid re-allocating and re-freeing it every time round a
904 static void enum_rects(game_params
*params
, int *grid
, struct rect
*r
, int *n
,
905 int sx
, int sy
, int *scratch
)
909 int maxarea
, realmaxarea
;
914 * Maximum rectangle area is 1/6 of total grid size, unless
915 * this means we can't place any rectangles at all in which
916 * case we set it to 2 at minimum.
918 maxarea
= params
->w
* params
->h
/ 6;
923 * Scan the grid to find the limits of the region within which
924 * any rectangle containing this point must fall. This will
925 * save us trawling the inside of every rectangle later on to
926 * see if it contains any used squares.
929 bottom
= scratch
+ params
->w
;
930 for (dy
= -1; dy
<= +1; dy
+= 2) {
931 int *array
= (dy
== -1 ? top
: bottom
);
932 for (dx
= -1; dx
<= +1; dx
+= 2) {
933 for (x
= sx
; x
>= 0 && x
< params
->w
; x
+= dx
) {
934 array
[x
] = -2 * params
->h
* dy
;
935 for (y
= sy
; y
>= 0 && y
< params
->h
; y
+= dy
) {
936 if (index(params
, grid
, x
, y
) == -1 &&
937 (x
== sx
|| dy
*y
<= dy
*array
[x
-dx
]))
947 * Now scan again to work out the largest rectangles we can fit
948 * in the grid, so that we can terminate the following loops
949 * early once we get down to not having much space left in the
953 for (x
= 0; x
< params
->w
; x
++) {
956 rh
= bottom
[x
] - top
[x
] + 1;
958 continue; /* no rectangles can start here */
960 dx
= (x
> sx ?
-1 : +1);
961 for (x2
= x
; x2
>= 0 && x2
< params
->w
; x2
+= dx
)
962 if (bottom
[x2
] < bottom
[x
] || top
[x2
] > top
[x
])
966 if (realmaxarea
< rw
* rh
)
967 realmaxarea
= rw
* rh
;
970 if (realmaxarea
> maxarea
)
971 realmaxarea
= maxarea
;
974 * Rectangles which go right the way across the grid are
975 * boring, although they can't be helped in the case of
976 * extremely small grids. (Also they might be generated later
977 * on by the singleton-removal process; we can't help that.)
984 for (rw
= 1; rw
<= mw
; rw
++)
985 for (rh
= 1; rh
<= mh
; rh
++) {
986 if (rw
* rh
> realmaxarea
)
990 for (x
= max(sx
- rw
+ 1, 0); x
<= min(sx
, params
->w
- rw
); x
++)
991 for (y
= max(sy
- rh
+ 1, 0); y
<= min(sy
, params
->h
- rh
);
994 * Check this rectangle against the region we
997 if (top
[x
] <= y
&& top
[x
+rw
-1] <= y
&&
998 bottom
[x
] >= y
+rh
-1 && bottom
[x
+rw
-1] >= y
+rh
-1) {
999 if (r
&& index
== *n
) {
1015 static void place_rect(game_params
*params
, int *grid
, struct rect r
)
1017 int idx
= INDEX(params
, r
.x
, r
.y
);
1020 for (x
= r
.x
; x
< r
.x
+r
.w
; x
++)
1021 for (y
= r
.y
; y
< r
.y
+r
.h
; y
++) {
1022 index(params
, grid
, x
, y
) = idx
;
1024 #ifdef GENERATION_DIAGNOSTICS
1025 printf(" placing rectangle at (%d,%d) size %d x %d\n",
1026 r
.x
, r
.y
, r
.w
, r
.h
);
1030 static struct rect
find_rect(game_params
*params
, int *grid
, int x
, int y
)
1036 * Find the top left of the rectangle.
1038 idx
= index(params
, grid
, x
, y
);
1044 return r
; /* 1x1 singleton here */
1047 y
= idx
/ params
->w
;
1048 x
= idx
% params
->w
;
1051 * Find the width and height of the rectangle.
1054 (x
+w
< params
->w
&& index(params
,grid
,x
+w
,y
)==idx
);
1057 (y
+h
< params
->h
&& index(params
,grid
,x
,y
+h
)==idx
);
1068 #ifdef GENERATION_DIAGNOSTICS
1069 static void display_grid(game_params
*params
, int *grid
, int *numbers
, int all
)
1071 unsigned char *egrid
= snewn((params
->w
*2+3) * (params
->h
*2+3),
1074 int r
= (params
->w
*2+3);
1076 memset(egrid
, 0, (params
->w
*2+3) * (params
->h
*2+3));
1078 for (x
= 0; x
< params
->w
; x
++)
1079 for (y
= 0; y
< params
->h
; y
++) {
1080 int i
= index(params
, grid
, x
, y
);
1081 if (x
== 0 || index(params
, grid
, x
-1, y
) != i
)
1082 egrid
[(2*y
+2) * r
+ (2*x
+1)] = 1;
1083 if (x
== params
->w
-1 || index(params
, grid
, x
+1, y
) != i
)
1084 egrid
[(2*y
+2) * r
+ (2*x
+3)] = 1;
1085 if (y
== 0 || index(params
, grid
, x
, y
-1) != i
)
1086 egrid
[(2*y
+1) * r
+ (2*x
+2)] = 1;
1087 if (y
== params
->h
-1 || index(params
, grid
, x
, y
+1) != i
)
1088 egrid
[(2*y
+3) * r
+ (2*x
+2)] = 1;
1091 for (y
= 1; y
< 2*params
->h
+2; y
++) {
1092 for (x
= 1; x
< 2*params
->w
+2; x
++) {
1094 int k
= numbers ?
index(params
, numbers
, x
/2-1, y
/2-1) : 0;
1095 if (k
|| (all
&& numbers
)) printf("%2d", k
); else printf(" ");
1096 } else if (!((y
&x
)&1)) {
1097 int v
= egrid
[y
*r
+x
];
1098 if ((y
&1) && v
) v
= '-';
1099 if ((x
&1) && v
) v
= '|';
1102 if (!(x
&1)) putchar(v
);
1105 if (egrid
[y
*r
+(x
+1)]) d
|= 1;
1106 if (egrid
[(y
-1)*r
+x
]) d
|= 2;
1107 if (egrid
[y
*r
+(x
-1)]) d
|= 4;
1108 if (egrid
[(y
+1)*r
+x
]) d
|= 8;
1109 c
= " ??+?-++?+|+++++"[d
];
1111 if (!(x
&1)) putchar(c
);
1121 static char *new_game_desc(game_params
*params
, random_state
*rs
,
1122 char **aux
, int interactive
)
1124 int *grid
, *numbers
= NULL
;
1125 int x
, y
, y2
, y2last
, yx
, run
, i
, nsquares
;
1127 int *enum_rects_scratch
;
1128 game_params params2real
, *params2
= ¶ms2real
;
1132 * Set up the smaller width and height which we will use to
1133 * generate the base grid.
1135 params2
->w
= params
->w
/ (1.0F
+ params
->expandfactor
);
1136 if (params2
->w
< 2 && params
->w
>= 2) params2
->w
= 2;
1137 params2
->h
= params
->h
/ (1.0F
+ params
->expandfactor
);
1138 if (params2
->h
< 2 && params
->h
>= 2) params2
->h
= 2;
1140 grid
= snewn(params2
->w
* params2
->h
, int);
1142 enum_rects_scratch
= snewn(2 * params2
->w
, int);
1145 for (y
= 0; y
< params2
->h
; y
++)
1146 for (x
= 0; x
< params2
->w
; x
++) {
1147 index(params2
, grid
, x
, y
) = -1;
1152 * Place rectangles until we can't any more. We do this by
1153 * finding a square we haven't yet covered, and randomly
1154 * choosing a rectangle to cover it.
1157 while (nsquares
> 0) {
1158 int square
= random_upto(rs
, nsquares
);
1164 for (y
= 0; y
< params2
->h
; y
++) {
1165 for (x
= 0; x
< params2
->w
; x
++) {
1166 if (index(params2
, grid
, x
, y
) == -1 && square
-- == 0)
1172 assert(x
< params2
->w
&& y
< params2
->h
);
1175 * Now see how many rectangles fit around this one.
1177 enum_rects(params2
, grid
, NULL
, &n
, x
, y
, enum_rects_scratch
);
1181 * There are no possible rectangles covering this
1182 * square, meaning it must be a singleton. Mark it
1183 * -2 so we know not to keep trying.
1185 index(params2
, grid
, x
, y
) = -2;
1189 * Pick one at random.
1191 n
= random_upto(rs
, n
);
1192 enum_rects(params2
, grid
, &r
, &n
, x
, y
, enum_rects_scratch
);
1197 place_rect(params2
, grid
, r
);
1198 nsquares
-= r
.w
* r
.h
;
1202 sfree(enum_rects_scratch
);
1205 * Deal with singleton spaces remaining in the grid, one by
1208 * We do this by making a local change to the layout. There are
1209 * several possibilities:
1211 * +-----+-----+ Here, we can remove the singleton by
1212 * | | | extending the 1x2 rectangle below it
1213 * +--+--+-----+ into a 1x3.
1221 * +--+--+--+ Here, that trick doesn't work: there's no
1222 * | | | 1 x n rectangle with the singleton at one
1223 * | | | end. Instead, we extend a 1 x n rectangle
1224 * | | | _out_ from the singleton, shaving a layer
1225 * +--+--+ | off the end of another rectangle. So if we
1226 * | | | | extended up, we'd make our singleton part
1227 * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2
1228 * | | | used to be; or we could extend right into
1229 * +--+-----+ a 2x1, turning the 1x3 into a 1x2.
1231 * +-----+--+ Here, we can't even do _that_, since any
1232 * | | | direction we choose to extend the singleton
1233 * +--+--+ | will produce a new singleton as a result of
1234 * | | | | truncating one of the size-2 rectangles.
1235 * | +--+--+ Fortunately, this case can _only_ occur when
1236 * | | | a singleton is surrounded by four size-2s
1237 * +--+-----+ in this fashion; so instead we can simply
1238 * replace the whole section with a single 3x3.
1240 for (x
= 0; x
< params2
->w
; x
++) {
1241 for (y
= 0; y
< params2
->h
; y
++) {
1242 if (index(params2
, grid
, x
, y
) < 0) {
1245 #ifdef GENERATION_DIAGNOSTICS
1246 display_grid(params2
, grid
, NULL
, FALSE
);
1247 printf("singleton at %d,%d\n", x
, y
);
1251 * Check in which directions we can feasibly extend
1252 * the singleton. We can extend in a particular
1253 * direction iff either:
1255 * - the rectangle on that side of the singleton
1256 * is not 2x1, and we are at one end of the edge
1257 * of it we are touching
1259 * - it is 2x1 but we are on its short side.
1261 * FIXME: we could plausibly choose between these
1262 * based on the sizes of the rectangles they would
1266 if (x
< params2
->w
-1) {
1267 struct rect r
= find_rect(params2
, grid
, x
+1, y
);
1268 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
1269 dirs
[ndirs
++] = 1; /* right */
1272 struct rect r
= find_rect(params2
, grid
, x
, y
-1);
1273 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
1274 dirs
[ndirs
++] = 2; /* up */
1277 struct rect r
= find_rect(params2
, grid
, x
-1, y
);
1278 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
1279 dirs
[ndirs
++] = 4; /* left */
1281 if (y
< params2
->h
-1) {
1282 struct rect r
= find_rect(params2
, grid
, x
, y
+1);
1283 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
1284 dirs
[ndirs
++] = 8; /* down */
1291 which
= random_upto(rs
, ndirs
);
1296 assert(x
< params2
->w
+1);
1297 #ifdef GENERATION_DIAGNOSTICS
1298 printf("extending right\n");
1300 r1
= find_rect(params2
, grid
, x
+1, y
);
1311 #ifdef GENERATION_DIAGNOSTICS
1312 printf("extending up\n");
1314 r1
= find_rect(params2
, grid
, x
, y
-1);
1325 #ifdef GENERATION_DIAGNOSTICS
1326 printf("extending left\n");
1328 r1
= find_rect(params2
, grid
, x
-1, y
);
1338 assert(y
< params2
->h
+1);
1339 #ifdef GENERATION_DIAGNOSTICS
1340 printf("extending down\n");
1342 r1
= find_rect(params2
, grid
, x
, y
+1);
1351 default: /* should never happen */
1352 assert(!"invalid direction");
1354 if (r1
.h
> 0 && r1
.w
> 0)
1355 place_rect(params2
, grid
, r1
);
1356 place_rect(params2
, grid
, r2
);
1360 * Sanity-check that there really is a 3x3
1361 * rectangle surrounding this singleton and it
1362 * contains absolutely everything we could
1367 assert(x
> 0 && x
< params2
->w
-1);
1368 assert(y
> 0 && y
< params2
->h
-1);
1370 for (xx
= x
-1; xx
<= x
+1; xx
++)
1371 for (yy
= y
-1; yy
<= y
+1; yy
++) {
1372 struct rect r
= find_rect(params2
,grid
,xx
,yy
);
1375 assert(r
.x
+r
.w
-1 <= x
+1);
1376 assert(r
.y
+r
.h
-1 <= y
+1);
1381 #ifdef GENERATION_DIAGNOSTICS
1382 printf("need the 3x3 trick\n");
1386 * FIXME: If the maximum rectangle area for
1387 * this grid is less than 9, we ought to
1388 * subdivide the 3x3 in some fashion. There are
1389 * five other possibilities:
1392 * - a 4, a 3 and a 2
1394 * - a 3 and three 2s (two different arrangements).
1402 place_rect(params2
, grid
, r
);
1410 * We have now constructed a grid of the size specified in
1411 * params2. Now we extend it into a grid of the size specified
1412 * in params. We do this in two passes: we extend it vertically
1413 * until it's the right height, then we transpose it, then
1414 * extend it vertically again (getting it effectively the right
1415 * width), then finally transpose again.
1417 for (i
= 0; i
< 2; i
++) {
1418 int *grid2
, *expand
, *where
;
1419 game_params params3real
, *params3
= ¶ms3real
;
1421 #ifdef GENERATION_DIAGNOSTICS
1422 printf("before expansion:\n");
1423 display_grid(params2
, grid
, NULL
, TRUE
);
1427 * Set up the new grid.
1429 grid2
= snewn(params2
->w
* params
->h
, int);
1430 expand
= snewn(params2
->h
-1, int);
1431 where
= snewn(params2
->w
, int);
1432 params3
->w
= params2
->w
;
1433 params3
->h
= params
->h
;
1436 * Decide which horizontal edges are going to get expanded,
1439 for (y
= 0; y
< params2
->h
-1; y
++)
1441 for (y
= params2
->h
; y
< params
->h
; y
++) {
1442 x
= random_upto(rs
, params2
->h
-1);
1446 #ifdef GENERATION_DIAGNOSTICS
1447 printf("expand[] = {");
1448 for (y
= 0; y
< params2
->h
-1; y
++)
1449 printf(" %d", expand
[y
]);
1454 * Perform the expansion. The way this works is that we
1457 * - copy a row from grid into grid2
1459 * - invent some number of additional rows in grid2 where
1460 * there was previously only a horizontal line between
1461 * rows in grid, and make random decisions about where
1462 * among these to place each rectangle edge that ran
1465 for (y
= y2
= y2last
= 0; y
< params2
->h
; y
++) {
1467 * Copy a single line from row y of grid into row y2 of
1470 for (x
= 0; x
< params2
->w
; x
++) {
1471 int val
= index(params2
, grid
, x
, y
);
1472 if (val
/ params2
->w
== y
&& /* rect starts on this line */
1473 (y2
== 0 || /* we're at the very top, or... */
1474 index(params3
, grid2
, x
, y2
-1) / params3
->w
< y2last
1475 /* this rect isn't already started */))
1476 index(params3
, grid2
, x
, y2
) =
1477 INDEX(params3
, val
% params2
->w
, y2
);
1479 index(params3
, grid2
, x
, y2
) =
1480 index(params3
, grid2
, x
, y2
-1);
1484 * If that was the last line, terminate the loop early.
1486 if (++y2
== params3
->h
)
1492 * Invent some number of additional lines. First walk
1493 * along this line working out where to put all the
1494 * edges that coincide with it.
1497 for (x
= 0; x
< params2
->w
; x
++) {
1498 if (index(params2
, grid
, x
, y
) !=
1499 index(params2
, grid
, x
, y
+1)) {
1501 * This is a horizontal edge, so it needs
1505 (index(params2
, grid
, x
-1, y
) !=
1506 index(params2
, grid
, x
, y
) &&
1507 index(params2
, grid
, x
-1, y
+1) !=
1508 index(params2
, grid
, x
, y
+1))) {
1510 * Here we have the chance to make a new
1513 yx
= random_upto(rs
, expand
[y
]+1);
1516 * Here we just reuse the previous value of
1525 for (yx
= 0; yx
< expand
[y
]; yx
++) {
1527 * Invent a single row. For each square in the row,
1528 * we copy the grid entry from the square above it,
1529 * unless we're starting the new rectangle here.
1531 for (x
= 0; x
< params2
->w
; x
++) {
1532 if (yx
== where
[x
]) {
1533 int val
= index(params2
, grid
, x
, y
+1);
1535 val
= INDEX(params3
, val
, y2
);
1536 index(params3
, grid2
, x
, y2
) = val
;
1538 index(params3
, grid2
, x
, y2
) =
1539 index(params3
, grid2
, x
, y2
-1);
1549 #ifdef GENERATION_DIAGNOSTICS
1550 printf("after expansion:\n");
1551 display_grid(params3
, grid2
, NULL
, TRUE
);
1556 params2
->w
= params3
->h
;
1557 params2
->h
= params3
->w
;
1559 grid
= snewn(params2
->w
* params2
->h
, int);
1560 for (x
= 0; x
< params2
->w
; x
++)
1561 for (y
= 0; y
< params2
->h
; y
++) {
1562 int idx1
= INDEX(params2
, x
, y
);
1563 int idx2
= INDEX(params3
, y
, x
);
1567 tmp
= (tmp
% params3
->w
) * params2
->w
+ (tmp
/ params3
->w
);
1576 params
->w
= params
->h
;
1580 #ifdef GENERATION_DIAGNOSTICS
1581 printf("after transposition:\n");
1582 display_grid(params2
, grid
, NULL
, TRUE
);
1587 * Run the solver to narrow down the possible number
1591 struct numberdata
*nd
;
1592 int nnumbers
, i
, ret
;
1594 /* Count the rectangles. */
1596 for (y
= 0; y
< params
->h
; y
++) {
1597 for (x
= 0; x
< params
->w
; x
++) {
1598 int idx
= INDEX(params
, x
, y
);
1599 if (index(params
, grid
, x
, y
) == idx
)
1604 nd
= snewn(nnumbers
, struct numberdata
);
1606 /* Now set up each number's candidate position list. */
1608 for (y
= 0; y
< params
->h
; y
++) {
1609 for (x
= 0; x
< params
->w
; x
++) {
1610 int idx
= INDEX(params
, x
, y
);
1611 if (index(params
, grid
, x
, y
) == idx
) {
1612 struct rect r
= find_rect(params
, grid
, x
, y
);
1615 nd
[i
].area
= r
.w
* r
.h
;
1616 nd
[i
].npoints
= nd
[i
].area
;
1617 nd
[i
].points
= snewn(nd
[i
].npoints
, struct point
);
1619 for (j
= 0; j
< r
.h
; j
++)
1620 for (k
= 0; k
< r
.w
; k
++) {
1621 nd
[i
].points
[m
].x
= k
+ r
.x
;
1622 nd
[i
].points
[m
].y
= j
+ r
.y
;
1625 assert(m
== nd
[i
].npoints
);
1633 ret
= rect_solver(params
->w
, params
->h
, nnumbers
, nd
,
1636 ret
= TRUE
; /* allow any number placement at all */
1640 * Now place the numbers according to the solver's
1643 numbers
= snewn(params
->w
* params
->h
, int);
1645 for (y
= 0; y
< params
->h
; y
++)
1646 for (x
= 0; x
< params
->w
; x
++) {
1647 index(params
, numbers
, x
, y
) = 0;
1650 for (i
= 0; i
< nnumbers
; i
++) {
1651 int idx
= random_upto(rs
, nd
[i
].npoints
);
1652 int x
= nd
[i
].points
[idx
].x
;
1653 int y
= nd
[i
].points
[idx
].y
;
1654 index(params
,numbers
,x
,y
) = nd
[i
].area
;
1661 for (i
= 0; i
< nnumbers
; i
++)
1662 sfree(nd
[i
].points
);
1666 * If we've succeeded, then terminate the loop.
1673 * Give up and go round again.
1679 * Store the solution in aux.
1685 len
= 2 + (params
->w
-1)*params
->h
+ (params
->h
-1)*params
->w
;
1686 ai
= snewn(len
, char);
1692 for (y
= 0; y
< params
->h
; y
++)
1693 for (x
= 1; x
< params
->w
; x
++)
1694 *p
++ = (index(params
, grid
, x
, y
) !=
1695 index(params
, grid
, x
-1, y
) ?
'1' : '0');
1697 for (y
= 1; y
< params
->h
; y
++)
1698 for (x
= 0; x
< params
->w
; x
++)
1699 *p
++ = (index(params
, grid
, x
, y
) !=
1700 index(params
, grid
, x
, y
-1) ?
'1' : '0');
1702 assert(p
- ai
== len
-1);
1708 #ifdef GENERATION_DIAGNOSTICS
1709 display_grid(params
, grid
, numbers
, FALSE
);
1712 desc
= snewn(11 * params
->w
* params
->h
, char);
1715 for (i
= 0; i
<= params
->w
* params
->h
; i
++) {
1716 int n
= (i
< params
->w
* params
->h ? numbers
[i
] : -1);
1723 int c
= 'a' - 1 + run
;
1727 run
-= c
- ('a' - 1);
1731 * If there's a number in the very top left or
1732 * bottom right, there's no point putting an
1733 * unnecessary _ before or after it.
1735 if (p
> desc
&& n
> 0)
1739 p
+= sprintf(p
, "%d", n
);
1751 static char *validate_desc(game_params
*params
, char *desc
)
1753 int area
= params
->w
* params
->h
;
1758 if (n
>= 'a' && n
<= 'z') {
1759 squares
+= n
- 'a' + 1;
1760 } else if (n
== '_') {
1762 } else if (n
> '0' && n
<= '9') {
1764 while (*desc
>= '0' && *desc
<= '9')
1767 return "Invalid character in game description";
1771 return "Not enough data to fill grid";
1774 return "Too much data to fit in grid";
1779 static unsigned char *get_correct(game_state
*state
)
1784 ret
= snewn(state
->w
* state
->h
, unsigned char);
1785 memset(ret
, 0xFF, state
->w
* state
->h
);
1787 for (x
= 0; x
< state
->w
; x
++)
1788 for (y
= 0; y
< state
->h
; y
++)
1789 if (index(state
,ret
,x
,y
) == 0xFF) {
1792 int num
, area
, valid
;
1795 * Find a rectangle starting at this point.
1798 while (x
+rw
< state
->w
&& !vedge(state
,x
+rw
,y
))
1801 while (y
+rh
< state
->h
&& !hedge(state
,x
,y
+rh
))
1805 * We know what the dimensions of the rectangle
1806 * should be if it's there at all. Find out if we
1807 * really have a valid rectangle.
1810 /* Check the horizontal edges. */
1811 for (xx
= x
; xx
< x
+rw
; xx
++) {
1812 for (yy
= y
; yy
<= y
+rh
; yy
++) {
1813 int e
= !HRANGE(state
,xx
,yy
) || hedge(state
,xx
,yy
);
1814 int ec
= (yy
== y
|| yy
== y
+rh
);
1819 /* Check the vertical edges. */
1820 for (yy
= y
; yy
< y
+rh
; yy
++) {
1821 for (xx
= x
; xx
<= x
+rw
; xx
++) {
1822 int e
= !VRANGE(state
,xx
,yy
) || vedge(state
,xx
,yy
);
1823 int ec
= (xx
== x
|| xx
== x
+rw
);
1830 * If this is not a valid rectangle with no other
1831 * edges inside it, we just mark this square as not
1832 * complete and proceed to the next square.
1835 index(state
, ret
, x
, y
) = 0;
1840 * We have a rectangle. Now see what its area is,
1841 * and how many numbers are in it.
1845 for (xx
= x
; xx
< x
+rw
; xx
++) {
1846 for (yy
= y
; yy
< y
+rh
; yy
++) {
1848 if (grid(state
,xx
,yy
)) {
1850 valid
= FALSE
; /* two numbers */
1851 num
= grid(state
,xx
,yy
);
1859 * Now fill in the whole rectangle based on the
1862 for (xx
= x
; xx
< x
+rw
; xx
++) {
1863 for (yy
= y
; yy
< y
+rh
; yy
++) {
1864 index(state
, ret
, xx
, yy
) = valid
;
1872 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
1874 game_state
*state
= snew(game_state
);
1877 state
->w
= params
->w
;
1878 state
->h
= params
->h
;
1880 area
= state
->w
* state
->h
;
1882 state
->grid
= snewn(area
, int);
1883 state
->vedge
= snewn(area
, unsigned char);
1884 state
->hedge
= snewn(area
, unsigned char);
1885 state
->completed
= state
->cheated
= FALSE
;
1890 if (n
>= 'a' && n
<= 'z') {
1891 int run
= n
- 'a' + 1;
1892 assert(i
+ run
<= area
);
1894 state
->grid
[i
++] = 0;
1895 } else if (n
== '_') {
1897 } else if (n
> '0' && n
<= '9') {
1899 state
->grid
[i
++] = atoi(desc
-1);
1900 while (*desc
>= '0' && *desc
<= '9')
1903 assert(!"We can't get here");
1908 for (y
= 0; y
< state
->h
; y
++)
1909 for (x
= 0; x
< state
->w
; x
++)
1910 vedge(state
,x
,y
) = hedge(state
,x
,y
) = 0;
1912 state
->correct
= get_correct(state
);
1917 static game_state
*dup_game(game_state
*state
)
1919 game_state
*ret
= snew(game_state
);
1924 ret
->vedge
= snewn(state
->w
* state
->h
, unsigned char);
1925 ret
->hedge
= snewn(state
->w
* state
->h
, unsigned char);
1926 ret
->grid
= snewn(state
->w
* state
->h
, int);
1927 ret
->correct
= snewn(ret
->w
* ret
->h
, unsigned char);
1929 ret
->completed
= state
->completed
;
1930 ret
->cheated
= state
->cheated
;
1932 memcpy(ret
->grid
, state
->grid
, state
->w
* state
->h
* sizeof(int));
1933 memcpy(ret
->vedge
, state
->vedge
, state
->w
*state
->h
*sizeof(unsigned char));
1934 memcpy(ret
->hedge
, state
->hedge
, state
->w
*state
->h
*sizeof(unsigned char));
1936 memcpy(ret
->correct
, state
->correct
, state
->w
*state
->h
*sizeof(unsigned char));
1941 static void free_game(game_state
*state
)
1944 sfree(state
->vedge
);
1945 sfree(state
->hedge
);
1946 sfree(state
->correct
);
1950 static char *solve_game(game_state
*state
, game_state
*currstate
,
1951 char *ai
, char **error
)
1953 unsigned char *vedge
, *hedge
;
1957 struct numberdata
*nd
;
1963 * Attempt the in-built solver.
1966 /* Set up each number's (very short) candidate position list. */
1967 for (i
= n
= 0; i
< state
->h
* state
->w
; i
++)
1971 nd
= snewn(n
, struct numberdata
);
1973 for (i
= j
= 0; i
< state
->h
* state
->w
; i
++)
1974 if (state
->grid
[i
]) {
1975 nd
[j
].area
= state
->grid
[i
];
1977 nd
[j
].points
= snewn(1, struct point
);
1978 nd
[j
].points
[0].x
= i
% state
->w
;
1979 nd
[j
].points
[0].y
= i
/ state
->w
;
1985 vedge
= snewn(state
->w
* state
->h
, unsigned char);
1986 hedge
= snewn(state
->w
* state
->h
, unsigned char);
1987 memset(vedge
, 0, state
->w
* state
->h
);
1988 memset(hedge
, 0, state
->w
* state
->h
);
1990 rect_solver(state
->w
, state
->h
, n
, nd
, hedge
, vedge
, NULL
);
1995 for (i
= 0; i
< n
; i
++)
1996 sfree(nd
[i
].points
);
1999 len
= 2 + (state
->w
-1)*state
->h
+ (state
->h
-1)*state
->w
;
2000 ret
= snewn(len
, char);
2004 for (y
= 0; y
< state
->h
; y
++)
2005 for (x
= 1; x
< state
->w
; x
++)
2006 *p
++ = vedge
[y
*state
->w
+x
] ?
'1' : '0';
2007 for (y
= 1; y
< state
->h
; y
++)
2008 for (x
= 0; x
< state
->w
; x
++)
2009 *p
++ = hedge
[y
*state
->w
+x
] ?
'1' : '0';
2011 assert(p
- ret
== len
);
2019 static char *game_text_format(game_state
*state
)
2021 char *ret
, *p
, buf
[80];
2022 int i
, x
, y
, col
, maxlen
;
2025 * First determine the number of spaces required to display a
2026 * number. We'll use at least two, because one looks a bit
2030 for (i
= 0; i
< state
->w
* state
->h
; i
++) {
2031 x
= sprintf(buf
, "%d", state
->grid
[i
]);
2032 if (col
< x
) col
= x
;
2036 * Now we know the exact total size of the grid we're going to
2037 * produce: it's got 2*h+1 rows, each containing w lots of col,
2038 * w+1 boundary characters and a trailing newline.
2040 maxlen
= (2*state
->h
+1) * (state
->w
* (col
+1) + 2);
2042 ret
= snewn(maxlen
+1, char);
2045 for (y
= 0; y
<= 2*state
->h
; y
++) {
2046 for (x
= 0; x
<= 2*state
->w
; x
++) {
2051 int v
= grid(state
, x
/2, y
/2);
2053 sprintf(buf
, "%*d", col
, v
);
2055 sprintf(buf
, "%*s", col
, "");
2056 memcpy(p
, buf
, col
);
2060 * Display a horizontal edge or nothing.
2062 int h
= (y
==0 || y
==2*state
->h ?
1 :
2063 HRANGE(state
, x
/2, y
/2) && hedge(state
, x
/2, y
/2));
2069 for (i
= 0; i
< col
; i
++)
2073 * Display a vertical edge or nothing.
2075 int v
= (x
==0 || x
==2*state
->w ?
1 :
2076 VRANGE(state
, x
/2, y
/2) && vedge(state
, x
/2, y
/2));
2083 * Display a corner, or a vertical edge, or a
2084 * horizontal edge, or nothing.
2086 int hl
= (y
==0 || y
==2*state
->h ?
1 :
2087 HRANGE(state
, (x
-1)/2, y
/2) && hedge(state
, (x
-1)/2, y
/2));
2088 int hr
= (y
==0 || y
==2*state
->h ?
1 :
2089 HRANGE(state
, (x
+1)/2, y
/2) && hedge(state
, (x
+1)/2, y
/2));
2090 int vu
= (x
==0 || x
==2*state
->w ?
1 :
2091 VRANGE(state
, x
/2, (y
-1)/2) && vedge(state
, x
/2, (y
-1)/2));
2092 int vd
= (x
==0 || x
==2*state
->w ?
1 :
2093 VRANGE(state
, x
/2, (y
+1)/2) && vedge(state
, x
/2, (y
+1)/2));
2094 if (!hl
&& !hr
&& !vu
&& !vd
)
2096 else if (hl
&& hr
&& !vu
&& !vd
)
2098 else if (!hl
&& !hr
&& vu
&& vd
)
2107 assert(p
- ret
== maxlen
);
2114 * These coordinates are 2 times the obvious grid coordinates.
2115 * Hence, the top left of the grid is (0,0), the grid point to
2116 * the right of that is (2,0), the one _below that_ is (2,2)
2117 * and so on. This is so that we can specify a drag start point
2118 * on an edge (one odd coordinate) or in the middle of a square
2119 * (two odd coordinates) rather than always at a corner.
2121 * -1,-1 means no drag is in progress.
2128 * This flag is set as soon as a dragging action moves the
2129 * mouse pointer away from its starting point, so that even if
2130 * the pointer _returns_ to its starting point the action is
2131 * treated as a small drag rather than a click.
2135 * These are the co-ordinates of the top-left and bottom-right squares
2136 * in the drag box, respectively, or -1 otherwise.
2144 static game_ui
*new_ui(game_state
*state
)
2146 game_ui
*ui
= snew(game_ui
);
2147 ui
->drag_start_x
= -1;
2148 ui
->drag_start_y
= -1;
2149 ui
->drag_end_x
= -1;
2150 ui
->drag_end_y
= -1;
2151 ui
->dragged
= FALSE
;
2159 static void free_ui(game_ui
*ui
)
2164 static char *encode_ui(game_ui
*ui
)
2169 static void decode_ui(game_ui
*ui
, char *encoding
)
2173 static void coord_round(float x
, float y
, int *xr
, int *yr
)
2175 float xs
, ys
, xv
, yv
, dx
, dy
, dist
;
2178 * Find the nearest square-centre.
2180 xs
= (float)floor(x
) + 0.5F
;
2181 ys
= (float)floor(y
) + 0.5F
;
2184 * And find the nearest grid vertex.
2186 xv
= (float)floor(x
+ 0.5F
);
2187 yv
= (float)floor(y
+ 0.5F
);
2190 * We allocate clicks in parts of the grid square to either
2191 * corners, edges or square centres, as follows:
2207 * In other words: we measure the square distance (i.e.
2208 * max(dx,dy)) from the click to the nearest corner, and if
2209 * it's within CORNER_TOLERANCE then we return a corner click.
2210 * We measure the square distance from the click to the nearest
2211 * centre, and if that's within CENTRE_TOLERANCE we return a
2212 * centre click. Failing that, we find which of the two edge
2213 * centres is nearer to the click and return that edge.
2217 * Check for corner click.
2219 dx
= (float)fabs(x
- xv
);
2220 dy
= (float)fabs(y
- yv
);
2221 dist
= (dx
> dy ? dx
: dy
);
2222 if (dist
< CORNER_TOLERANCE
) {
2227 * Check for centre click.
2229 dx
= (float)fabs(x
- xs
);
2230 dy
= (float)fabs(y
- ys
);
2231 dist
= (dx
> dy ? dx
: dy
);
2232 if (dist
< CENTRE_TOLERANCE
) {
2233 *xr
= 1 + 2 * (int)xs
;
2234 *yr
= 1 + 2 * (int)ys
;
2237 * Failing both of those, see which edge we're closer to.
2238 * Conveniently, this is simply done by testing the relative
2239 * magnitude of dx and dy (which are currently distances from
2240 * the square centre).
2243 /* Vertical edge: x-coord of corner,
2244 * y-coord of square centre. */
2246 *yr
= 1 + 2 * (int)floor(ys
);
2248 /* Horizontal edge: x-coord of square centre,
2249 * y-coord of corner. */
2250 *xr
= 1 + 2 * (int)floor(xs
);
2258 * Returns TRUE if it has made any change to the grid.
2260 static int grid_draw_rect(game_state
*state
,
2261 unsigned char *hedge
, unsigned char *vedge
,
2263 int x1
, int y1
, int x2
, int y2
)
2266 int changed
= FALSE
;
2269 * Draw horizontal edges of rectangles.
2271 for (x
= x1
; x
< x2
; x
++)
2272 for (y
= y1
; y
<= y2
; y
++)
2273 if (HRANGE(state
,x
,y
)) {
2274 int val
= index(state
,hedge
,x
,y
);
2275 if (y
== y1
|| y
== y2
)
2279 changed
= changed
|| (index(state
,hedge
,x
,y
) != val
);
2281 index(state
,hedge
,x
,y
) = val
;
2285 * Draw vertical edges of rectangles.
2287 for (y
= y1
; y
< y2
; y
++)
2288 for (x
= x1
; x
<= x2
; x
++)
2289 if (VRANGE(state
,x
,y
)) {
2290 int val
= index(state
,vedge
,x
,y
);
2291 if (x
== x1
|| x
== x2
)
2295 changed
= changed
|| (index(state
,vedge
,x
,y
) != val
);
2297 index(state
,vedge
,x
,y
) = val
;
2303 static int ui_draw_rect(game_state
*state
, game_ui
*ui
,
2304 unsigned char *hedge
, unsigned char *vedge
, int c
,
2307 return grid_draw_rect(state
, hedge
, vedge
, c
, really
,
2308 ui
->x1
, ui
->y1
, ui
->x2
, ui
->y2
);
2311 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2312 game_state
*newstate
)
2316 struct game_drawstate
{
2319 unsigned long *visible
;
2322 static char *interpret_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
2323 int x
, int y
, int button
)
2326 int startdrag
= FALSE
, enddrag
= FALSE
, active
= FALSE
;
2329 button
&= ~MOD_MASK
;
2331 if (button
== LEFT_BUTTON
) {
2333 } else if (button
== LEFT_RELEASE
) {
2335 } else if (button
!= LEFT_DRAG
) {
2339 coord_round(FROMCOORD((float)x
), FROMCOORD((float)y
), &xc
, &yc
);
2342 xc
>= 0 && xc
<= 2*from
->w
&&
2343 yc
>= 0 && yc
<= 2*from
->h
) {
2345 ui
->drag_start_x
= xc
;
2346 ui
->drag_start_y
= yc
;
2347 ui
->drag_end_x
= xc
;
2348 ui
->drag_end_y
= yc
;
2349 ui
->dragged
= FALSE
;
2353 if (ui
->drag_start_x
>= 0 &&
2354 (xc
!= ui
->drag_end_x
|| yc
!= ui
->drag_end_y
)) {
2357 ui
->drag_end_x
= xc
;
2358 ui
->drag_end_y
= yc
;
2362 if (xc
>= 0 && xc
<= 2*from
->w
&&
2363 yc
>= 0 && yc
<= 2*from
->h
) {
2364 ui
->x1
= ui
->drag_start_x
;
2365 ui
->x2
= ui
->drag_end_x
;
2366 if (ui
->x2
< ui
->x1
) { t
= ui
->x1
; ui
->x1
= ui
->x2
; ui
->x2
= t
; }
2368 ui
->y1
= ui
->drag_start_y
;
2369 ui
->y2
= ui
->drag_end_y
;
2370 if (ui
->y2
< ui
->y1
) { t
= ui
->y1
; ui
->y1
= ui
->y2
; ui
->y2
= t
; }
2372 ui
->x1
= ui
->x1
/ 2; /* rounds down */
2373 ui
->x2
= (ui
->x2
+1) / 2; /* rounds up */
2374 ui
->y1
= ui
->y1
/ 2; /* rounds down */
2375 ui
->y2
= (ui
->y2
+1) / 2; /* rounds up */
2386 if (enddrag
&& (ui
->drag_start_x
>= 0)) {
2387 if (xc
>= 0 && xc
<= 2*from
->w
&&
2388 yc
>= 0 && yc
<= 2*from
->h
) {
2391 if (ui_draw_rect(from
, ui
, from
->hedge
,
2392 from
->vedge
, 1, FALSE
)) {
2393 sprintf(buf
, "R%d,%d,%d,%d",
2394 ui
->x1
, ui
->y1
, ui
->x2
- ui
->x1
, ui
->y2
- ui
->y1
);
2398 if ((xc
& 1) && !(yc
& 1) && HRANGE(from
,xc
/2,yc
/2)) {
2399 sprintf(buf
, "H%d,%d", xc
/2, yc
/2);
2402 if ((yc
& 1) && !(xc
& 1) && VRANGE(from
,xc
/2,yc
/2)) {
2403 sprintf(buf
, "V%d,%d", xc
/2, yc
/2);
2409 ui
->drag_start_x
= -1;
2410 ui
->drag_start_y
= -1;
2411 ui
->drag_end_x
= -1;
2412 ui
->drag_end_y
= -1;
2417 ui
->dragged
= FALSE
;
2422 return ret
; /* a move has been made */
2424 return ""; /* UI activity has occurred */
2429 static game_state
*execute_move(game_state
*from
, char *move
)
2432 int x1
, y1
, x2
, y2
, mode
;
2434 if (move
[0] == 'S') {
2438 ret
= dup_game(from
);
2439 ret
->cheated
= TRUE
;
2441 for (y
= 0; y
< ret
->h
; y
++)
2442 for (x
= 1; x
< ret
->w
; x
++) {
2443 vedge(ret
, x
, y
) = (*p
== '1');
2446 for (y
= 1; y
< ret
->h
; y
++)
2447 for (x
= 0; x
< ret
->w
; x
++) {
2448 hedge(ret
, x
, y
) = (*p
== '1');
2452 sfree(ret
->correct
);
2453 ret
->correct
= get_correct(ret
);
2457 } else if (move
[0] == 'R' &&
2458 sscanf(move
+1, "%d,%d,%d,%d", &x1
, &y1
, &x2
, &y2
) == 4 &&
2459 x1
>= 0 && x2
>= 0 && x1
+x2
<= from
->w
&&
2460 y1
>= 0 && y2
>= 0 && y1
+y2
<= from
->h
) {
2464 } else if ((move
[0] == 'H' || move
[0] == 'V') &&
2465 sscanf(move
+1, "%d,%d", &x1
, &y1
) == 2 &&
2466 (move
[0] == 'H' ?
HRANGE(from
, x1
, y1
) :
2467 VRANGE(from
, x1
, y1
))) {
2470 return NULL
; /* can't parse move string */
2472 ret
= dup_game(from
);
2475 grid_draw_rect(ret
, ret
->hedge
, ret
->vedge
, 1, TRUE
, x1
, y1
, x2
, y2
);
2476 } else if (mode
== 'H') {
2477 hedge(ret
,x1
,y1
) = !hedge(ret
,x1
,y1
);
2478 } else if (mode
== 'V') {
2479 vedge(ret
,x1
,y1
) = !vedge(ret
,x1
,y1
);
2482 sfree(ret
->correct
);
2483 ret
->correct
= get_correct(ret
);
2486 * We've made a real change to the grid. Check to see
2487 * if the game has been completed.
2489 if (!ret
->completed
) {
2493 for (x
= 0; x
< ret
->w
; x
++)
2494 for (y
= 0; y
< ret
->h
; y
++)
2495 if (!index(ret
, ret
->correct
, x
, y
))
2499 ret
->completed
= TRUE
;
2505 /* ----------------------------------------------------------------------
2509 #define CORRECT (1L<<16)
2511 #define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG )
2512 #define MAX4(x,y,z,w) ( max(max(x,y),max(z,w)) )
2514 static void game_compute_size(game_params
*params
, int tilesize
,
2517 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2518 struct { int tilesize
; } ads
, *ds
= &ads
;
2519 ads
.tilesize
= tilesize
;
2521 *x
= params
->w
* TILE_SIZE
+ 2*BORDER
+ 1;
2522 *y
= params
->h
* TILE_SIZE
+ 2*BORDER
+ 1;
2525 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
2526 game_params
*params
, int tilesize
)
2528 ds
->tilesize
= tilesize
;
2531 static float *game_colours(frontend
*fe
, int *ncolours
)
2533 float *ret
= snewn(3 * NCOLOURS
, float);
2535 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2537 ret
[COL_GRID
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
2538 ret
[COL_GRID
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
2539 ret
[COL_GRID
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
2541 ret
[COL_DRAG
* 3 + 0] = 1.0F
;
2542 ret
[COL_DRAG
* 3 + 1] = 0.0F
;
2543 ret
[COL_DRAG
* 3 + 2] = 0.0F
;
2545 ret
[COL_CORRECT
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
2546 ret
[COL_CORRECT
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
2547 ret
[COL_CORRECT
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
2549 ret
[COL_LINE
* 3 + 0] = 0.0F
;
2550 ret
[COL_LINE
* 3 + 1] = 0.0F
;
2551 ret
[COL_LINE
* 3 + 2] = 0.0F
;
2553 ret
[COL_TEXT
* 3 + 0] = 0.0F
;
2554 ret
[COL_TEXT
* 3 + 1] = 0.0F
;
2555 ret
[COL_TEXT
* 3 + 2] = 0.0F
;
2557 *ncolours
= NCOLOURS
;
2561 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
2563 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2566 ds
->started
= FALSE
;
2569 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned long);
2570 ds
->tilesize
= 0; /* not decided yet */
2571 for (i
= 0; i
< ds
->w
* ds
->h
; i
++)
2572 ds
->visible
[i
] = 0xFFFF;
2577 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2583 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, game_state
*state
,
2584 int x
, int y
, unsigned char *hedge
, unsigned char *vedge
,
2585 unsigned char *corners
, int correct
)
2587 int cx
= COORD(x
), cy
= COORD(y
);
2590 draw_rect(dr
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1, COL_GRID
);
2591 draw_rect(dr
, cx
+1, cy
+1, TILE_SIZE
-1, TILE_SIZE
-1,
2592 correct ? COL_CORRECT
: COL_BACKGROUND
);
2594 if (grid(state
,x
,y
)) {
2595 sprintf(str
, "%d", grid(state
,x
,y
));
2596 draw_text(dr
, cx
+TILE_SIZE
/2, cy
+TILE_SIZE
/2, FONT_VARIABLE
,
2597 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
, COL_TEXT
, str
);
2603 if (!HRANGE(state
,x
,y
) || index(state
,hedge
,x
,y
))
2604 draw_rect(dr
, cx
, cy
, TILE_SIZE
+1, 2,
2605 HRANGE(state
,x
,y
) ?
COLOUR(index(state
,hedge
,x
,y
)) :
2607 if (!HRANGE(state
,x
,y
+1) || index(state
,hedge
,x
,y
+1))
2608 draw_rect(dr
, cx
, cy
+TILE_SIZE
-1, TILE_SIZE
+1, 2,
2609 HRANGE(state
,x
,y
+1) ?
COLOUR(index(state
,hedge
,x
,y
+1)) :
2611 if (!VRANGE(state
,x
,y
) || index(state
,vedge
,x
,y
))
2612 draw_rect(dr
, cx
, cy
, 2, TILE_SIZE
+1,
2613 VRANGE(state
,x
,y
) ?
COLOUR(index(state
,vedge
,x
,y
)) :
2615 if (!VRANGE(state
,x
+1,y
) || index(state
,vedge
,x
+1,y
))
2616 draw_rect(dr
, cx
+TILE_SIZE
-1, cy
, 2, TILE_SIZE
+1,
2617 VRANGE(state
,x
+1,y
) ?
COLOUR(index(state
,vedge
,x
+1,y
)) :
2623 if (index(state
,corners
,x
,y
))
2624 draw_rect(dr
, cx
, cy
, 2, 2,
2625 COLOUR(index(state
,corners
,x
,y
)));
2626 if (x
+1 < state
->w
&& index(state
,corners
,x
+1,y
))
2627 draw_rect(dr
, cx
+TILE_SIZE
-1, cy
, 2, 2,
2628 COLOUR(index(state
,corners
,x
+1,y
)));
2629 if (y
+1 < state
->h
&& index(state
,corners
,x
,y
+1))
2630 draw_rect(dr
, cx
, cy
+TILE_SIZE
-1, 2, 2,
2631 COLOUR(index(state
,corners
,x
,y
+1)));
2632 if (x
+1 < state
->w
&& y
+1 < state
->h
&& index(state
,corners
,x
+1,y
+1))
2633 draw_rect(dr
, cx
+TILE_SIZE
-1, cy
+TILE_SIZE
-1, 2, 2,
2634 COLOUR(index(state
,corners
,x
+1,y
+1)));
2636 draw_update(dr
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1);
2639 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
2640 game_state
*state
, int dir
, game_ui
*ui
,
2641 float animtime
, float flashtime
)
2644 unsigned char *hedge
, *vedge
, *corners
;
2647 hedge
= snewn(state
->w
*state
->h
, unsigned char);
2648 vedge
= snewn(state
->w
*state
->h
, unsigned char);
2649 memcpy(hedge
, state
->hedge
, state
->w
*state
->h
);
2650 memcpy(vedge
, state
->vedge
, state
->w
*state
->h
);
2651 ui_draw_rect(state
, ui
, hedge
, vedge
, 2, TRUE
);
2653 hedge
= state
->hedge
;
2654 vedge
= state
->vedge
;
2657 corners
= snewn(state
->w
* state
->h
, unsigned char);
2658 memset(corners
, 0, state
->w
* state
->h
);
2659 for (x
= 0; x
< state
->w
; x
++)
2660 for (y
= 0; y
< state
->h
; y
++) {
2662 int e
= index(state
, vedge
, x
, y
);
2663 if (index(state
,corners
,x
,y
) < e
)
2664 index(state
,corners
,x
,y
) = e
;
2665 if (y
+1 < state
->h
&&
2666 index(state
,corners
,x
,y
+1) < e
)
2667 index(state
,corners
,x
,y
+1) = e
;
2670 int e
= index(state
, hedge
, x
, y
);
2671 if (index(state
,corners
,x
,y
) < e
)
2672 index(state
,corners
,x
,y
) = e
;
2673 if (x
+1 < state
->w
&&
2674 index(state
,corners
,x
+1,y
) < e
)
2675 index(state
,corners
,x
+1,y
) = e
;
2681 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2682 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1, COL_BACKGROUND
);
2683 draw_rect(dr
, COORD(0)-1, COORD(0)-1,
2684 ds
->w
*TILE_SIZE
+3, ds
->h
*TILE_SIZE
+3, COL_LINE
);
2686 draw_update(dr
, 0, 0,
2687 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2688 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
2691 for (x
= 0; x
< state
->w
; x
++)
2692 for (y
= 0; y
< state
->h
; y
++) {
2693 unsigned long c
= 0;
2695 if (HRANGE(state
,x
,y
))
2696 c
|= index(state
,hedge
,x
,y
);
2697 if (HRANGE(state
,x
,y
+1))
2698 c
|= index(state
,hedge
,x
,y
+1) << 2;
2699 if (VRANGE(state
,x
,y
))
2700 c
|= index(state
,vedge
,x
,y
) << 4;
2701 if (VRANGE(state
,x
+1,y
))
2702 c
|= index(state
,vedge
,x
+1,y
) << 6;
2703 c
|= index(state
,corners
,x
,y
) << 8;
2705 c
|= index(state
,corners
,x
+1,y
) << 10;
2707 c
|= index(state
,corners
,x
,y
+1) << 12;
2708 if (x
+1 < state
->w
&& y
+1 < state
->h
)
2709 /* cast to prevent 2<<14 sign-extending on promotion to long */
2710 c
|= (unsigned long)index(state
,corners
,x
+1,y
+1) << 14;
2711 if (index(state
, state
->correct
, x
, y
) && !flashtime
)
2714 if (index(ds
,ds
->visible
,x
,y
) != c
) {
2715 draw_tile(dr
, ds
, state
, x
, y
, hedge
, vedge
, corners
,
2716 (c
& CORRECT
) ?
1 : 0);
2717 index(ds
,ds
->visible
,x
,y
) = c
;
2724 if (ui
->x1
>= 0 && ui
->y1
>= 0 &&
2725 ui
->x2
>= 0 && ui
->y2
>= 0) {
2726 sprintf(buf
, "%dx%d ",
2734 strcat(buf
, "Auto-solved.");
2735 else if (state
->completed
)
2736 strcat(buf
, "COMPLETED!");
2738 status_bar(dr
, buf
);
2741 if (hedge
!= state
->hedge
) {
2749 static float game_anim_length(game_state
*oldstate
,
2750 game_state
*newstate
, int dir
, game_ui
*ui
)
2755 static float game_flash_length(game_state
*oldstate
,
2756 game_state
*newstate
, int dir
, game_ui
*ui
)
2758 if (!oldstate
->completed
&& newstate
->completed
&&
2759 !oldstate
->cheated
&& !newstate
->cheated
)
2764 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2769 static void game_print_size(game_params
*params
, float *x
, float *y
)
2774 * I'll use 5mm squares by default.
2776 game_compute_size(params
, 500, &pw
, &ph
);
2781 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
2783 int w
= state
->w
, h
= state
->h
;
2784 int ink
= print_mono_colour(dr
, 0);
2787 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2788 game_drawstate ads
, *ds
= &ads
;
2789 game_set_size(dr
, ds
, NULL
, tilesize
);
2794 print_line_width(dr
, TILE_SIZE
/ 10);
2795 draw_rect_outline(dr
, COORD(0), COORD(0), w
*TILE_SIZE
, h
*TILE_SIZE
, ink
);
2798 * Grid. We have to make the grid lines particularly thin,
2799 * because users will be drawing lines _along_ them and we want
2800 * those lines to be visible.
2802 print_line_width(dr
, TILE_SIZE
/ 256);
2803 for (x
= 1; x
< w
; x
++)
2804 draw_line(dr
, COORD(x
), COORD(0), COORD(x
), COORD(h
), ink
);
2805 for (y
= 1; y
< h
; y
++)
2806 draw_line(dr
, COORD(0), COORD(y
), COORD(w
), COORD(y
), ink
);
2811 print_line_width(dr
, TILE_SIZE
/ 10);
2812 for (y
= 0; y
<= h
; y
++)
2813 for (x
= 0; x
<= w
; x
++) {
2814 if (HRANGE(state
,x
,y
) && hedge(state
,x
,y
))
2815 draw_line(dr
, COORD(x
), COORD(y
), COORD(x
+1), COORD(y
), ink
);
2816 if (VRANGE(state
,x
,y
) && vedge(state
,x
,y
))
2817 draw_line(dr
, COORD(x
), COORD(y
), COORD(x
), COORD(y
+1), ink
);
2823 for (y
= 0; y
< h
; y
++)
2824 for (x
= 0; x
< w
; x
++)
2825 if (grid(state
,x
,y
)) {
2827 sprintf(str
, "%d", grid(state
,x
,y
));
2828 draw_text(dr
, COORD(x
)+TILE_SIZE
/2, COORD(y
)+TILE_SIZE
/2,
2829 FONT_VARIABLE
, TILE_SIZE
/2,
2830 ALIGN_HCENTRE
| ALIGN_VCENTRE
, ink
, str
);
2835 #define thegame rect
2838 const struct game thegame
= {
2839 "Rectangles", "games.rectangles",
2846 TRUE
, game_configure
, custom_params
,
2854 TRUE
, game_text_format
,
2862 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2865 game_free_drawstate
,
2869 TRUE
, FALSE
, game_print_size
, game_print
,
2870 TRUE
, /* wants_statusbar */
2871 FALSE
, game_timing_state
,