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 on singleton removal by making an aesthetic choice
13 * about which of the options to take.
15 * - When doing the 3x3 trick in singleton removal, limit the size
16 * of the generated rectangles in accordance with the max
19 * - It might be interesting to deliberately try to place
20 * numbers so as to reduce alternative solution patterns. I
21 * doubt we can do a perfect job of this, but we can make a
22 * start by, for example, noticing pairs of 2-rects
23 * alongside one another and _not_ putting their numbers at
26 * - If we start by sorting the rectlist in descending order
27 * of area, we might be able to bias our random number
28 * selection to produce a few large rectangles more often
29 * than oodles of small ones? Unsure, but might be worth a
57 #define INDEX(state, x, y) (((y) * (state)->w) + (x))
58 #define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
59 #define grid(state,x,y) index(state, (state)->grid, x, y)
60 #define vedge(state,x,y) index(state, (state)->vedge, x, y)
61 #define hedge(state,x,y) index(state, (state)->hedge, x, y)
63 #define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
64 (y) >= dy && (y) < (state)->h )
65 #define RANGE(state,x,y) CRANGE(state,x,y,0,0)
66 #define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
67 #define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
72 #define CORNER_TOLERANCE 0.15F
73 #define CENTRE_TOLERANCE 0.15F
75 #define FLASH_TIME 0.13F
77 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
78 #define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
82 int *grid
; /* contains the numbers */
83 unsigned char *vedge
; /* (w+1) x h */
84 unsigned char *hedge
; /* w x (h+1) */
88 static game_params
*default_params(void)
90 game_params
*ret
= snew(game_params
);
93 ret
->expandfactor
= 0.0F
;
98 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
105 case 0: w
= 7, h
= 7; break;
106 case 1: w
= 11, h
= 11; break;
107 case 2: w
= 15, h
= 15; break;
108 case 3: w
= 19, h
= 19; break;
109 default: return FALSE
;
112 sprintf(buf
, "%dx%d", w
, h
);
114 *params
= ret
= snew(game_params
);
117 ret
->expandfactor
= 0.0F
;
121 static void free_params(game_params
*params
)
126 static game_params
*dup_params(game_params
*params
)
128 game_params
*ret
= snew(game_params
);
129 *ret
= *params
; /* structure copy */
133 static game_params
*decode_params(char const *string
)
135 game_params
*ret
= default_params();
137 ret
->w
= ret
->h
= atoi(string
);
138 ret
->expandfactor
= 0.0F
;
139 while (*string
&& isdigit((unsigned char)*string
)) string
++;
140 if (*string
== 'x') {
142 ret
->h
= atoi(string
);
143 while (*string
&& isdigit((unsigned char)*string
)) string
++;
145 if (*string
== 'e') {
147 ret
->expandfactor
= atof(string
);
153 static char *encode_params(game_params
*params
)
157 sprintf(data
, "%dx%d", params
->w
, params
->h
);
162 static config_item
*game_configure(game_params
*params
)
167 ret
= snewn(5, config_item
);
169 ret
[0].name
= "Width";
170 ret
[0].type
= C_STRING
;
171 sprintf(buf
, "%d", params
->w
);
172 ret
[0].sval
= dupstr(buf
);
175 ret
[1].name
= "Height";
176 ret
[1].type
= C_STRING
;
177 sprintf(buf
, "%d", params
->h
);
178 ret
[1].sval
= dupstr(buf
);
181 ret
[2].name
= "Expansion factor";
182 ret
[2].type
= C_STRING
;
183 sprintf(buf
, "%g", params
->expandfactor
);
184 ret
[2].sval
= dupstr(buf
);
195 static game_params
*custom_params(config_item
*cfg
)
197 game_params
*ret
= snew(game_params
);
199 ret
->w
= atoi(cfg
[0].sval
);
200 ret
->h
= atoi(cfg
[1].sval
);
201 ret
->expandfactor
= atof(cfg
[2].sval
);
206 static char *validate_params(game_params
*params
)
208 if (params
->w
<= 0 && params
->h
<= 0)
209 return "Width and height must both be greater than zero";
210 if (params
->w
< 2 && params
->h
< 2)
211 return "Grid area must be greater than one";
212 if (params
->expandfactor
< 0.0F
)
213 return "Expansion factor may not be negative";
227 static struct rectlist
*get_rectlist(game_params
*params
, int *grid
)
232 struct rect
*rects
= NULL
;
233 int nrects
= 0, rectsize
= 0;
236 * Maximum rectangle area is 1/6 of total grid size, unless
237 * this means we can't place any rectangles at all in which
238 * case we set it to 2 at minimum.
240 maxarea
= params
->w
* params
->h
/ 6;
244 for (rw
= 1; rw
<= params
->w
; rw
++)
245 for (rh
= 1; rh
<= params
->h
; rh
++) {
246 if (rw
* rh
> maxarea
)
250 for (x
= 0; x
<= params
->w
- rw
; x
++)
251 for (y
= 0; y
<= params
->h
- rh
; y
++) {
252 if (nrects
>= rectsize
) {
253 rectsize
= nrects
+ 256;
254 rects
= sresize(rects
, rectsize
, struct rect
);
259 rects
[nrects
].w
= rw
;
260 rects
[nrects
].h
= rh
;
266 struct rectlist
*ret
;
267 ret
= snew(struct rectlist
);
272 assert(rects
== NULL
); /* hence no need to free */
277 static void free_rectlist(struct rectlist
*list
)
283 static void place_rect(game_params
*params
, int *grid
, struct rect r
)
285 int idx
= INDEX(params
, r
.x
, r
.y
);
288 for (x
= r
.x
; x
< r
.x
+r
.w
; x
++)
289 for (y
= r
.y
; y
< r
.y
+r
.h
; y
++) {
290 index(params
, grid
, x
, y
) = idx
;
292 #ifdef GENERATION_DIAGNOSTICS
293 printf(" placing rectangle at (%d,%d) size %d x %d\n",
298 static struct rect
find_rect(game_params
*params
, int *grid
, int x
, int y
)
304 * Find the top left of the rectangle.
306 idx
= index(params
, grid
, x
, y
);
312 return r
; /* 1x1 singleton here */
319 * Find the width and height of the rectangle.
322 (x
+w
< params
->w
&& index(params
,grid
,x
+w
,y
)==idx
);
325 (y
+h
< params
->h
&& index(params
,grid
,x
,y
+h
)==idx
);
336 #ifdef GENERATION_DIAGNOSTICS
337 static void display_grid(game_params
*params
, int *grid
, int *numbers
, int all
)
339 unsigned char *egrid
= snewn((params
->w
*2+3) * (params
->h
*2+3),
342 int r
= (params
->w
*2+3);
344 memset(egrid
, 0, (params
->w
*2+3) * (params
->h
*2+3));
346 for (x
= 0; x
< params
->w
; x
++)
347 for (y
= 0; y
< params
->h
; y
++) {
348 int i
= index(params
, grid
, x
, y
);
349 if (x
== 0 || index(params
, grid
, x
-1, y
) != i
)
350 egrid
[(2*y
+2) * r
+ (2*x
+1)] = 1;
351 if (x
== params
->w
-1 || index(params
, grid
, x
+1, y
) != i
)
352 egrid
[(2*y
+2) * r
+ (2*x
+3)] = 1;
353 if (y
== 0 || index(params
, grid
, x
, y
-1) != i
)
354 egrid
[(2*y
+1) * r
+ (2*x
+2)] = 1;
355 if (y
== params
->h
-1 || index(params
, grid
, x
, y
+1) != i
)
356 egrid
[(2*y
+3) * r
+ (2*x
+2)] = 1;
359 for (y
= 1; y
< 2*params
->h
+2; y
++) {
360 for (x
= 1; x
< 2*params
->w
+2; x
++) {
362 int k
= numbers ?
index(params
, numbers
, x
/2-1, y
/2-1) : 0;
363 if (k
|| (all
&& numbers
)) printf("%2d", k
); else printf(" ");
364 } else if (!((y
&x
)&1)) {
365 int v
= egrid
[y
*r
+x
];
366 if ((y
&1) && v
) v
= '-';
367 if ((x
&1) && v
) v
= '|';
370 if (!(x
&1)) putchar(v
);
373 if (egrid
[y
*r
+(x
+1)]) d
|= 1;
374 if (egrid
[(y
-1)*r
+x
]) d
|= 2;
375 if (egrid
[y
*r
+(x
-1)]) d
|= 4;
376 if (egrid
[(y
+1)*r
+x
]) d
|= 8;
377 c
= " ??+?-++?+|+++++"[d
];
379 if (!(x
&1)) putchar(c
);
389 static char *new_game_seed(game_params
*params
, random_state
*rs
)
392 struct rectlist
*list
;
393 int x
, y
, y2
, y2last
, yx
, run
, i
;
395 game_params params2real
, *params2
= ¶ms2real
;
398 * Set up the smaller width and height which we will use to
399 * generate the base grid.
401 params2
->w
= params
->w
/ (1.0F
+ params
->expandfactor
);
402 if (params2
->w
< 2 && params
->w
>= 2) params2
->w
= 2;
403 params2
->h
= params
->h
/ (1.0F
+ params
->expandfactor
);
404 if (params2
->h
< 2 && params
->h
>= 2) params2
->h
= 2;
406 grid
= snewn(params2
->w
* params2
->h
, int);
408 for (y
= 0; y
< params2
->h
; y
++)
409 for (x
= 0; x
< params2
->w
; x
++) {
410 index(params2
, grid
, x
, y
) = -1;
413 list
= get_rectlist(params2
, grid
);
414 assert(list
!= NULL
);
417 * Place rectangles until we can't any more.
419 while (list
->n
> 0) {
424 * Pick a random rectangle.
426 i
= random_upto(rs
, list
->n
);
432 place_rect(params2
, grid
, r
);
435 * Winnow the list by removing any rectangles which
439 for (i
= 0; i
< list
->n
; i
++) {
440 struct rect s
= list
->rects
[i
];
441 if (s
.x
+s
.w
<= r
.x
|| r
.x
+r
.w
<= s
.x
||
442 s
.y
+s
.h
<= r
.y
|| r
.y
+r
.h
<= s
.y
)
443 list
->rects
[m
++] = s
;
451 * Deal with singleton spaces remaining in the grid, one by
454 * We do this by making a local change to the layout. There are
455 * several possibilities:
457 * +-----+-----+ Here, we can remove the singleton by
458 * | | | extending the 1x2 rectangle below it
459 * +--+--+-----+ into a 1x3.
467 * +--+--+--+ Here, that trick doesn't work: there's no
468 * | | | 1 x n rectangle with the singleton at one
469 * | | | end. Instead, we extend a 1 x n rectangle
470 * | | | _out_ from the singleton, shaving a layer
471 * +--+--+ | off the end of another rectangle. So if we
472 * | | | | extended up, we'd make our singleton part
473 * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2
474 * | | | used to be; or we could extend right into
475 * +--+-----+ a 2x1, turning the 1x3 into a 1x2.
477 * +-----+--+ Here, we can't even do _that_, since any
478 * | | | direction we choose to extend the singleton
479 * +--+--+ | will produce a new singleton as a result of
480 * | | | | truncating one of the size-2 rectangles.
481 * | +--+--+ Fortunately, this case can _only_ occur when
482 * | | | a singleton is surrounded by four size-2s
483 * +--+-----+ in this fashion; so instead we can simply
484 * replace the whole section with a single 3x3.
486 for (x
= 0; x
< params2
->w
; x
++) {
487 for (y
= 0; y
< params2
->h
; y
++) {
488 if (index(params2
, grid
, x
, y
) < 0) {
491 #ifdef GENERATION_DIAGNOSTICS
492 display_grid(params2
, grid
, NULL
, FALSE
);
493 printf("singleton at %d,%d\n", x
, y
);
497 * Check in which directions we can feasibly extend
498 * the singleton. We can extend in a particular
499 * direction iff either:
501 * - the rectangle on that side of the singleton
502 * is not 2x1, and we are at one end of the edge
503 * of it we are touching
505 * - it is 2x1 but we are on its short side.
507 * FIXME: we could plausibly choose between these
508 * based on the sizes of the rectangles they would
512 if (x
< params2
->w
-1) {
513 struct rect r
= find_rect(params2
, grid
, x
+1, y
);
514 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
515 dirs
[ndirs
++] = 1; /* right */
518 struct rect r
= find_rect(params2
, grid
, x
, y
-1);
519 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
520 dirs
[ndirs
++] = 2; /* up */
523 struct rect r
= find_rect(params2
, grid
, x
-1, y
);
524 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
525 dirs
[ndirs
++] = 4; /* left */
527 if (y
< params2
->h
-1) {
528 struct rect r
= find_rect(params2
, grid
, x
, y
+1);
529 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
530 dirs
[ndirs
++] = 8; /* down */
537 which
= random_upto(rs
, ndirs
);
542 assert(x
< params2
->w
+1);
543 #ifdef GENERATION_DIAGNOSTICS
544 printf("extending right\n");
546 r1
= find_rect(params2
, grid
, x
+1, y
);
557 #ifdef GENERATION_DIAGNOSTICS
558 printf("extending up\n");
560 r1
= find_rect(params2
, grid
, x
, y
-1);
571 #ifdef GENERATION_DIAGNOSTICS
572 printf("extending left\n");
574 r1
= find_rect(params2
, grid
, x
-1, y
);
584 assert(y
< params2
->h
+1);
585 #ifdef GENERATION_DIAGNOSTICS
586 printf("extending down\n");
588 r1
= find_rect(params2
, grid
, x
, y
+1);
598 if (r1
.h
> 0 && r1
.w
> 0)
599 place_rect(params2
, grid
, r1
);
600 place_rect(params2
, grid
, r2
);
604 * Sanity-check that there really is a 3x3
605 * rectangle surrounding this singleton and it
606 * contains absolutely everything we could
611 assert(x
> 0 && x
< params2
->w
-1);
612 assert(y
> 0 && y
< params2
->h
-1);
614 for (xx
= x
-1; xx
<= x
+1; xx
++)
615 for (yy
= y
-1; yy
<= y
+1; yy
++) {
616 struct rect r
= find_rect(params2
,grid
,xx
,yy
);
619 assert(r
.x
+r
.w
-1 <= x
+1);
620 assert(r
.y
+r
.h
-1 <= y
+1);
625 #ifdef GENERATION_DIAGNOSTICS
626 printf("need the 3x3 trick\n");
630 * FIXME: If the maximum rectangle area for
631 * this grid is less than 9, we ought to
632 * subdivide the 3x3 in some fashion. There are
633 * five other possibilities:
638 * - a 3 and three 2s (two different arrangements).
646 place_rect(params2
, grid
, r
);
654 * We have now constructed a grid of the size specified in
655 * params2. Now we extend it into a grid of the size specified
656 * in params. We do this in two passes: we extend it vertically
657 * until it's the right height, then we transpose it, then
658 * extend it vertically again (getting it effectively the right
659 * width), then finally transpose again.
661 for (i
= 0; i
< 2; i
++) {
662 int *grid2
, *expand
, *where
;
663 game_params params3real
, *params3
= ¶ms3real
;
665 #ifdef GENERATION_DIAGNOSTICS
666 printf("before expansion:\n");
667 display_grid(params2
, grid
, NULL
, TRUE
);
671 * Set up the new grid.
673 grid2
= snewn(params2
->w
* params
->h
, int);
674 expand
= snewn(params2
->h
-1, int);
675 where
= snewn(params2
->w
, int);
676 params3
->w
= params2
->w
;
677 params3
->h
= params
->h
;
680 * Decide which horizontal edges are going to get expanded,
683 for (y
= 0; y
< params2
->h
-1; y
++)
685 for (y
= params2
->h
; y
< params
->h
; y
++) {
686 x
= random_upto(rs
, params2
->h
-1);
690 #ifdef GENERATION_DIAGNOSTICS
691 printf("expand[] = {");
692 for (y
= 0; y
< params2
->h
-1; y
++)
693 printf(" %d", expand
[y
]);
698 * Perform the expansion. The way this works is that we
701 * - copy a row from grid into grid2
703 * - invent some number of additional rows in grid2 where
704 * there was previously only a horizontal line between
705 * rows in grid, and make random decisions about where
706 * among these to place each rectangle edge that ran
709 for (y
= y2
= y2last
= 0; y
< params2
->h
; y
++) {
711 * Copy a single line from row y of grid into row y2 of
714 for (x
= 0; x
< params2
->w
; x
++) {
715 int val
= index(params2
, grid
, x
, y
);
716 if (val
/ params2
->w
== y
&& /* rect starts on this line */
717 (y2
== 0 || /* we're at the very top, or... */
718 index(params3
, grid2
, x
, y2
-1) / params3
->w
< y2last
719 /* this rect isn't already started */))
720 index(params3
, grid2
, x
, y2
) =
721 INDEX(params3
, val
% params2
->w
, y2
);
723 index(params3
, grid2
, x
, y2
) =
724 index(params3
, grid2
, x
, y2
-1);
728 * If that was the last line, terminate the loop early.
730 if (++y2
== params3
->h
)
736 * Invent some number of additional lines. First walk
737 * along this line working out where to put all the
738 * edges that coincide with it.
741 for (x
= 0; x
< params2
->w
; x
++) {
742 if (index(params2
, grid
, x
, y
) !=
743 index(params2
, grid
, x
, y
+1)) {
745 * This is a horizontal edge, so it needs
749 (index(params2
, grid
, x
-1, y
) !=
750 index(params2
, grid
, x
, y
) &&
751 index(params2
, grid
, x
-1, y
+1) !=
752 index(params2
, grid
, x
, y
+1))) {
754 * Here we have the chance to make a new
757 yx
= random_upto(rs
, expand
[y
]+1);
760 * Here we just reuse the previous value of
769 for (yx
= 0; yx
< expand
[y
]; yx
++) {
771 * Invent a single row. For each square in the row,
772 * we copy the grid entry from the square above it,
773 * unless we're starting the new rectangle here.
775 for (x
= 0; x
< params2
->w
; x
++) {
776 if (yx
== where
[x
]) {
777 int val
= index(params2
, grid
, x
, y
+1);
779 val
= INDEX(params3
, val
, y2
);
780 index(params3
, grid2
, x
, y2
) = val
;
782 index(params3
, grid2
, x
, y2
) =
783 index(params3
, grid2
, x
, y2
-1);
793 #ifdef GENERATION_DIAGNOSTICS
794 printf("after expansion:\n");
795 display_grid(params3
, grid2
, NULL
, TRUE
);
800 params2
->w
= params3
->h
;
801 params2
->h
= params3
->w
;
803 grid
= snewn(params2
->w
* params2
->h
, int);
804 for (x
= 0; x
< params2
->w
; x
++)
805 for (y
= 0; y
< params2
->h
; y
++) {
806 int idx1
= INDEX(params2
, x
, y
);
807 int idx2
= INDEX(params3
, y
, x
);
811 tmp
= (tmp
% params3
->w
) * params2
->w
+ (tmp
/ params3
->w
);
820 params
->w
= params
->h
;
824 #ifdef GENERATION_DIAGNOSTICS
825 printf("after transposition:\n");
826 display_grid(params2
, grid
, NULL
, TRUE
);
833 numbers
= snewn(params
->w
* params
->h
, int);
835 for (y
= 0; y
< params
->h
; y
++)
836 for (x
= 0; x
< params
->w
; x
++) {
837 index(params
, numbers
, x
, y
) = 0;
840 for (x
= 0; x
< params
->w
; x
++) {
841 for (y
= 0; y
< params
->h
; y
++) {
842 int idx
= INDEX(params
, x
, y
);
843 if (index(params
, grid
, x
, y
) == idx
) {
844 struct rect r
= find_rect(params
, grid
, x
, y
);
848 * Decide where to put the number.
850 n
= random_upto(rs
, r
.w
*r
.h
);
853 index(params
,numbers
,x
+xx
,y
+yy
) = r
.w
*r
.h
;
858 #ifdef GENERATION_DIAGNOSTICS
859 display_grid(params
, grid
, numbers
, FALSE
);
862 seed
= snewn(11 * params
->w
* params
->h
, char);
865 for (i
= 0; i
<= params
->w
* params
->h
; i
++) {
866 int n
= (i
< params
->w
* params
->h ? numbers
[i
] : -1);
873 int c
= 'a' - 1 + run
;
877 run
-= c
- ('a' - 1);
881 * If there's a number in the very top left or
882 * bottom right, there's no point putting an
883 * unnecessary _ before or after it.
885 if (p
> seed
&& n
> 0)
889 p
+= sprintf(p
, "%d", n
);
901 static char *validate_seed(game_params
*params
, char *seed
)
903 int area
= params
->w
* params
->h
;
908 if (n
>= 'a' && n
<= 'z') {
909 squares
+= n
- 'a' + 1;
910 } else if (n
== '_') {
912 } else if (n
> '0' && n
<= '9') {
914 while (*seed
>= '0' && *seed
<= '9')
917 return "Invalid character in game specification";
921 return "Not enough data to fill grid";
924 return "Too much data to fit in grid";
929 static game_state
*new_game(game_params
*params
, char *seed
)
931 game_state
*state
= snew(game_state
);
934 state
->w
= params
->w
;
935 state
->h
= params
->h
;
937 area
= state
->w
* state
->h
;
939 state
->grid
= snewn(area
, int);
940 state
->vedge
= snewn(area
, unsigned char);
941 state
->hedge
= snewn(area
, unsigned char);
942 state
->completed
= FALSE
;
947 if (n
>= 'a' && n
<= 'z') {
948 int run
= n
- 'a' + 1;
949 assert(i
+ run
<= area
);
951 state
->grid
[i
++] = 0;
952 } else if (n
== '_') {
954 } else if (n
> '0' && n
<= '9') {
956 state
->grid
[i
++] = atoi(seed
-1);
957 while (*seed
>= '0' && *seed
<= '9')
960 assert(!"We can't get here");
965 for (y
= 0; y
< state
->h
; y
++)
966 for (x
= 0; x
< state
->w
; x
++)
967 vedge(state
,x
,y
) = hedge(state
,x
,y
) = 0;
972 static game_state
*dup_game(game_state
*state
)
974 game_state
*ret
= snew(game_state
);
979 ret
->vedge
= snewn(state
->w
* state
->h
, unsigned char);
980 ret
->hedge
= snewn(state
->w
* state
->h
, unsigned char);
981 ret
->grid
= snewn(state
->w
* state
->h
, int);
983 ret
->completed
= state
->completed
;
985 memcpy(ret
->grid
, state
->grid
, state
->w
* state
->h
* sizeof(int));
986 memcpy(ret
->vedge
, state
->vedge
, state
->w
*state
->h
*sizeof(unsigned char));
987 memcpy(ret
->hedge
, state
->hedge
, state
->w
*state
->h
*sizeof(unsigned char));
992 static void free_game(game_state
*state
)
1000 static char *game_text_format(game_state
*state
)
1002 char *ret
, *p
, buf
[80];
1003 int i
, x
, y
, col
, maxlen
;
1006 * First determine the number of spaces required to display a
1007 * number. We'll use at least two, because one looks a bit
1011 for (i
= 0; i
< state
->w
* state
->h
; i
++) {
1012 x
= sprintf(buf
, "%d", state
->grid
[i
]);
1013 if (col
< x
) col
= x
;
1017 * Now we know the exact total size of the grid we're going to
1018 * produce: it's got 2*h+1 rows, each containing w lots of col,
1019 * w+1 boundary characters and a trailing newline.
1021 maxlen
= (2*state
->h
+1) * (state
->w
* (col
+1) + 2);
1023 ret
= snewn(maxlen
, char);
1026 for (y
= 0; y
<= 2*state
->h
; y
++) {
1027 for (x
= 0; x
<= 2*state
->w
; x
++) {
1032 int v
= grid(state
, x
/2, y
/2);
1034 sprintf(buf
, "%*d", col
, v
);
1036 sprintf(buf
, "%*s", col
, "");
1037 memcpy(p
, buf
, col
);
1041 * Display a horizontal edge or nothing.
1043 int h
= (y
==0 || y
==2*state
->h ?
1 :
1044 HRANGE(state
, x
/2, y
/2) && hedge(state
, x
/2, y
/2));
1050 for (i
= 0; i
< col
; i
++)
1054 * Display a vertical edge or nothing.
1056 int v
= (x
==0 || x
==2*state
->w ?
1 :
1057 VRANGE(state
, x
/2, y
/2) && vedge(state
, x
/2, y
/2));
1064 * Display a corner, or a vertical edge, or a
1065 * horizontal edge, or nothing.
1067 int hl
= (y
==0 || y
==2*state
->h ?
1 :
1068 HRANGE(state
, (x
-1)/2, y
/2) && hedge(state
, (x
-1)/2, y
/2));
1069 int hr
= (y
==0 || y
==2*state
->h ?
1 :
1070 HRANGE(state
, (x
+1)/2, y
/2) && hedge(state
, (x
+1)/2, y
/2));
1071 int vu
= (x
==0 || x
==2*state
->w ?
1 :
1072 VRANGE(state
, x
/2, (y
-1)/2) && vedge(state
, x
/2, (y
-1)/2));
1073 int vd
= (x
==0 || x
==2*state
->w ?
1 :
1074 VRANGE(state
, x
/2, (y
+1)/2) && vedge(state
, x
/2, (y
+1)/2));
1075 if (!hl
&& !hr
&& !vu
&& !vd
)
1077 else if (hl
&& hr
&& !vu
&& !vd
)
1079 else if (!hl
&& !hr
&& vu
&& vd
)
1088 assert(p
- ret
== maxlen
);
1093 static unsigned char *get_correct(game_state
*state
)
1098 ret
= snewn(state
->w
* state
->h
, unsigned char);
1099 memset(ret
, 0xFF, state
->w
* state
->h
);
1101 for (x
= 0; x
< state
->w
; x
++)
1102 for (y
= 0; y
< state
->h
; y
++)
1103 if (index(state
,ret
,x
,y
) == 0xFF) {
1106 int num
, area
, valid
;
1109 * Find a rectangle starting at this point.
1112 while (x
+rw
< state
->w
&& !vedge(state
,x
+rw
,y
))
1115 while (y
+rh
< state
->h
&& !hedge(state
,x
,y
+rh
))
1119 * We know what the dimensions of the rectangle
1120 * should be if it's there at all. Find out if we
1121 * really have a valid rectangle.
1124 /* Check the horizontal edges. */
1125 for (xx
= x
; xx
< x
+rw
; xx
++) {
1126 for (yy
= y
; yy
<= y
+rh
; yy
++) {
1127 int e
= !HRANGE(state
,xx
,yy
) || hedge(state
,xx
,yy
);
1128 int ec
= (yy
== y
|| yy
== y
+rh
);
1133 /* Check the vertical edges. */
1134 for (yy
= y
; yy
< y
+rh
; yy
++) {
1135 for (xx
= x
; xx
<= x
+rw
; xx
++) {
1136 int e
= !VRANGE(state
,xx
,yy
) || vedge(state
,xx
,yy
);
1137 int ec
= (xx
== x
|| xx
== x
+rw
);
1144 * If this is not a valid rectangle with no other
1145 * edges inside it, we just mark this square as not
1146 * complete and proceed to the next square.
1149 index(state
, ret
, x
, y
) = 0;
1154 * We have a rectangle. Now see what its area is,
1155 * and how many numbers are in it.
1159 for (xx
= x
; xx
< x
+rw
; xx
++) {
1160 for (yy
= y
; yy
< y
+rh
; yy
++) {
1162 if (grid(state
,xx
,yy
)) {
1164 valid
= FALSE
; /* two numbers */
1165 num
= grid(state
,xx
,yy
);
1173 * Now fill in the whole rectangle based on the
1176 for (xx
= x
; xx
< x
+rw
; xx
++) {
1177 for (yy
= y
; yy
< y
+rh
; yy
++) {
1178 index(state
, ret
, xx
, yy
) = valid
;
1188 * These coordinates are 2 times the obvious grid coordinates.
1189 * Hence, the top left of the grid is (0,0), the grid point to
1190 * the right of that is (2,0), the one _below that_ is (2,2)
1191 * and so on. This is so that we can specify a drag start point
1192 * on an edge (one odd coordinate) or in the middle of a square
1193 * (two odd coordinates) rather than always at a corner.
1195 * -1,-1 means no drag is in progress.
1202 * This flag is set as soon as a dragging action moves the
1203 * mouse pointer away from its starting point, so that even if
1204 * the pointer _returns_ to its starting point the action is
1205 * treated as a small drag rather than a click.
1210 static game_ui
*new_ui(game_state
*state
)
1212 game_ui
*ui
= snew(game_ui
);
1213 ui
->drag_start_x
= -1;
1214 ui
->drag_start_y
= -1;
1215 ui
->drag_end_x
= -1;
1216 ui
->drag_end_y
= -1;
1217 ui
->dragged
= FALSE
;
1221 static void free_ui(game_ui
*ui
)
1226 static void coord_round(float x
, float y
, int *xr
, int *yr
)
1228 float xs
, ys
, xv
, yv
, dx
, dy
, dist
;
1231 * Find the nearest square-centre.
1233 xs
= (float)floor(x
) + 0.5F
;
1234 ys
= (float)floor(y
) + 0.5F
;
1237 * And find the nearest grid vertex.
1239 xv
= (float)floor(x
+ 0.5F
);
1240 yv
= (float)floor(y
+ 0.5F
);
1243 * We allocate clicks in parts of the grid square to either
1244 * corners, edges or square centres, as follows:
1260 * In other words: we measure the square distance (i.e.
1261 * max(dx,dy)) from the click to the nearest corner, and if
1262 * it's within CORNER_TOLERANCE then we return a corner click.
1263 * We measure the square distance from the click to the nearest
1264 * centre, and if that's within CENTRE_TOLERANCE we return a
1265 * centre click. Failing that, we find which of the two edge
1266 * centres is nearer to the click and return that edge.
1270 * Check for corner click.
1272 dx
= (float)fabs(x
- xv
);
1273 dy
= (float)fabs(y
- yv
);
1274 dist
= (dx
> dy ? dx
: dy
);
1275 if (dist
< CORNER_TOLERANCE
) {
1280 * Check for centre click.
1282 dx
= (float)fabs(x
- xs
);
1283 dy
= (float)fabs(y
- ys
);
1284 dist
= (dx
> dy ? dx
: dy
);
1285 if (dist
< CENTRE_TOLERANCE
) {
1286 *xr
= 1 + 2 * (int)xs
;
1287 *yr
= 1 + 2 * (int)ys
;
1290 * Failing both of those, see which edge we're closer to.
1291 * Conveniently, this is simply done by testing the relative
1292 * magnitude of dx and dy (which are currently distances from
1293 * the square centre).
1296 /* Vertical edge: x-coord of corner,
1297 * y-coord of square centre. */
1299 *yr
= 1 + 2 * (int)ys
;
1301 /* Horizontal edge: x-coord of square centre,
1302 * y-coord of corner. */
1303 *xr
= 1 + 2 * (int)xs
;
1310 static void ui_draw_rect(game_state
*state
, game_ui
*ui
,
1311 unsigned char *hedge
, unsigned char *vedge
, int c
)
1313 int x1
, x2
, y1
, y2
, x
, y
, t
;
1315 x1
= ui
->drag_start_x
;
1316 x2
= ui
->drag_end_x
;
1317 if (x2
< x1
) { t
= x1
; x1
= x2
; x2
= t
; }
1319 y1
= ui
->drag_start_y
;
1320 y2
= ui
->drag_end_y
;
1321 if (y2
< y1
) { t
= y1
; y1
= y2
; y2
= t
; }
1323 x1
= x1
/ 2; /* rounds down */
1324 x2
= (x2
+1) / 2; /* rounds up */
1325 y1
= y1
/ 2; /* rounds down */
1326 y2
= (y2
+1) / 2; /* rounds up */
1329 * Draw horizontal edges of rectangles.
1331 for (x
= x1
; x
< x2
; x
++)
1332 for (y
= y1
; y
<= y2
; y
++)
1333 if (HRANGE(state
,x
,y
)) {
1334 int val
= index(state
,hedge
,x
,y
);
1335 if (y
== y1
|| y
== y2
)
1339 index(state
,hedge
,x
,y
) = val
;
1343 * Draw vertical edges of rectangles.
1345 for (y
= y1
; y
< y2
; y
++)
1346 for (x
= x1
; x
<= x2
; x
++)
1347 if (VRANGE(state
,x
,y
)) {
1348 int val
= index(state
,vedge
,x
,y
);
1349 if (x
== x1
|| x
== x2
)
1353 index(state
,vedge
,x
,y
) = val
;
1357 static game_state
*make_move(game_state
*from
, game_ui
*ui
,
1358 int x
, int y
, int button
)
1361 int startdrag
= FALSE
, enddrag
= FALSE
, active
= FALSE
;
1364 if (button
== LEFT_BUTTON
) {
1366 } else if (button
== LEFT_RELEASE
) {
1368 } else if (button
!= LEFT_DRAG
) {
1372 coord_round(FROMCOORD((float)x
), FROMCOORD((float)y
), &xc
, &yc
);
1375 ui
->drag_start_x
= xc
;
1376 ui
->drag_start_y
= yc
;
1377 ui
->drag_end_x
= xc
;
1378 ui
->drag_end_y
= yc
;
1379 ui
->dragged
= FALSE
;
1383 if (xc
!= ui
->drag_end_x
|| yc
!= ui
->drag_end_y
) {
1384 ui
->drag_end_x
= xc
;
1385 ui
->drag_end_y
= yc
;
1393 if (xc
>= 0 && xc
<= 2*from
->w
&&
1394 yc
>= 0 && yc
<= 2*from
->h
) {
1395 ret
= dup_game(from
);
1398 ui_draw_rect(ret
, ui
, ret
->hedge
, ret
->vedge
, 1);
1400 if ((xc
& 1) && !(yc
& 1) && HRANGE(from
,xc
/2,yc
/2)) {
1401 hedge(ret
,xc
/2,yc
/2) = !hedge(ret
,xc
/2,yc
/2);
1403 if ((yc
& 1) && !(xc
& 1) && VRANGE(from
,xc
/2,yc
/2)) {
1404 vedge(ret
,xc
/2,yc
/2) = !vedge(ret
,xc
/2,yc
/2);
1408 if (!memcmp(ret
->hedge
, from
->hedge
, from
->w
*from
->h
) &&
1409 !memcmp(ret
->vedge
, from
->vedge
, from
->w
*from
->h
)) {
1415 * We've made a real change to the grid. Check to see
1416 * if the game has been completed.
1418 if (ret
&& !ret
->completed
) {
1420 unsigned char *correct
= get_correct(ret
);
1423 for (x
= 0; x
< ret
->w
; x
++)
1424 for (y
= 0; y
< ret
->h
; y
++)
1425 if (!index(ret
, correct
, x
, y
))
1431 ret
->completed
= TRUE
;
1435 ui
->drag_start_x
= -1;
1436 ui
->drag_start_y
= -1;
1437 ui
->drag_end_x
= -1;
1438 ui
->drag_end_y
= -1;
1439 ui
->dragged
= FALSE
;
1444 return ret
; /* a move has been made */
1446 return from
; /* UI activity has occurred */
1451 /* ----------------------------------------------------------------------
1455 #define CORRECT 65536
1457 #define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG )
1458 #define MAX(x,y) ( (x)>(y) ? (x) : (y) )
1459 #define MAX4(x,y,z,w) ( MAX(MAX(x,y),MAX(z,w)) )
1461 struct game_drawstate
{
1464 unsigned int *visible
;
1467 static void game_size(game_params
*params
, int *x
, int *y
)
1469 *x
= params
->w
* TILE_SIZE
+ 2*BORDER
+ 1;
1470 *y
= params
->h
* TILE_SIZE
+ 2*BORDER
+ 1;
1473 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1475 float *ret
= snewn(3 * NCOLOURS
, float);
1477 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1479 ret
[COL_GRID
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1480 ret
[COL_GRID
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1481 ret
[COL_GRID
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
1483 ret
[COL_DRAG
* 3 + 0] = 1.0F
;
1484 ret
[COL_DRAG
* 3 + 1] = 0.0F
;
1485 ret
[COL_DRAG
* 3 + 2] = 0.0F
;
1487 ret
[COL_CORRECT
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
1488 ret
[COL_CORRECT
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
1489 ret
[COL_CORRECT
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
1491 ret
[COL_LINE
* 3 + 0] = 0.0F
;
1492 ret
[COL_LINE
* 3 + 1] = 0.0F
;
1493 ret
[COL_LINE
* 3 + 2] = 0.0F
;
1495 ret
[COL_TEXT
* 3 + 0] = 0.0F
;
1496 ret
[COL_TEXT
* 3 + 1] = 0.0F
;
1497 ret
[COL_TEXT
* 3 + 2] = 0.0F
;
1499 *ncolours
= NCOLOURS
;
1503 static game_drawstate
*game_new_drawstate(game_state
*state
)
1505 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1508 ds
->started
= FALSE
;
1511 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned int);
1512 for (i
= 0; i
< ds
->w
* ds
->h
; i
++)
1513 ds
->visible
[i
] = 0xFFFF;
1518 static void game_free_drawstate(game_drawstate
*ds
)
1524 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
1525 unsigned char *hedge
, unsigned char *vedge
,
1526 unsigned char *corners
, int correct
)
1528 int cx
= COORD(x
), cy
= COORD(y
);
1531 draw_rect(fe
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1, COL_GRID
);
1532 draw_rect(fe
, cx
+1, cy
+1, TILE_SIZE
-1, TILE_SIZE
-1,
1533 correct ? COL_CORRECT
: COL_BACKGROUND
);
1535 if (grid(state
,x
,y
)) {
1536 sprintf(str
, "%d", grid(state
,x
,y
));
1537 draw_text(fe
, cx
+TILE_SIZE
/2, cy
+TILE_SIZE
/2, FONT_VARIABLE
,
1538 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
, COL_TEXT
, str
);
1544 if (!HRANGE(state
,x
,y
) || index(state
,hedge
,x
,y
))
1545 draw_rect(fe
, cx
, cy
, TILE_SIZE
+1, 2,
1546 HRANGE(state
,x
,y
) ?
COLOUR(index(state
,hedge
,x
,y
)) :
1548 if (!HRANGE(state
,x
,y
+1) || index(state
,hedge
,x
,y
+1))
1549 draw_rect(fe
, cx
, cy
+TILE_SIZE
-1, TILE_SIZE
+1, 2,
1550 HRANGE(state
,x
,y
+1) ?
COLOUR(index(state
,hedge
,x
,y
+1)) :
1552 if (!VRANGE(state
,x
,y
) || index(state
,vedge
,x
,y
))
1553 draw_rect(fe
, cx
, cy
, 2, TILE_SIZE
+1,
1554 VRANGE(state
,x
,y
) ?
COLOUR(index(state
,vedge
,x
,y
)) :
1556 if (!VRANGE(state
,x
+1,y
) || index(state
,vedge
,x
+1,y
))
1557 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
, 2, TILE_SIZE
+1,
1558 VRANGE(state
,x
+1,y
) ?
COLOUR(index(state
,vedge
,x
+1,y
)) :
1564 if (index(state
,corners
,x
,y
))
1565 draw_rect(fe
, cx
, cy
, 2, 2,
1566 COLOUR(index(state
,corners
,x
,y
)));
1567 if (x
+1 < state
->w
&& index(state
,corners
,x
+1,y
))
1568 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
, 2, 2,
1569 COLOUR(index(state
,corners
,x
+1,y
)));
1570 if (y
+1 < state
->h
&& index(state
,corners
,x
,y
+1))
1571 draw_rect(fe
, cx
, cy
+TILE_SIZE
-1, 2, 2,
1572 COLOUR(index(state
,corners
,x
,y
+1)));
1573 if (x
+1 < state
->w
&& y
+1 < state
->h
&& index(state
,corners
,x
+1,y
+1))
1574 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
+TILE_SIZE
-1, 2, 2,
1575 COLOUR(index(state
,corners
,x
+1,y
+1)));
1577 draw_update(fe
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1);
1580 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1581 game_state
*state
, int dir
, game_ui
*ui
,
1582 float animtime
, float flashtime
)
1585 unsigned char *correct
;
1586 unsigned char *hedge
, *vedge
, *corners
;
1588 correct
= get_correct(state
);
1591 hedge
= snewn(state
->w
*state
->h
, unsigned char);
1592 vedge
= snewn(state
->w
*state
->h
, unsigned char);
1593 memcpy(hedge
, state
->hedge
, state
->w
*state
->h
);
1594 memcpy(vedge
, state
->vedge
, state
->w
*state
->h
);
1595 ui_draw_rect(state
, ui
, hedge
, vedge
, 2);
1597 hedge
= state
->hedge
;
1598 vedge
= state
->vedge
;
1601 corners
= snewn(state
->w
* state
->h
, unsigned char);
1602 memset(corners
, 0, state
->w
* state
->h
);
1603 for (x
= 0; x
< state
->w
; x
++)
1604 for (y
= 0; y
< state
->h
; y
++) {
1606 int e
= index(state
, vedge
, x
, y
);
1607 if (index(state
,corners
,x
,y
) < e
)
1608 index(state
,corners
,x
,y
) = e
;
1609 if (y
+1 < state
->h
&&
1610 index(state
,corners
,x
,y
+1) < e
)
1611 index(state
,corners
,x
,y
+1) = e
;
1614 int e
= index(state
, hedge
, x
, y
);
1615 if (index(state
,corners
,x
,y
) < e
)
1616 index(state
,corners
,x
,y
) = e
;
1617 if (x
+1 < state
->w
&&
1618 index(state
,corners
,x
+1,y
) < e
)
1619 index(state
,corners
,x
+1,y
) = e
;
1625 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
1626 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1, COL_BACKGROUND
);
1627 draw_rect(fe
, COORD(0)-1, COORD(0)-1,
1628 ds
->w
*TILE_SIZE
+3, ds
->h
*TILE_SIZE
+3, COL_LINE
);
1630 draw_update(fe
, 0, 0,
1631 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
1632 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
1635 for (x
= 0; x
< state
->w
; x
++)
1636 for (y
= 0; y
< state
->h
; y
++) {
1639 if (HRANGE(state
,x
,y
))
1640 c
|= index(state
,hedge
,x
,y
);
1641 if (HRANGE(state
,x
,y
+1))
1642 c
|= index(state
,hedge
,x
,y
+1) << 2;
1643 if (VRANGE(state
,x
,y
))
1644 c
|= index(state
,vedge
,x
,y
) << 4;
1645 if (VRANGE(state
,x
+1,y
))
1646 c
|= index(state
,vedge
,x
+1,y
) << 6;
1647 c
|= index(state
,corners
,x
,y
) << 8;
1649 c
|= index(state
,corners
,x
+1,y
) << 10;
1651 c
|= index(state
,corners
,x
,y
+1) << 12;
1652 if (x
+1 < state
->w
&& y
+1 < state
->h
)
1653 c
|= index(state
,corners
,x
+1,y
+1) << 14;
1654 if (index(state
, correct
, x
, y
) && !flashtime
)
1657 if (index(ds
,ds
->visible
,x
,y
) != c
) {
1658 draw_tile(fe
, state
, x
, y
, hedge
, vedge
, corners
, c
& CORRECT
);
1659 index(ds
,ds
->visible
,x
,y
) = c
;
1663 if (hedge
!= state
->hedge
) {
1672 static float game_anim_length(game_state
*oldstate
,
1673 game_state
*newstate
, int dir
)
1678 static float game_flash_length(game_state
*oldstate
,
1679 game_state
*newstate
, int dir
)
1681 if (!oldstate
->completed
&& newstate
->completed
)
1686 static int game_wants_statusbar(void)
1692 #define thegame rect
1695 const struct game thegame
= {
1696 "Rectangles", "games.rectangles",
1703 TRUE
, game_configure
, custom_params
,
1710 TRUE
, game_text_format
,
1717 game_free_drawstate
,
1721 game_wants_statusbar
,