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].u.string.sval = dupstr(buf);
185 ret[1].name = "Height";
186 ret[1].type = C_STRING;
187 sprintf(buf, "%d", params->h);
188 ret[1].u.string.sval = dupstr(buf);
190 ret[2].name = "Expansion factor";
191 ret[2].type = C_STRING;
192 sprintf(buf, "%g", params->expandfactor);
193 ret[2].u.string.sval = dupstr(buf);
195 ret[3].name = "Ensure unique solution";
196 ret[3].type = C_BOOLEAN;
197 ret[3].u.boolean.bval = params->unique;
205 static game_params *custom_params(const config_item *cfg)
207 game_params *ret = snew(game_params);
209 ret->w = atoi(cfg[0].u.string.sval);
210 ret->h = atoi(cfg[1].u.string.sval);
211 ret->expandfactor = (float)atof(cfg[2].u.string.sval);
212 ret->unique = cfg[3].u.boolean.bval;
217 static char *validate_params(const game_params *params, int full)
219 if (params->w <= 0 || params->h <= 0)
220 return "Width and height must both be greater than zero";
221 if (params->w*params->h < 2)
222 return "Grid area must be greater than one";
223 if (params->expandfactor < 0.0F)
224 return "Expansion factor may not be negative";
245 struct point *points;
248 /* ----------------------------------------------------------------------
249 * Solver for Rectangles games.
251 * This solver is souped up beyond the needs of actually _solving_
252 * a puzzle. It is also designed to cope with uncertainty about
253 * where the numbers have been placed. This is because I run it on
254 * my generated grids _before_ placing the numbers, and have it
255 * tell me where I need to place the numbers to ensure a unique
259 static void remove_rect_placement(int w, int h,
260 struct rectlist *rectpositions,
262 int rectnum, int placement)
266 #ifdef SOLVER_DIAGNOSTICS
267 printf("ruling out rect %d placement at %d,%d w=%d h=%d\n", rectnum,
268 rectpositions[rectnum].rects[placement].x,
269 rectpositions[rectnum].rects[placement].y,
270 rectpositions[rectnum].rects[placement].w,
271 rectpositions[rectnum].rects[placement].h);
275 * Decrement each entry in the overlaps array to reflect the
276 * removal of this rectangle placement.
278 for (yy = 0; yy < rectpositions[rectnum].rects[placement].h; yy++) {
279 y = yy + rectpositions[rectnum].rects[placement].y;
280 for (xx = 0; xx < rectpositions[rectnum].rects[placement].w; xx++) {
281 x = xx + rectpositions[rectnum].rects[placement].x;
283 assert(overlaps[(rectnum * h + y) * w + x] != 0);
285 if (overlaps[(rectnum * h + y) * w + x] > 0)
286 overlaps[(rectnum * h + y) * w + x]--;
291 * Remove the placement from the list of positions for that
292 * rectangle, by interchanging it with the one on the end.
294 if (placement < rectpositions[rectnum].n - 1) {
297 t = rectpositions[rectnum].rects[rectpositions[rectnum].n - 1];
298 rectpositions[rectnum].rects[rectpositions[rectnum].n - 1] =
299 rectpositions[rectnum].rects[placement];
300 rectpositions[rectnum].rects[placement] = t;
302 rectpositions[rectnum].n--;
305 static void remove_number_placement(int w, int h, struct numberdata *number,
306 int index, int *rectbyplace)
309 * Remove the entry from the rectbyplace array.
311 rectbyplace[number->points[index].y * w + number->points[index].x] = -1;
314 * Remove the placement from the list of candidates for that
315 * number, by interchanging it with the one on the end.
317 if (index < number->npoints - 1) {
320 t = number->points[number->npoints - 1];
321 number->points[number->npoints - 1] = number->points[index];
322 number->points[index] = t;
328 * Returns 0 for failure to solve due to inconsistency; 1 for
329 * success; 2 for failure to complete a solution due to either
330 * ambiguity or it being too difficult.
332 static int rect_solver(int w, int h, int nrects, struct numberdata *numbers,
333 unsigned char *hedge, unsigned char *vedge,
336 struct rectlist *rectpositions;
337 int *overlaps, *rectbyplace, *workspace;
341 * Start by setting up a list of candidate positions for each
344 rectpositions = snewn(nrects, struct rectlist);
345 for (i = 0; i < nrects; i++) {
346 int rw, rh, area = numbers[i].area;
347 int j, minx, miny, maxx, maxy;
349 int rlistn, rlistsize;
352 * For each rectangle, begin by finding the bounding
353 * rectangle of its candidate number placements.
358 for (j = 0; j < numbers[i].npoints; j++) {
359 if (minx > numbers[i].points[j].x) minx = numbers[i].points[j].x;
360 if (miny > numbers[i].points[j].y) miny = numbers[i].points[j].y;
361 if (maxx < numbers[i].points[j].x) maxx = numbers[i].points[j].x;
362 if (maxy < numbers[i].points[j].y) maxy = numbers[i].points[j].y;
366 * Now loop over all possible rectangle placements
367 * overlapping a point within that bounding rectangle;
368 * ensure each one actually contains a candidate number
369 * placement, and add it to the list.
372 rlistn = rlistsize = 0;
374 for (rw = 1; rw <= area && rw <= w; rw++) {
383 for (y = miny - rh + 1; y <= maxy; y++) {
384 if (y < 0 || y+rh > h)
387 for (x = minx - rw + 1; x <= maxx; x++) {
388 if (x < 0 || x+rw > w)
392 * See if we can find a candidate number
393 * placement within this rectangle.
395 for (j = 0; j < numbers[i].npoints; j++)
396 if (numbers[i].points[j].x >= x &&
397 numbers[i].points[j].x < x+rw &&
398 numbers[i].points[j].y >= y &&
399 numbers[i].points[j].y < y+rh)
402 if (j < numbers[i].npoints) {
404 * Add this to the list of candidate
405 * placements for this rectangle.
407 if (rlistn >= rlistsize) {
408 rlistsize = rlistn + 32;
409 rlist = sresize(rlist, rlistsize, struct rect);
413 rlist[rlistn].w = rw;
414 rlist[rlistn].h = rh;
415 #ifdef SOLVER_DIAGNOSTICS
416 printf("rect %d [area %d]: candidate position at"
417 " %d,%d w=%d h=%d\n",
418 i, area, x, y, rw, rh);
426 rectpositions[i].rects = rlist;
427 rectpositions[i].n = rlistn;
431 * Next, construct a multidimensional array tracking how many
432 * candidate positions for each rectangle overlap each square.
434 * Indexing of this array is by the formula
436 * overlaps[(rectindex * h + y) * w + x]
438 * A positive or zero value indicates what it sounds as if it
439 * should; -1 indicates that this square _cannot_ be part of
440 * this rectangle; and -2 indicates that it _definitely_ is
441 * (which is distinct from 1, because one might very well know
442 * that _if_ square S is part of rectangle R then it must be
443 * because R is placed in a certain position without knowing
444 * that it definitely _is_).
446 overlaps = snewn(nrects * w * h, int);
447 memset(overlaps, 0, nrects * w * h * sizeof(int));
448 for (i = 0; i < nrects; i++) {
451 for (j = 0; j < rectpositions[i].n; j++) {
454 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++)
455 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++)
456 overlaps[(i * h + yy+rectpositions[i].rects[j].y) * w +
457 xx+rectpositions[i].rects[j].x]++;
462 * Also we want an array covering the grid once, to make it
463 * easy to figure out which squares are candidate number
464 * placements for which rectangles. (The existence of this
465 * single array assumes that no square starts off as a
466 * candidate number placement for more than one rectangle. This
467 * assumption is justified, because this solver is _either_
468 * used to solve real problems - in which case there is a
469 * single placement for every number - _or_ used to decide on
470 * number placements for a new puzzle, in which case each
471 * number's placements are confined to the intended position of
472 * the rectangle containing that number.)
474 rectbyplace = snewn(w * h, int);
475 for (i = 0; i < w*h; i++)
478 for (i = 0; i < nrects; i++) {
481 for (j = 0; j < numbers[i].npoints; j++) {
482 int x = numbers[i].points[j].x;
483 int y = numbers[i].points[j].y;
485 assert(rectbyplace[y * w + x] == -1);
486 rectbyplace[y * w + x] = i;
490 workspace = snewn(nrects, int);
493 * Now run the actual deduction loop.
496 int done_something = FALSE;
498 #ifdef SOLVER_DIAGNOSTICS
499 printf("starting deduction loop\n");
501 for (i = 0; i < nrects; i++) {
502 printf("rect %d overlaps:\n", i);
505 for (y = 0; y < h; y++) {
506 for (x = 0; x < w; x++) {
507 printf("%3d", overlaps[(i * h + y) * w + x]);
513 printf("rectbyplace:\n");
516 for (y = 0; y < h; y++) {
517 for (x = 0; x < w; x++) {
518 printf("%3d", rectbyplace[y * w + x]);
526 * Housekeeping. Look for rectangles whose number has only
527 * one candidate position left, and mark that square as
528 * known if it isn't already.
530 for (i = 0; i < nrects; i++) {
531 if (numbers[i].npoints == 1) {
532 int x = numbers[i].points[0].x;
533 int y = numbers[i].points[0].y;
534 if (overlaps[(i * h + y) * w + x] >= -1) {
537 if (overlaps[(i * h + y) * w + x] <= 0) {
538 ret = 0; /* inconsistency */
541 #ifdef SOLVER_DIAGNOSTICS
542 printf("marking %d,%d as known for rect %d"
543 " (sole remaining number position)\n", x, y, i);
546 for (j = 0; j < nrects; j++)
547 overlaps[(j * h + y) * w + x] = -1;
549 overlaps[(i * h + y) * w + x] = -2;
555 * Now look at the intersection of all possible placements
556 * for each rectangle, and mark all squares in that
557 * intersection as known for that rectangle if they aren't
560 for (i = 0; i < nrects; i++) {
561 int minx, miny, maxx, maxy, xx, yy, j;
567 for (j = 0; j < rectpositions[i].n; j++) {
568 int x = rectpositions[i].rects[j].x;
569 int y = rectpositions[i].rects[j].y;
570 int w = rectpositions[i].rects[j].w;
571 int h = rectpositions[i].rects[j].h;
573 if (minx < x) minx = x;
574 if (miny < y) miny = y;
575 if (maxx > x+w) maxx = x+w;
576 if (maxy > y+h) maxy = y+h;
579 for (yy = miny; yy < maxy; yy++)
580 for (xx = minx; xx < maxx; xx++)
581 if (overlaps[(i * h + yy) * w + xx] >= -1) {
582 if (overlaps[(i * h + yy) * w + xx] <= 0) {
583 ret = 0; /* inconsistency */
586 #ifdef SOLVER_DIAGNOSTICS
587 printf("marking %d,%d as known for rect %d"
588 " (intersection of all placements)\n",
592 for (j = 0; j < nrects; j++)
593 overlaps[(j * h + yy) * w + xx] = -1;
595 overlaps[(i * h + yy) * w + xx] = -2;
600 * Rectangle-focused deduction. Look at each rectangle in
601 * turn and try to rule out some of its candidate
604 for (i = 0; i < nrects; i++) {
607 for (j = 0; j < rectpositions[i].n; j++) {
611 for (k = 0; k < nrects; k++)
614 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
615 int y = yy + rectpositions[i].rects[j].y;
616 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
617 int x = xx + rectpositions[i].rects[j].x;
619 if (overlaps[(i * h + y) * w + x] == -1) {
621 * This placement overlaps a square
622 * which is _known_ to be part of
623 * another rectangle. Therefore we must
626 #ifdef SOLVER_DIAGNOSTICS
627 printf("rect %d placement at %d,%d w=%d h=%d "
628 "contains %d,%d which is known-other\n", i,
629 rectpositions[i].rects[j].x,
630 rectpositions[i].rects[j].y,
631 rectpositions[i].rects[j].w,
632 rectpositions[i].rects[j].h,
638 if (rectbyplace[y * w + x] != -1) {
640 * This placement overlaps one of the
641 * candidate number placements for some
642 * rectangle. Count it.
644 workspace[rectbyplace[y * w + x]]++;
651 * If we haven't ruled this placement out
652 * already, see if it overlaps _all_ of the
653 * candidate number placements for any
654 * rectangle. If so, we can rule it out.
656 for (k = 0; k < nrects; k++)
657 if (k != i && workspace[k] == numbers[k].npoints) {
658 #ifdef SOLVER_DIAGNOSTICS
659 printf("rect %d placement at %d,%d w=%d h=%d "
660 "contains all number points for rect %d\n",
662 rectpositions[i].rects[j].x,
663 rectpositions[i].rects[j].y,
664 rectpositions[i].rects[j].w,
665 rectpositions[i].rects[j].h,
673 * Failing that, see if it overlaps at least
674 * one of the candidate number placements for
675 * itself! (This might not be the case if one
676 * of those number placements has been removed
679 if (!del && workspace[i] == 0) {
680 #ifdef SOLVER_DIAGNOSTICS
681 printf("rect %d placement at %d,%d w=%d h=%d "
682 "contains none of its own number points\n",
684 rectpositions[i].rects[j].x,
685 rectpositions[i].rects[j].y,
686 rectpositions[i].rects[j].w,
687 rectpositions[i].rects[j].h);
694 remove_rect_placement(w, h, rectpositions, overlaps, i, j);
696 j--; /* don't skip over next placement */
698 done_something = TRUE;
704 * Square-focused deduction. Look at each square not marked
705 * as known, and see if there are any which can only be
706 * part of a single rectangle.
710 for (y = 0; y < h; y++) for (x = 0; x < w; x++) {
711 /* Known squares are marked as <0 everywhere, so we only need
712 * to check the overlaps entry for rect 0. */
713 if (overlaps[y * w + x] < 0)
714 continue; /* known already */
718 for (i = 0; i < nrects; i++)
719 if (overlaps[(i * h + y) * w + x] > 0)
726 * Now we can rule out all placements for
727 * rectangle `index' which _don't_ contain
730 #ifdef SOLVER_DIAGNOSTICS
731 printf("square %d,%d can only be in rectangle %d\n",
734 for (j = 0; j < rectpositions[index].n; j++) {
735 struct rect *r = &rectpositions[index].rects[j];
736 if (x >= r->x && x < r->x + r->w &&
737 y >= r->y && y < r->y + r->h)
738 continue; /* this one is OK */
739 remove_rect_placement(w, h, rectpositions, overlaps,
741 j--; /* don't skip over next placement */
742 done_something = TRUE;
749 * If we've managed to deduce anything by normal means,
750 * loop round again and see if there's more to be done.
751 * Only if normal deduction has completely failed us should
752 * we now move on to narrowing down the possible number
759 * Now we have done everything we can with the current set
760 * of number placements. So we need to winnow the number
761 * placements so as to narrow down the possibilities. We do
762 * this by searching for a candidate placement (of _any_
763 * rectangle) which overlaps a candidate placement of the
764 * number for some other rectangle.
772 size_t nrpns = 0, rpnsize = 0;
775 for (i = 0; i < nrects; i++) {
776 for (j = 0; j < rectpositions[i].n; j++) {
779 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
780 int y = yy + rectpositions[i].rects[j].y;
781 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
782 int x = xx + rectpositions[i].rects[j].x;
784 if (rectbyplace[y * w + x] >= 0 &&
785 rectbyplace[y * w + x] != i) {
787 * Add this to the list of
788 * winnowing possibilities.
790 if (nrpns >= rpnsize) {
791 rpnsize = rpnsize * 3 / 2 + 32;
792 rpns = sresize(rpns, rpnsize, struct rpn);
794 rpns[nrpns].rect = i;
795 rpns[nrpns].placement = j;
796 rpns[nrpns].number = rectbyplace[y * w + x];
805 #ifdef SOLVER_DIAGNOSTICS
806 printf("%d candidate rect placements we could eliminate\n", nrpns);
810 * Now choose one of these unwanted rectangle
811 * placements, and eliminate it.
813 int index = random_upto(rs, nrpns);
815 struct rpn rpn = rpns[index];
822 r = rectpositions[i].rects[j];
825 * We rule out placement j of rectangle i by means
826 * of removing all of rectangle k's candidate
827 * number placements which do _not_ overlap it.
828 * This will ensure that it is eliminated during
829 * the next pass of rectangle-focused deduction.
831 #ifdef SOLVER_DIAGNOSTICS
832 printf("ensuring number for rect %d is within"
833 " rect %d's placement at %d,%d w=%d h=%d\n",
834 k, i, r.x, r.y, r.w, r.h);
837 for (m = 0; m < numbers[k].npoints; m++) {
838 int x = numbers[k].points[m].x;
839 int y = numbers[k].points[m].y;
841 if (x < r.x || x >= r.x + r.w ||
842 y < r.y || y >= r.y + r.h) {
843 #ifdef SOLVER_DIAGNOSTICS
844 printf("eliminating number for rect %d at %d,%d\n",
847 remove_number_placement(w, h, &numbers[k],
849 m--; /* don't skip the next one */
850 done_something = TRUE;
856 if (!done_something) {
857 #ifdef SOLVER_DIAGNOSTICS
858 printf("terminating deduction loop\n");
866 for (i = 0; i < nrects; i++) {
867 #ifdef SOLVER_DIAGNOSTICS
868 printf("rect %d has %d possible placements\n",
869 i, rectpositions[i].n);
871 if (rectpositions[i].n <= 0) {
872 ret = 0; /* inconsistency */
873 } else if (rectpositions[i].n > 1) {
874 ret = 2; /* remaining uncertainty */
875 } else if (hedge && vedge) {
877 * Place the rectangle in its only possible position.
880 struct rect *r = &rectpositions[i].rects[0];
882 for (y = 0; y < r->h; y++) {
884 vedge[(r->y+y) * w + r->x] = 1;
886 vedge[(r->y+y) * w + r->x+r->w] = 1;
888 for (x = 0; x < r->w; x++) {
890 hedge[r->y * w + r->x+x] = 1;
892 hedge[(r->y+r->h) * w + r->x+x] = 1;
898 * Free up all allocated storage.
903 for (i = 0; i < nrects; i++)
904 sfree(rectpositions[i].rects);
905 sfree(rectpositions);
910 /* ----------------------------------------------------------------------
911 * Grid generation code.
915 * This function does one of two things. If passed r==NULL, it
916 * counts the number of possible rectangles which cover the given
917 * square, and returns it in *n. If passed r!=NULL then it _reads_
918 * *n to find an index, counts the possible rectangles until it
919 * reaches the nth, and writes it into r.
921 * `scratch' is expected to point to an array of 2 * params->w
922 * ints, used internally as scratch space (and passed in like this
923 * to avoid re-allocating and re-freeing it every time round a
926 static void enum_rects(game_params *params, int *grid, struct rect *r, int *n,
927 int sx, int sy, int *scratch)
931 int maxarea, realmaxarea;
936 * Maximum rectangle area is 1/6 of total grid size, unless
937 * this means we can't place any rectangles at all in which
938 * case we set it to 2 at minimum.
940 maxarea = params->w * params->h / 6;
945 * Scan the grid to find the limits of the region within which
946 * any rectangle containing this point must fall. This will
947 * save us trawling the inside of every rectangle later on to
948 * see if it contains any used squares.
951 bottom = scratch + params->w;
952 for (dy = -1; dy <= +1; dy += 2) {
953 int *array = (dy == -1 ? top : bottom);
954 for (dx = -1; dx <= +1; dx += 2) {
955 for (x = sx; x >= 0 && x < params->w; x += dx) {
956 array[x] = -2 * params->h * dy;
957 for (y = sy; y >= 0 && y < params->h; y += dy) {
958 if (index(params, grid, x, y) == -1 &&
959 (x == sx || dy*y <= dy*array[x-dx]))
969 * Now scan again to work out the largest rectangles we can fit
970 * in the grid, so that we can terminate the following loops
971 * early once we get down to not having much space left in the
975 for (x = 0; x < params->w; x++) {
978 rh = bottom[x] - top[x] + 1;
980 continue; /* no rectangles can start here */
982 dx = (x > sx ? -1 : +1);
983 for (x2 = x; x2 >= 0 && x2 < params->w; x2 += dx)
984 if (bottom[x2] < bottom[x] || top[x2] > top[x])
988 if (realmaxarea < rw * rh)
989 realmaxarea = rw * rh;
992 if (realmaxarea > maxarea)
993 realmaxarea = maxarea;
996 * Rectangles which go right the way across the grid are
997 * boring, although they can't be helped in the case of
998 * extremely small grids. (Also they might be generated later
999 * on by the singleton-removal process; we can't help that.)
1006 for (rw = 1; rw <= mw; rw++)
1007 for (rh = 1; rh <= mh; rh++) {
1008 if (rw * rh > realmaxarea)
1012 for (x = max(sx - rw + 1, 0); x <= min(sx, params->w - rw); x++)
1013 for (y = max(sy - rh + 1, 0); y <= min(sy, params->h - rh);
1016 * Check this rectangle against the region we
1019 if (top[x] <= y && top[x+rw-1] <= y &&
1020 bottom[x] >= y+rh-1 && bottom[x+rw-1] >= y+rh-1) {
1021 if (r && index == *n) {
1037 static void place_rect(game_params *params, int *grid, struct rect r)
1039 int idx = INDEX(params, r.x, r.y);
1042 for (x = r.x; x < r.x+r.w; x++)
1043 for (y = r.y; y < r.y+r.h; y++) {
1044 index(params, grid, x, y) = idx;
1046 #ifdef GENERATION_DIAGNOSTICS
1047 printf(" placing rectangle at (%d,%d) size %d x %d\n",
1048 r.x, r.y, r.w, r.h);
1052 static struct rect find_rect(game_params *params, int *grid, int x, int y)
1058 * Find the top left of the rectangle.
1060 idx = index(params, grid, x, y);
1066 return r; /* 1x1 singleton here */
1069 y = idx / params->w;
1070 x = idx % params->w;
1073 * Find the width and height of the rectangle.
1076 (x+w < params->w && index(params,grid,x+w,y)==idx);
1079 (y+h < params->h && index(params,grid,x,y+h)==idx);
1090 #ifdef GENERATION_DIAGNOSTICS
1091 static void display_grid(game_params *params, int *grid, int *numbers, int all)
1093 unsigned char *egrid = snewn((params->w*2+3) * (params->h*2+3),
1096 int r = (params->w*2+3);
1098 memset(egrid, 0, (params->w*2+3) * (params->h*2+3));
1100 for (x = 0; x < params->w; x++)
1101 for (y = 0; y < params->h; y++) {
1102 int i = index(params, grid, x, y);
1103 if (x == 0 || index(params, grid, x-1, y) != i)
1104 egrid[(2*y+2) * r + (2*x+1)] = 1;
1105 if (x == params->w-1 || index(params, grid, x+1, y) != i)
1106 egrid[(2*y+2) * r + (2*x+3)] = 1;
1107 if (y == 0 || index(params, grid, x, y-1) != i)
1108 egrid[(2*y+1) * r + (2*x+2)] = 1;
1109 if (y == params->h-1 || index(params, grid, x, y+1) != i)
1110 egrid[(2*y+3) * r + (2*x+2)] = 1;
1113 for (y = 1; y < 2*params->h+2; y++) {
1114 for (x = 1; x < 2*params->w+2; x++) {
1116 int k = numbers ? index(params, numbers, x/2-1, y/2-1) : 0;
1117 if (k || (all && numbers)) printf("%2d", k); else printf(" ");
1118 } else if (!((y&x)&1)) {
1119 int v = egrid[y*r+x];
1120 if ((y&1) && v) v = '-';
1121 if ((x&1) && v) v = '|';
1124 if (!(x&1)) putchar(v);
1127 if (egrid[y*r+(x+1)]) d |= 1;
1128 if (egrid[(y-1)*r+x]) d |= 2;
1129 if (egrid[y*r+(x-1)]) d |= 4;
1130 if (egrid[(y+1)*r+x]) d |= 8;
1131 c = " ??+?-++?+|+++++"[d];
1133 if (!(x&1)) putchar(c);
1143 static char *new_game_desc(const game_params *params_in, random_state *rs,
1144 char **aux, int interactive)
1146 game_params params_copy = *params_in; /* structure copy */
1147 game_params *params = ¶ms_copy;
1148 int *grid, *numbers = NULL;
1149 int x, y, y2, y2last, yx, run, i, nsquares;
1151 int *enum_rects_scratch;
1152 game_params params2real, *params2 = ¶ms2real;
1156 * Set up the smaller width and height which we will use to
1157 * generate the base grid.
1159 params2->w = (int)((float)params->w / (1.0F + params->expandfactor));
1160 if (params2->w < 2 && params->w >= 2) params2->w = 2;
1161 params2->h = (int)((float)params->h / (1.0F + params->expandfactor));
1162 if (params2->h < 2 && params->h >= 2) params2->h = 2;
1164 grid = snewn(params2->w * params2->h, int);
1166 enum_rects_scratch = snewn(2 * params2->w, int);
1169 for (y = 0; y < params2->h; y++)
1170 for (x = 0; x < params2->w; x++) {
1171 index(params2, grid, x, y) = -1;
1176 * Place rectangles until we can't any more. We do this by
1177 * finding a square we haven't yet covered, and randomly
1178 * choosing a rectangle to cover it.
1181 while (nsquares > 0) {
1182 int square = random_upto(rs, nsquares);
1188 for (y = 0; y < params2->h; y++) {
1189 for (x = 0; x < params2->w; x++) {
1190 if (index(params2, grid, x, y) == -1 && square-- == 0)
1196 assert(x < params2->w && y < params2->h);
1199 * Now see how many rectangles fit around this one.
1201 enum_rects(params2, grid, NULL, &n, x, y, enum_rects_scratch);
1205 * There are no possible rectangles covering this
1206 * square, meaning it must be a singleton. Mark it
1207 * -2 so we know not to keep trying.
1209 index(params2, grid, x, y) = -2;
1213 * Pick one at random.
1215 n = random_upto(rs, n);
1216 enum_rects(params2, grid, &r, &n, x, y, enum_rects_scratch);
1221 place_rect(params2, grid, r);
1222 nsquares -= r.w * r.h;
1226 sfree(enum_rects_scratch);
1229 * Deal with singleton spaces remaining in the grid, one by
1232 * We do this by making a local change to the layout. There are
1233 * several possibilities:
1235 * +-----+-----+ Here, we can remove the singleton by
1236 * | | | extending the 1x2 rectangle below it
1237 * +--+--+-----+ into a 1x3.
1245 * +--+--+--+ Here, that trick doesn't work: there's no
1246 * | | | 1 x n rectangle with the singleton at one
1247 * | | | end. Instead, we extend a 1 x n rectangle
1248 * | | | _out_ from the singleton, shaving a layer
1249 * +--+--+ | off the end of another rectangle. So if we
1250 * | | | | extended up, we'd make our singleton part
1251 * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2
1252 * | | | used to be; or we could extend right into
1253 * +--+-----+ a 2x1, turning the 1x3 into a 1x2.
1255 * +-----+--+ Here, we can't even do _that_, since any
1256 * | | | direction we choose to extend the singleton
1257 * +--+--+ | will produce a new singleton as a result of
1258 * | | | | truncating one of the size-2 rectangles.
1259 * | +--+--+ Fortunately, this case can _only_ occur when
1260 * | | | a singleton is surrounded by four size-2s
1261 * +--+-----+ in this fashion; so instead we can simply
1262 * replace the whole section with a single 3x3.
1264 for (x = 0; x < params2->w; x++) {
1265 for (y = 0; y < params2->h; y++) {
1266 if (index(params2, grid, x, y) < 0) {
1269 #ifdef GENERATION_DIAGNOSTICS
1270 display_grid(params2, grid, NULL, FALSE);
1271 printf("singleton at %d,%d\n", x, y);
1275 * Check in which directions we can feasibly extend
1276 * the singleton. We can extend in a particular
1277 * direction iff either:
1279 * - the rectangle on that side of the singleton
1280 * is not 2x1, and we are at one end of the edge
1281 * of it we are touching
1283 * - it is 2x1 but we are on its short side.
1285 * FIXME: we could plausibly choose between these
1286 * based on the sizes of the rectangles they would
1290 if (x < params2->w-1) {
1291 struct rect r = find_rect(params2, grid, x+1, y);
1292 if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1)
1293 dirs[ndirs++] = 1; /* right */
1296 struct rect r = find_rect(params2, grid, x, y-1);
1297 if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1)
1298 dirs[ndirs++] = 2; /* up */
1301 struct rect r = find_rect(params2, grid, x-1, y);
1302 if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1)
1303 dirs[ndirs++] = 4; /* left */
1305 if (y < params2->h-1) {
1306 struct rect r = find_rect(params2, grid, x, y+1);
1307 if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1)
1308 dirs[ndirs++] = 8; /* down */
1314 memset(&r1, 0, sizeof(struct rect));
1315 memset(&r2, 0, sizeof(struct rect));
1316 which = random_upto(rs, ndirs);
1321 assert(x < params2->w+1);
1322 #ifdef GENERATION_DIAGNOSTICS
1323 printf("extending right\n");
1325 r1 = find_rect(params2, grid, x+1, y);
1336 #ifdef GENERATION_DIAGNOSTICS
1337 printf("extending up\n");
1339 r1 = find_rect(params2, grid, x, y-1);
1350 #ifdef GENERATION_DIAGNOSTICS
1351 printf("extending left\n");
1353 r1 = find_rect(params2, grid, x-1, y);
1363 assert(y < params2->h+1);
1364 #ifdef GENERATION_DIAGNOSTICS
1365 printf("extending down\n");
1367 r1 = find_rect(params2, grid, x, y+1);
1376 default: /* should never happen */
1377 assert(!"invalid direction");
1379 if (r1.h > 0 && r1.w > 0)
1380 place_rect(params2, grid, r1);
1381 place_rect(params2, grid, r2);
1385 * Sanity-check that there really is a 3x3
1386 * rectangle surrounding this singleton and it
1387 * contains absolutely everything we could
1392 assert(x > 0 && x < params2->w-1);
1393 assert(y > 0 && y < params2->h-1);
1395 for (xx = x-1; xx <= x+1; xx++)
1396 for (yy = y-1; yy <= y+1; yy++) {
1397 struct rect r = find_rect(params2,grid,xx,yy);
1400 assert(r.x+r.w-1 <= x+1);
1401 assert(r.y+r.h-1 <= y+1);
1406 #ifdef GENERATION_DIAGNOSTICS
1407 printf("need the 3x3 trick\n");
1411 * FIXME: If the maximum rectangle area for
1412 * this grid is less than 9, we ought to
1413 * subdivide the 3x3 in some fashion. There are
1414 * five other possibilities:
1417 * - a 4, a 3 and a 2
1419 * - a 3 and three 2s (two different arrangements).
1427 place_rect(params2, grid, r);
1435 * We have now constructed a grid of the size specified in
1436 * params2. Now we extend it into a grid of the size specified
1437 * in params. We do this in two passes: we extend it vertically
1438 * until it's the right height, then we transpose it, then
1439 * extend it vertically again (getting it effectively the right
1440 * width), then finally transpose again.
1442 for (i = 0; i < 2; i++) {
1443 int *grid2, *expand, *where;
1444 game_params params3real, *params3 = ¶ms3real;
1446 #ifdef GENERATION_DIAGNOSTICS
1447 printf("before expansion:\n");
1448 display_grid(params2, grid, NULL, TRUE);
1452 * Set up the new grid.
1454 grid2 = snewn(params2->w * params->h, int);
1455 expand = snewn(params2->h-1, int);
1456 where = snewn(params2->w, int);
1457 params3->w = params2->w;
1458 params3->h = params->h;
1461 * Decide which horizontal edges are going to get expanded,
1464 for (y = 0; y < params2->h-1; y++)
1466 for (y = params2->h; y < params->h; y++) {
1467 x = random_upto(rs, params2->h-1);
1471 #ifdef GENERATION_DIAGNOSTICS
1472 printf("expand[] = {");
1473 for (y = 0; y < params2->h-1; y++)
1474 printf(" %d", expand[y]);
1479 * Perform the expansion. The way this works is that we
1482 * - copy a row from grid into grid2
1484 * - invent some number of additional rows in grid2 where
1485 * there was previously only a horizontal line between
1486 * rows in grid, and make random decisions about where
1487 * among these to place each rectangle edge that ran
1490 for (y = y2 = y2last = 0; y < params2->h; y++) {
1492 * Copy a single line from row y of grid into row y2 of
1495 for (x = 0; x < params2->w; x++) {
1496 int val = index(params2, grid, x, y);
1497 if (val / params2->w == y && /* rect starts on this line */
1498 (y2 == 0 || /* we're at the very top, or... */
1499 index(params3, grid2, x, y2-1) / params3->w < y2last
1500 /* this rect isn't already started */))
1501 index(params3, grid2, x, y2) =
1502 INDEX(params3, val % params2->w, y2);
1504 index(params3, grid2, x, y2) =
1505 index(params3, grid2, x, y2-1);
1509 * If that was the last line, terminate the loop early.
1511 if (++y2 == params3->h)
1517 * Invent some number of additional lines. First walk
1518 * along this line working out where to put all the
1519 * edges that coincide with it.
1522 for (x = 0; x < params2->w; x++) {
1523 if (index(params2, grid, x, y) !=
1524 index(params2, grid, x, y+1)) {
1526 * This is a horizontal edge, so it needs
1530 (index(params2, grid, x-1, y) !=
1531 index(params2, grid, x, y) &&
1532 index(params2, grid, x-1, y+1) !=
1533 index(params2, grid, x, y+1))) {
1535 * Here we have the chance to make a new
1538 yx = random_upto(rs, expand[y]+1);
1541 * Here we just reuse the previous value of
1550 for (yx = 0; yx < expand[y]; yx++) {
1552 * Invent a single row. For each square in the row,
1553 * we copy the grid entry from the square above it,
1554 * unless we're starting the new rectangle here.
1556 for (x = 0; x < params2->w; x++) {
1557 if (yx == where[x]) {
1558 int val = index(params2, grid, x, y+1);
1560 val = INDEX(params3, val, y2);
1561 index(params3, grid2, x, y2) = val;
1563 index(params3, grid2, x, y2) =
1564 index(params3, grid2, x, y2-1);
1574 #ifdef GENERATION_DIAGNOSTICS
1575 printf("after expansion:\n");
1576 display_grid(params3, grid2, NULL, TRUE);
1581 params2->w = params3->h;
1582 params2->h = params3->w;
1584 grid = snewn(params2->w * params2->h, int);
1585 for (x = 0; x < params2->w; x++)
1586 for (y = 0; y < params2->h; y++) {
1587 int idx1 = INDEX(params2, x, y);
1588 int idx2 = INDEX(params3, y, x);
1592 tmp = (tmp % params3->w) * params2->w + (tmp / params3->w);
1601 params->w = params->h;
1605 #ifdef GENERATION_DIAGNOSTICS
1606 printf("after transposition:\n");
1607 display_grid(params2, grid, NULL, TRUE);
1612 * Run the solver to narrow down the possible number
1616 struct numberdata *nd;
1617 int nnumbers, i, ret;
1619 /* Count the rectangles. */
1621 for (y = 0; y < params->h; y++) {
1622 for (x = 0; x < params->w; x++) {
1623 int idx = INDEX(params, x, y);
1624 if (index(params, grid, x, y) == idx)
1629 nd = snewn(nnumbers, struct numberdata);
1631 /* Now set up each number's candidate position list. */
1633 for (y = 0; y < params->h; y++) {
1634 for (x = 0; x < params->w; x++) {
1635 int idx = INDEX(params, x, y);
1636 if (index(params, grid, x, y) == idx) {
1637 struct rect r = find_rect(params, grid, x, y);
1640 nd[i].area = r.w * r.h;
1641 nd[i].npoints = nd[i].area;
1642 nd[i].points = snewn(nd[i].npoints, struct point);
1644 for (j = 0; j < r.h; j++)
1645 for (k = 0; k < r.w; k++) {
1646 nd[i].points[m].x = k + r.x;
1647 nd[i].points[m].y = j + r.y;
1650 assert(m == nd[i].npoints);
1658 ret = rect_solver(params->w, params->h, nnumbers, nd,
1661 ret = 1; /* allow any number placement at all */
1665 * Now place the numbers according to the solver's
1668 numbers = snewn(params->w * params->h, int);
1670 for (y = 0; y < params->h; y++)
1671 for (x = 0; x < params->w; x++) {
1672 index(params, numbers, x, y) = 0;
1675 for (i = 0; i < nnumbers; i++) {
1676 int idx = random_upto(rs, nd[i].npoints);
1677 int x = nd[i].points[idx].x;
1678 int y = nd[i].points[idx].y;
1679 index(params,numbers,x,y) = nd[i].area;
1686 for (i = 0; i < nnumbers; i++)
1687 sfree(nd[i].points);
1691 * If we've succeeded, then terminate the loop.
1698 * Give up and go round again.
1704 * Store the solution in aux.
1710 len = 2 + (params->w-1)*params->h + (params->h-1)*params->w;
1711 ai = snewn(len, char);
1717 for (y = 0; y < params->h; y++)
1718 for (x = 1; x < params->w; x++)
1719 *p++ = (index(params, grid, x, y) !=
1720 index(params, grid, x-1, y) ? '1' : '0');
1722 for (y = 1; y < params->h; y++)
1723 for (x = 0; x < params->w; x++)
1724 *p++ = (index(params, grid, x, y) !=
1725 index(params, grid, x, y-1) ? '1' : '0');
1727 assert(p - ai == len-1);
1733 #ifdef GENERATION_DIAGNOSTICS
1734 display_grid(params, grid, numbers, FALSE);
1737 desc = snewn(11 * params->w * params->h, char);
1740 for (i = 0; i <= params->w * params->h; i++) {
1741 int n = (i < params->w * params->h ? numbers[i] : -1);
1748 int c = 'a' - 1 + run;
1752 run -= c - ('a' - 1);
1756 * If there's a number in the very top left or
1757 * bottom right, there's no point putting an
1758 * unnecessary _ before or after it.
1760 if (p > desc && n > 0)
1764 p += sprintf(p, "%d", n);
1776 static char *validate_desc(const game_params *params, const char *desc)
1778 int area = params->w * params->h;
1783 if (n >= 'a' && n <= 'z') {
1784 squares += n - 'a' + 1;
1785 } else if (n == '_') {
1787 } else if (n > '0' && n <= '9') {
1789 while (*desc >= '0' && *desc <= '9')
1792 return "Invalid character in game description";
1796 return "Not enough data to fill grid";
1799 return "Too much data to fit in grid";
1804 static unsigned char *get_correct(game_state *state)
1809 ret = snewn(state->w * state->h, unsigned char);
1810 memset(ret, 0xFF, state->w * state->h);
1812 for (x = 0; x < state->w; x++)
1813 for (y = 0; y < state->h; y++)
1814 if (index(state,ret,x,y) == 0xFF) {
1817 int num, area, valid;
1820 * Find a rectangle starting at this point.
1823 while (x+rw < state->w && !vedge(state,x+rw,y))
1826 while (y+rh < state->h && !hedge(state,x,y+rh))
1830 * We know what the dimensions of the rectangle
1831 * should be if it's there at all. Find out if we
1832 * really have a valid rectangle.
1835 /* Check the horizontal edges. */
1836 for (xx = x; xx < x+rw; xx++) {
1837 for (yy = y; yy <= y+rh; yy++) {
1838 int e = !HRANGE(state,xx,yy) || hedge(state,xx,yy);
1839 int ec = (yy == y || yy == y+rh);
1844 /* Check the vertical edges. */
1845 for (yy = y; yy < y+rh; yy++) {
1846 for (xx = x; xx <= x+rw; xx++) {
1847 int e = !VRANGE(state,xx,yy) || vedge(state,xx,yy);
1848 int ec = (xx == x || xx == x+rw);
1855 * If this is not a valid rectangle with no other
1856 * edges inside it, we just mark this square as not
1857 * complete and proceed to the next square.
1860 index(state, ret, x, y) = 0;
1865 * We have a rectangle. Now see what its area is,
1866 * and how many numbers are in it.
1870 for (xx = x; xx < x+rw; xx++) {
1871 for (yy = y; yy < y+rh; yy++) {
1873 if (grid(state,xx,yy)) {
1875 valid = FALSE; /* two numbers */
1876 num = grid(state,xx,yy);
1884 * Now fill in the whole rectangle based on the
1887 for (xx = x; xx < x+rw; xx++) {
1888 for (yy = y; yy < y+rh; yy++) {
1889 index(state, ret, xx, yy) = valid;
1897 static game_state *new_game(midend *me, const game_params *params,
1900 game_state *state = snew(game_state);
1903 state->w = params->w;
1904 state->h = params->h;
1906 area = state->w * state->h;
1908 state->grid = snewn(area, int);
1909 state->vedge = snewn(area, unsigned char);
1910 state->hedge = snewn(area, unsigned char);
1911 state->completed = state->cheated = FALSE;
1916 if (n >= 'a' && n <= 'z') {
1917 int run = n - 'a' + 1;
1918 assert(i + run <= area);
1920 state->grid[i++] = 0;
1921 } else if (n == '_') {
1923 } else if (n > '0' && n <= '9') {
1925 state->grid[i++] = atoi(desc-1);
1926 while (*desc >= '0' && *desc <= '9')
1929 assert(!"We can't get here");
1934 for (y = 0; y < state->h; y++)
1935 for (x = 0; x < state->w; x++)
1936 vedge(state,x,y) = hedge(state,x,y) = 0;
1938 state->correct = get_correct(state);
1943 static game_state *dup_game(const game_state *state)
1945 game_state *ret = snew(game_state);
1950 ret->vedge = snewn(state->w * state->h, unsigned char);
1951 ret->hedge = snewn(state->w * state->h, unsigned char);
1952 ret->grid = snewn(state->w * state->h, int);
1953 ret->correct = snewn(ret->w * ret->h, unsigned char);
1955 ret->completed = state->completed;
1956 ret->cheated = state->cheated;
1958 memcpy(ret->grid, state->grid, state->w * state->h * sizeof(int));
1959 memcpy(ret->vedge, state->vedge, state->w*state->h*sizeof(unsigned char));
1960 memcpy(ret->hedge, state->hedge, state->w*state->h*sizeof(unsigned char));
1962 memcpy(ret->correct, state->correct, state->w*state->h*sizeof(unsigned char));
1967 static void free_game(game_state *state)
1970 sfree(state->vedge);
1971 sfree(state->hedge);
1972 sfree(state->correct);
1976 static char *solve_game(const game_state *state, const game_state *currstate,
1977 const char *ai, char **error)
1979 unsigned char *vedge, *hedge;
1983 struct numberdata *nd;
1989 * Attempt the in-built solver.
1992 /* Set up each number's (very short) candidate position list. */
1993 for (i = n = 0; i < state->h * state->w; i++)
1997 nd = snewn(n, struct numberdata);
1999 for (i = j = 0; i < state->h * state->w; i++)
2000 if (state->grid[i]) {
2001 nd[j].area = state->grid[i];
2003 nd[j].points = snewn(1, struct point);
2004 nd[j].points[0].x = i % state->w;
2005 nd[j].points[0].y = i / state->w;
2011 vedge = snewn(state->w * state->h, unsigned char);
2012 hedge = snewn(state->w * state->h, unsigned char);
2013 memset(vedge, 0, state->w * state->h);
2014 memset(hedge, 0, state->w * state->h);
2016 rect_solver(state->w, state->h, n, nd, hedge, vedge, NULL);
2021 for (i = 0; i < n; i++)
2022 sfree(nd[i].points);
2025 len = 2 + (state->w-1)*state->h + (state->h-1)*state->w;
2026 ret = snewn(len, char);
2030 for (y = 0; y < state->h; y++)
2031 for (x = 1; x < state->w; x++)
2032 *p++ = vedge[y*state->w+x] ? '1' : '0';
2033 for (y = 1; y < state->h; y++)
2034 for (x = 0; x < state->w; x++)
2035 *p++ = hedge[y*state->w+x] ? '1' : '0';
2037 assert(p - ret == len);
2045 static int game_can_format_as_text_now(const game_params *params)
2050 static char *game_text_format(const game_state *state)
2052 char *ret, *p, buf[80];
2053 int i, x, y, col, maxlen;
2056 * First determine the number of spaces required to display a
2057 * number. We'll use at least two, because one looks a bit
2061 for (i = 0; i < state->w * state->h; i++) {
2062 x = sprintf(buf, "%d", state->grid[i]);
2063 if (col < x) col = x;
2067 * Now we know the exact total size of the grid we're going to
2068 * produce: it's got 2*h+1 rows, each containing w lots of col,
2069 * w+1 boundary characters and a trailing newline.
2071 maxlen = (2*state->h+1) * (state->w * (col+1) + 2);
2073 ret = snewn(maxlen+1, char);
2076 for (y = 0; y <= 2*state->h; y++) {
2077 for (x = 0; x <= 2*state->w; x++) {
2082 int v = grid(state, x/2, y/2);
2084 sprintf(buf, "%*d", col, v);
2086 sprintf(buf, "%*s", col, "");
2087 memcpy(p, buf, col);
2091 * Display a horizontal edge or nothing.
2093 int h = (y==0 || y==2*state->h ? 1 :
2094 HRANGE(state, x/2, y/2) && hedge(state, x/2, y/2));
2100 for (i = 0; i < col; i++)
2104 * Display a vertical edge or nothing.
2106 int v = (x==0 || x==2*state->w ? 1 :
2107 VRANGE(state, x/2, y/2) && vedge(state, x/2, y/2));
2114 * Display a corner, or a vertical edge, or a
2115 * horizontal edge, or nothing.
2117 int hl = (y==0 || y==2*state->h ? 1 :
2118 HRANGE(state, (x-1)/2, y/2) && hedge(state, (x-1)/2, y/2));
2119 int hr = (y==0 || y==2*state->h ? 1 :
2120 HRANGE(state, (x+1)/2, y/2) && hedge(state, (x+1)/2, y/2));
2121 int vu = (x==0 || x==2*state->w ? 1 :
2122 VRANGE(state, x/2, (y-1)/2) && vedge(state, x/2, (y-1)/2));
2123 int vd = (x==0 || x==2*state->w ? 1 :
2124 VRANGE(state, x/2, (y+1)/2) && vedge(state, x/2, (y+1)/2));
2125 if (!hl && !hr && !vu && !vd)
2127 else if (hl && hr && !vu && !vd)
2129 else if (!hl && !hr && vu && vd)
2138 assert(p - ret == maxlen);
2145 * These coordinates are 2 times the obvious grid coordinates.
2146 * Hence, the top left of the grid is (0,0), the grid point to
2147 * the right of that is (2,0), the one _below that_ is (2,2)
2148 * and so on. This is so that we can specify a drag start point
2149 * on an edge (one odd coordinate) or in the middle of a square
2150 * (two odd coordinates) rather than always at a corner.
2152 * -1,-1 means no drag is in progress.
2159 * This flag is set as soon as a dragging action moves the
2160 * mouse pointer away from its starting point, so that even if
2161 * the pointer _returns_ to its starting point the action is
2162 * treated as a small drag rather than a click.
2165 /* This flag is set if we're doing an erase operation (i.e.
2166 * removing edges in the centre of the rectangle without altering
2171 * These are the co-ordinates of the top-left and bottom-right squares
2172 * in the drag box, respectively, or -1 otherwise.
2179 * These are the coordinates of a cursor, whether it's visible, and
2180 * whether it was used to start a drag.
2182 int cur_x, cur_y, cur_visible, cur_dragging;
2185 static void reset_ui(game_ui *ui)
2187 ui->drag_start_x = -1;
2188 ui->drag_start_y = -1;
2189 ui->drag_end_x = -1;
2190 ui->drag_end_y = -1;
2195 ui->dragged = FALSE;
2198 static game_ui *new_ui(const game_state *state)
2200 game_ui *ui = snew(game_ui);
2202 ui->erasing = FALSE;
2203 ui->cur_x = ui->cur_y = ui->cur_visible = ui->cur_dragging = 0;
2207 static void free_ui(game_ui *ui)
2212 static char *encode_ui(const game_ui *ui)
2217 static void decode_ui(game_ui *ui, const char *encoding)
2221 static void coord_round(float x, float y, int *xr, int *yr)
2223 float xs, ys, xv, yv, dx, dy, dist;
2226 * Find the nearest square-centre.
2228 xs = (float)floor(x) + 0.5F;
2229 ys = (float)floor(y) + 0.5F;
2232 * And find the nearest grid vertex.
2234 xv = (float)floor(x + 0.5F);
2235 yv = (float)floor(y + 0.5F);
2238 * We allocate clicks in parts of the grid square to either
2239 * corners, edges or square centres, as follows:
2255 * In other words: we measure the square distance (i.e.
2256 * max(dx,dy)) from the click to the nearest corner, and if
2257 * it's within CORNER_TOLERANCE then we return a corner click.
2258 * We measure the square distance from the click to the nearest
2259 * centre, and if that's within CENTRE_TOLERANCE we return a
2260 * centre click. Failing that, we find which of the two edge
2261 * centres is nearer to the click and return that edge.
2265 * Check for corner click.
2267 dx = (float)fabs(x - xv);
2268 dy = (float)fabs(y - yv);
2269 dist = (dx > dy ? dx : dy);
2270 if (dist < CORNER_TOLERANCE) {
2275 * Check for centre click.
2277 dx = (float)fabs(x - xs);
2278 dy = (float)fabs(y - ys);
2279 dist = (dx > dy ? dx : dy);
2280 if (dist < CENTRE_TOLERANCE) {
2281 *xr = 1 + 2 * (int)xs;
2282 *yr = 1 + 2 * (int)ys;
2285 * Failing both of those, see which edge we're closer to.
2286 * Conveniently, this is simply done by testing the relative
2287 * magnitude of dx and dy (which are currently distances from
2288 * the square centre).
2291 /* Vertical edge: x-coord of corner,
2292 * y-coord of square centre. */
2294 *yr = 1 + 2 * (int)floor(ys);
2296 /* Horizontal edge: x-coord of square centre,
2297 * y-coord of corner. */
2298 *xr = 1 + 2 * (int)floor(xs);
2306 * Returns TRUE if it has made any change to the grid.
2308 static int grid_draw_rect(const game_state *state,
2309 unsigned char *hedge, unsigned char *vedge,
2310 int c, int really, int outline,
2311 int x1, int y1, int x2, int y2)
2314 int changed = FALSE;
2317 * Draw horizontal edges of rectangles.
2319 for (x = x1; x < x2; x++)
2320 for (y = y1; y <= y2; y++)
2321 if (HRANGE(state,x,y)) {
2322 int val = index(state,hedge,x,y);
2323 if (y == y1 || y == y2) {
2324 if (!outline) continue;
2328 changed = changed || (index(state,hedge,x,y) != val);
2330 index(state,hedge,x,y) = val;
2334 * Draw vertical edges of rectangles.
2336 for (y = y1; y < y2; y++)
2337 for (x = x1; x <= x2; x++)
2338 if (VRANGE(state,x,y)) {
2339 int val = index(state,vedge,x,y);
2340 if (x == x1 || x == x2) {
2341 if (!outline) continue;
2345 changed = changed || (index(state,vedge,x,y) != val);
2347 index(state,vedge,x,y) = val;
2353 static int ui_draw_rect(const game_state *state, const game_ui *ui,
2354 unsigned char *hedge, unsigned char *vedge, int c,
2355 int really, int outline)
2357 return grid_draw_rect(state, hedge, vedge, c, really, outline,
2358 ui->x1, ui->y1, ui->x2, ui->y2);
2361 static void game_changed_state(game_ui *ui, const game_state *oldstate,
2362 const game_state *newstate)
2366 struct game_drawstate {
2369 unsigned long *visible;
2372 static char *interpret_move(const game_state *from, game_ui *ui,
2373 const game_drawstate *ds,
2374 int x, int y, int button)
2377 int startdrag = FALSE, enddrag = FALSE, active = FALSE, erasing = FALSE;
2380 button &= ~MOD_MASK;
2382 coord_round(FROMCOORD((float)x), FROMCOORD((float)y), &xc, &yc);
2384 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2385 if (ui->drag_start_x >= 0 && ui->cur_dragging)
2386 reset_ui(ui); /* cancel keyboard dragging */
2388 ui->cur_visible = ui->cur_dragging = FALSE;
2390 erasing = (button == RIGHT_BUTTON);
2391 } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) {
2392 /* We assert we should have had a LEFT_BUTTON first. */
2393 if (ui->cur_visible) {
2394 ui->cur_visible = FALSE;
2397 assert(!ui->cur_dragging);
2399 erasing = (button == RIGHT_RELEASE);
2400 } else if (IS_CURSOR_MOVE(button)) {
2401 move_cursor(button, &ui->cur_x, &ui->cur_y, from->w, from->h, 0);
2402 ui->cur_visible = TRUE;
2404 if (!ui->cur_dragging) return UI_UPDATE;
2405 coord_round((float)ui->cur_x + 0.5F, (float)ui->cur_y + 0.5F, &xc, &yc);
2406 } else if (IS_CURSOR_SELECT(button)) {
2407 if (ui->drag_start_x >= 0 && !ui->cur_dragging) {
2409 * If a mouse drag is in progress, ignore attempts to
2410 * start a keyboard one.
2414 if (!ui->cur_visible) {
2415 assert(!ui->cur_dragging);
2416 ui->cur_visible = TRUE;
2419 coord_round((float)ui->cur_x + 0.5F, (float)ui->cur_y + 0.5F, &xc, &yc);
2420 erasing = (button == CURSOR_SELECT2);
2421 if (ui->cur_dragging) {
2422 ui->cur_dragging = FALSE;
2426 ui->cur_dragging = TRUE;
2430 } else if (button == '\b' || button == 27) {
2431 if (!ui->cur_dragging) {
2432 ui->cur_visible = FALSE;
2434 assert(ui->cur_visible);
2435 reset_ui(ui); /* cancel keyboard dragging */
2436 ui->cur_dragging = FALSE;
2439 } else if (button != LEFT_DRAG && button != RIGHT_DRAG) {
2444 xc >= 0 && xc <= 2*from->w &&
2445 yc >= 0 && yc <= 2*from->h) {
2447 ui->drag_start_x = xc;
2448 ui->drag_start_y = yc;
2449 ui->drag_end_x = -1;
2450 ui->drag_end_y = -1;
2451 ui->dragged = FALSE;
2452 ui->erasing = erasing;
2456 if (ui->drag_start_x >= 0 &&
2457 (xc != ui->drag_end_x || yc != ui->drag_end_y)) {
2460 if (ui->drag_end_x != -1 && ui->drag_end_y != -1)
2462 ui->drag_end_x = xc;
2463 ui->drag_end_y = yc;
2466 if (xc >= 0 && xc <= 2*from->w &&
2467 yc >= 0 && yc <= 2*from->h) {
2468 ui->x1 = ui->drag_start_x;
2469 ui->x2 = ui->drag_end_x;
2470 if (ui->x2 < ui->x1) { t = ui->x1; ui->x1 = ui->x2; ui->x2 = t; }
2472 ui->y1 = ui->drag_start_y;
2473 ui->y2 = ui->drag_end_y;
2474 if (ui->y2 < ui->y1) { t = ui->y1; ui->y1 = ui->y2; ui->y2 = t; }
2476 ui->x1 = ui->x1 / 2; /* rounds down */
2477 ui->x2 = (ui->x2+1) / 2; /* rounds up */
2478 ui->y1 = ui->y1 / 2; /* rounds down */
2479 ui->y2 = (ui->y2+1) / 2; /* rounds up */
2490 if (enddrag && (ui->drag_start_x >= 0)) {
2491 if (xc >= 0 && xc <= 2*from->w &&
2492 yc >= 0 && yc <= 2*from->h &&
2493 erasing == ui->erasing) {
2496 if (ui_draw_rect(from, ui, from->hedge,
2497 from->vedge, 1, FALSE, !ui->erasing)) {
2498 sprintf(buf, "%c%d,%d,%d,%d",
2499 (int)(ui->erasing ? 'E' : 'R'),
2500 ui->x1, ui->y1, ui->x2 - ui->x1, ui->y2 - ui->y1);
2504 if ((xc & 1) && !(yc & 1) && HRANGE(from,xc/2,yc/2)) {
2505 sprintf(buf, "H%d,%d", xc/2, yc/2);
2508 if ((yc & 1) && !(xc & 1) && VRANGE(from,xc/2,yc/2)) {
2509 sprintf(buf, "V%d,%d", xc/2, yc/2);
2520 return ret; /* a move has been made */
2527 static game_state *execute_move(const game_state *from, const char *move)
2530 int x1, y1, x2, y2, mode;
2532 if (move[0] == 'S') {
2533 const char *p = move+1;
2536 ret = dup_game(from);
2537 ret->cheated = TRUE;
2539 for (y = 0; y < ret->h; y++)
2540 for (x = 1; x < ret->w; x++) {
2541 vedge(ret, x, y) = (*p == '1');
2544 for (y = 1; y < ret->h; y++)
2545 for (x = 0; x < ret->w; x++) {
2546 hedge(ret, x, y) = (*p == '1');
2550 sfree(ret->correct);
2551 ret->correct = get_correct(ret);
2555 } else if ((move[0] == 'R' || move[0] == 'E') &&
2556 sscanf(move+1, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) == 4 &&
2557 x1 >= 0 && x2 >= 0 && x1+x2 <= from->w &&
2558 y1 >= 0 && y2 >= 0 && y1+y2 <= from->h) {
2562 } else if ((move[0] == 'H' || move[0] == 'V') &&
2563 sscanf(move+1, "%d,%d", &x1, &y1) == 2 &&
2564 (move[0] == 'H' ? HRANGE(from, x1, y1) :
2565 VRANGE(from, x1, y1))) {
2568 return NULL; /* can't parse move string */
2570 ret = dup_game(from);
2572 if (mode == 'R' || mode == 'E') {
2573 grid_draw_rect(ret, ret->hedge, ret->vedge, 1, TRUE,
2574 mode == 'R', x1, y1, x2, y2);
2575 } else if (mode == 'H') {
2576 hedge(ret,x1,y1) = !hedge(ret,x1,y1);
2577 } else if (mode == 'V') {
2578 vedge(ret,x1,y1) = !vedge(ret,x1,y1);
2581 sfree(ret->correct);
2582 ret->correct = get_correct(ret);
2585 * We've made a real change to the grid. Check to see
2586 * if the game has been completed.
2588 if (!ret->completed) {
2592 for (x = 0; x < ret->w; x++)
2593 for (y = 0; y < ret->h; y++)
2594 if (!index(ret, ret->correct, x, y))
2598 ret->completed = TRUE;
2604 /* ----------------------------------------------------------------------
2608 #define CORRECT (1L<<16)
2609 #define CURSOR (1L<<17)
2611 #define COLOUR(k) ( (k)==1 ? COL_LINE : (k)==2 ? COL_DRAG : COL_DRAGERASE )
2612 #define MAX4(x,y,z,w) ( max(max(x,y),max(z,w)) )
2614 static void game_compute_size(const game_params *params, int tilesize,
2617 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2618 struct { int tilesize; } ads, *ds = &ads;
2619 ads.tilesize = tilesize;
2621 *x = params->w * TILE_SIZE + 2*BORDER + 1;
2622 *y = params->h * TILE_SIZE + 2*BORDER + 1;
2625 static void game_set_size(drawing *dr, game_drawstate *ds,
2626 const game_params *params, int tilesize)
2628 ds->tilesize = tilesize;
2631 static float *game_colours(frontend *fe, int *ncolours)
2633 float *ret = snewn(3 * NCOLOURS, float);
2635 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2637 ret[COL_GRID * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
2638 ret[COL_GRID * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
2639 ret[COL_GRID * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
2641 ret[COL_DRAG * 3 + 0] = 1.0F;
2642 ret[COL_DRAG * 3 + 1] = 0.0F;
2643 ret[COL_DRAG * 3 + 2] = 0.0F;
2645 ret[COL_DRAGERASE * 3 + 0] = 0.2F;
2646 ret[COL_DRAGERASE * 3 + 1] = 0.2F;
2647 ret[COL_DRAGERASE * 3 + 2] = 1.0F;
2649 ret[COL_CORRECT * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
2650 ret[COL_CORRECT * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
2651 ret[COL_CORRECT * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
2653 ret[COL_LINE * 3 + 0] = 0.0F;
2654 ret[COL_LINE * 3 + 1] = 0.0F;
2655 ret[COL_LINE * 3 + 2] = 0.0F;
2657 ret[COL_TEXT * 3 + 0] = 0.0F;
2658 ret[COL_TEXT * 3 + 1] = 0.0F;
2659 ret[COL_TEXT * 3 + 2] = 0.0F;
2661 ret[COL_CURSOR * 3 + 0] = 1.0F;
2662 ret[COL_CURSOR * 3 + 1] = 0.5F;
2663 ret[COL_CURSOR * 3 + 2] = 0.5F;
2665 *ncolours = NCOLOURS;
2669 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
2671 struct game_drawstate *ds = snew(struct game_drawstate);
2674 ds->started = FALSE;
2677 ds->visible = snewn(ds->w * ds->h, unsigned long);
2678 ds->tilesize = 0; /* not decided yet */
2679 for (i = 0; i < ds->w * ds->h; i++)
2680 ds->visible[i] = 0xFFFF;
2685 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2691 static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state,
2692 int x, int y, unsigned char *hedge, unsigned char *vedge,
2693 unsigned char *corners, unsigned long bgflags)
2695 int cx = COORD(x), cy = COORD(y);
2698 draw_rect(dr, cx, cy, TILE_SIZE+1, TILE_SIZE+1, COL_GRID);
2699 draw_rect(dr, cx+1, cy+1, TILE_SIZE-1, TILE_SIZE-1,
2700 (bgflags & CURSOR) ? COL_CURSOR :
2701 (bgflags & CORRECT) ? COL_CORRECT : COL_BACKGROUND);
2703 if (grid(state,x,y)) {
2704 sprintf(str, "%d", grid(state,x,y));
2705 draw_text(dr, cx+TILE_SIZE/2, cy+TILE_SIZE/2, FONT_VARIABLE,
2706 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, COL_TEXT, str);
2712 if (!HRANGE(state,x,y) || index(state,hedge,x,y))
2713 draw_rect(dr, cx, cy, TILE_SIZE+1, 2,
2714 HRANGE(state,x,y) ? COLOUR(index(state,hedge,x,y)) :
2716 if (!HRANGE(state,x,y+1) || index(state,hedge,x,y+1))
2717 draw_rect(dr, cx, cy+TILE_SIZE-1, TILE_SIZE+1, 2,
2718 HRANGE(state,x,y+1) ? COLOUR(index(state,hedge,x,y+1)) :
2720 if (!VRANGE(state,x,y) || index(state,vedge,x,y))
2721 draw_rect(dr, cx, cy, 2, TILE_SIZE+1,
2722 VRANGE(state,x,y) ? COLOUR(index(state,vedge,x,y)) :
2724 if (!VRANGE(state,x+1,y) || index(state,vedge,x+1,y))
2725 draw_rect(dr, cx+TILE_SIZE-1, cy, 2, TILE_SIZE+1,
2726 VRANGE(state,x+1,y) ? COLOUR(index(state,vedge,x+1,y)) :
2732 if (index(state,corners,x,y))
2733 draw_rect(dr, cx, cy, 2, 2,
2734 COLOUR(index(state,corners,x,y)));
2735 if (x+1 < state->w && index(state,corners,x+1,y))
2736 draw_rect(dr, cx+TILE_SIZE-1, cy, 2, 2,
2737 COLOUR(index(state,corners,x+1,y)));
2738 if (y+1 < state->h && index(state,corners,x,y+1))
2739 draw_rect(dr, cx, cy+TILE_SIZE-1, 2, 2,
2740 COLOUR(index(state,corners,x,y+1)));
2741 if (x+1 < state->w && y+1 < state->h && index(state,corners,x+1,y+1))
2742 draw_rect(dr, cx+TILE_SIZE-1, cy+TILE_SIZE-1, 2, 2,
2743 COLOUR(index(state,corners,x+1,y+1)));
2745 draw_update(dr, cx, cy, TILE_SIZE+1, TILE_SIZE+1);
2748 static void game_redraw(drawing *dr, game_drawstate *ds,
2749 const game_state *oldstate, const game_state *state,
2750 int dir, const game_ui *ui,
2751 float animtime, float flashtime)
2754 unsigned char *hedge, *vedge, *corners;
2757 hedge = snewn(state->w*state->h, unsigned char);
2758 vedge = snewn(state->w*state->h, unsigned char);
2759 memcpy(hedge, state->hedge, state->w*state->h);
2760 memcpy(vedge, state->vedge, state->w*state->h);
2761 ui_draw_rect(state, ui, hedge, vedge, ui->erasing ? 3 : 2, TRUE, TRUE);
2763 hedge = state->hedge;
2764 vedge = state->vedge;
2767 corners = snewn(state->w * state->h, unsigned char);
2768 memset(corners, 0, state->w * state->h);
2769 for (x = 0; x < state->w; x++)
2770 for (y = 0; y < state->h; y++) {
2772 int e = index(state, vedge, x, y);
2773 if (index(state,corners,x,y) < e)
2774 index(state,corners,x,y) = e;
2775 if (y+1 < state->h &&
2776 index(state,corners,x,y+1) < e)
2777 index(state,corners,x,y+1) = e;
2780 int e = index(state, hedge, x, y);
2781 if (index(state,corners,x,y) < e)
2782 index(state,corners,x,y) = e;
2783 if (x+1 < state->w &&
2784 index(state,corners,x+1,y) < e)
2785 index(state,corners,x+1,y) = e;
2791 state->w * TILE_SIZE + 2*BORDER + 1,
2792 state->h * TILE_SIZE + 2*BORDER + 1, COL_BACKGROUND);
2793 draw_rect(dr, COORD(0)-1, COORD(0)-1,
2794 ds->w*TILE_SIZE+3, ds->h*TILE_SIZE+3, COL_LINE);
2796 draw_update(dr, 0, 0,
2797 state->w * TILE_SIZE + 2*BORDER + 1,
2798 state->h * TILE_SIZE + 2*BORDER + 1);
2801 for (x = 0; x < state->w; x++)
2802 for (y = 0; y < state->h; y++) {
2803 unsigned long c = 0;
2805 if (HRANGE(state,x,y))
2806 c |= index(state,hedge,x,y);
2807 if (HRANGE(state,x,y+1))
2808 c |= index(state,hedge,x,y+1) << 2;
2809 if (VRANGE(state,x,y))
2810 c |= index(state,vedge,x,y) << 4;
2811 if (VRANGE(state,x+1,y))
2812 c |= index(state,vedge,x+1,y) << 6;
2813 c |= index(state,corners,x,y) << 8;
2815 c |= index(state,corners,x+1,y) << 10;
2817 c |= index(state,corners,x,y+1) << 12;
2818 if (x+1 < state->w && y+1 < state->h)
2819 /* cast to prevent 2<<14 sign-extending on promotion to long */
2820 c |= (unsigned long)index(state,corners,x+1,y+1) << 14;
2821 if (index(state, state->correct, x, y) && !flashtime)
2823 if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y)
2826 if (index(ds,ds->visible,x,y) != c) {
2827 draw_tile(dr, ds, state, x, y, hedge, vedge, corners,
2828 (c & (CORRECT|CURSOR)) );
2829 index(ds,ds->visible,x,y) = c;
2837 ui->x1 >= 0 && ui->y1 >= 0 &&
2838 ui->x2 >= 0 && ui->y2 >= 0) {
2839 sprintf(buf, "%dx%d ",
2847 strcat(buf, "Auto-solved.");
2848 else if (state->completed)
2849 strcat(buf, "COMPLETED!");
2851 status_bar(dr, buf);
2854 if (hedge != state->hedge) {
2862 static float game_anim_length(const game_state *oldstate,
2863 const game_state *newstate, int dir, game_ui *ui)
2868 static float game_flash_length(const game_state *oldstate,
2869 const game_state *newstate, int dir, game_ui *ui)
2871 if (!oldstate->completed && newstate->completed &&
2872 !oldstate->cheated && !newstate->cheated)
2877 static int game_status(const game_state *state)
2879 return state->completed ? +1 : 0;
2882 static int game_timing_state(const game_state *state, game_ui *ui)
2887 static void game_print_size(const game_params *params, float *x, float *y)
2892 * I'll use 5mm squares by default.
2894 game_compute_size(params, 500, &pw, &ph);
2899 static void game_print(drawing *dr, const game_state *state, int tilesize)
2901 int w = state->w, h = state->h;
2902 int ink = print_mono_colour(dr, 0);
2905 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2906 game_drawstate ads, *ds = &ads;
2907 game_set_size(dr, ds, NULL, tilesize);
2912 print_line_width(dr, TILE_SIZE / 10);
2913 draw_rect_outline(dr, COORD(0), COORD(0), w*TILE_SIZE, h*TILE_SIZE, ink);
2916 * Grid. We have to make the grid lines particularly thin,
2917 * because users will be drawing lines _along_ them and we want
2918 * those lines to be visible.
2920 print_line_width(dr, TILE_SIZE / 256);
2921 for (x = 1; x < w; x++)
2922 draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h), ink);
2923 for (y = 1; y < h; y++)
2924 draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y), ink);
2929 print_line_width(dr, TILE_SIZE / 10);
2930 for (y = 0; y <= h; y++)
2931 for (x = 0; x <= w; x++) {
2932 if (HRANGE(state,x,y) && hedge(state,x,y))
2933 draw_line(dr, COORD(x), COORD(y), COORD(x+1), COORD(y), ink);
2934 if (VRANGE(state,x,y) && vedge(state,x,y))
2935 draw_line(dr, COORD(x), COORD(y), COORD(x), COORD(y+1), ink);
2941 for (y = 0; y < h; y++)
2942 for (x = 0; x < w; x++)
2943 if (grid(state,x,y)) {
2945 sprintf(str, "%d", grid(state,x,y));
2946 draw_text(dr, COORD(x)+TILE_SIZE/2, COORD(y)+TILE_SIZE/2,
2947 FONT_VARIABLE, TILE_SIZE/2,
2948 ALIGN_HCENTRE | ALIGN_VCENTRE, ink, str);
2953 #define thegame rect
2956 const struct game thegame = {
2957 "Rectangles", "games.rectangles", "rect",
2959 game_fetch_preset, NULL,
2964 TRUE, game_configure, custom_params,
2972 TRUE, game_can_format_as_text_now, game_text_format,
2980 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2983 game_free_drawstate,
2988 TRUE, FALSE, game_print_size, game_print,
2989 TRUE, /* wants_statusbar */
2990 FALSE, game_timing_state,
2994 /* vim: set shiftwidth=4 tabstop=8: */