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);
1352 if (r1
.h
> 0 && r1
.w
> 0)
1353 place_rect(params2
, grid
, r1
);
1354 place_rect(params2
, grid
, r2
);
1358 * Sanity-check that there really is a 3x3
1359 * rectangle surrounding this singleton and it
1360 * contains absolutely everything we could
1365 assert(x
> 0 && x
< params2
->w
-1);
1366 assert(y
> 0 && y
< params2
->h
-1);
1368 for (xx
= x
-1; xx
<= x
+1; xx
++)
1369 for (yy
= y
-1; yy
<= y
+1; yy
++) {
1370 struct rect r
= find_rect(params2
,grid
,xx
,yy
);
1373 assert(r
.x
+r
.w
-1 <= x
+1);
1374 assert(r
.y
+r
.h
-1 <= y
+1);
1379 #ifdef GENERATION_DIAGNOSTICS
1380 printf("need the 3x3 trick\n");
1384 * FIXME: If the maximum rectangle area for
1385 * this grid is less than 9, we ought to
1386 * subdivide the 3x3 in some fashion. There are
1387 * five other possibilities:
1390 * - a 4, a 3 and a 2
1392 * - a 3 and three 2s (two different arrangements).
1400 place_rect(params2
, grid
, r
);
1408 * We have now constructed a grid of the size specified in
1409 * params2. Now we extend it into a grid of the size specified
1410 * in params. We do this in two passes: we extend it vertically
1411 * until it's the right height, then we transpose it, then
1412 * extend it vertically again (getting it effectively the right
1413 * width), then finally transpose again.
1415 for (i
= 0; i
< 2; i
++) {
1416 int *grid2
, *expand
, *where
;
1417 game_params params3real
, *params3
= ¶ms3real
;
1419 #ifdef GENERATION_DIAGNOSTICS
1420 printf("before expansion:\n");
1421 display_grid(params2
, grid
, NULL
, TRUE
);
1425 * Set up the new grid.
1427 grid2
= snewn(params2
->w
* params
->h
, int);
1428 expand
= snewn(params2
->h
-1, int);
1429 where
= snewn(params2
->w
, int);
1430 params3
->w
= params2
->w
;
1431 params3
->h
= params
->h
;
1434 * Decide which horizontal edges are going to get expanded,
1437 for (y
= 0; y
< params2
->h
-1; y
++)
1439 for (y
= params2
->h
; y
< params
->h
; y
++) {
1440 x
= random_upto(rs
, params2
->h
-1);
1444 #ifdef GENERATION_DIAGNOSTICS
1445 printf("expand[] = {");
1446 for (y
= 0; y
< params2
->h
-1; y
++)
1447 printf(" %d", expand
[y
]);
1452 * Perform the expansion. The way this works is that we
1455 * - copy a row from grid into grid2
1457 * - invent some number of additional rows in grid2 where
1458 * there was previously only a horizontal line between
1459 * rows in grid, and make random decisions about where
1460 * among these to place each rectangle edge that ran
1463 for (y
= y2
= y2last
= 0; y
< params2
->h
; y
++) {
1465 * Copy a single line from row y of grid into row y2 of
1468 for (x
= 0; x
< params2
->w
; x
++) {
1469 int val
= index(params2
, grid
, x
, y
);
1470 if (val
/ params2
->w
== y
&& /* rect starts on this line */
1471 (y2
== 0 || /* we're at the very top, or... */
1472 index(params3
, grid2
, x
, y2
-1) / params3
->w
< y2last
1473 /* this rect isn't already started */))
1474 index(params3
, grid2
, x
, y2
) =
1475 INDEX(params3
, val
% params2
->w
, y2
);
1477 index(params3
, grid2
, x
, y2
) =
1478 index(params3
, grid2
, x
, y2
-1);
1482 * If that was the last line, terminate the loop early.
1484 if (++y2
== params3
->h
)
1490 * Invent some number of additional lines. First walk
1491 * along this line working out where to put all the
1492 * edges that coincide with it.
1495 for (x
= 0; x
< params2
->w
; x
++) {
1496 if (index(params2
, grid
, x
, y
) !=
1497 index(params2
, grid
, x
, y
+1)) {
1499 * This is a horizontal edge, so it needs
1503 (index(params2
, grid
, x
-1, y
) !=
1504 index(params2
, grid
, x
, y
) &&
1505 index(params2
, grid
, x
-1, y
+1) !=
1506 index(params2
, grid
, x
, y
+1))) {
1508 * Here we have the chance to make a new
1511 yx
= random_upto(rs
, expand
[y
]+1);
1514 * Here we just reuse the previous value of
1523 for (yx
= 0; yx
< expand
[y
]; yx
++) {
1525 * Invent a single row. For each square in the row,
1526 * we copy the grid entry from the square above it,
1527 * unless we're starting the new rectangle here.
1529 for (x
= 0; x
< params2
->w
; x
++) {
1530 if (yx
== where
[x
]) {
1531 int val
= index(params2
, grid
, x
, y
+1);
1533 val
= INDEX(params3
, val
, y2
);
1534 index(params3
, grid2
, x
, y2
) = val
;
1536 index(params3
, grid2
, x
, y2
) =
1537 index(params3
, grid2
, x
, y2
-1);
1547 #ifdef GENERATION_DIAGNOSTICS
1548 printf("after expansion:\n");
1549 display_grid(params3
, grid2
, NULL
, TRUE
);
1554 params2
->w
= params3
->h
;
1555 params2
->h
= params3
->w
;
1557 grid
= snewn(params2
->w
* params2
->h
, int);
1558 for (x
= 0; x
< params2
->w
; x
++)
1559 for (y
= 0; y
< params2
->h
; y
++) {
1560 int idx1
= INDEX(params2
, x
, y
);
1561 int idx2
= INDEX(params3
, y
, x
);
1565 tmp
= (tmp
% params3
->w
) * params2
->w
+ (tmp
/ params3
->w
);
1574 params
->w
= params
->h
;
1578 #ifdef GENERATION_DIAGNOSTICS
1579 printf("after transposition:\n");
1580 display_grid(params2
, grid
, NULL
, TRUE
);
1585 * Run the solver to narrow down the possible number
1589 struct numberdata
*nd
;
1590 int nnumbers
, i
, ret
;
1592 /* Count the rectangles. */
1594 for (y
= 0; y
< params
->h
; y
++) {
1595 for (x
= 0; x
< params
->w
; x
++) {
1596 int idx
= INDEX(params
, x
, y
);
1597 if (index(params
, grid
, x
, y
) == idx
)
1602 nd
= snewn(nnumbers
, struct numberdata
);
1604 /* Now set up each number's candidate position list. */
1606 for (y
= 0; y
< params
->h
; y
++) {
1607 for (x
= 0; x
< params
->w
; x
++) {
1608 int idx
= INDEX(params
, x
, y
);
1609 if (index(params
, grid
, x
, y
) == idx
) {
1610 struct rect r
= find_rect(params
, grid
, x
, y
);
1613 nd
[i
].area
= r
.w
* r
.h
;
1614 nd
[i
].npoints
= nd
[i
].area
;
1615 nd
[i
].points
= snewn(nd
[i
].npoints
, struct point
);
1617 for (j
= 0; j
< r
.h
; j
++)
1618 for (k
= 0; k
< r
.w
; k
++) {
1619 nd
[i
].points
[m
].x
= k
+ r
.x
;
1620 nd
[i
].points
[m
].y
= j
+ r
.y
;
1623 assert(m
== nd
[i
].npoints
);
1631 ret
= rect_solver(params
->w
, params
->h
, nnumbers
, nd
,
1634 ret
= TRUE
; /* allow any number placement at all */
1638 * Now place the numbers according to the solver's
1641 numbers
= snewn(params
->w
* params
->h
, int);
1643 for (y
= 0; y
< params
->h
; y
++)
1644 for (x
= 0; x
< params
->w
; x
++) {
1645 index(params
, numbers
, x
, y
) = 0;
1648 for (i
= 0; i
< nnumbers
; i
++) {
1649 int idx
= random_upto(rs
, nd
[i
].npoints
);
1650 int x
= nd
[i
].points
[idx
].x
;
1651 int y
= nd
[i
].points
[idx
].y
;
1652 index(params
,numbers
,x
,y
) = nd
[i
].area
;
1659 for (i
= 0; i
< nnumbers
; i
++)
1660 sfree(nd
[i
].points
);
1664 * If we've succeeded, then terminate the loop.
1671 * Give up and go round again.
1677 * Store the solution in aux.
1683 len
= 2 + (params
->w
-1)*params
->h
+ (params
->h
-1)*params
->w
;
1684 ai
= snewn(len
, char);
1690 for (y
= 0; y
< params
->h
; y
++)
1691 for (x
= 1; x
< params
->w
; x
++)
1692 *p
++ = (index(params
, grid
, x
, y
) !=
1693 index(params
, grid
, x
-1, y
) ?
'1' : '0');
1695 for (y
= 1; y
< params
->h
; y
++)
1696 for (x
= 0; x
< params
->w
; x
++)
1697 *p
++ = (index(params
, grid
, x
, y
) !=
1698 index(params
, grid
, x
, y
-1) ?
'1' : '0');
1700 assert(p
- ai
== len
-1);
1706 #ifdef GENERATION_DIAGNOSTICS
1707 display_grid(params
, grid
, numbers
, FALSE
);
1710 desc
= snewn(11 * params
->w
* params
->h
, char);
1713 for (i
= 0; i
<= params
->w
* params
->h
; i
++) {
1714 int n
= (i
< params
->w
* params
->h ? numbers
[i
] : -1);
1721 int c
= 'a' - 1 + run
;
1725 run
-= c
- ('a' - 1);
1729 * If there's a number in the very top left or
1730 * bottom right, there's no point putting an
1731 * unnecessary _ before or after it.
1733 if (p
> desc
&& n
> 0)
1737 p
+= sprintf(p
, "%d", n
);
1749 static char *validate_desc(game_params
*params
, char *desc
)
1751 int area
= params
->w
* params
->h
;
1756 if (n
>= 'a' && n
<= 'z') {
1757 squares
+= n
- 'a' + 1;
1758 } else if (n
== '_') {
1760 } else if (n
> '0' && n
<= '9') {
1762 while (*desc
>= '0' && *desc
<= '9')
1765 return "Invalid character in game description";
1769 return "Not enough data to fill grid";
1772 return "Too much data to fit in grid";
1777 static unsigned char *get_correct(game_state
*state
)
1782 ret
= snewn(state
->w
* state
->h
, unsigned char);
1783 memset(ret
, 0xFF, state
->w
* state
->h
);
1785 for (x
= 0; x
< state
->w
; x
++)
1786 for (y
= 0; y
< state
->h
; y
++)
1787 if (index(state
,ret
,x
,y
) == 0xFF) {
1790 int num
, area
, valid
;
1793 * Find a rectangle starting at this point.
1796 while (x
+rw
< state
->w
&& !vedge(state
,x
+rw
,y
))
1799 while (y
+rh
< state
->h
&& !hedge(state
,x
,y
+rh
))
1803 * We know what the dimensions of the rectangle
1804 * should be if it's there at all. Find out if we
1805 * really have a valid rectangle.
1808 /* Check the horizontal edges. */
1809 for (xx
= x
; xx
< x
+rw
; xx
++) {
1810 for (yy
= y
; yy
<= y
+rh
; yy
++) {
1811 int e
= !HRANGE(state
,xx
,yy
) || hedge(state
,xx
,yy
);
1812 int ec
= (yy
== y
|| yy
== y
+rh
);
1817 /* Check the vertical edges. */
1818 for (yy
= y
; yy
< y
+rh
; yy
++) {
1819 for (xx
= x
; xx
<= x
+rw
; xx
++) {
1820 int e
= !VRANGE(state
,xx
,yy
) || vedge(state
,xx
,yy
);
1821 int ec
= (xx
== x
|| xx
== x
+rw
);
1828 * If this is not a valid rectangle with no other
1829 * edges inside it, we just mark this square as not
1830 * complete and proceed to the next square.
1833 index(state
, ret
, x
, y
) = 0;
1838 * We have a rectangle. Now see what its area is,
1839 * and how many numbers are in it.
1843 for (xx
= x
; xx
< x
+rw
; xx
++) {
1844 for (yy
= y
; yy
< y
+rh
; yy
++) {
1846 if (grid(state
,xx
,yy
)) {
1848 valid
= FALSE
; /* two numbers */
1849 num
= grid(state
,xx
,yy
);
1857 * Now fill in the whole rectangle based on the
1860 for (xx
= x
; xx
< x
+rw
; xx
++) {
1861 for (yy
= y
; yy
< y
+rh
; yy
++) {
1862 index(state
, ret
, xx
, yy
) = valid
;
1870 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
1872 game_state
*state
= snew(game_state
);
1875 state
->w
= params
->w
;
1876 state
->h
= params
->h
;
1878 area
= state
->w
* state
->h
;
1880 state
->grid
= snewn(area
, int);
1881 state
->vedge
= snewn(area
, unsigned char);
1882 state
->hedge
= snewn(area
, unsigned char);
1883 state
->completed
= state
->cheated
= FALSE
;
1888 if (n
>= 'a' && n
<= 'z') {
1889 int run
= n
- 'a' + 1;
1890 assert(i
+ run
<= area
);
1892 state
->grid
[i
++] = 0;
1893 } else if (n
== '_') {
1895 } else if (n
> '0' && n
<= '9') {
1897 state
->grid
[i
++] = atoi(desc
-1);
1898 while (*desc
>= '0' && *desc
<= '9')
1901 assert(!"We can't get here");
1906 for (y
= 0; y
< state
->h
; y
++)
1907 for (x
= 0; x
< state
->w
; x
++)
1908 vedge(state
,x
,y
) = hedge(state
,x
,y
) = 0;
1910 state
->correct
= get_correct(state
);
1915 static game_state
*dup_game(game_state
*state
)
1917 game_state
*ret
= snew(game_state
);
1922 ret
->vedge
= snewn(state
->w
* state
->h
, unsigned char);
1923 ret
->hedge
= snewn(state
->w
* state
->h
, unsigned char);
1924 ret
->grid
= snewn(state
->w
* state
->h
, int);
1925 ret
->correct
= snewn(ret
->w
* ret
->h
, unsigned char);
1927 ret
->completed
= state
->completed
;
1928 ret
->cheated
= state
->cheated
;
1930 memcpy(ret
->grid
, state
->grid
, state
->w
* state
->h
* sizeof(int));
1931 memcpy(ret
->vedge
, state
->vedge
, state
->w
*state
->h
*sizeof(unsigned char));
1932 memcpy(ret
->hedge
, state
->hedge
, state
->w
*state
->h
*sizeof(unsigned char));
1934 memcpy(ret
->correct
, state
->correct
, state
->w
*state
->h
*sizeof(unsigned char));
1939 static void free_game(game_state
*state
)
1942 sfree(state
->vedge
);
1943 sfree(state
->hedge
);
1944 sfree(state
->correct
);
1948 static char *solve_game(game_state
*state
, game_state
*currstate
,
1949 char *ai
, char **error
)
1951 unsigned char *vedge
, *hedge
;
1955 struct numberdata
*nd
;
1961 * Attempt the in-built solver.
1964 /* Set up each number's (very short) candidate position list. */
1965 for (i
= n
= 0; i
< state
->h
* state
->w
; i
++)
1969 nd
= snewn(n
, struct numberdata
);
1971 for (i
= j
= 0; i
< state
->h
* state
->w
; i
++)
1972 if (state
->grid
[i
]) {
1973 nd
[j
].area
= state
->grid
[i
];
1975 nd
[j
].points
= snewn(1, struct point
);
1976 nd
[j
].points
[0].x
= i
% state
->w
;
1977 nd
[j
].points
[0].y
= i
/ state
->w
;
1983 vedge
= snewn(state
->w
* state
->h
, unsigned char);
1984 hedge
= snewn(state
->w
* state
->h
, unsigned char);
1985 memset(vedge
, 0, state
->w
* state
->h
);
1986 memset(hedge
, 0, state
->w
* state
->h
);
1988 rect_solver(state
->w
, state
->h
, n
, nd
, hedge
, vedge
, NULL
);
1993 for (i
= 0; i
< n
; i
++)
1994 sfree(nd
[i
].points
);
1997 len
= 2 + (state
->w
-1)*state
->h
+ (state
->h
-1)*state
->w
;
1998 ret
= snewn(len
, char);
2002 for (y
= 0; y
< state
->h
; y
++)
2003 for (x
= 1; x
< state
->w
; x
++)
2004 *p
++ = vedge
[y
*state
->w
+x
] ?
'1' : '0';
2005 for (y
= 1; y
< state
->h
; y
++)
2006 for (x
= 0; x
< state
->w
; x
++)
2007 *p
++ = hedge
[y
*state
->w
+x
] ?
'1' : '0';
2009 assert(p
- ret
== len
);
2017 static char *game_text_format(game_state
*state
)
2019 char *ret
, *p
, buf
[80];
2020 int i
, x
, y
, col
, maxlen
;
2023 * First determine the number of spaces required to display a
2024 * number. We'll use at least two, because one looks a bit
2028 for (i
= 0; i
< state
->w
* state
->h
; i
++) {
2029 x
= sprintf(buf
, "%d", state
->grid
[i
]);
2030 if (col
< x
) col
= x
;
2034 * Now we know the exact total size of the grid we're going to
2035 * produce: it's got 2*h+1 rows, each containing w lots of col,
2036 * w+1 boundary characters and a trailing newline.
2038 maxlen
= (2*state
->h
+1) * (state
->w
* (col
+1) + 2);
2040 ret
= snewn(maxlen
+1, char);
2043 for (y
= 0; y
<= 2*state
->h
; y
++) {
2044 for (x
= 0; x
<= 2*state
->w
; x
++) {
2049 int v
= grid(state
, x
/2, y
/2);
2051 sprintf(buf
, "%*d", col
, v
);
2053 sprintf(buf
, "%*s", col
, "");
2054 memcpy(p
, buf
, col
);
2058 * Display a horizontal edge or nothing.
2060 int h
= (y
==0 || y
==2*state
->h ?
1 :
2061 HRANGE(state
, x
/2, y
/2) && hedge(state
, x
/2, y
/2));
2067 for (i
= 0; i
< col
; i
++)
2071 * Display a vertical edge or nothing.
2073 int v
= (x
==0 || x
==2*state
->w ?
1 :
2074 VRANGE(state
, x
/2, y
/2) && vedge(state
, x
/2, y
/2));
2081 * Display a corner, or a vertical edge, or a
2082 * horizontal edge, or nothing.
2084 int hl
= (y
==0 || y
==2*state
->h ?
1 :
2085 HRANGE(state
, (x
-1)/2, y
/2) && hedge(state
, (x
-1)/2, y
/2));
2086 int hr
= (y
==0 || y
==2*state
->h ?
1 :
2087 HRANGE(state
, (x
+1)/2, y
/2) && hedge(state
, (x
+1)/2, y
/2));
2088 int vu
= (x
==0 || x
==2*state
->w ?
1 :
2089 VRANGE(state
, x
/2, (y
-1)/2) && vedge(state
, x
/2, (y
-1)/2));
2090 int vd
= (x
==0 || x
==2*state
->w ?
1 :
2091 VRANGE(state
, x
/2, (y
+1)/2) && vedge(state
, x
/2, (y
+1)/2));
2092 if (!hl
&& !hr
&& !vu
&& !vd
)
2094 else if (hl
&& hr
&& !vu
&& !vd
)
2096 else if (!hl
&& !hr
&& vu
&& vd
)
2105 assert(p
- ret
== maxlen
);
2112 * These coordinates are 2 times the obvious grid coordinates.
2113 * Hence, the top left of the grid is (0,0), the grid point to
2114 * the right of that is (2,0), the one _below that_ is (2,2)
2115 * and so on. This is so that we can specify a drag start point
2116 * on an edge (one odd coordinate) or in the middle of a square
2117 * (two odd coordinates) rather than always at a corner.
2119 * -1,-1 means no drag is in progress.
2126 * This flag is set as soon as a dragging action moves the
2127 * mouse pointer away from its starting point, so that even if
2128 * the pointer _returns_ to its starting point the action is
2129 * treated as a small drag rather than a click.
2133 * These are the co-ordinates of the top-left and bottom-right squares
2134 * in the drag box, respectively, or -1 otherwise.
2142 static game_ui
*new_ui(game_state
*state
)
2144 game_ui
*ui
= snew(game_ui
);
2145 ui
->drag_start_x
= -1;
2146 ui
->drag_start_y
= -1;
2147 ui
->drag_end_x
= -1;
2148 ui
->drag_end_y
= -1;
2149 ui
->dragged
= FALSE
;
2157 static void free_ui(game_ui
*ui
)
2162 static char *encode_ui(game_ui
*ui
)
2167 static void decode_ui(game_ui
*ui
, char *encoding
)
2171 static void coord_round(float x
, float y
, int *xr
, int *yr
)
2173 float xs
, ys
, xv
, yv
, dx
, dy
, dist
;
2176 * Find the nearest square-centre.
2178 xs
= (float)floor(x
) + 0.5F
;
2179 ys
= (float)floor(y
) + 0.5F
;
2182 * And find the nearest grid vertex.
2184 xv
= (float)floor(x
+ 0.5F
);
2185 yv
= (float)floor(y
+ 0.5F
);
2188 * We allocate clicks in parts of the grid square to either
2189 * corners, edges or square centres, as follows:
2205 * In other words: we measure the square distance (i.e.
2206 * max(dx,dy)) from the click to the nearest corner, and if
2207 * it's within CORNER_TOLERANCE then we return a corner click.
2208 * We measure the square distance from the click to the nearest
2209 * centre, and if that's within CENTRE_TOLERANCE we return a
2210 * centre click. Failing that, we find which of the two edge
2211 * centres is nearer to the click and return that edge.
2215 * Check for corner click.
2217 dx
= (float)fabs(x
- xv
);
2218 dy
= (float)fabs(y
- yv
);
2219 dist
= (dx
> dy ? dx
: dy
);
2220 if (dist
< CORNER_TOLERANCE
) {
2225 * Check for centre click.
2227 dx
= (float)fabs(x
- xs
);
2228 dy
= (float)fabs(y
- ys
);
2229 dist
= (dx
> dy ? dx
: dy
);
2230 if (dist
< CENTRE_TOLERANCE
) {
2231 *xr
= 1 + 2 * (int)xs
;
2232 *yr
= 1 + 2 * (int)ys
;
2235 * Failing both of those, see which edge we're closer to.
2236 * Conveniently, this is simply done by testing the relative
2237 * magnitude of dx and dy (which are currently distances from
2238 * the square centre).
2241 /* Vertical edge: x-coord of corner,
2242 * y-coord of square centre. */
2244 *yr
= 1 + 2 * (int)floor(ys
);
2246 /* Horizontal edge: x-coord of square centre,
2247 * y-coord of corner. */
2248 *xr
= 1 + 2 * (int)floor(xs
);
2256 * Returns TRUE if it has made any change to the grid.
2258 static int grid_draw_rect(game_state
*state
,
2259 unsigned char *hedge
, unsigned char *vedge
,
2261 int x1
, int y1
, int x2
, int y2
)
2264 int changed
= FALSE
;
2267 * Draw horizontal edges of rectangles.
2269 for (x
= x1
; x
< x2
; x
++)
2270 for (y
= y1
; y
<= y2
; y
++)
2271 if (HRANGE(state
,x
,y
)) {
2272 int val
= index(state
,hedge
,x
,y
);
2273 if (y
== y1
|| y
== y2
)
2277 changed
= changed
|| (index(state
,hedge
,x
,y
) != val
);
2279 index(state
,hedge
,x
,y
) = val
;
2283 * Draw vertical edges of rectangles.
2285 for (y
= y1
; y
< y2
; y
++)
2286 for (x
= x1
; x
<= x2
; x
++)
2287 if (VRANGE(state
,x
,y
)) {
2288 int val
= index(state
,vedge
,x
,y
);
2289 if (x
== x1
|| x
== x2
)
2293 changed
= changed
|| (index(state
,vedge
,x
,y
) != val
);
2295 index(state
,vedge
,x
,y
) = val
;
2301 static int ui_draw_rect(game_state
*state
, game_ui
*ui
,
2302 unsigned char *hedge
, unsigned char *vedge
, int c
,
2305 return grid_draw_rect(state
, hedge
, vedge
, c
, really
,
2306 ui
->x1
, ui
->y1
, ui
->x2
, ui
->y2
);
2309 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
2310 game_state
*newstate
)
2314 struct game_drawstate
{
2317 unsigned long *visible
;
2320 static char *interpret_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
2321 int x
, int y
, int button
)
2324 int startdrag
= FALSE
, enddrag
= FALSE
, active
= FALSE
;
2327 button
&= ~MOD_MASK
;
2329 if (button
== LEFT_BUTTON
) {
2331 } else if (button
== LEFT_RELEASE
) {
2333 } else if (button
!= LEFT_DRAG
) {
2337 coord_round(FROMCOORD((float)x
), FROMCOORD((float)y
), &xc
, &yc
);
2340 xc
>= 0 && xc
<= 2*from
->w
&&
2341 yc
>= 0 && yc
<= 2*from
->h
) {
2343 ui
->drag_start_x
= xc
;
2344 ui
->drag_start_y
= yc
;
2345 ui
->drag_end_x
= xc
;
2346 ui
->drag_end_y
= yc
;
2347 ui
->dragged
= FALSE
;
2351 if (ui
->drag_start_x
>= 0 &&
2352 (xc
!= ui
->drag_end_x
|| yc
!= ui
->drag_end_y
)) {
2355 ui
->drag_end_x
= xc
;
2356 ui
->drag_end_y
= yc
;
2360 if (xc
>= 0 && xc
<= 2*from
->w
&&
2361 yc
>= 0 && yc
<= 2*from
->h
) {
2362 ui
->x1
= ui
->drag_start_x
;
2363 ui
->x2
= ui
->drag_end_x
;
2364 if (ui
->x2
< ui
->x1
) { t
= ui
->x1
; ui
->x1
= ui
->x2
; ui
->x2
= t
; }
2366 ui
->y1
= ui
->drag_start_y
;
2367 ui
->y2
= ui
->drag_end_y
;
2368 if (ui
->y2
< ui
->y1
) { t
= ui
->y1
; ui
->y1
= ui
->y2
; ui
->y2
= t
; }
2370 ui
->x1
= ui
->x1
/ 2; /* rounds down */
2371 ui
->x2
= (ui
->x2
+1) / 2; /* rounds up */
2372 ui
->y1
= ui
->y1
/ 2; /* rounds down */
2373 ui
->y2
= (ui
->y2
+1) / 2; /* rounds up */
2384 if (enddrag
&& (ui
->drag_start_x
>= 0)) {
2385 if (xc
>= 0 && xc
<= 2*from
->w
&&
2386 yc
>= 0 && yc
<= 2*from
->h
) {
2389 if (ui_draw_rect(from
, ui
, from
->hedge
,
2390 from
->vedge
, 1, FALSE
)) {
2391 sprintf(buf
, "R%d,%d,%d,%d",
2392 ui
->x1
, ui
->y1
, ui
->x2
- ui
->x1
, ui
->y2
- ui
->y1
);
2396 if ((xc
& 1) && !(yc
& 1) && HRANGE(from
,xc
/2,yc
/2)) {
2397 sprintf(buf
, "H%d,%d", xc
/2, yc
/2);
2400 if ((yc
& 1) && !(xc
& 1) && VRANGE(from
,xc
/2,yc
/2)) {
2401 sprintf(buf
, "V%d,%d", xc
/2, yc
/2);
2407 ui
->drag_start_x
= -1;
2408 ui
->drag_start_y
= -1;
2409 ui
->drag_end_x
= -1;
2410 ui
->drag_end_y
= -1;
2415 ui
->dragged
= FALSE
;
2420 return ret
; /* a move has been made */
2422 return ""; /* UI activity has occurred */
2427 static game_state
*execute_move(game_state
*from
, char *move
)
2430 int x1
, y1
, x2
, y2
, mode
;
2432 if (move
[0] == 'S') {
2436 ret
= dup_game(from
);
2437 ret
->cheated
= TRUE
;
2439 for (y
= 0; y
< ret
->h
; y
++)
2440 for (x
= 1; x
< ret
->w
; x
++) {
2441 vedge(ret
, x
, y
) = (*p
== '1');
2444 for (y
= 1; y
< ret
->h
; y
++)
2445 for (x
= 0; x
< ret
->w
; x
++) {
2446 hedge(ret
, x
, y
) = (*p
== '1');
2450 sfree(ret
->correct
);
2451 ret
->correct
= get_correct(ret
);
2455 } else if (move
[0] == 'R' &&
2456 sscanf(move
+1, "%d,%d,%d,%d", &x1
, &y1
, &x2
, &y2
) == 4 &&
2457 x1
>= 0 && x2
>= 0 && x1
+x2
<= from
->w
&&
2458 y1
>= 0 && y2
>= 0 && y1
+y2
<= from
->h
) {
2462 } else if ((move
[0] == 'H' || move
[0] == 'V') &&
2463 sscanf(move
+1, "%d,%d", &x1
, &y1
) == 2 &&
2464 (move
[0] == 'H' ?
HRANGE(from
, x1
, y1
) :
2465 VRANGE(from
, x1
, y1
))) {
2468 return NULL
; /* can't parse move string */
2470 ret
= dup_game(from
);
2473 grid_draw_rect(ret
, ret
->hedge
, ret
->vedge
, 1, TRUE
, x1
, y1
, x2
, y2
);
2474 } else if (mode
== 'H') {
2475 hedge(ret
,x1
,y1
) = !hedge(ret
,x1
,y1
);
2476 } else if (mode
== 'V') {
2477 vedge(ret
,x1
,y1
) = !vedge(ret
,x1
,y1
);
2480 sfree(ret
->correct
);
2481 ret
->correct
= get_correct(ret
);
2484 * We've made a real change to the grid. Check to see
2485 * if the game has been completed.
2487 if (!ret
->completed
) {
2491 for (x
= 0; x
< ret
->w
; x
++)
2492 for (y
= 0; y
< ret
->h
; y
++)
2493 if (!index(ret
, ret
->correct
, x
, y
))
2497 ret
->completed
= TRUE
;
2503 /* ----------------------------------------------------------------------
2507 #define CORRECT (1L<<16)
2509 #define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG )
2510 #define MAX4(x,y,z,w) ( max(max(x,y),max(z,w)) )
2512 static void game_compute_size(game_params
*params
, int tilesize
,
2515 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2516 struct { int tilesize
; } ads
, *ds
= &ads
;
2517 ads
.tilesize
= tilesize
;
2519 *x
= params
->w
* TILE_SIZE
+ 2*BORDER
+ 1;
2520 *y
= params
->h
* TILE_SIZE
+ 2*BORDER
+ 1;
2523 static void game_set_size(game_drawstate
*ds
, game_params
*params
,
2526 ds
->tilesize
= tilesize
;
2529 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
2531 float *ret
= snewn(3 * NCOLOURS
, float);
2533 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2535 ret
[COL_GRID
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
2536 ret
[COL_GRID
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
2537 ret
[COL_GRID
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
2539 ret
[COL_DRAG
* 3 + 0] = 1.0F
;
2540 ret
[COL_DRAG
* 3 + 1] = 0.0F
;
2541 ret
[COL_DRAG
* 3 + 2] = 0.0F
;
2543 ret
[COL_CORRECT
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
2544 ret
[COL_CORRECT
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
2545 ret
[COL_CORRECT
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
2547 ret
[COL_LINE
* 3 + 0] = 0.0F
;
2548 ret
[COL_LINE
* 3 + 1] = 0.0F
;
2549 ret
[COL_LINE
* 3 + 2] = 0.0F
;
2551 ret
[COL_TEXT
* 3 + 0] = 0.0F
;
2552 ret
[COL_TEXT
* 3 + 1] = 0.0F
;
2553 ret
[COL_TEXT
* 3 + 2] = 0.0F
;
2555 *ncolours
= NCOLOURS
;
2559 static game_drawstate
*game_new_drawstate(game_state
*state
)
2561 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2564 ds
->started
= FALSE
;
2567 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned long);
2568 ds
->tilesize
= 0; /* not decided yet */
2569 for (i
= 0; i
< ds
->w
* ds
->h
; i
++)
2570 ds
->visible
[i
] = 0xFFFF;
2575 static void game_free_drawstate(game_drawstate
*ds
)
2581 static void draw_tile(frontend
*fe
, game_drawstate
*ds
, game_state
*state
,
2582 int x
, int y
, unsigned char *hedge
, unsigned char *vedge
,
2583 unsigned char *corners
, int correct
)
2585 int cx
= COORD(x
), cy
= COORD(y
);
2588 draw_rect(fe
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1, COL_GRID
);
2589 draw_rect(fe
, cx
+1, cy
+1, TILE_SIZE
-1, TILE_SIZE
-1,
2590 correct ? COL_CORRECT
: COL_BACKGROUND
);
2592 if (grid(state
,x
,y
)) {
2593 sprintf(str
, "%d", grid(state
,x
,y
));
2594 draw_text(fe
, cx
+TILE_SIZE
/2, cy
+TILE_SIZE
/2, FONT_VARIABLE
,
2595 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
, COL_TEXT
, str
);
2601 if (!HRANGE(state
,x
,y
) || index(state
,hedge
,x
,y
))
2602 draw_rect(fe
, cx
, cy
, TILE_SIZE
+1, 2,
2603 HRANGE(state
,x
,y
) ?
COLOUR(index(state
,hedge
,x
,y
)) :
2605 if (!HRANGE(state
,x
,y
+1) || index(state
,hedge
,x
,y
+1))
2606 draw_rect(fe
, cx
, cy
+TILE_SIZE
-1, TILE_SIZE
+1, 2,
2607 HRANGE(state
,x
,y
+1) ?
COLOUR(index(state
,hedge
,x
,y
+1)) :
2609 if (!VRANGE(state
,x
,y
) || index(state
,vedge
,x
,y
))
2610 draw_rect(fe
, cx
, cy
, 2, TILE_SIZE
+1,
2611 VRANGE(state
,x
,y
) ?
COLOUR(index(state
,vedge
,x
,y
)) :
2613 if (!VRANGE(state
,x
+1,y
) || index(state
,vedge
,x
+1,y
))
2614 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
, 2, TILE_SIZE
+1,
2615 VRANGE(state
,x
+1,y
) ?
COLOUR(index(state
,vedge
,x
+1,y
)) :
2621 if (index(state
,corners
,x
,y
))
2622 draw_rect(fe
, cx
, cy
, 2, 2,
2623 COLOUR(index(state
,corners
,x
,y
)));
2624 if (x
+1 < state
->w
&& index(state
,corners
,x
+1,y
))
2625 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
, 2, 2,
2626 COLOUR(index(state
,corners
,x
+1,y
)));
2627 if (y
+1 < state
->h
&& index(state
,corners
,x
,y
+1))
2628 draw_rect(fe
, cx
, cy
+TILE_SIZE
-1, 2, 2,
2629 COLOUR(index(state
,corners
,x
,y
+1)));
2630 if (x
+1 < state
->w
&& y
+1 < state
->h
&& index(state
,corners
,x
+1,y
+1))
2631 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
+TILE_SIZE
-1, 2, 2,
2632 COLOUR(index(state
,corners
,x
+1,y
+1)));
2634 draw_update(fe
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1);
2637 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
2638 game_state
*state
, int dir
, game_ui
*ui
,
2639 float animtime
, float flashtime
)
2642 unsigned char *hedge
, *vedge
, *corners
;
2645 hedge
= snewn(state
->w
*state
->h
, unsigned char);
2646 vedge
= snewn(state
->w
*state
->h
, unsigned char);
2647 memcpy(hedge
, state
->hedge
, state
->w
*state
->h
);
2648 memcpy(vedge
, state
->vedge
, state
->w
*state
->h
);
2649 ui_draw_rect(state
, ui
, hedge
, vedge
, 2, TRUE
);
2651 hedge
= state
->hedge
;
2652 vedge
= state
->vedge
;
2655 corners
= snewn(state
->w
* state
->h
, unsigned char);
2656 memset(corners
, 0, state
->w
* state
->h
);
2657 for (x
= 0; x
< state
->w
; x
++)
2658 for (y
= 0; y
< state
->h
; y
++) {
2660 int e
= index(state
, vedge
, x
, y
);
2661 if (index(state
,corners
,x
,y
) < e
)
2662 index(state
,corners
,x
,y
) = e
;
2663 if (y
+1 < state
->h
&&
2664 index(state
,corners
,x
,y
+1) < e
)
2665 index(state
,corners
,x
,y
+1) = e
;
2668 int e
= index(state
, hedge
, x
, y
);
2669 if (index(state
,corners
,x
,y
) < e
)
2670 index(state
,corners
,x
,y
) = e
;
2671 if (x
+1 < state
->w
&&
2672 index(state
,corners
,x
+1,y
) < e
)
2673 index(state
,corners
,x
+1,y
) = e
;
2679 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2680 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1, COL_BACKGROUND
);
2681 draw_rect(fe
, COORD(0)-1, COORD(0)-1,
2682 ds
->w
*TILE_SIZE
+3, ds
->h
*TILE_SIZE
+3, COL_LINE
);
2684 draw_update(fe
, 0, 0,
2685 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2686 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
2689 for (x
= 0; x
< state
->w
; x
++)
2690 for (y
= 0; y
< state
->h
; y
++) {
2691 unsigned long c
= 0;
2693 if (HRANGE(state
,x
,y
))
2694 c
|= index(state
,hedge
,x
,y
);
2695 if (HRANGE(state
,x
,y
+1))
2696 c
|= index(state
,hedge
,x
,y
+1) << 2;
2697 if (VRANGE(state
,x
,y
))
2698 c
|= index(state
,vedge
,x
,y
) << 4;
2699 if (VRANGE(state
,x
+1,y
))
2700 c
|= index(state
,vedge
,x
+1,y
) << 6;
2701 c
|= index(state
,corners
,x
,y
) << 8;
2703 c
|= index(state
,corners
,x
+1,y
) << 10;
2705 c
|= index(state
,corners
,x
,y
+1) << 12;
2706 if (x
+1 < state
->w
&& y
+1 < state
->h
)
2707 /* cast to prevent 2<<14 sign-extending on promotion to long */
2708 c
|= (unsigned long)index(state
,corners
,x
+1,y
+1) << 14;
2709 if (index(state
, state
->correct
, x
, y
) && !flashtime
)
2712 if (index(ds
,ds
->visible
,x
,y
) != c
) {
2713 draw_tile(fe
, ds
, state
, x
, y
, hedge
, vedge
, corners
,
2714 (c
& CORRECT
) ?
1 : 0);
2715 index(ds
,ds
->visible
,x
,y
) = c
;
2722 if (ui
->x1
>= 0 && ui
->y1
>= 0 &&
2723 ui
->x2
>= 0 && ui
->y2
>= 0) {
2724 sprintf(buf
, "%dx%d ",
2732 strcat(buf
, "Auto-solved.");
2733 else if (state
->completed
)
2734 strcat(buf
, "COMPLETED!");
2736 status_bar(fe
, buf
);
2739 if (hedge
!= state
->hedge
) {
2747 static float game_anim_length(game_state
*oldstate
,
2748 game_state
*newstate
, int dir
, game_ui
*ui
)
2753 static float game_flash_length(game_state
*oldstate
,
2754 game_state
*newstate
, int dir
, game_ui
*ui
)
2756 if (!oldstate
->completed
&& newstate
->completed
&&
2757 !oldstate
->cheated
&& !newstate
->cheated
)
2762 static int game_wants_statusbar(void)
2767 static int game_timing_state(game_state
*state
, game_ui
*ui
)
2773 #define thegame rect
2776 const struct game thegame
= {
2777 "Rectangles", "games.rectangles",
2784 TRUE
, game_configure
, custom_params
,
2792 TRUE
, game_text_format
,
2800 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2803 game_free_drawstate
,
2807 game_wants_statusbar
,
2808 FALSE
, game_timing_state
,
2809 0, /* mouse_priorities */