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
39 COL_DRAG
, COL_DRAGERASE
,
50 #define INDEX(state, x, y) (((y) * (state)->w) + (x))
51 #define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
52 #define grid(state,x,y) index(state, (state)->grid, x, y)
53 #define vedge(state,x,y) index(state, (state)->vedge, x, y)
54 #define hedge(state,x,y) index(state, (state)->hedge, x, y)
56 #define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
57 (y) >= dy && (y) < (state)->h )
58 #define RANGE(state,x,y) CRANGE(state,x,y,0,0)
59 #define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
60 #define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
62 #define PREFERRED_TILE_SIZE 24
63 #define TILE_SIZE (ds->tilesize)
67 #define BORDER (TILE_SIZE * 3 / 4)
70 #define CORNER_TOLERANCE 0.15F
71 #define CENTRE_TOLERANCE 0.15F
73 #define FLASH_TIME 0.13F
75 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
76 #define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
80 int *grid
; /* contains the numbers */
81 unsigned char *vedge
; /* (w+1) x h */
82 unsigned char *hedge
; /* w x (h+1) */
83 int completed
, cheated
;
84 unsigned char *correct
;
87 static game_params
*default_params(void)
89 game_params
*ret
= snew(game_params
);
92 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
= 9, h
= 9; break;
107 case 2: w
= 11, h
= 11; break;
108 case 3: w
= 13, h
= 13; break;
109 case 4: w
= 15, h
= 15; break;
111 case 5: w
= 17, h
= 17; break;
112 case 6: w
= 19, h
= 19; break;
114 default: return FALSE
;
117 sprintf(buf
, "%dx%d", w
, h
);
119 *params
= ret
= snew(game_params
);
122 ret
->expandfactor
= 0.0F
;
127 static void free_params(game_params
*params
)
132 static game_params
*dup_params(const game_params
*params
)
134 game_params
*ret
= snew(game_params
);
135 *ret
= *params
; /* structure copy */
139 static void decode_params(game_params
*ret
, char const *string
)
141 ret
->w
= ret
->h
= atoi(string
);
142 while (*string
&& isdigit((unsigned char)*string
)) string
++;
143 if (*string
== 'x') {
145 ret
->h
= atoi(string
);
146 while (*string
&& isdigit((unsigned char)*string
)) string
++;
148 if (*string
== 'e') {
150 ret
->expandfactor
= (float)atof(string
);
152 (*string
== '.' || isdigit((unsigned char)*string
))) string
++;
154 if (*string
== 'a') {
160 static char *encode_params(const game_params
*params
, int full
)
164 sprintf(data
, "%dx%d", params
->w
, params
->h
);
165 if (full
&& params
->expandfactor
)
166 sprintf(data
+ strlen(data
), "e%g", params
->expandfactor
);
167 if (full
&& !params
->unique
)
173 static config_item
*game_configure(const game_params
*params
)
178 ret
= snewn(5, config_item
);
180 ret
[0].name
= "Width";
181 ret
[0].type
= C_STRING
;
182 sprintf(buf
, "%d", params
->w
);
183 ret
[0].sval
= dupstr(buf
);
186 ret
[1].name
= "Height";
187 ret
[1].type
= C_STRING
;
188 sprintf(buf
, "%d", params
->h
);
189 ret
[1].sval
= dupstr(buf
);
192 ret
[2].name
= "Expansion factor";
193 ret
[2].type
= C_STRING
;
194 sprintf(buf
, "%g", params
->expandfactor
);
195 ret
[2].sval
= dupstr(buf
);
198 ret
[3].name
= "Ensure unique solution";
199 ret
[3].type
= C_BOOLEAN
;
201 ret
[3].ival
= params
->unique
;
211 static game_params
*custom_params(const config_item
*cfg
)
213 game_params
*ret
= snew(game_params
);
215 ret
->w
= atoi(cfg
[0].sval
);
216 ret
->h
= atoi(cfg
[1].sval
);
217 ret
->expandfactor
= (float)atof(cfg
[2].sval
);
218 ret
->unique
= cfg
[3].ival
;
223 static char *validate_params(const game_params
*params
, int full
)
225 if (params
->w
<= 0 || params
->h
<= 0)
226 return "Width and height must both be greater than zero";
227 if (params
->w
*params
->h
< 2)
228 return "Grid area must be greater than one";
229 if (params
->expandfactor
< 0.0F
)
230 return "Expansion factor may not be negative";
251 struct point
*points
;
254 /* ----------------------------------------------------------------------
255 * Solver for Rectangles games.
257 * This solver is souped up beyond the needs of actually _solving_
258 * a puzzle. It is also designed to cope with uncertainty about
259 * where the numbers have been placed. This is because I run it on
260 * my generated grids _before_ placing the numbers, and have it
261 * tell me where I need to place the numbers to ensure a unique
265 static void remove_rect_placement(int w
, int h
,
266 struct rectlist
*rectpositions
,
268 int rectnum
, int placement
)
272 #ifdef SOLVER_DIAGNOSTICS
273 printf("ruling out rect %d placement at %d,%d w=%d h=%d\n", rectnum
,
274 rectpositions
[rectnum
].rects
[placement
].x
,
275 rectpositions
[rectnum
].rects
[placement
].y
,
276 rectpositions
[rectnum
].rects
[placement
].w
,
277 rectpositions
[rectnum
].rects
[placement
].h
);
281 * Decrement each entry in the overlaps array to reflect the
282 * removal of this rectangle placement.
284 for (yy
= 0; yy
< rectpositions
[rectnum
].rects
[placement
].h
; yy
++) {
285 y
= yy
+ rectpositions
[rectnum
].rects
[placement
].y
;
286 for (xx
= 0; xx
< rectpositions
[rectnum
].rects
[placement
].w
; xx
++) {
287 x
= xx
+ rectpositions
[rectnum
].rects
[placement
].x
;
289 assert(overlaps
[(rectnum
* h
+ y
) * w
+ x
] != 0);
291 if (overlaps
[(rectnum
* h
+ y
) * w
+ x
] > 0)
292 overlaps
[(rectnum
* h
+ y
) * w
+ x
]--;
297 * Remove the placement from the list of positions for that
298 * rectangle, by interchanging it with the one on the end.
300 if (placement
< rectpositions
[rectnum
].n
- 1) {
303 t
= rectpositions
[rectnum
].rects
[rectpositions
[rectnum
].n
- 1];
304 rectpositions
[rectnum
].rects
[rectpositions
[rectnum
].n
- 1] =
305 rectpositions
[rectnum
].rects
[placement
];
306 rectpositions
[rectnum
].rects
[placement
] = t
;
308 rectpositions
[rectnum
].n
--;
311 static void remove_number_placement(int w
, int h
, struct numberdata
*number
,
312 int index
, int *rectbyplace
)
315 * Remove the entry from the rectbyplace array.
317 rectbyplace
[number
->points
[index
].y
* w
+ number
->points
[index
].x
] = -1;
320 * Remove the placement from the list of candidates for that
321 * number, by interchanging it with the one on the end.
323 if (index
< number
->npoints
- 1) {
326 t
= number
->points
[number
->npoints
- 1];
327 number
->points
[number
->npoints
- 1] = number
->points
[index
];
328 number
->points
[index
] = t
;
334 * Returns 0 for failure to solve due to inconsistency; 1 for
335 * success; 2 for failure to complete a solution due to either
336 * ambiguity or it being too difficult.
338 static int rect_solver(int w
, int h
, int nrects
, struct numberdata
*numbers
,
339 unsigned char *hedge
, unsigned char *vedge
,
342 struct rectlist
*rectpositions
;
343 int *overlaps
, *rectbyplace
, *workspace
;
347 * Start by setting up a list of candidate positions for each
350 rectpositions
= snewn(nrects
, struct rectlist
);
351 for (i
= 0; i
< nrects
; i
++) {
352 int rw
, rh
, area
= numbers
[i
].area
;
353 int j
, minx
, miny
, maxx
, maxy
;
355 int rlistn
, rlistsize
;
358 * For each rectangle, begin by finding the bounding
359 * rectangle of its candidate number placements.
364 for (j
= 0; j
< numbers
[i
].npoints
; j
++) {
365 if (minx
> numbers
[i
].points
[j
].x
) minx
= numbers
[i
].points
[j
].x
;
366 if (miny
> numbers
[i
].points
[j
].y
) miny
= numbers
[i
].points
[j
].y
;
367 if (maxx
< numbers
[i
].points
[j
].x
) maxx
= numbers
[i
].points
[j
].x
;
368 if (maxy
< numbers
[i
].points
[j
].y
) maxy
= numbers
[i
].points
[j
].y
;
372 * Now loop over all possible rectangle placements
373 * overlapping a point within that bounding rectangle;
374 * ensure each one actually contains a candidate number
375 * placement, and add it to the list.
378 rlistn
= rlistsize
= 0;
380 for (rw
= 1; rw
<= area
&& rw
<= w
; rw
++) {
389 for (y
= miny
- rh
+ 1; y
<= maxy
; y
++) {
390 if (y
< 0 || y
+rh
> h
)
393 for (x
= minx
- rw
+ 1; x
<= maxx
; x
++) {
394 if (x
< 0 || x
+rw
> w
)
398 * See if we can find a candidate number
399 * placement within this rectangle.
401 for (j
= 0; j
< numbers
[i
].npoints
; j
++)
402 if (numbers
[i
].points
[j
].x
>= x
&&
403 numbers
[i
].points
[j
].x
< x
+rw
&&
404 numbers
[i
].points
[j
].y
>= y
&&
405 numbers
[i
].points
[j
].y
< y
+rh
)
408 if (j
< numbers
[i
].npoints
) {
410 * Add this to the list of candidate
411 * placements for this rectangle.
413 if (rlistn
>= rlistsize
) {
414 rlistsize
= rlistn
+ 32;
415 rlist
= sresize(rlist
, rlistsize
, struct rect
);
419 rlist
[rlistn
].w
= rw
;
420 rlist
[rlistn
].h
= rh
;
421 #ifdef SOLVER_DIAGNOSTICS
422 printf("rect %d [area %d]: candidate position at"
423 " %d,%d w=%d h=%d\n",
424 i
, area
, x
, y
, rw
, rh
);
432 rectpositions
[i
].rects
= rlist
;
433 rectpositions
[i
].n
= rlistn
;
437 * Next, construct a multidimensional array tracking how many
438 * candidate positions for each rectangle overlap each square.
440 * Indexing of this array is by the formula
442 * overlaps[(rectindex * h + y) * w + x]
444 * A positive or zero value indicates what it sounds as if it
445 * should; -1 indicates that this square _cannot_ be part of
446 * this rectangle; and -2 indicates that it _definitely_ is
447 * (which is distinct from 1, because one might very well know
448 * that _if_ square S is part of rectangle R then it must be
449 * because R is placed in a certain position without knowing
450 * that it definitely _is_).
452 overlaps
= snewn(nrects
* w
* h
, int);
453 memset(overlaps
, 0, nrects
* w
* h
* sizeof(int));
454 for (i
= 0; i
< nrects
; i
++) {
457 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
460 for (yy
= 0; yy
< rectpositions
[i
].rects
[j
].h
; yy
++)
461 for (xx
= 0; xx
< rectpositions
[i
].rects
[j
].w
; xx
++)
462 overlaps
[(i
* h
+ yy
+rectpositions
[i
].rects
[j
].y
) * w
+
463 xx
+rectpositions
[i
].rects
[j
].x
]++;
468 * Also we want an array covering the grid once, to make it
469 * easy to figure out which squares are candidate number
470 * placements for which rectangles. (The existence of this
471 * single array assumes that no square starts off as a
472 * candidate number placement for more than one rectangle. This
473 * assumption is justified, because this solver is _either_
474 * used to solve real problems - in which case there is a
475 * single placement for every number - _or_ used to decide on
476 * number placements for a new puzzle, in which case each
477 * number's placements are confined to the intended position of
478 * the rectangle containing that number.)
480 rectbyplace
= snewn(w
* h
, int);
481 for (i
= 0; i
< w
*h
; i
++)
484 for (i
= 0; i
< nrects
; i
++) {
487 for (j
= 0; j
< numbers
[i
].npoints
; j
++) {
488 int x
= numbers
[i
].points
[j
].x
;
489 int y
= numbers
[i
].points
[j
].y
;
491 assert(rectbyplace
[y
* w
+ x
] == -1);
492 rectbyplace
[y
* w
+ x
] = i
;
496 workspace
= snewn(nrects
, int);
499 * Now run the actual deduction loop.
502 int done_something
= FALSE
;
504 #ifdef SOLVER_DIAGNOSTICS
505 printf("starting deduction loop\n");
507 for (i
= 0; i
< nrects
; i
++) {
508 printf("rect %d overlaps:\n", i
);
511 for (y
= 0; y
< h
; y
++) {
512 for (x
= 0; x
< w
; x
++) {
513 printf("%3d", overlaps
[(i
* h
+ y
) * w
+ x
]);
519 printf("rectbyplace:\n");
522 for (y
= 0; y
< h
; y
++) {
523 for (x
= 0; x
< w
; x
++) {
524 printf("%3d", rectbyplace
[y
* w
+ x
]);
532 * Housekeeping. Look for rectangles whose number has only
533 * one candidate position left, and mark that square as
534 * known if it isn't already.
536 for (i
= 0; i
< nrects
; i
++) {
537 if (numbers
[i
].npoints
== 1) {
538 int x
= numbers
[i
].points
[0].x
;
539 int y
= numbers
[i
].points
[0].y
;
540 if (overlaps
[(i
* h
+ y
) * w
+ x
] >= -1) {
543 if (overlaps
[(i
* h
+ y
) * w
+ x
] <= 0) {
544 ret
= 0; /* inconsistency */
547 #ifdef SOLVER_DIAGNOSTICS
548 printf("marking %d,%d as known for rect %d"
549 " (sole remaining number position)\n", x
, y
, i
);
552 for (j
= 0; j
< nrects
; j
++)
553 overlaps
[(j
* h
+ y
) * w
+ x
] = -1;
555 overlaps
[(i
* h
+ y
) * w
+ x
] = -2;
561 * Now look at the intersection of all possible placements
562 * for each rectangle, and mark all squares in that
563 * intersection as known for that rectangle if they aren't
566 for (i
= 0; i
< nrects
; i
++) {
567 int minx
, miny
, maxx
, maxy
, xx
, yy
, j
;
573 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
574 int x
= rectpositions
[i
].rects
[j
].x
;
575 int y
= rectpositions
[i
].rects
[j
].y
;
576 int w
= rectpositions
[i
].rects
[j
].w
;
577 int h
= rectpositions
[i
].rects
[j
].h
;
579 if (minx
< x
) minx
= x
;
580 if (miny
< y
) miny
= y
;
581 if (maxx
> x
+w
) maxx
= x
+w
;
582 if (maxy
> y
+h
) maxy
= y
+h
;
585 for (yy
= miny
; yy
< maxy
; yy
++)
586 for (xx
= minx
; xx
< maxx
; xx
++)
587 if (overlaps
[(i
* h
+ yy
) * w
+ xx
] >= -1) {
588 if (overlaps
[(i
* h
+ yy
) * w
+ xx
] <= 0) {
589 ret
= 0; /* inconsistency */
592 #ifdef SOLVER_DIAGNOSTICS
593 printf("marking %d,%d as known for rect %d"
594 " (intersection of all placements)\n",
598 for (j
= 0; j
< nrects
; j
++)
599 overlaps
[(j
* h
+ yy
) * w
+ xx
] = -1;
601 overlaps
[(i
* h
+ yy
) * w
+ xx
] = -2;
606 * Rectangle-focused deduction. Look at each rectangle in
607 * turn and try to rule out some of its candidate
610 for (i
= 0; i
< nrects
; i
++) {
613 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
617 for (k
= 0; k
< nrects
; k
++)
620 for (yy
= 0; yy
< rectpositions
[i
].rects
[j
].h
; yy
++) {
621 int y
= yy
+ rectpositions
[i
].rects
[j
].y
;
622 for (xx
= 0; xx
< rectpositions
[i
].rects
[j
].w
; xx
++) {
623 int x
= xx
+ rectpositions
[i
].rects
[j
].x
;
625 if (overlaps
[(i
* h
+ y
) * w
+ x
] == -1) {
627 * This placement overlaps a square
628 * which is _known_ to be part of
629 * another rectangle. Therefore we must
632 #ifdef SOLVER_DIAGNOSTICS
633 printf("rect %d placement at %d,%d w=%d h=%d "
634 "contains %d,%d which is known-other\n", i
,
635 rectpositions
[i
].rects
[j
].x
,
636 rectpositions
[i
].rects
[j
].y
,
637 rectpositions
[i
].rects
[j
].w
,
638 rectpositions
[i
].rects
[j
].h
,
644 if (rectbyplace
[y
* w
+ x
] != -1) {
646 * This placement overlaps one of the
647 * candidate number placements for some
648 * rectangle. Count it.
650 workspace
[rectbyplace
[y
* w
+ x
]]++;
657 * If we haven't ruled this placement out
658 * already, see if it overlaps _all_ of the
659 * candidate number placements for any
660 * rectangle. If so, we can rule it out.
662 for (k
= 0; k
< nrects
; k
++)
663 if (k
!= i
&& workspace
[k
] == numbers
[k
].npoints
) {
664 #ifdef SOLVER_DIAGNOSTICS
665 printf("rect %d placement at %d,%d w=%d h=%d "
666 "contains all number points for rect %d\n",
668 rectpositions
[i
].rects
[j
].x
,
669 rectpositions
[i
].rects
[j
].y
,
670 rectpositions
[i
].rects
[j
].w
,
671 rectpositions
[i
].rects
[j
].h
,
679 * Failing that, see if it overlaps at least
680 * one of the candidate number placements for
681 * itself! (This might not be the case if one
682 * of those number placements has been removed
685 if (!del
&& workspace
[i
] == 0) {
686 #ifdef SOLVER_DIAGNOSTICS
687 printf("rect %d placement at %d,%d w=%d h=%d "
688 "contains none of its own number points\n",
690 rectpositions
[i
].rects
[j
].x
,
691 rectpositions
[i
].rects
[j
].y
,
692 rectpositions
[i
].rects
[j
].w
,
693 rectpositions
[i
].rects
[j
].h
);
700 remove_rect_placement(w
, h
, rectpositions
, overlaps
, i
, j
);
702 j
--; /* don't skip over next placement */
704 done_something
= TRUE
;
710 * Square-focused deduction. Look at each square not marked
711 * as known, and see if there are any which can only be
712 * part of a single rectangle.
716 for (y
= 0; y
< h
; y
++) for (x
= 0; x
< w
; x
++) {
717 /* Known squares are marked as <0 everywhere, so we only need
718 * to check the overlaps entry for rect 0. */
719 if (overlaps
[y
* w
+ x
] < 0)
720 continue; /* known already */
724 for (i
= 0; i
< nrects
; i
++)
725 if (overlaps
[(i
* h
+ y
) * w
+ x
] > 0)
732 * Now we can rule out all placements for
733 * rectangle `index' which _don't_ contain
736 #ifdef SOLVER_DIAGNOSTICS
737 printf("square %d,%d can only be in rectangle %d\n",
740 for (j
= 0; j
< rectpositions
[index
].n
; j
++) {
741 struct rect
*r
= &rectpositions
[index
].rects
[j
];
742 if (x
>= r
->x
&& x
< r
->x
+ r
->w
&&
743 y
>= r
->y
&& y
< r
->y
+ r
->h
)
744 continue; /* this one is OK */
745 remove_rect_placement(w
, h
, rectpositions
, overlaps
,
747 j
--; /* don't skip over next placement */
748 done_something
= TRUE
;
755 * If we've managed to deduce anything by normal means,
756 * loop round again and see if there's more to be done.
757 * Only if normal deduction has completely failed us should
758 * we now move on to narrowing down the possible number
765 * Now we have done everything we can with the current set
766 * of number placements. So we need to winnow the number
767 * placements so as to narrow down the possibilities. We do
768 * this by searching for a candidate placement (of _any_
769 * rectangle) which overlaps a candidate placement of the
770 * number for some other rectangle.
778 size_t nrpns
= 0, rpnsize
= 0;
781 for (i
= 0; i
< nrects
; i
++) {
782 for (j
= 0; j
< rectpositions
[i
].n
; j
++) {
785 for (yy
= 0; yy
< rectpositions
[i
].rects
[j
].h
; yy
++) {
786 int y
= yy
+ rectpositions
[i
].rects
[j
].y
;
787 for (xx
= 0; xx
< rectpositions
[i
].rects
[j
].w
; xx
++) {
788 int x
= xx
+ rectpositions
[i
].rects
[j
].x
;
790 if (rectbyplace
[y
* w
+ x
] >= 0 &&
791 rectbyplace
[y
* w
+ x
] != i
) {
793 * Add this to the list of
794 * winnowing possibilities.
796 if (nrpns
>= rpnsize
) {
797 rpnsize
= rpnsize
* 3 / 2 + 32;
798 rpns
= sresize(rpns
, rpnsize
, struct rpn
);
800 rpns
[nrpns
].rect
= i
;
801 rpns
[nrpns
].placement
= j
;
802 rpns
[nrpns
].number
= rectbyplace
[y
* w
+ x
];
811 #ifdef SOLVER_DIAGNOSTICS
812 printf("%d candidate rect placements we could eliminate\n", nrpns
);
816 * Now choose one of these unwanted rectangle
817 * placements, and eliminate it.
819 int index
= random_upto(rs
, nrpns
);
821 struct rpn rpn
= rpns
[index
];
828 r
= rectpositions
[i
].rects
[j
];
831 * We rule out placement j of rectangle i by means
832 * of removing all of rectangle k's candidate
833 * number placements which do _not_ overlap it.
834 * This will ensure that it is eliminated during
835 * the next pass of rectangle-focused deduction.
837 #ifdef SOLVER_DIAGNOSTICS
838 printf("ensuring number for rect %d is within"
839 " rect %d's placement at %d,%d w=%d h=%d\n",
840 k
, i
, r
.x
, r
.y
, r
.w
, r
.h
);
843 for (m
= 0; m
< numbers
[k
].npoints
; m
++) {
844 int x
= numbers
[k
].points
[m
].x
;
845 int y
= numbers
[k
].points
[m
].y
;
847 if (x
< r
.x
|| x
>= r
.x
+ r
.w
||
848 y
< r
.y
|| y
>= r
.y
+ r
.h
) {
849 #ifdef SOLVER_DIAGNOSTICS
850 printf("eliminating number for rect %d at %d,%d\n",
853 remove_number_placement(w
, h
, &numbers
[k
],
855 m
--; /* don't skip the next one */
856 done_something
= TRUE
;
862 if (!done_something
) {
863 #ifdef SOLVER_DIAGNOSTICS
864 printf("terminating deduction loop\n");
872 for (i
= 0; i
< nrects
; i
++) {
873 #ifdef SOLVER_DIAGNOSTICS
874 printf("rect %d has %d possible placements\n",
875 i
, rectpositions
[i
].n
);
877 if (rectpositions
[i
].n
<= 0) {
878 ret
= 0; /* inconsistency */
879 } else if (rectpositions
[i
].n
> 1) {
880 ret
= 2; /* remaining uncertainty */
881 } else if (hedge
&& vedge
) {
883 * Place the rectangle in its only possible position.
886 struct rect
*r
= &rectpositions
[i
].rects
[0];
888 for (y
= 0; y
< r
->h
; y
++) {
890 vedge
[(r
->y
+y
) * w
+ r
->x
] = 1;
892 vedge
[(r
->y
+y
) * w
+ r
->x
+r
->w
] = 1;
894 for (x
= 0; x
< r
->w
; x
++) {
896 hedge
[r
->y
* w
+ r
->x
+x
] = 1;
898 hedge
[(r
->y
+r
->h
) * w
+ r
->x
+x
] = 1;
904 * Free up all allocated storage.
909 for (i
= 0; i
< nrects
; i
++)
910 sfree(rectpositions
[i
].rects
);
911 sfree(rectpositions
);
916 /* ----------------------------------------------------------------------
917 * Grid generation code.
921 * This function does one of two things. If passed r==NULL, it
922 * counts the number of possible rectangles which cover the given
923 * square, and returns it in *n. If passed r!=NULL then it _reads_
924 * *n to find an index, counts the possible rectangles until it
925 * reaches the nth, and writes it into r.
927 * `scratch' is expected to point to an array of 2 * params->w
928 * ints, used internally as scratch space (and passed in like this
929 * to avoid re-allocating and re-freeing it every time round a
932 static void enum_rects(game_params
*params
, int *grid
, struct rect
*r
, int *n
,
933 int sx
, int sy
, int *scratch
)
937 int maxarea
, realmaxarea
;
942 * Maximum rectangle area is 1/6 of total grid size, unless
943 * this means we can't place any rectangles at all in which
944 * case we set it to 2 at minimum.
946 maxarea
= params
->w
* params
->h
/ 6;
951 * Scan the grid to find the limits of the region within which
952 * any rectangle containing this point must fall. This will
953 * save us trawling the inside of every rectangle later on to
954 * see if it contains any used squares.
957 bottom
= scratch
+ params
->w
;
958 for (dy
= -1; dy
<= +1; dy
+= 2) {
959 int *array
= (dy
== -1 ? top
: bottom
);
960 for (dx
= -1; dx
<= +1; dx
+= 2) {
961 for (x
= sx
; x
>= 0 && x
< params
->w
; x
+= dx
) {
962 array
[x
] = -2 * params
->h
* dy
;
963 for (y
= sy
; y
>= 0 && y
< params
->h
; y
+= dy
) {
964 if (index(params
, grid
, x
, y
) == -1 &&
965 (x
== sx
|| dy
*y
<= dy
*array
[x
-dx
]))
975 * Now scan again to work out the largest rectangles we can fit
976 * in the grid, so that we can terminate the following loops
977 * early once we get down to not having much space left in the
981 for (x
= 0; x
< params
->w
; x
++) {
984 rh
= bottom
[x
] - top
[x
] + 1;
986 continue; /* no rectangles can start here */
988 dx
= (x
> sx
? -1 : +1);
989 for (x2
= x
; x2
>= 0 && x2
< params
->w
; x2
+= dx
)
990 if (bottom
[x2
] < bottom
[x
] || top
[x2
] > top
[x
])
994 if (realmaxarea
< rw
* rh
)
995 realmaxarea
= rw
* rh
;
998 if (realmaxarea
> maxarea
)
999 realmaxarea
= maxarea
;
1002 * Rectangles which go right the way across the grid are
1003 * boring, although they can't be helped in the case of
1004 * extremely small grids. (Also they might be generated later
1005 * on by the singleton-removal process; we can't help that.)
1012 for (rw
= 1; rw
<= mw
; rw
++)
1013 for (rh
= 1; rh
<= mh
; rh
++) {
1014 if (rw
* rh
> realmaxarea
)
1018 for (x
= max(sx
- rw
+ 1, 0); x
<= min(sx
, params
->w
- rw
); x
++)
1019 for (y
= max(sy
- rh
+ 1, 0); y
<= min(sy
, params
->h
- rh
);
1022 * Check this rectangle against the region we
1025 if (top
[x
] <= y
&& top
[x
+rw
-1] <= y
&&
1026 bottom
[x
] >= y
+rh
-1 && bottom
[x
+rw
-1] >= y
+rh
-1) {
1027 if (r
&& index
== *n
) {
1043 static void place_rect(game_params
*params
, int *grid
, struct rect r
)
1045 int idx
= INDEX(params
, r
.x
, r
.y
);
1048 for (x
= r
.x
; x
< r
.x
+r
.w
; x
++)
1049 for (y
= r
.y
; y
< r
.y
+r
.h
; y
++) {
1050 index(params
, grid
, x
, y
) = idx
;
1052 #ifdef GENERATION_DIAGNOSTICS
1053 printf(" placing rectangle at (%d,%d) size %d x %d\n",
1054 r
.x
, r
.y
, r
.w
, r
.h
);
1058 static struct rect
find_rect(game_params
*params
, int *grid
, int x
, int y
)
1064 * Find the top left of the rectangle.
1066 idx
= index(params
, grid
, x
, y
);
1072 return r
; /* 1x1 singleton here */
1075 y
= idx
/ params
->w
;
1076 x
= idx
% params
->w
;
1079 * Find the width and height of the rectangle.
1082 (x
+w
< params
->w
&& index(params
,grid
,x
+w
,y
)==idx
);
1085 (y
+h
< params
->h
&& index(params
,grid
,x
,y
+h
)==idx
);
1096 #ifdef GENERATION_DIAGNOSTICS
1097 static void display_grid(game_params
*params
, int *grid
, int *numbers
, int all
)
1099 unsigned char *egrid
= snewn((params
->w
*2+3) * (params
->h
*2+3),
1102 int r
= (params
->w
*2+3);
1104 memset(egrid
, 0, (params
->w
*2+3) * (params
->h
*2+3));
1106 for (x
= 0; x
< params
->w
; x
++)
1107 for (y
= 0; y
< params
->h
; y
++) {
1108 int i
= index(params
, grid
, x
, y
);
1109 if (x
== 0 || index(params
, grid
, x
-1, y
) != i
)
1110 egrid
[(2*y
+2) * r
+ (2*x
+1)] = 1;
1111 if (x
== params
->w
-1 || index(params
, grid
, x
+1, y
) != i
)
1112 egrid
[(2*y
+2) * r
+ (2*x
+3)] = 1;
1113 if (y
== 0 || index(params
, grid
, x
, y
-1) != i
)
1114 egrid
[(2*y
+1) * r
+ (2*x
+2)] = 1;
1115 if (y
== params
->h
-1 || index(params
, grid
, x
, y
+1) != i
)
1116 egrid
[(2*y
+3) * r
+ (2*x
+2)] = 1;
1119 for (y
= 1; y
< 2*params
->h
+2; y
++) {
1120 for (x
= 1; x
< 2*params
->w
+2; x
++) {
1122 int k
= numbers
? index(params
, numbers
, x
/2-1, y
/2-1) : 0;
1123 if (k
|| (all
&& numbers
)) printf("%2d", k
); else printf(" ");
1124 } else if (!((y
&x
)&1)) {
1125 int v
= egrid
[y
*r
+x
];
1126 if ((y
&1) && v
) v
= '-';
1127 if ((x
&1) && v
) v
= '|';
1130 if (!(x
&1)) putchar(v
);
1133 if (egrid
[y
*r
+(x
+1)]) d
|= 1;
1134 if (egrid
[(y
-1)*r
+x
]) d
|= 2;
1135 if (egrid
[y
*r
+(x
-1)]) d
|= 4;
1136 if (egrid
[(y
+1)*r
+x
]) d
|= 8;
1137 c
= " ??+?-++?+|+++++"[d
];
1139 if (!(x
&1)) putchar(c
);
1149 static char *new_game_desc(const game_params
*params_in
, random_state
*rs
,
1150 char **aux
, int interactive
)
1152 game_params params_copy
= *params_in
; /* structure copy */
1153 game_params
*params
= ¶ms_copy
;
1154 int *grid
, *numbers
= NULL
;
1155 int x
, y
, y2
, y2last
, yx
, run
, i
, nsquares
;
1157 int *enum_rects_scratch
;
1158 game_params params2real
, *params2
= ¶ms2real
;
1162 * Set up the smaller width and height which we will use to
1163 * generate the base grid.
1165 params2
->w
= (int)((float)params
->w
/ (1.0F
+ params
->expandfactor
));
1166 if (params2
->w
< 2 && params
->w
>= 2) params2
->w
= 2;
1167 params2
->h
= (int)((float)params
->h
/ (1.0F
+ params
->expandfactor
));
1168 if (params2
->h
< 2 && params
->h
>= 2) params2
->h
= 2;
1170 grid
= snewn(params2
->w
* params2
->h
, int);
1172 enum_rects_scratch
= snewn(2 * params2
->w
, int);
1175 for (y
= 0; y
< params2
->h
; y
++)
1176 for (x
= 0; x
< params2
->w
; x
++) {
1177 index(params2
, grid
, x
, y
) = -1;
1182 * Place rectangles until we can't any more. We do this by
1183 * finding a square we haven't yet covered, and randomly
1184 * choosing a rectangle to cover it.
1187 while (nsquares
> 0) {
1188 int square
= random_upto(rs
, nsquares
);
1194 for (y
= 0; y
< params2
->h
; y
++) {
1195 for (x
= 0; x
< params2
->w
; x
++) {
1196 if (index(params2
, grid
, x
, y
) == -1 && square
-- == 0)
1202 assert(x
< params2
->w
&& y
< params2
->h
);
1205 * Now see how many rectangles fit around this one.
1207 enum_rects(params2
, grid
, NULL
, &n
, x
, y
, enum_rects_scratch
);
1211 * There are no possible rectangles covering this
1212 * square, meaning it must be a singleton. Mark it
1213 * -2 so we know not to keep trying.
1215 index(params2
, grid
, x
, y
) = -2;
1219 * Pick one at random.
1221 n
= random_upto(rs
, n
);
1222 enum_rects(params2
, grid
, &r
, &n
, x
, y
, enum_rects_scratch
);
1227 place_rect(params2
, grid
, r
);
1228 nsquares
-= r
.w
* r
.h
;
1232 sfree(enum_rects_scratch
);
1235 * Deal with singleton spaces remaining in the grid, one by
1238 * We do this by making a local change to the layout. There are
1239 * several possibilities:
1241 * +-----+-----+ Here, we can remove the singleton by
1242 * | | | extending the 1x2 rectangle below it
1243 * +--+--+-----+ into a 1x3.
1251 * +--+--+--+ Here, that trick doesn't work: there's no
1252 * | | | 1 x n rectangle with the singleton at one
1253 * | | | end. Instead, we extend a 1 x n rectangle
1254 * | | | _out_ from the singleton, shaving a layer
1255 * +--+--+ | off the end of another rectangle. So if we
1256 * | | | | extended up, we'd make our singleton part
1257 * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2
1258 * | | | used to be; or we could extend right into
1259 * +--+-----+ a 2x1, turning the 1x3 into a 1x2.
1261 * +-----+--+ Here, we can't even do _that_, since any
1262 * | | | direction we choose to extend the singleton
1263 * +--+--+ | will produce a new singleton as a result of
1264 * | | | | truncating one of the size-2 rectangles.
1265 * | +--+--+ Fortunately, this case can _only_ occur when
1266 * | | | a singleton is surrounded by four size-2s
1267 * +--+-----+ in this fashion; so instead we can simply
1268 * replace the whole section with a single 3x3.
1270 for (x
= 0; x
< params2
->w
; x
++) {
1271 for (y
= 0; y
< params2
->h
; y
++) {
1272 if (index(params2
, grid
, x
, y
) < 0) {
1275 #ifdef GENERATION_DIAGNOSTICS
1276 display_grid(params2
, grid
, NULL
, FALSE
);
1277 printf("singleton at %d,%d\n", x
, y
);
1281 * Check in which directions we can feasibly extend
1282 * the singleton. We can extend in a particular
1283 * direction iff either:
1285 * - the rectangle on that side of the singleton
1286 * is not 2x1, and we are at one end of the edge
1287 * of it we are touching
1289 * - it is 2x1 but we are on its short side.
1291 * FIXME: we could plausibly choose between these
1292 * based on the sizes of the rectangles they would
1296 if (x
< params2
->w
-1) {
1297 struct rect r
= find_rect(params2
, grid
, x
+1, y
);
1298 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
1299 dirs
[ndirs
++] = 1; /* right */
1302 struct rect r
= find_rect(params2
, grid
, x
, y
-1);
1303 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
1304 dirs
[ndirs
++] = 2; /* up */
1307 struct rect r
= find_rect(params2
, grid
, x
-1, y
);
1308 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
1309 dirs
[ndirs
++] = 4; /* left */
1311 if (y
< params2
->h
-1) {
1312 struct rect r
= find_rect(params2
, grid
, x
, y
+1);
1313 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
1314 dirs
[ndirs
++] = 8; /* down */
1320 memset(&r1
, 0, sizeof(struct rect
));
1321 memset(&r2
, 0, sizeof(struct rect
));
1322 which
= random_upto(rs
, ndirs
);
1327 assert(x
< params2
->w
+1);
1328 #ifdef GENERATION_DIAGNOSTICS
1329 printf("extending right\n");
1331 r1
= find_rect(params2
, grid
, x
+1, y
);
1342 #ifdef GENERATION_DIAGNOSTICS
1343 printf("extending up\n");
1345 r1
= find_rect(params2
, grid
, x
, y
-1);
1356 #ifdef GENERATION_DIAGNOSTICS
1357 printf("extending left\n");
1359 r1
= find_rect(params2
, grid
, x
-1, y
);
1369 assert(y
< params2
->h
+1);
1370 #ifdef GENERATION_DIAGNOSTICS
1371 printf("extending down\n");
1373 r1
= find_rect(params2
, grid
, x
, y
+1);
1382 default: /* should never happen */
1383 assert(!"invalid direction");
1385 if (r1
.h
> 0 && r1
.w
> 0)
1386 place_rect(params2
, grid
, r1
);
1387 place_rect(params2
, grid
, r2
);
1391 * Sanity-check that there really is a 3x3
1392 * rectangle surrounding this singleton and it
1393 * contains absolutely everything we could
1398 assert(x
> 0 && x
< params2
->w
-1);
1399 assert(y
> 0 && y
< params2
->h
-1);
1401 for (xx
= x
-1; xx
<= x
+1; xx
++)
1402 for (yy
= y
-1; yy
<= y
+1; yy
++) {
1403 struct rect r
= find_rect(params2
,grid
,xx
,yy
);
1406 assert(r
.x
+r
.w
-1 <= x
+1);
1407 assert(r
.y
+r
.h
-1 <= y
+1);
1412 #ifdef GENERATION_DIAGNOSTICS
1413 printf("need the 3x3 trick\n");
1417 * FIXME: If the maximum rectangle area for
1418 * this grid is less than 9, we ought to
1419 * subdivide the 3x3 in some fashion. There are
1420 * five other possibilities:
1423 * - a 4, a 3 and a 2
1425 * - a 3 and three 2s (two different arrangements).
1433 place_rect(params2
, grid
, r
);
1441 * We have now constructed a grid of the size specified in
1442 * params2. Now we extend it into a grid of the size specified
1443 * in params. We do this in two passes: we extend it vertically
1444 * until it's the right height, then we transpose it, then
1445 * extend it vertically again (getting it effectively the right
1446 * width), then finally transpose again.
1448 for (i
= 0; i
< 2; i
++) {
1449 int *grid2
, *expand
, *where
;
1450 game_params params3real
, *params3
= ¶ms3real
;
1452 #ifdef GENERATION_DIAGNOSTICS
1453 printf("before expansion:\n");
1454 display_grid(params2
, grid
, NULL
, TRUE
);
1458 * Set up the new grid.
1460 grid2
= snewn(params2
->w
* params
->h
, int);
1461 expand
= snewn(params2
->h
-1, int);
1462 where
= snewn(params2
->w
, int);
1463 params3
->w
= params2
->w
;
1464 params3
->h
= params
->h
;
1467 * Decide which horizontal edges are going to get expanded,
1470 for (y
= 0; y
< params2
->h
-1; y
++)
1472 for (y
= params2
->h
; y
< params
->h
; y
++) {
1473 x
= random_upto(rs
, params2
->h
-1);
1477 #ifdef GENERATION_DIAGNOSTICS
1478 printf("expand[] = {");
1479 for (y
= 0; y
< params2
->h
-1; y
++)
1480 printf(" %d", expand
[y
]);
1485 * Perform the expansion. The way this works is that we
1488 * - copy a row from grid into grid2
1490 * - invent some number of additional rows in grid2 where
1491 * there was previously only a horizontal line between
1492 * rows in grid, and make random decisions about where
1493 * among these to place each rectangle edge that ran
1496 for (y
= y2
= y2last
= 0; y
< params2
->h
; y
++) {
1498 * Copy a single line from row y of grid into row y2 of
1501 for (x
= 0; x
< params2
->w
; x
++) {
1502 int val
= index(params2
, grid
, x
, y
);
1503 if (val
/ params2
->w
== y
&& /* rect starts on this line */
1504 (y2
== 0 || /* we're at the very top, or... */
1505 index(params3
, grid2
, x
, y2
-1) / params3
->w
< y2last
1506 /* this rect isn't already started */))
1507 index(params3
, grid2
, x
, y2
) =
1508 INDEX(params3
, val
% params2
->w
, y2
);
1510 index(params3
, grid2
, x
, y2
) =
1511 index(params3
, grid2
, x
, y2
-1);
1515 * If that was the last line, terminate the loop early.
1517 if (++y2
== params3
->h
)
1523 * Invent some number of additional lines. First walk
1524 * along this line working out where to put all the
1525 * edges that coincide with it.
1528 for (x
= 0; x
< params2
->w
; x
++) {
1529 if (index(params2
, grid
, x
, y
) !=
1530 index(params2
, grid
, x
, y
+1)) {
1532 * This is a horizontal edge, so it needs
1536 (index(params2
, grid
, x
-1, y
) !=
1537 index(params2
, grid
, x
, y
) &&
1538 index(params2
, grid
, x
-1, y
+1) !=
1539 index(params2
, grid
, x
, y
+1))) {
1541 * Here we have the chance to make a new
1544 yx
= random_upto(rs
, expand
[y
]+1);
1547 * Here we just reuse the previous value of
1556 for (yx
= 0; yx
< expand
[y
]; yx
++) {
1558 * Invent a single row. For each square in the row,
1559 * we copy the grid entry from the square above it,
1560 * unless we're starting the new rectangle here.
1562 for (x
= 0; x
< params2
->w
; x
++) {
1563 if (yx
== where
[x
]) {
1564 int val
= index(params2
, grid
, x
, y
+1);
1566 val
= INDEX(params3
, val
, y2
);
1567 index(params3
, grid2
, x
, y2
) = val
;
1569 index(params3
, grid2
, x
, y2
) =
1570 index(params3
, grid2
, x
, y2
-1);
1580 #ifdef GENERATION_DIAGNOSTICS
1581 printf("after expansion:\n");
1582 display_grid(params3
, grid2
, NULL
, TRUE
);
1587 params2
->w
= params3
->h
;
1588 params2
->h
= params3
->w
;
1590 grid
= snewn(params2
->w
* params2
->h
, int);
1591 for (x
= 0; x
< params2
->w
; x
++)
1592 for (y
= 0; y
< params2
->h
; y
++) {
1593 int idx1
= INDEX(params2
, x
, y
);
1594 int idx2
= INDEX(params3
, y
, x
);
1598 tmp
= (tmp
% params3
->w
) * params2
->w
+ (tmp
/ params3
->w
);
1607 params
->w
= params
->h
;
1611 #ifdef GENERATION_DIAGNOSTICS
1612 printf("after transposition:\n");
1613 display_grid(params2
, grid
, NULL
, TRUE
);
1618 * Run the solver to narrow down the possible number
1622 struct numberdata
*nd
;
1623 int nnumbers
, i
, ret
;
1625 /* Count the rectangles. */
1627 for (y
= 0; y
< params
->h
; y
++) {
1628 for (x
= 0; x
< params
->w
; x
++) {
1629 int idx
= INDEX(params
, x
, y
);
1630 if (index(params
, grid
, x
, y
) == idx
)
1635 nd
= snewn(nnumbers
, struct numberdata
);
1637 /* Now set up each number's candidate position list. */
1639 for (y
= 0; y
< params
->h
; y
++) {
1640 for (x
= 0; x
< params
->w
; x
++) {
1641 int idx
= INDEX(params
, x
, y
);
1642 if (index(params
, grid
, x
, y
) == idx
) {
1643 struct rect r
= find_rect(params
, grid
, x
, y
);
1646 nd
[i
].area
= r
.w
* r
.h
;
1647 nd
[i
].npoints
= nd
[i
].area
;
1648 nd
[i
].points
= snewn(nd
[i
].npoints
, struct point
);
1650 for (j
= 0; j
< r
.h
; j
++)
1651 for (k
= 0; k
< r
.w
; k
++) {
1652 nd
[i
].points
[m
].x
= k
+ r
.x
;
1653 nd
[i
].points
[m
].y
= j
+ r
.y
;
1656 assert(m
== nd
[i
].npoints
);
1664 ret
= rect_solver(params
->w
, params
->h
, nnumbers
, nd
,
1667 ret
= 1; /* allow any number placement at all */
1671 * Now place the numbers according to the solver's
1674 numbers
= snewn(params
->w
* params
->h
, int);
1676 for (y
= 0; y
< params
->h
; y
++)
1677 for (x
= 0; x
< params
->w
; x
++) {
1678 index(params
, numbers
, x
, y
) = 0;
1681 for (i
= 0; i
< nnumbers
; i
++) {
1682 int idx
= random_upto(rs
, nd
[i
].npoints
);
1683 int x
= nd
[i
].points
[idx
].x
;
1684 int y
= nd
[i
].points
[idx
].y
;
1685 index(params
,numbers
,x
,y
) = nd
[i
].area
;
1692 for (i
= 0; i
< nnumbers
; i
++)
1693 sfree(nd
[i
].points
);
1697 * If we've succeeded, then terminate the loop.
1704 * Give up and go round again.
1710 * Store the solution in aux.
1716 len
= 2 + (params
->w
-1)*params
->h
+ (params
->h
-1)*params
->w
;
1717 ai
= snewn(len
, char);
1723 for (y
= 0; y
< params
->h
; y
++)
1724 for (x
= 1; x
< params
->w
; x
++)
1725 *p
++ = (index(params
, grid
, x
, y
) !=
1726 index(params
, grid
, x
-1, y
) ? '1' : '0');
1728 for (y
= 1; y
< params
->h
; y
++)
1729 for (x
= 0; x
< params
->w
; x
++)
1730 *p
++ = (index(params
, grid
, x
, y
) !=
1731 index(params
, grid
, x
, y
-1) ? '1' : '0');
1733 assert(p
- ai
== len
-1);
1739 #ifdef GENERATION_DIAGNOSTICS
1740 display_grid(params
, grid
, numbers
, FALSE
);
1743 desc
= snewn(11 * params
->w
* params
->h
, char);
1746 for (i
= 0; i
<= params
->w
* params
->h
; i
++) {
1747 int n
= (i
< params
->w
* params
->h
? numbers
[i
] : -1);
1754 int c
= 'a' - 1 + run
;
1758 run
-= c
- ('a' - 1);
1762 * If there's a number in the very top left or
1763 * bottom right, there's no point putting an
1764 * unnecessary _ before or after it.
1766 if (p
> desc
&& n
> 0)
1770 p
+= sprintf(p
, "%d", n
);
1782 static char *validate_desc(const game_params
*params
, const char *desc
)
1784 int area
= params
->w
* params
->h
;
1789 if (n
>= 'a' && n
<= 'z') {
1790 squares
+= n
- 'a' + 1;
1791 } else if (n
== '_') {
1793 } else if (n
> '0' && n
<= '9') {
1795 while (*desc
>= '0' && *desc
<= '9')
1798 return "Invalid character in game description";
1802 return "Not enough data to fill grid";
1805 return "Too much data to fit in grid";
1810 static unsigned char *get_correct(game_state
*state
)
1815 ret
= snewn(state
->w
* state
->h
, unsigned char);
1816 memset(ret
, 0xFF, state
->w
* state
->h
);
1818 for (x
= 0; x
< state
->w
; x
++)
1819 for (y
= 0; y
< state
->h
; y
++)
1820 if (index(state
,ret
,x
,y
) == 0xFF) {
1823 int num
, area
, valid
;
1826 * Find a rectangle starting at this point.
1829 while (x
+rw
< state
->w
&& !vedge(state
,x
+rw
,y
))
1832 while (y
+rh
< state
->h
&& !hedge(state
,x
,y
+rh
))
1836 * We know what the dimensions of the rectangle
1837 * should be if it's there at all. Find out if we
1838 * really have a valid rectangle.
1841 /* Check the horizontal edges. */
1842 for (xx
= x
; xx
< x
+rw
; xx
++) {
1843 for (yy
= y
; yy
<= y
+rh
; yy
++) {
1844 int e
= !HRANGE(state
,xx
,yy
) || hedge(state
,xx
,yy
);
1845 int ec
= (yy
== y
|| yy
== y
+rh
);
1850 /* Check the vertical edges. */
1851 for (yy
= y
; yy
< y
+rh
; yy
++) {
1852 for (xx
= x
; xx
<= x
+rw
; xx
++) {
1853 int e
= !VRANGE(state
,xx
,yy
) || vedge(state
,xx
,yy
);
1854 int ec
= (xx
== x
|| xx
== x
+rw
);
1861 * If this is not a valid rectangle with no other
1862 * edges inside it, we just mark this square as not
1863 * complete and proceed to the next square.
1866 index(state
, ret
, x
, y
) = 0;
1871 * We have a rectangle. Now see what its area is,
1872 * and how many numbers are in it.
1876 for (xx
= x
; xx
< x
+rw
; xx
++) {
1877 for (yy
= y
; yy
< y
+rh
; yy
++) {
1879 if (grid(state
,xx
,yy
)) {
1881 valid
= FALSE
; /* two numbers */
1882 num
= grid(state
,xx
,yy
);
1890 * Now fill in the whole rectangle based on the
1893 for (xx
= x
; xx
< x
+rw
; xx
++) {
1894 for (yy
= y
; yy
< y
+rh
; yy
++) {
1895 index(state
, ret
, xx
, yy
) = valid
;
1903 static game_state
*new_game(midend
*me
, const game_params
*params
,
1906 game_state
*state
= snew(game_state
);
1909 state
->w
= params
->w
;
1910 state
->h
= params
->h
;
1912 area
= state
->w
* state
->h
;
1914 state
->grid
= snewn(area
, int);
1915 state
->vedge
= snewn(area
, unsigned char);
1916 state
->hedge
= snewn(area
, unsigned char);
1917 state
->completed
= state
->cheated
= FALSE
;
1922 if (n
>= 'a' && n
<= 'z') {
1923 int run
= n
- 'a' + 1;
1924 assert(i
+ run
<= area
);
1926 state
->grid
[i
++] = 0;
1927 } else if (n
== '_') {
1929 } else if (n
> '0' && n
<= '9') {
1931 state
->grid
[i
++] = atoi(desc
-1);
1932 while (*desc
>= '0' && *desc
<= '9')
1935 assert(!"We can't get here");
1940 for (y
= 0; y
< state
->h
; y
++)
1941 for (x
= 0; x
< state
->w
; x
++)
1942 vedge(state
,x
,y
) = hedge(state
,x
,y
) = 0;
1944 state
->correct
= get_correct(state
);
1949 static game_state
*dup_game(const game_state
*state
)
1951 game_state
*ret
= snew(game_state
);
1956 ret
->vedge
= snewn(state
->w
* state
->h
, unsigned char);
1957 ret
->hedge
= snewn(state
->w
* state
->h
, unsigned char);
1958 ret
->grid
= snewn(state
->w
* state
->h
, int);
1959 ret
->correct
= snewn(ret
->w
* ret
->h
, unsigned char);
1961 ret
->completed
= state
->completed
;
1962 ret
->cheated
= state
->cheated
;
1964 memcpy(ret
->grid
, state
->grid
, state
->w
* state
->h
* sizeof(int));
1965 memcpy(ret
->vedge
, state
->vedge
, state
->w
*state
->h
*sizeof(unsigned char));
1966 memcpy(ret
->hedge
, state
->hedge
, state
->w
*state
->h
*sizeof(unsigned char));
1968 memcpy(ret
->correct
, state
->correct
, state
->w
*state
->h
*sizeof(unsigned char));
1973 static void free_game(game_state
*state
)
1976 sfree(state
->vedge
);
1977 sfree(state
->hedge
);
1978 sfree(state
->correct
);
1982 static char *solve_game(const game_state
*state
, const game_state
*currstate
,
1983 const char *ai
, char **error
)
1985 unsigned char *vedge
, *hedge
;
1989 struct numberdata
*nd
;
1995 * Attempt the in-built solver.
1998 /* Set up each number's (very short) candidate position list. */
1999 for (i
= n
= 0; i
< state
->h
* state
->w
; i
++)
2003 nd
= snewn(n
, struct numberdata
);
2005 for (i
= j
= 0; i
< state
->h
* state
->w
; i
++)
2006 if (state
->grid
[i
]) {
2007 nd
[j
].area
= state
->grid
[i
];
2009 nd
[j
].points
= snewn(1, struct point
);
2010 nd
[j
].points
[0].x
= i
% state
->w
;
2011 nd
[j
].points
[0].y
= i
/ state
->w
;
2017 vedge
= snewn(state
->w
* state
->h
, unsigned char);
2018 hedge
= snewn(state
->w
* state
->h
, unsigned char);
2019 memset(vedge
, 0, state
->w
* state
->h
);
2020 memset(hedge
, 0, state
->w
* state
->h
);
2022 rect_solver(state
->w
, state
->h
, n
, nd
, hedge
, vedge
, NULL
);
2027 for (i
= 0; i
< n
; i
++)
2028 sfree(nd
[i
].points
);
2031 len
= 2 + (state
->w
-1)*state
->h
+ (state
->h
-1)*state
->w
;
2032 ret
= snewn(len
, char);
2036 for (y
= 0; y
< state
->h
; y
++)
2037 for (x
= 1; x
< state
->w
; x
++)
2038 *p
++ = vedge
[y
*state
->w
+x
] ? '1' : '0';
2039 for (y
= 1; y
< state
->h
; y
++)
2040 for (x
= 0; x
< state
->w
; x
++)
2041 *p
++ = hedge
[y
*state
->w
+x
] ? '1' : '0';
2043 assert(p
- ret
== len
);
2051 static int game_can_format_as_text_now(const game_params
*params
)
2056 static char *game_text_format(const game_state
*state
)
2058 char *ret
, *p
, buf
[80];
2059 int i
, x
, y
, col
, maxlen
;
2062 * First determine the number of spaces required to display a
2063 * number. We'll use at least two, because one looks a bit
2067 for (i
= 0; i
< state
->w
* state
->h
; i
++) {
2068 x
= sprintf(buf
, "%d", state
->grid
[i
]);
2069 if (col
< x
) col
= x
;
2073 * Now we know the exact total size of the grid we're going to
2074 * produce: it's got 2*h+1 rows, each containing w lots of col,
2075 * w+1 boundary characters and a trailing newline.
2077 maxlen
= (2*state
->h
+1) * (state
->w
* (col
+1) + 2);
2079 ret
= snewn(maxlen
+1, char);
2082 for (y
= 0; y
<= 2*state
->h
; y
++) {
2083 for (x
= 0; x
<= 2*state
->w
; x
++) {
2088 int v
= grid(state
, x
/2, y
/2);
2090 sprintf(buf
, "%*d", col
, v
);
2092 sprintf(buf
, "%*s", col
, "");
2093 memcpy(p
, buf
, col
);
2097 * Display a horizontal edge or nothing.
2099 int h
= (y
==0 || y
==2*state
->h
? 1 :
2100 HRANGE(state
, x
/2, y
/2) && hedge(state
, x
/2, y
/2));
2106 for (i
= 0; i
< col
; i
++)
2110 * Display a vertical edge or nothing.
2112 int v
= (x
==0 || x
==2*state
->w
? 1 :
2113 VRANGE(state
, x
/2, y
/2) && vedge(state
, x
/2, y
/2));
2120 * Display a corner, or a vertical edge, or a
2121 * horizontal edge, or nothing.
2123 int hl
= (y
==0 || y
==2*state
->h
? 1 :
2124 HRANGE(state
, (x
-1)/2, y
/2) && hedge(state
, (x
-1)/2, y
/2));
2125 int hr
= (y
==0 || y
==2*state
->h
? 1 :
2126 HRANGE(state
, (x
+1)/2, y
/2) && hedge(state
, (x
+1)/2, y
/2));
2127 int vu
= (x
==0 || x
==2*state
->w
? 1 :
2128 VRANGE(state
, x
/2, (y
-1)/2) && vedge(state
, x
/2, (y
-1)/2));
2129 int vd
= (x
==0 || x
==2*state
->w
? 1 :
2130 VRANGE(state
, x
/2, (y
+1)/2) && vedge(state
, x
/2, (y
+1)/2));
2131 if (!hl
&& !hr
&& !vu
&& !vd
)
2133 else if (hl
&& hr
&& !vu
&& !vd
)
2135 else if (!hl
&& !hr
&& vu
&& vd
)
2144 assert(p
- ret
== maxlen
);
2151 * These coordinates are 2 times the obvious grid coordinates.
2152 * Hence, the top left of the grid is (0,0), the grid point to
2153 * the right of that is (2,0), the one _below that_ is (2,2)
2154 * and so on. This is so that we can specify a drag start point
2155 * on an edge (one odd coordinate) or in the middle of a square
2156 * (two odd coordinates) rather than always at a corner.
2158 * -1,-1 means no drag is in progress.
2165 * This flag is set as soon as a dragging action moves the
2166 * mouse pointer away from its starting point, so that even if
2167 * the pointer _returns_ to its starting point the action is
2168 * treated as a small drag rather than a click.
2171 /* This flag is set if we're doing an erase operation (i.e.
2172 * removing edges in the centre of the rectangle without altering
2177 * These are the co-ordinates of the top-left and bottom-right squares
2178 * in the drag box, respectively, or -1 otherwise.
2185 * These are the coordinates of a cursor, whether it's visible, and
2186 * whether it was used to start a drag.
2188 int cur_x
, cur_y
, cur_visible
, cur_dragging
;
2191 static void reset_ui(game_ui
*ui
)
2193 ui
->drag_start_x
= -1;
2194 ui
->drag_start_y
= -1;
2195 ui
->drag_end_x
= -1;
2196 ui
->drag_end_y
= -1;
2201 ui
->dragged
= FALSE
;
2204 static game_ui
*new_ui(const game_state
*state
)
2206 game_ui
*ui
= snew(game_ui
);
2208 ui
->erasing
= FALSE
;
2209 ui
->cur_x
= ui
->cur_y
= ui
->cur_visible
= ui
->cur_dragging
= 0;
2213 static void free_ui(game_ui
*ui
)
2218 static char *encode_ui(const game_ui
*ui
)
2223 static void decode_ui(game_ui
*ui
, const char *encoding
)
2227 static void coord_round(float x
, float y
, int *xr
, int *yr
)
2229 float xs
, ys
, xv
, yv
, dx
, dy
, dist
;
2232 * Find the nearest square-centre.
2234 xs
= (float)floor(x
) + 0.5F
;
2235 ys
= (float)floor(y
) + 0.5F
;
2238 * And find the nearest grid vertex.
2240 xv
= (float)floor(x
+ 0.5F
);
2241 yv
= (float)floor(y
+ 0.5F
);
2244 * We allocate clicks in parts of the grid square to either
2245 * corners, edges or square centres, as follows:
2261 * In other words: we measure the square distance (i.e.
2262 * max(dx,dy)) from the click to the nearest corner, and if
2263 * it's within CORNER_TOLERANCE then we return a corner click.
2264 * We measure the square distance from the click to the nearest
2265 * centre, and if that's within CENTRE_TOLERANCE we return a
2266 * centre click. Failing that, we find which of the two edge
2267 * centres is nearer to the click and return that edge.
2271 * Check for corner click.
2273 dx
= (float)fabs(x
- xv
);
2274 dy
= (float)fabs(y
- yv
);
2275 dist
= (dx
> dy
? dx
: dy
);
2276 if (dist
< CORNER_TOLERANCE
) {
2281 * Check for centre click.
2283 dx
= (float)fabs(x
- xs
);
2284 dy
= (float)fabs(y
- ys
);
2285 dist
= (dx
> dy
? dx
: dy
);
2286 if (dist
< CENTRE_TOLERANCE
) {
2287 *xr
= 1 + 2 * (int)xs
;
2288 *yr
= 1 + 2 * (int)ys
;
2291 * Failing both of those, see which edge we're closer to.
2292 * Conveniently, this is simply done by testing the relative
2293 * magnitude of dx and dy (which are currently distances from
2294 * the square centre).
2297 /* Vertical edge: x-coord of corner,
2298 * y-coord of square centre. */
2300 *yr
= 1 + 2 * (int)floor(ys
);
2302 /* Horizontal edge: x-coord of square centre,
2303 * y-coord of corner. */
2304 *xr
= 1 + 2 * (int)floor(xs
);
2312 * Returns TRUE if it has made any change to the grid.
2314 static int grid_draw_rect(const game_state
*state
,
2315 unsigned char *hedge
, unsigned char *vedge
,
2316 int c
, int really
, int outline
,
2317 int x1
, int y1
, int x2
, int y2
)
2320 int changed
= FALSE
;
2323 * Draw horizontal edges of rectangles.
2325 for (x
= x1
; x
< x2
; x
++)
2326 for (y
= y1
; y
<= y2
; y
++)
2327 if (HRANGE(state
,x
,y
)) {
2328 int val
= index(state
,hedge
,x
,y
);
2329 if (y
== y1
|| y
== y2
) {
2330 if (!outline
) continue;
2334 changed
= changed
|| (index(state
,hedge
,x
,y
) != val
);
2336 index(state
,hedge
,x
,y
) = val
;
2340 * Draw vertical edges of rectangles.
2342 for (y
= y1
; y
< y2
; y
++)
2343 for (x
= x1
; x
<= x2
; x
++)
2344 if (VRANGE(state
,x
,y
)) {
2345 int val
= index(state
,vedge
,x
,y
);
2346 if (x
== x1
|| x
== x2
) {
2347 if (!outline
) continue;
2351 changed
= changed
|| (index(state
,vedge
,x
,y
) != val
);
2353 index(state
,vedge
,x
,y
) = val
;
2359 static int ui_draw_rect(const game_state
*state
, const game_ui
*ui
,
2360 unsigned char *hedge
, unsigned char *vedge
, int c
,
2361 int really
, int outline
)
2363 return grid_draw_rect(state
, hedge
, vedge
, c
, really
, outline
,
2364 ui
->x1
, ui
->y1
, ui
->x2
, ui
->y2
);
2367 static void game_changed_state(game_ui
*ui
, const game_state
*oldstate
,
2368 const game_state
*newstate
)
2372 struct game_drawstate
{
2375 unsigned long *visible
;
2378 static char *interpret_move(const game_state
*from
, game_ui
*ui
,
2379 const game_drawstate
*ds
,
2380 int x
, int y
, int button
)
2383 int startdrag
= FALSE
, enddrag
= FALSE
, active
= FALSE
, erasing
= FALSE
;
2386 button
&= ~MOD_MASK
;
2388 coord_round(FROMCOORD((float)x
), FROMCOORD((float)y
), &xc
, &yc
);
2390 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
2391 if (ui
->drag_start_x
>= 0 && ui
->cur_dragging
)
2392 reset_ui(ui
); /* cancel keyboard dragging */
2394 ui
->cur_visible
= ui
->cur_dragging
= FALSE
;
2396 erasing
= (button
== RIGHT_BUTTON
);
2397 } else if (button
== LEFT_RELEASE
|| button
== RIGHT_RELEASE
) {
2398 /* We assert we should have had a LEFT_BUTTON first. */
2399 if (ui
->cur_visible
) {
2400 ui
->cur_visible
= FALSE
;
2403 assert(!ui
->cur_dragging
);
2405 erasing
= (button
== RIGHT_RELEASE
);
2406 } else if (IS_CURSOR_MOVE(button
)) {
2407 move_cursor(button
, &ui
->cur_x
, &ui
->cur_y
, from
->w
, from
->h
, 0);
2408 ui
->cur_visible
= TRUE
;
2410 if (!ui
->cur_dragging
) return "";
2411 coord_round((float)ui
->cur_x
+ 0.5F
, (float)ui
->cur_y
+ 0.5F
, &xc
, &yc
);
2412 } else if (IS_CURSOR_SELECT(button
)) {
2413 if (ui
->drag_start_x
>= 0 && !ui
->cur_dragging
) {
2415 * If a mouse drag is in progress, ignore attempts to
2416 * start a keyboard one.
2420 if (!ui
->cur_visible
) {
2421 assert(!ui
->cur_dragging
);
2422 ui
->cur_visible
= TRUE
;
2425 coord_round((float)ui
->cur_x
+ 0.5F
, (float)ui
->cur_y
+ 0.5F
, &xc
, &yc
);
2426 erasing
= (button
== CURSOR_SELECT2
);
2427 if (ui
->cur_dragging
) {
2428 ui
->cur_dragging
= FALSE
;
2432 ui
->cur_dragging
= TRUE
;
2436 } else if (button
== '\b' || button
== 27) {
2437 if (!ui
->cur_dragging
) {
2438 ui
->cur_visible
= FALSE
;
2440 assert(ui
->cur_visible
);
2441 reset_ui(ui
); /* cancel keyboard dragging */
2442 ui
->cur_dragging
= FALSE
;
2445 } else if (button
!= LEFT_DRAG
&& button
!= RIGHT_DRAG
) {
2450 xc
>= 0 && xc
<= 2*from
->w
&&
2451 yc
>= 0 && yc
<= 2*from
->h
) {
2453 ui
->drag_start_x
= xc
;
2454 ui
->drag_start_y
= yc
;
2455 ui
->drag_end_x
= -1;
2456 ui
->drag_end_y
= -1;
2457 ui
->dragged
= FALSE
;
2458 ui
->erasing
= erasing
;
2462 if (ui
->drag_start_x
>= 0 &&
2463 (xc
!= ui
->drag_end_x
|| yc
!= ui
->drag_end_y
)) {
2466 if (ui
->drag_end_x
!= -1 && ui
->drag_end_y
!= -1)
2468 ui
->drag_end_x
= xc
;
2469 ui
->drag_end_y
= yc
;
2472 if (xc
>= 0 && xc
<= 2*from
->w
&&
2473 yc
>= 0 && yc
<= 2*from
->h
) {
2474 ui
->x1
= ui
->drag_start_x
;
2475 ui
->x2
= ui
->drag_end_x
;
2476 if (ui
->x2
< ui
->x1
) { t
= ui
->x1
; ui
->x1
= ui
->x2
; ui
->x2
= t
; }
2478 ui
->y1
= ui
->drag_start_y
;
2479 ui
->y2
= ui
->drag_end_y
;
2480 if (ui
->y2
< ui
->y1
) { t
= ui
->y1
; ui
->y1
= ui
->y2
; ui
->y2
= t
; }
2482 ui
->x1
= ui
->x1
/ 2; /* rounds down */
2483 ui
->x2
= (ui
->x2
+1) / 2; /* rounds up */
2484 ui
->y1
= ui
->y1
/ 2; /* rounds down */
2485 ui
->y2
= (ui
->y2
+1) / 2; /* rounds up */
2496 if (enddrag
&& (ui
->drag_start_x
>= 0)) {
2497 if (xc
>= 0 && xc
<= 2*from
->w
&&
2498 yc
>= 0 && yc
<= 2*from
->h
&&
2499 erasing
== ui
->erasing
) {
2502 if (ui_draw_rect(from
, ui
, from
->hedge
,
2503 from
->vedge
, 1, FALSE
, !ui
->erasing
)) {
2504 sprintf(buf
, "%c%d,%d,%d,%d",
2505 (int)(ui
->erasing
? 'E' : 'R'),
2506 ui
->x1
, ui
->y1
, ui
->x2
- ui
->x1
, ui
->y2
- ui
->y1
);
2510 if ((xc
& 1) && !(yc
& 1) && HRANGE(from
,xc
/2,yc
/2)) {
2511 sprintf(buf
, "H%d,%d", xc
/2, yc
/2);
2514 if ((yc
& 1) && !(xc
& 1) && VRANGE(from
,xc
/2,yc
/2)) {
2515 sprintf(buf
, "V%d,%d", xc
/2, yc
/2);
2526 return ret
; /* a move has been made */
2528 return ""; /* UI activity has occurred */
2533 static game_state
*execute_move(const game_state
*from
, const char *move
)
2536 int x1
, y1
, x2
, y2
, mode
;
2538 if (move
[0] == 'S') {
2539 const char *p
= move
+1;
2542 ret
= dup_game(from
);
2543 ret
->cheated
= TRUE
;
2545 for (y
= 0; y
< ret
->h
; y
++)
2546 for (x
= 1; x
< ret
->w
; x
++) {
2547 vedge(ret
, x
, y
) = (*p
== '1');
2550 for (y
= 1; y
< ret
->h
; y
++)
2551 for (x
= 0; x
< ret
->w
; x
++) {
2552 hedge(ret
, x
, y
) = (*p
== '1');
2556 sfree(ret
->correct
);
2557 ret
->correct
= get_correct(ret
);
2561 } else if ((move
[0] == 'R' || move
[0] == 'E') &&
2562 sscanf(move
+1, "%d,%d,%d,%d", &x1
, &y1
, &x2
, &y2
) == 4 &&
2563 x1
>= 0 && x2
>= 0 && x1
+x2
<= from
->w
&&
2564 y1
>= 0 && y2
>= 0 && y1
+y2
<= from
->h
) {
2568 } else if ((move
[0] == 'H' || move
[0] == 'V') &&
2569 sscanf(move
+1, "%d,%d", &x1
, &y1
) == 2 &&
2570 (move
[0] == 'H' ? HRANGE(from
, x1
, y1
) :
2571 VRANGE(from
, x1
, y1
))) {
2574 return NULL
; /* can't parse move string */
2576 ret
= dup_game(from
);
2578 if (mode
== 'R' || mode
== 'E') {
2579 grid_draw_rect(ret
, ret
->hedge
, ret
->vedge
, 1, TRUE
,
2580 mode
== 'R', x1
, y1
, x2
, y2
);
2581 } else if (mode
== 'H') {
2582 hedge(ret
,x1
,y1
) = !hedge(ret
,x1
,y1
);
2583 } else if (mode
== 'V') {
2584 vedge(ret
,x1
,y1
) = !vedge(ret
,x1
,y1
);
2587 sfree(ret
->correct
);
2588 ret
->correct
= get_correct(ret
);
2591 * We've made a real change to the grid. Check to see
2592 * if the game has been completed.
2594 if (!ret
->completed
) {
2598 for (x
= 0; x
< ret
->w
; x
++)
2599 for (y
= 0; y
< ret
->h
; y
++)
2600 if (!index(ret
, ret
->correct
, x
, y
))
2604 ret
->completed
= TRUE
;
2610 /* ----------------------------------------------------------------------
2614 #define CORRECT (1L<<16)
2615 #define CURSOR (1L<<17)
2617 #define COLOUR(k) ( (k)==1 ? COL_LINE : (k)==2 ? COL_DRAG : COL_DRAGERASE )
2618 #define MAX4(x,y,z,w) ( max(max(x,y),max(z,w)) )
2620 static void game_compute_size(const game_params
*params
, int tilesize
,
2623 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2624 struct { int tilesize
; } ads
, *ds
= &ads
;
2625 ads
.tilesize
= tilesize
;
2627 *x
= params
->w
* TILE_SIZE
+ 2*BORDER
+ 1;
2628 *y
= params
->h
* TILE_SIZE
+ 2*BORDER
+ 1;
2631 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
2632 const game_params
*params
, int tilesize
)
2634 ds
->tilesize
= tilesize
;
2637 static float *game_colours(frontend
*fe
, int *ncolours
)
2639 float *ret
= snewn(3 * NCOLOURS
, float);
2641 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
2643 ret
[COL_GRID
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
2644 ret
[COL_GRID
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
2645 ret
[COL_GRID
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
2647 ret
[COL_DRAG
* 3 + 0] = 1.0F
;
2648 ret
[COL_DRAG
* 3 + 1] = 0.0F
;
2649 ret
[COL_DRAG
* 3 + 2] = 0.0F
;
2651 ret
[COL_DRAGERASE
* 3 + 0] = 0.2F
;
2652 ret
[COL_DRAGERASE
* 3 + 1] = 0.2F
;
2653 ret
[COL_DRAGERASE
* 3 + 2] = 1.0F
;
2655 ret
[COL_CORRECT
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
2656 ret
[COL_CORRECT
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
2657 ret
[COL_CORRECT
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
2659 ret
[COL_LINE
* 3 + 0] = 0.0F
;
2660 ret
[COL_LINE
* 3 + 1] = 0.0F
;
2661 ret
[COL_LINE
* 3 + 2] = 0.0F
;
2663 ret
[COL_TEXT
* 3 + 0] = 0.0F
;
2664 ret
[COL_TEXT
* 3 + 1] = 0.0F
;
2665 ret
[COL_TEXT
* 3 + 2] = 0.0F
;
2667 ret
[COL_CURSOR
* 3 + 0] = 1.0F
;
2668 ret
[COL_CURSOR
* 3 + 1] = 0.5F
;
2669 ret
[COL_CURSOR
* 3 + 2] = 0.5F
;
2671 *ncolours
= NCOLOURS
;
2675 static game_drawstate
*game_new_drawstate(drawing
*dr
, const game_state
*state
)
2677 struct game_drawstate
*ds
= snew(struct game_drawstate
);
2680 ds
->started
= FALSE
;
2683 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned long);
2684 ds
->tilesize
= 0; /* not decided yet */
2685 for (i
= 0; i
< ds
->w
* ds
->h
; i
++)
2686 ds
->visible
[i
] = 0xFFFF;
2691 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
2697 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, const game_state
*state
,
2698 int x
, int y
, unsigned char *hedge
, unsigned char *vedge
,
2699 unsigned char *corners
, unsigned long bgflags
)
2701 int cx
= COORD(x
), cy
= COORD(y
);
2704 draw_rect(dr
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1, COL_GRID
);
2705 draw_rect(dr
, cx
+1, cy
+1, TILE_SIZE
-1, TILE_SIZE
-1,
2706 (bgflags
& CURSOR
) ? COL_CURSOR
:
2707 (bgflags
& CORRECT
) ? COL_CORRECT
: COL_BACKGROUND
);
2709 if (grid(state
,x
,y
)) {
2710 sprintf(str
, "%d", grid(state
,x
,y
));
2711 draw_text(dr
, cx
+TILE_SIZE
/2, cy
+TILE_SIZE
/2, FONT_VARIABLE
,
2712 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
, COL_TEXT
, str
);
2718 if (!HRANGE(state
,x
,y
) || index(state
,hedge
,x
,y
))
2719 draw_rect(dr
, cx
, cy
, TILE_SIZE
+1, 2,
2720 HRANGE(state
,x
,y
) ? COLOUR(index(state
,hedge
,x
,y
)) :
2722 if (!HRANGE(state
,x
,y
+1) || index(state
,hedge
,x
,y
+1))
2723 draw_rect(dr
, cx
, cy
+TILE_SIZE
-1, TILE_SIZE
+1, 2,
2724 HRANGE(state
,x
,y
+1) ? COLOUR(index(state
,hedge
,x
,y
+1)) :
2726 if (!VRANGE(state
,x
,y
) || index(state
,vedge
,x
,y
))
2727 draw_rect(dr
, cx
, cy
, 2, TILE_SIZE
+1,
2728 VRANGE(state
,x
,y
) ? COLOUR(index(state
,vedge
,x
,y
)) :
2730 if (!VRANGE(state
,x
+1,y
) || index(state
,vedge
,x
+1,y
))
2731 draw_rect(dr
, cx
+TILE_SIZE
-1, cy
, 2, TILE_SIZE
+1,
2732 VRANGE(state
,x
+1,y
) ? COLOUR(index(state
,vedge
,x
+1,y
)) :
2738 if (index(state
,corners
,x
,y
))
2739 draw_rect(dr
, cx
, cy
, 2, 2,
2740 COLOUR(index(state
,corners
,x
,y
)));
2741 if (x
+1 < state
->w
&& index(state
,corners
,x
+1,y
))
2742 draw_rect(dr
, cx
+TILE_SIZE
-1, cy
, 2, 2,
2743 COLOUR(index(state
,corners
,x
+1,y
)));
2744 if (y
+1 < state
->h
&& index(state
,corners
,x
,y
+1))
2745 draw_rect(dr
, cx
, cy
+TILE_SIZE
-1, 2, 2,
2746 COLOUR(index(state
,corners
,x
,y
+1)));
2747 if (x
+1 < state
->w
&& y
+1 < state
->h
&& index(state
,corners
,x
+1,y
+1))
2748 draw_rect(dr
, cx
+TILE_SIZE
-1, cy
+TILE_SIZE
-1, 2, 2,
2749 COLOUR(index(state
,corners
,x
+1,y
+1)));
2751 draw_update(dr
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1);
2754 static void game_redraw(drawing
*dr
, game_drawstate
*ds
,
2755 const game_state
*oldstate
, const game_state
*state
,
2756 int dir
, const game_ui
*ui
,
2757 float animtime
, float flashtime
)
2760 unsigned char *hedge
, *vedge
, *corners
;
2763 hedge
= snewn(state
->w
*state
->h
, unsigned char);
2764 vedge
= snewn(state
->w
*state
->h
, unsigned char);
2765 memcpy(hedge
, state
->hedge
, state
->w
*state
->h
);
2766 memcpy(vedge
, state
->vedge
, state
->w
*state
->h
);
2767 ui_draw_rect(state
, ui
, hedge
, vedge
, ui
->erasing
? 3 : 2, TRUE
, TRUE
);
2769 hedge
= state
->hedge
;
2770 vedge
= state
->vedge
;
2773 corners
= snewn(state
->w
* state
->h
, unsigned char);
2774 memset(corners
, 0, state
->w
* state
->h
);
2775 for (x
= 0; x
< state
->w
; x
++)
2776 for (y
= 0; y
< state
->h
; y
++) {
2778 int e
= index(state
, vedge
, x
, y
);
2779 if (index(state
,corners
,x
,y
) < e
)
2780 index(state
,corners
,x
,y
) = e
;
2781 if (y
+1 < state
->h
&&
2782 index(state
,corners
,x
,y
+1) < e
)
2783 index(state
,corners
,x
,y
+1) = e
;
2786 int e
= index(state
, hedge
, x
, y
);
2787 if (index(state
,corners
,x
,y
) < e
)
2788 index(state
,corners
,x
,y
) = e
;
2789 if (x
+1 < state
->w
&&
2790 index(state
,corners
,x
+1,y
) < e
)
2791 index(state
,corners
,x
+1,y
) = e
;
2797 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2798 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1, COL_BACKGROUND
);
2799 draw_rect(dr
, COORD(0)-1, COORD(0)-1,
2800 ds
->w
*TILE_SIZE
+3, ds
->h
*TILE_SIZE
+3, COL_LINE
);
2802 draw_update(dr
, 0, 0,
2803 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
2804 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
2807 for (x
= 0; x
< state
->w
; x
++)
2808 for (y
= 0; y
< state
->h
; y
++) {
2809 unsigned long c
= 0;
2811 if (HRANGE(state
,x
,y
))
2812 c
|= index(state
,hedge
,x
,y
);
2813 if (HRANGE(state
,x
,y
+1))
2814 c
|= index(state
,hedge
,x
,y
+1) << 2;
2815 if (VRANGE(state
,x
,y
))
2816 c
|= index(state
,vedge
,x
,y
) << 4;
2817 if (VRANGE(state
,x
+1,y
))
2818 c
|= index(state
,vedge
,x
+1,y
) << 6;
2819 c
|= index(state
,corners
,x
,y
) << 8;
2821 c
|= index(state
,corners
,x
+1,y
) << 10;
2823 c
|= index(state
,corners
,x
,y
+1) << 12;
2824 if (x
+1 < state
->w
&& y
+1 < state
->h
)
2825 /* cast to prevent 2<<14 sign-extending on promotion to long */
2826 c
|= (unsigned long)index(state
,corners
,x
+1,y
+1) << 14;
2827 if (index(state
, state
->correct
, x
, y
) && !flashtime
)
2829 if (ui
->cur_visible
&& ui
->cur_x
== x
&& ui
->cur_y
== y
)
2832 if (index(ds
,ds
->visible
,x
,y
) != c
) {
2833 draw_tile(dr
, ds
, state
, x
, y
, hedge
, vedge
, corners
,
2834 (c
& (CORRECT
|CURSOR
)) );
2835 index(ds
,ds
->visible
,x
,y
) = c
;
2843 ui
->x1
>= 0 && ui
->y1
>= 0 &&
2844 ui
->x2
>= 0 && ui
->y2
>= 0) {
2845 sprintf(buf
, "%dx%d ",
2853 strcat(buf
, "Auto-solved.");
2854 else if (state
->completed
)
2855 strcat(buf
, "COMPLETED!");
2857 status_bar(dr
, buf
);
2860 if (hedge
!= state
->hedge
) {
2868 static float game_anim_length(const game_state
*oldstate
,
2869 const game_state
*newstate
, int dir
, game_ui
*ui
)
2874 static float game_flash_length(const game_state
*oldstate
,
2875 const game_state
*newstate
, int dir
, game_ui
*ui
)
2877 if (!oldstate
->completed
&& newstate
->completed
&&
2878 !oldstate
->cheated
&& !newstate
->cheated
)
2883 static int game_status(const game_state
*state
)
2885 return state
->completed
? +1 : 0;
2888 static int game_timing_state(const game_state
*state
, game_ui
*ui
)
2893 static void game_print_size(const game_params
*params
, float *x
, float *y
)
2898 * I'll use 5mm squares by default.
2900 game_compute_size(params
, 500, &pw
, &ph
);
2905 static void game_print(drawing
*dr
, const game_state
*state
, int tilesize
)
2907 int w
= state
->w
, h
= state
->h
;
2908 int ink
= print_mono_colour(dr
, 0);
2911 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2912 game_drawstate ads
, *ds
= &ads
;
2913 game_set_size(dr
, ds
, NULL
, tilesize
);
2918 print_line_width(dr
, TILE_SIZE
/ 10);
2919 draw_rect_outline(dr
, COORD(0), COORD(0), w
*TILE_SIZE
, h
*TILE_SIZE
, ink
);
2922 * Grid. We have to make the grid lines particularly thin,
2923 * because users will be drawing lines _along_ them and we want
2924 * those lines to be visible.
2926 print_line_width(dr
, TILE_SIZE
/ 256);
2927 for (x
= 1; x
< w
; x
++)
2928 draw_line(dr
, COORD(x
), COORD(0), COORD(x
), COORD(h
), ink
);
2929 for (y
= 1; y
< h
; y
++)
2930 draw_line(dr
, COORD(0), COORD(y
), COORD(w
), COORD(y
), ink
);
2935 print_line_width(dr
, TILE_SIZE
/ 10);
2936 for (y
= 0; y
<= h
; y
++)
2937 for (x
= 0; x
<= w
; x
++) {
2938 if (HRANGE(state
,x
,y
) && hedge(state
,x
,y
))
2939 draw_line(dr
, COORD(x
), COORD(y
), COORD(x
+1), COORD(y
), ink
);
2940 if (VRANGE(state
,x
,y
) && vedge(state
,x
,y
))
2941 draw_line(dr
, COORD(x
), COORD(y
), COORD(x
), COORD(y
+1), ink
);
2947 for (y
= 0; y
< h
; y
++)
2948 for (x
= 0; x
< w
; x
++)
2949 if (grid(state
,x
,y
)) {
2951 sprintf(str
, "%d", grid(state
,x
,y
));
2952 draw_text(dr
, COORD(x
)+TILE_SIZE
/2, COORD(y
)+TILE_SIZE
/2,
2953 FONT_VARIABLE
, TILE_SIZE
/2,
2954 ALIGN_HCENTRE
| ALIGN_VCENTRE
, ink
, str
);
2959 #define thegame rect
2962 const struct game thegame
= {
2963 "Rectangles", "games.rectangles", "rect",
2965 game_fetch_preset
, NULL
,
2970 TRUE
, game_configure
, custom_params
,
2978 TRUE
, game_can_format_as_text_now
, game_text_format
,
2986 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
2989 game_free_drawstate
,
2994 TRUE
, FALSE
, game_print_size
, game_print
,
2995 TRUE
, /* wants_statusbar */
2996 FALSE
, game_timing_state
,
3000 /* vim: set shiftwidth=4 tabstop=8: */