2 * rect.c: Puzzle from nikoli.co.jp. You have a square grid with
3 * numbers in some squares; you must divide the square grid up into
4 * variously sized rectangles, such that every rectangle contains
5 * exactly one numbered square and the area of each rectangle is
6 * equal to the number contained in it.
12 * - Improve on singleton removal by making an aesthetic choice
13 * about which of the options to take.
15 * - When doing the 3x3 trick in singleton removal, limit the size
16 * of the generated rectangles in accordance with the max
19 * - If we start by sorting the rectlist in descending order
20 * of area, we might be able to bias our random number
21 * selection to produce a few large rectangles more often
22 * than oodles of small ones? Unsure, but might be worth a
51 #define INDEX(state, x, y) (((y) * (state)->w) + (x))
52 #define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
53 #define grid(state,x,y) index(state, (state)->grid, x, y)
54 #define vedge(state,x,y) index(state, (state)->vedge, x, y)
55 #define hedge(state,x,y) index(state, (state)->hedge, x, y)
57 #define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
58 (y) >= dy && (y) < (state)->h )
59 #define RANGE(state,x,y) CRANGE(state,x,y,0,0)
60 #define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
61 #define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
66 #define CORNER_TOLERANCE 0.15F
67 #define CENTRE_TOLERANCE 0.15F
69 #define FLASH_TIME 0.13F
71 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
72 #define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
76 int *grid; /* contains the numbers */
77 unsigned char *vedge; /* (w+1) x h */
78 unsigned char *hedge; /* w x (h+1) */
79 int completed, cheated;
82 static game_params *default_params(void)
84 game_params *ret = snew(game_params);
87 ret->expandfactor = 0.0F;
93 static int game_fetch_preset(int i, char **name, game_params **params)
100 case 0: w = 7, h = 7; break;
101 case 1: w = 11, h = 11; break;
102 case 2: w = 15, h = 15; break;
103 case 3: w = 19, h = 19; break;
104 default: return FALSE;
107 sprintf(buf, "%dx%d", w, h);
109 *params = ret = snew(game_params);
112 ret->expandfactor = 0.0F;
117 static void free_params(game_params *params)
122 static game_params *dup_params(game_params *params)
124 game_params *ret = snew(game_params);
125 *ret = *params; /* structure copy */
129 static void decode_params(game_params *ret, char const *string)
131 ret->w = ret->h = atoi(string);
132 while (*string && isdigit((unsigned char)*string)) string++;
133 if (*string == 'x') {
135 ret->h = atoi(string);
136 while (*string && isdigit((unsigned char)*string)) string++;
138 if (*string == 'e') {
140 ret->expandfactor = atof(string);
142 (*string == '.' || isdigit((unsigned char)*string))) string++;
144 if (*string == 'a') {
150 static char *encode_params(game_params *params, int full)
154 sprintf(data, "%dx%d", params->w, params->h);
155 if (full && params->expandfactor)
156 sprintf(data + strlen(data), "e%g", params->expandfactor);
157 if (full && !params->unique)
163 static config_item *game_configure(game_params *params)
168 ret = snewn(5, config_item);
170 ret[0].name = "Width";
171 ret[0].type = C_STRING;
172 sprintf(buf, "%d", params->w);
173 ret[0].sval = dupstr(buf);
176 ret[1].name = "Height";
177 ret[1].type = C_STRING;
178 sprintf(buf, "%d", params->h);
179 ret[1].sval = dupstr(buf);
182 ret[2].name = "Expansion factor";
183 ret[2].type = C_STRING;
184 sprintf(buf, "%g", params->expandfactor);
185 ret[2].sval = dupstr(buf);
188 ret[3].name = "Ensure unique solution";
189 ret[3].type = C_BOOLEAN;
191 ret[3].ival = params->unique;
201 static game_params *custom_params(config_item *cfg)
203 game_params *ret = snew(game_params);
205 ret->w = atoi(cfg[0].sval);
206 ret->h = atoi(cfg[1].sval);
207 ret->expandfactor = atof(cfg[2].sval);
208 ret->unique = cfg[3].ival;
213 static char *validate_params(game_params *params)
215 if (params->w <= 0 && params->h <= 0)
216 return "Width and height must both be greater than zero";
217 if (params->w < 2 && params->h < 2)
218 return "Grid area must be greater than one";
219 if (params->expandfactor < 0.0F)
220 return "Expansion factor may not be negative";
241 struct point *points;
244 /* ----------------------------------------------------------------------
245 * Solver for Rectangles games.
247 * This solver is souped up beyond the needs of actually _solving_
248 * a puzzle. It is also designed to cope with uncertainty about
249 * where the numbers have been placed. This is because I run it on
250 * my generated grids _before_ placing the numbers, and have it
251 * tell me where I need to place the numbers to ensure a unique
255 static void remove_rect_placement(int w, int h,
256 struct rectlist *rectpositions,
258 int rectnum, int placement)
262 #ifdef SOLVER_DIAGNOSTICS
263 printf("ruling out rect %d placement at %d,%d w=%d h=%d\n", rectnum,
264 rectpositions[rectnum].rects[placement].x,
265 rectpositions[rectnum].rects[placement].y,
266 rectpositions[rectnum].rects[placement].w,
267 rectpositions[rectnum].rects[placement].h);
271 * Decrement each entry in the overlaps array to reflect the
272 * removal of this rectangle placement.
274 for (yy = 0; yy < rectpositions[rectnum].rects[placement].h; yy++) {
275 y = yy + rectpositions[rectnum].rects[placement].y;
276 for (xx = 0; xx < rectpositions[rectnum].rects[placement].w; xx++) {
277 x = xx + rectpositions[rectnum].rects[placement].x;
279 assert(overlaps[(rectnum * h + y) * w + x] != 0);
281 if (overlaps[(rectnum * h + y) * w + x] > 0)
282 overlaps[(rectnum * h + y) * w + x]--;
287 * Remove the placement from the list of positions for that
288 * rectangle, by interchanging it with the one on the end.
290 if (placement < rectpositions[rectnum].n - 1) {
293 t = rectpositions[rectnum].rects[rectpositions[rectnum].n - 1];
294 rectpositions[rectnum].rects[rectpositions[rectnum].n - 1] =
295 rectpositions[rectnum].rects[placement];
296 rectpositions[rectnum].rects[placement] = t;
298 rectpositions[rectnum].n--;
301 static void remove_number_placement(int w, int h, struct numberdata *number,
302 int index, int *rectbyplace)
305 * Remove the entry from the rectbyplace array.
307 rectbyplace[number->points[index].y * w + number->points[index].x] = -1;
310 * Remove the placement from the list of candidates for that
311 * number, by interchanging it with the one on the end.
313 if (index < number->npoints - 1) {
316 t = number->points[number->npoints - 1];
317 number->points[number->npoints - 1] = number->points[index];
318 number->points[index] = t;
323 static int rect_solver(int w, int h, int nrects, struct numberdata *numbers,
324 game_state *result, random_state *rs)
326 struct rectlist *rectpositions;
327 int *overlaps, *rectbyplace, *workspace;
331 * Start by setting up a list of candidate positions for each
334 rectpositions = snewn(nrects, struct rectlist);
335 for (i = 0; i < nrects; i++) {
336 int rw, rh, area = numbers[i].area;
337 int j, minx, miny, maxx, maxy;
339 int rlistn, rlistsize;
342 * For each rectangle, begin by finding the bounding
343 * rectangle of its candidate number placements.
348 for (j = 0; j < numbers[i].npoints; j++) {
349 if (minx > numbers[i].points[j].x) minx = numbers[i].points[j].x;
350 if (miny > numbers[i].points[j].y) miny = numbers[i].points[j].y;
351 if (maxx < numbers[i].points[j].x) maxx = numbers[i].points[j].x;
352 if (maxy < numbers[i].points[j].y) maxy = numbers[i].points[j].y;
356 * Now loop over all possible rectangle placements
357 * overlapping a point within that bounding rectangle;
358 * ensure each one actually contains a candidate number
359 * placement, and add it to the list.
362 rlistn = rlistsize = 0;
364 for (rw = 1; rw <= area && rw <= w; rw++) {
373 for (y = miny - rh + 1; y <= maxy; y++) {
374 if (y < 0 || y+rh > h)
377 for (x = minx - rw + 1; x <= maxx; x++) {
378 if (x < 0 || x+rw > w)
382 * See if we can find a candidate number
383 * placement within this rectangle.
385 for (j = 0; j < numbers[i].npoints; j++)
386 if (numbers[i].points[j].x >= x &&
387 numbers[i].points[j].x < x+rw &&
388 numbers[i].points[j].y >= y &&
389 numbers[i].points[j].y < y+rh)
392 if (j < numbers[i].npoints) {
394 * Add this to the list of candidate
395 * placements for this rectangle.
397 if (rlistn >= rlistsize) {
398 rlistsize = rlistn + 32;
399 rlist = sresize(rlist, rlistsize, struct rect);
403 rlist[rlistn].w = rw;
404 rlist[rlistn].h = rh;
405 #ifdef SOLVER_DIAGNOSTICS
406 printf("rect %d [area %d]: candidate position at"
407 " %d,%d w=%d h=%d\n",
408 i, area, x, y, rw, rh);
416 rectpositions[i].rects = rlist;
417 rectpositions[i].n = rlistn;
421 * Next, construct a multidimensional array tracking how many
422 * candidate positions for each rectangle overlap each square.
424 * Indexing of this array is by the formula
426 * overlaps[(rectindex * h + y) * w + x]
428 overlaps = snewn(nrects * w * h, int);
429 memset(overlaps, 0, nrects * w * h * sizeof(int));
430 for (i = 0; i < nrects; i++) {
433 for (j = 0; j < rectpositions[i].n; j++) {
436 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++)
437 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++)
438 overlaps[(i * h + yy+rectpositions[i].rects[j].y) * w +
439 xx+rectpositions[i].rects[j].x]++;
444 * Also we want an array covering the grid once, to make it
445 * easy to figure out which squares are candidate number
446 * placements for which rectangles. (The existence of this
447 * single array assumes that no square starts off as a
448 * candidate number placement for more than one rectangle. This
449 * assumption is justified, because this solver is _either_
450 * used to solve real problems - in which case there is a
451 * single placement for every number - _or_ used to decide on
452 * number placements for a new puzzle, in which case each
453 * number's placements are confined to the intended position of
454 * the rectangle containing that number.)
456 rectbyplace = snewn(w * h, int);
457 for (i = 0; i < w*h; i++)
460 for (i = 0; i < nrects; i++) {
463 for (j = 0; j < numbers[i].npoints; j++) {
464 int x = numbers[i].points[j].x;
465 int y = numbers[i].points[j].y;
467 assert(rectbyplace[y * w + x] == -1);
468 rectbyplace[y * w + x] = i;
472 workspace = snewn(nrects, int);
475 * Now run the actual deduction loop.
478 int done_something = FALSE;
480 #ifdef SOLVER_DIAGNOSTICS
481 printf("starting deduction loop\n");
483 for (i = 0; i < nrects; i++) {
484 printf("rect %d overlaps:\n", i);
487 for (y = 0; y < h; y++) {
488 for (x = 0; x < w; x++) {
489 printf("%3d", overlaps[(i * h + y) * w + x]);
495 printf("rectbyplace:\n");
498 for (y = 0; y < h; y++) {
499 for (x = 0; x < w; x++) {
500 printf("%3d", rectbyplace[y * w + x]);
508 * Housekeeping. Look for rectangles whose number has only
509 * one candidate position left, and mark that square as
510 * known if it isn't already.
512 for (i = 0; i < nrects; i++) {
513 if (numbers[i].npoints == 1) {
514 int x = numbers[i].points[0].x;
515 int y = numbers[i].points[0].y;
516 if (overlaps[(i * h + y) * w + x] >= -1) {
519 assert(overlaps[(i * h + y) * w + x] > 0);
520 #ifdef SOLVER_DIAGNOSTICS
521 printf("marking %d,%d as known for rect %d"
522 " (sole remaining number position)\n", x, y, i);
525 for (j = 0; j < nrects; j++)
526 overlaps[(j * h + y) * w + x] = -1;
528 overlaps[(i * h + y) * w + x] = -2;
534 * Now look at the intersection of all possible placements
535 * for each rectangle, and mark all squares in that
536 * intersection as known for that rectangle if they aren't
539 for (i = 0; i < nrects; i++) {
540 int minx, miny, maxx, maxy, xx, yy, j;
546 for (j = 0; j < rectpositions[i].n; j++) {
547 int x = rectpositions[i].rects[j].x;
548 int y = rectpositions[i].rects[j].y;
549 int w = rectpositions[i].rects[j].w;
550 int h = rectpositions[i].rects[j].h;
552 if (minx < x) minx = x;
553 if (miny < y) miny = y;
554 if (maxx > x+w) maxx = x+w;
555 if (maxy > y+h) maxy = y+h;
558 for (yy = miny; yy < maxy; yy++)
559 for (xx = minx; xx < maxx; xx++)
560 if (overlaps[(i * h + yy) * w + xx] >= -1) {
561 assert(overlaps[(i * h + yy) * w + xx] > 0);
562 #ifdef SOLVER_DIAGNOSTICS
563 printf("marking %d,%d as known for rect %d"
564 " (intersection of all placements)\n",
568 for (j = 0; j < nrects; j++)
569 overlaps[(j * h + yy) * w + xx] = -1;
571 overlaps[(i * h + yy) * w + xx] = -2;
576 * Rectangle-focused deduction. Look at each rectangle in
577 * turn and try to rule out some of its candidate
580 for (i = 0; i < nrects; i++) {
583 for (j = 0; j < rectpositions[i].n; j++) {
587 for (k = 0; k < nrects; k++)
590 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
591 int y = yy + rectpositions[i].rects[j].y;
592 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
593 int x = xx + rectpositions[i].rects[j].x;
595 if (overlaps[(i * h + y) * w + x] == -1) {
597 * This placement overlaps a square
598 * which is _known_ to be part of
599 * another rectangle. Therefore we must
602 #ifdef SOLVER_DIAGNOSTICS
603 printf("rect %d placement at %d,%d w=%d h=%d "
604 "contains %d,%d which is known-other\n", i,
605 rectpositions[i].rects[j].x,
606 rectpositions[i].rects[j].y,
607 rectpositions[i].rects[j].w,
608 rectpositions[i].rects[j].h,
614 if (rectbyplace[y * w + x] != -1) {
616 * This placement overlaps one of the
617 * candidate number placements for some
618 * rectangle. Count it.
620 workspace[rectbyplace[y * w + x]]++;
627 * If we haven't ruled this placement out
628 * already, see if it overlaps _all_ of the
629 * candidate number placements for any
630 * rectangle. If so, we can rule it out.
632 for (k = 0; k < nrects; k++)
633 if (k != i && workspace[k] == numbers[k].npoints) {
634 #ifdef SOLVER_DIAGNOSTICS
635 printf("rect %d placement at %d,%d w=%d h=%d "
636 "contains all number points for rect %d\n",
638 rectpositions[i].rects[j].x,
639 rectpositions[i].rects[j].y,
640 rectpositions[i].rects[j].w,
641 rectpositions[i].rects[j].h,
649 * Failing that, see if it overlaps at least
650 * one of the candidate number placements for
651 * itself! (This might not be the case if one
652 * of those number placements has been removed
655 if (!del && workspace[i] == 0) {
656 #ifdef SOLVER_DIAGNOSTICS
657 printf("rect %d placement at %d,%d w=%d h=%d "
658 "contains none of its own number points\n",
660 rectpositions[i].rects[j].x,
661 rectpositions[i].rects[j].y,
662 rectpositions[i].rects[j].w,
663 rectpositions[i].rects[j].h);
670 remove_rect_placement(w, h, rectpositions, overlaps, i, j);
672 j--; /* don't skip over next placement */
674 done_something = TRUE;
680 * Square-focused deduction. Look at each square not marked
681 * as known, and see if there are any which can only be
682 * part of a single rectangle.
686 for (y = 0; y < h; y++) for (x = 0; x < w; x++) {
687 /* Known squares are marked as <0 everywhere, so we only need
688 * to check the overlaps entry for rect 0. */
689 if (overlaps[y * w + x] < 0)
690 continue; /* known already */
694 for (i = 0; i < nrects; i++)
695 if (overlaps[(i * h + y) * w + x] > 0)
702 * Now we can rule out all placements for
703 * rectangle `index' which _don't_ contain
706 #ifdef SOLVER_DIAGNOSTICS
707 printf("square %d,%d can only be in rectangle %d\n",
710 for (j = 0; j < rectpositions[index].n; j++) {
711 struct rect *r = &rectpositions[index].rects[j];
712 if (x >= r->x && x < r->x + r->w &&
713 y >= r->y && y < r->y + r->h)
714 continue; /* this one is OK */
715 remove_rect_placement(w, h, rectpositions, overlaps,
717 j--; /* don't skip over next placement */
718 done_something = TRUE;
725 * If we've managed to deduce anything by normal means,
726 * loop round again and see if there's more to be done.
727 * Only if normal deduction has completely failed us should
728 * we now move on to narrowing down the possible number
735 * Now we have done everything we can with the current set
736 * of number placements. So we need to winnow the number
737 * placements so as to narrow down the possibilities. We do
738 * this by searching for a candidate placement (of _any_
739 * rectangle) which overlaps a candidate placement of the
740 * number for some other rectangle.
748 int nrpns = 0, rpnsize = 0;
751 for (i = 0; i < nrects; i++) {
752 for (j = 0; j < rectpositions[i].n; j++) {
755 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
756 int y = yy + rectpositions[i].rects[j].y;
757 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
758 int x = xx + rectpositions[i].rects[j].x;
760 if (rectbyplace[y * w + x] >= 0 &&
761 rectbyplace[y * w + x] != i) {
763 * Add this to the list of
764 * winnowing possibilities.
766 if (nrpns >= rpnsize) {
767 rpnsize = rpnsize * 3 / 2 + 32;
768 rpns = sresize(rpns, rpnsize, struct rpn);
770 rpns[nrpns].rect = i;
771 rpns[nrpns].placement = j;
772 rpns[nrpns].number = rectbyplace[y * w + x];
781 #ifdef SOLVER_DIAGNOSTICS
782 printf("%d candidate rect placements we could eliminate\n", nrpns);
786 * Now choose one of these unwanted rectangle
787 * placements, and eliminate it.
789 int index = random_upto(rs, nrpns);
791 struct rpn rpn = rpns[index];
798 r = rectpositions[i].rects[j];
801 * We rule out placement j of rectangle i by means
802 * of removing all of rectangle k's candidate
803 * number placements which do _not_ overlap it.
804 * This will ensure that it is eliminated during
805 * the next pass of rectangle-focused deduction.
807 #ifdef SOLVER_DIAGNOSTICS
808 printf("ensuring number for rect %d is within"
809 " rect %d's placement at %d,%d w=%d h=%d\n",
810 k, i, r.x, r.y, r.w, r.h);
813 for (m = 0; m < numbers[k].npoints; m++) {
814 int x = numbers[k].points[m].x;
815 int y = numbers[k].points[m].y;
817 if (x < r.x || x >= r.x + r.w ||
818 y < r.y || y >= r.y + r.h) {
819 #ifdef SOLVER_DIAGNOSTICS
820 printf("eliminating number for rect %d at %d,%d\n",
823 remove_number_placement(w, h, &numbers[k],
825 m--; /* don't skip the next one */
826 done_something = TRUE;
832 if (!done_something) {
833 #ifdef SOLVER_DIAGNOSTICS
834 printf("terminating deduction loop\n");
841 for (i = 0; i < nrects; i++) {
842 #ifdef SOLVER_DIAGNOSTICS
843 printf("rect %d has %d possible placements\n",
844 i, rectpositions[i].n);
846 assert(rectpositions[i].n > 0);
847 if (rectpositions[i].n > 1) {
851 * Place the rectangle in its only possible position.
854 struct rect *r = &rectpositions[i].rects[0];
856 for (y = 0; y < r->h; y++) {
858 vedge(result, r->x, r->y+y) = 1;
859 if (r->x+r->w < result->w)
860 vedge(result, r->x+r->w, r->y+y) = 1;
862 for (x = 0; x < r->w; x++) {
864 hedge(result, r->x+x, r->y) = 1;
865 if (r->y+r->h < result->h)
866 hedge(result, r->x+x, r->y+r->h) = 1;
872 * Free up all allocated storage.
877 for (i = 0; i < nrects; i++)
878 sfree(rectpositions[i].rects);
879 sfree(rectpositions);
884 /* ----------------------------------------------------------------------
885 * Grid generation code.
888 static struct rectlist *get_rectlist(game_params *params, int *grid)
893 struct rect *rects = NULL;
894 int nrects = 0, rectsize = 0;
897 * Maximum rectangle area is 1/6 of total grid size, unless
898 * this means we can't place any rectangles at all in which
899 * case we set it to 2 at minimum.
901 maxarea = params->w * params->h / 6;
905 for (rw = 1; rw <= params->w; rw++)
906 for (rh = 1; rh <= params->h; rh++) {
907 if (rw * rh > maxarea)
911 for (x = 0; x <= params->w - rw; x++)
912 for (y = 0; y <= params->h - rh; y++) {
913 if (nrects >= rectsize) {
914 rectsize = nrects + 256;
915 rects = sresize(rects, rectsize, struct rect);
920 rects[nrects].w = rw;
921 rects[nrects].h = rh;
927 struct rectlist *ret;
928 ret = snew(struct rectlist);
933 assert(rects == NULL); /* hence no need to free */
938 static void free_rectlist(struct rectlist *list)
944 static void place_rect(game_params *params, int *grid, struct rect r)
946 int idx = INDEX(params, r.x, r.y);
949 for (x = r.x; x < r.x+r.w; x++)
950 for (y = r.y; y < r.y+r.h; y++) {
951 index(params, grid, x, y) = idx;
953 #ifdef GENERATION_DIAGNOSTICS
954 printf(" placing rectangle at (%d,%d) size %d x %d\n",
959 static struct rect find_rect(game_params *params, int *grid, int x, int y)
965 * Find the top left of the rectangle.
967 idx = index(params, grid, x, y);
973 return r; /* 1x1 singleton here */
980 * Find the width and height of the rectangle.
983 (x+w < params->w && index(params,grid,x+w,y)==idx);
986 (y+h < params->h && index(params,grid,x,y+h)==idx);
997 #ifdef GENERATION_DIAGNOSTICS
998 static void display_grid(game_params *params, int *grid, int *numbers, int all)
1000 unsigned char *egrid = snewn((params->w*2+3) * (params->h*2+3),
1003 int r = (params->w*2+3);
1005 memset(egrid, 0, (params->w*2+3) * (params->h*2+3));
1007 for (x = 0; x < params->w; x++)
1008 for (y = 0; y < params->h; y++) {
1009 int i = index(params, grid, x, y);
1010 if (x == 0 || index(params, grid, x-1, y) != i)
1011 egrid[(2*y+2) * r + (2*x+1)] = 1;
1012 if (x == params->w-1 || index(params, grid, x+1, y) != i)
1013 egrid[(2*y+2) * r + (2*x+3)] = 1;
1014 if (y == 0 || index(params, grid, x, y-1) != i)
1015 egrid[(2*y+1) * r + (2*x+2)] = 1;
1016 if (y == params->h-1 || index(params, grid, x, y+1) != i)
1017 egrid[(2*y+3) * r + (2*x+2)] = 1;
1020 for (y = 1; y < 2*params->h+2; y++) {
1021 for (x = 1; x < 2*params->w+2; x++) {
1023 int k = numbers ? index(params, numbers, x/2-1, y/2-1) : 0;
1024 if (k || (all && numbers)) printf("%2d", k); else printf(" ");
1025 } else if (!((y&x)&1)) {
1026 int v = egrid[y*r+x];
1027 if ((y&1) && v) v = '-';
1028 if ((x&1) && v) v = '|';
1031 if (!(x&1)) putchar(v);
1034 if (egrid[y*r+(x+1)]) d |= 1;
1035 if (egrid[(y-1)*r+x]) d |= 2;
1036 if (egrid[y*r+(x-1)]) d |= 4;
1037 if (egrid[(y+1)*r+x]) d |= 8;
1038 c = " ??+?-++?+|+++++"[d];
1040 if (!(x&1)) putchar(c);
1050 struct game_aux_info {
1052 unsigned char *vedge; /* (w+1) x h */
1053 unsigned char *hedge; /* w x (h+1) */
1056 static char *new_game_desc(game_params *params, random_state *rs,
1057 game_aux_info **aux, int interactive)
1059 int *grid, *numbers = NULL;
1060 struct rectlist *list;
1061 int x, y, y2, y2last, yx, run, i;
1063 game_params params2real, *params2 = ¶ms2real;
1067 * Set up the smaller width and height which we will use to
1068 * generate the base grid.
1070 params2->w = params->w / (1.0F + params->expandfactor);
1071 if (params2->w < 2 && params->w >= 2) params2->w = 2;
1072 params2->h = params->h / (1.0F + params->expandfactor);
1073 if (params2->h < 2 && params->h >= 2) params2->h = 2;
1075 grid = snewn(params2->w * params2->h, int);
1077 for (y = 0; y < params2->h; y++)
1078 for (x = 0; x < params2->w; x++) {
1079 index(params2, grid, x, y) = -1;
1082 list = get_rectlist(params2, grid);
1083 assert(list != NULL);
1086 * Place rectangles until we can't any more.
1088 while (list->n > 0) {
1093 * Pick a random rectangle.
1095 i = random_upto(rs, list->n);
1101 place_rect(params2, grid, r);
1104 * Winnow the list by removing any rectangles which
1108 for (i = 0; i < list->n; i++) {
1109 struct rect s = list->rects[i];
1110 if (s.x+s.w <= r.x || r.x+r.w <= s.x ||
1111 s.y+s.h <= r.y || r.y+r.h <= s.y)
1112 list->rects[m++] = s;
1117 free_rectlist(list);
1120 * Deal with singleton spaces remaining in the grid, one by
1123 * We do this by making a local change to the layout. There are
1124 * several possibilities:
1126 * +-----+-----+ Here, we can remove the singleton by
1127 * | | | extending the 1x2 rectangle below it
1128 * +--+--+-----+ into a 1x3.
1136 * +--+--+--+ Here, that trick doesn't work: there's no
1137 * | | | 1 x n rectangle with the singleton at one
1138 * | | | end. Instead, we extend a 1 x n rectangle
1139 * | | | _out_ from the singleton, shaving a layer
1140 * +--+--+ | off the end of another rectangle. So if we
1141 * | | | | extended up, we'd make our singleton part
1142 * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2
1143 * | | | used to be; or we could extend right into
1144 * +--+-----+ a 2x1, turning the 1x3 into a 1x2.
1146 * +-----+--+ Here, we can't even do _that_, since any
1147 * | | | direction we choose to extend the singleton
1148 * +--+--+ | will produce a new singleton as a result of
1149 * | | | | truncating one of the size-2 rectangles.
1150 * | +--+--+ Fortunately, this case can _only_ occur when
1151 * | | | a singleton is surrounded by four size-2s
1152 * +--+-----+ in this fashion; so instead we can simply
1153 * replace the whole section with a single 3x3.
1155 for (x = 0; x < params2->w; x++) {
1156 for (y = 0; y < params2->h; y++) {
1157 if (index(params2, grid, x, y) < 0) {
1160 #ifdef GENERATION_DIAGNOSTICS
1161 display_grid(params2, grid, NULL, FALSE);
1162 printf("singleton at %d,%d\n", x, y);
1166 * Check in which directions we can feasibly extend
1167 * the singleton. We can extend in a particular
1168 * direction iff either:
1170 * - the rectangle on that side of the singleton
1171 * is not 2x1, and we are at one end of the edge
1172 * of it we are touching
1174 * - it is 2x1 but we are on its short side.
1176 * FIXME: we could plausibly choose between these
1177 * based on the sizes of the rectangles they would
1181 if (x < params2->w-1) {
1182 struct rect r = find_rect(params2, grid, x+1, y);
1183 if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1)
1184 dirs[ndirs++] = 1; /* right */
1187 struct rect r = find_rect(params2, grid, x, y-1);
1188 if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1)
1189 dirs[ndirs++] = 2; /* up */
1192 struct rect r = find_rect(params2, grid, x-1, y);
1193 if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1)
1194 dirs[ndirs++] = 4; /* left */
1196 if (y < params2->h-1) {
1197 struct rect r = find_rect(params2, grid, x, y+1);
1198 if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1)
1199 dirs[ndirs++] = 8; /* down */
1206 which = random_upto(rs, ndirs);
1211 assert(x < params2->w+1);
1212 #ifdef GENERATION_DIAGNOSTICS
1213 printf("extending right\n");
1215 r1 = find_rect(params2, grid, x+1, y);
1226 #ifdef GENERATION_DIAGNOSTICS
1227 printf("extending up\n");
1229 r1 = find_rect(params2, grid, x, y-1);
1240 #ifdef GENERATION_DIAGNOSTICS
1241 printf("extending left\n");
1243 r1 = find_rect(params2, grid, x-1, y);
1253 assert(y < params2->h+1);
1254 #ifdef GENERATION_DIAGNOSTICS
1255 printf("extending down\n");
1257 r1 = find_rect(params2, grid, x, y+1);
1267 if (r1.h > 0 && r1.w > 0)
1268 place_rect(params2, grid, r1);
1269 place_rect(params2, grid, r2);
1273 * Sanity-check that there really is a 3x3
1274 * rectangle surrounding this singleton and it
1275 * contains absolutely everything we could
1280 assert(x > 0 && x < params2->w-1);
1281 assert(y > 0 && y < params2->h-1);
1283 for (xx = x-1; xx <= x+1; xx++)
1284 for (yy = y-1; yy <= y+1; yy++) {
1285 struct rect r = find_rect(params2,grid,xx,yy);
1288 assert(r.x+r.w-1 <= x+1);
1289 assert(r.y+r.h-1 <= y+1);
1294 #ifdef GENERATION_DIAGNOSTICS
1295 printf("need the 3x3 trick\n");
1299 * FIXME: If the maximum rectangle area for
1300 * this grid is less than 9, we ought to
1301 * subdivide the 3x3 in some fashion. There are
1302 * five other possibilities:
1305 * - a 4, a 3 and a 2
1307 * - a 3 and three 2s (two different arrangements).
1315 place_rect(params2, grid, r);
1323 * We have now constructed a grid of the size specified in
1324 * params2. Now we extend it into a grid of the size specified
1325 * in params. We do this in two passes: we extend it vertically
1326 * until it's the right height, then we transpose it, then
1327 * extend it vertically again (getting it effectively the right
1328 * width), then finally transpose again.
1330 for (i = 0; i < 2; i++) {
1331 int *grid2, *expand, *where;
1332 game_params params3real, *params3 = ¶ms3real;
1334 #ifdef GENERATION_DIAGNOSTICS
1335 printf("before expansion:\n");
1336 display_grid(params2, grid, NULL, TRUE);
1340 * Set up the new grid.
1342 grid2 = snewn(params2->w * params->h, int);
1343 expand = snewn(params2->h-1, int);
1344 where = snewn(params2->w, int);
1345 params3->w = params2->w;
1346 params3->h = params->h;
1349 * Decide which horizontal edges are going to get expanded,
1352 for (y = 0; y < params2->h-1; y++)
1354 for (y = params2->h; y < params->h; y++) {
1355 x = random_upto(rs, params2->h-1);
1359 #ifdef GENERATION_DIAGNOSTICS
1360 printf("expand[] = {");
1361 for (y = 0; y < params2->h-1; y++)
1362 printf(" %d", expand[y]);
1367 * Perform the expansion. The way this works is that we
1370 * - copy a row from grid into grid2
1372 * - invent some number of additional rows in grid2 where
1373 * there was previously only a horizontal line between
1374 * rows in grid, and make random decisions about where
1375 * among these to place each rectangle edge that ran
1378 for (y = y2 = y2last = 0; y < params2->h; y++) {
1380 * Copy a single line from row y of grid into row y2 of
1383 for (x = 0; x < params2->w; x++) {
1384 int val = index(params2, grid, x, y);
1385 if (val / params2->w == y && /* rect starts on this line */
1386 (y2 == 0 || /* we're at the very top, or... */
1387 index(params3, grid2, x, y2-1) / params3->w < y2last
1388 /* this rect isn't already started */))
1389 index(params3, grid2, x, y2) =
1390 INDEX(params3, val % params2->w, y2);
1392 index(params3, grid2, x, y2) =
1393 index(params3, grid2, x, y2-1);
1397 * If that was the last line, terminate the loop early.
1399 if (++y2 == params3->h)
1405 * Invent some number of additional lines. First walk
1406 * along this line working out where to put all the
1407 * edges that coincide with it.
1410 for (x = 0; x < params2->w; x++) {
1411 if (index(params2, grid, x, y) !=
1412 index(params2, grid, x, y+1)) {
1414 * This is a horizontal edge, so it needs
1418 (index(params2, grid, x-1, y) !=
1419 index(params2, grid, x, y) &&
1420 index(params2, grid, x-1, y+1) !=
1421 index(params2, grid, x, y+1))) {
1423 * Here we have the chance to make a new
1426 yx = random_upto(rs, expand[y]+1);
1429 * Here we just reuse the previous value of
1438 for (yx = 0; yx < expand[y]; yx++) {
1440 * Invent a single row. For each square in the row,
1441 * we copy the grid entry from the square above it,
1442 * unless we're starting the new rectangle here.
1444 for (x = 0; x < params2->w; x++) {
1445 if (yx == where[x]) {
1446 int val = index(params2, grid, x, y+1);
1448 val = INDEX(params3, val, y2);
1449 index(params3, grid2, x, y2) = val;
1451 index(params3, grid2, x, y2) =
1452 index(params3, grid2, x, y2-1);
1462 #ifdef GENERATION_DIAGNOSTICS
1463 printf("after expansion:\n");
1464 display_grid(params3, grid2, NULL, TRUE);
1469 params2->w = params3->h;
1470 params2->h = params3->w;
1472 grid = snewn(params2->w * params2->h, int);
1473 for (x = 0; x < params2->w; x++)
1474 for (y = 0; y < params2->h; y++) {
1475 int idx1 = INDEX(params2, x, y);
1476 int idx2 = INDEX(params3, y, x);
1480 tmp = (tmp % params3->w) * params2->w + (tmp / params3->w);
1489 params->w = params->h;
1493 #ifdef GENERATION_DIAGNOSTICS
1494 printf("after transposition:\n");
1495 display_grid(params2, grid, NULL, TRUE);
1500 * Run the solver to narrow down the possible number
1504 struct numberdata *nd;
1505 int nnumbers, i, ret;
1507 /* Count the rectangles. */
1509 for (y = 0; y < params->h; y++) {
1510 for (x = 0; x < params->w; x++) {
1511 int idx = INDEX(params, x, y);
1512 if (index(params, grid, x, y) == idx)
1517 nd = snewn(nnumbers, struct numberdata);
1519 /* Now set up each number's candidate position list. */
1521 for (y = 0; y < params->h; y++) {
1522 for (x = 0; x < params->w; x++) {
1523 int idx = INDEX(params, x, y);
1524 if (index(params, grid, x, y) == idx) {
1525 struct rect r = find_rect(params, grid, x, y);
1528 nd[i].area = r.w * r.h;
1529 nd[i].npoints = nd[i].area;
1530 nd[i].points = snewn(nd[i].npoints, struct point);
1532 for (j = 0; j < r.h; j++)
1533 for (k = 0; k < r.w; k++) {
1534 nd[i].points[m].x = k + r.x;
1535 nd[i].points[m].y = j + r.y;
1538 assert(m == nd[i].npoints);
1546 ret = rect_solver(params->w, params->h, nnumbers, nd,
1549 ret = TRUE; /* allow any number placement at all */
1553 * Now place the numbers according to the solver's
1556 numbers = snewn(params->w * params->h, int);
1558 for (y = 0; y < params->h; y++)
1559 for (x = 0; x < params->w; x++) {
1560 index(params, numbers, x, y) = 0;
1563 for (i = 0; i < nnumbers; i++) {
1564 int idx = random_upto(rs, nd[i].npoints);
1565 int x = nd[i].points[idx].x;
1566 int y = nd[i].points[idx].y;
1567 index(params,numbers,x,y) = nd[i].area;
1574 for (i = 0; i < nnumbers; i++)
1575 sfree(nd[i].points);
1579 * If we've succeeded, then terminate the loop.
1586 * Give up and go round again.
1592 * Store the rectangle data in the game_aux_info.
1595 game_aux_info *ai = snew(game_aux_info);
1599 ai->vedge = snewn(ai->w * ai->h, unsigned char);
1600 ai->hedge = snewn(ai->w * ai->h, unsigned char);
1602 for (y = 0; y < params->h; y++)
1603 for (x = 1; x < params->w; x++) {
1605 index(params, grid, x, y) != index(params, grid, x-1, y);
1607 for (y = 1; y < params->h; y++)
1608 for (x = 0; x < params->w; x++) {
1610 index(params, grid, x, y) != index(params, grid, x, y-1);
1616 #ifdef GENERATION_DIAGNOSTICS
1617 display_grid(params, grid, numbers, FALSE);
1620 desc = snewn(11 * params->w * params->h, char);
1623 for (i = 0; i <= params->w * params->h; i++) {
1624 int n = (i < params->w * params->h ? numbers[i] : -1);
1631 int c = 'a' - 1 + run;
1635 run -= c - ('a' - 1);
1639 * If there's a number in the very top left or
1640 * bottom right, there's no point putting an
1641 * unnecessary _ before or after it.
1643 if (p > desc && n > 0)
1647 p += sprintf(p, "%d", n);
1659 static void game_free_aux_info(game_aux_info *ai)
1666 static char *validate_desc(game_params *params, char *desc)
1668 int area = params->w * params->h;
1673 if (n >= 'a' && n <= 'z') {
1674 squares += n - 'a' + 1;
1675 } else if (n == '_') {
1677 } else if (n > '0' && n <= '9') {
1679 while (*desc >= '0' && *desc <= '9')
1682 return "Invalid character in game description";
1686 return "Not enough data to fill grid";
1689 return "Too much data to fit in grid";
1694 static game_state *new_game(midend_data *me, game_params *params, char *desc)
1696 game_state *state = snew(game_state);
1699 state->w = params->w;
1700 state->h = params->h;
1702 area = state->w * state->h;
1704 state->grid = snewn(area, int);
1705 state->vedge = snewn(area, unsigned char);
1706 state->hedge = snewn(area, unsigned char);
1707 state->completed = state->cheated = FALSE;
1712 if (n >= 'a' && n <= 'z') {
1713 int run = n - 'a' + 1;
1714 assert(i + run <= area);
1716 state->grid[i++] = 0;
1717 } else if (n == '_') {
1719 } else if (n > '0' && n <= '9') {
1721 state->grid[i++] = atoi(desc-1);
1722 while (*desc >= '0' && *desc <= '9')
1725 assert(!"We can't get here");
1730 for (y = 0; y < state->h; y++)
1731 for (x = 0; x < state->w; x++)
1732 vedge(state,x,y) = hedge(state,x,y) = 0;
1737 static game_state *dup_game(game_state *state)
1739 game_state *ret = snew(game_state);
1744 ret->vedge = snewn(state->w * state->h, unsigned char);
1745 ret->hedge = snewn(state->w * state->h, unsigned char);
1746 ret->grid = snewn(state->w * state->h, int);
1748 ret->completed = state->completed;
1749 ret->cheated = state->cheated;
1751 memcpy(ret->grid, state->grid, state->w * state->h * sizeof(int));
1752 memcpy(ret->vedge, state->vedge, state->w*state->h*sizeof(unsigned char));
1753 memcpy(ret->hedge, state->hedge, state->w*state->h*sizeof(unsigned char));
1758 static void free_game(game_state *state)
1761 sfree(state->vedge);
1762 sfree(state->hedge);
1766 static game_state *solve_game(game_state *state, game_aux_info *ai,
1773 struct numberdata *nd;
1776 * Attempt the in-built solver.
1779 /* Set up each number's (very short) candidate position list. */
1780 for (i = n = 0; i < state->h * state->w; i++)
1784 nd = snewn(n, struct numberdata);
1786 for (i = j = 0; i < state->h * state->w; i++)
1787 if (state->grid[i]) {
1788 nd[j].area = state->grid[i];
1790 nd[j].points = snewn(1, struct point);
1791 nd[j].points[0].x = i % state->w;
1792 nd[j].points[0].y = i / state->w;
1798 ret = dup_game(state);
1799 ret->cheated = TRUE;
1801 rect_solver(state->w, state->h, n, nd, ret, NULL);
1806 for (i = 0; i < n; i++)
1807 sfree(nd[i].points);
1813 assert(state->w == ai->w);
1814 assert(state->h == ai->h);
1816 ret = dup_game(state);
1817 memcpy(ret->vedge, ai->vedge, ai->w * ai->h * sizeof(unsigned char));
1818 memcpy(ret->hedge, ai->hedge, ai->w * ai->h * sizeof(unsigned char));
1819 ret->cheated = TRUE;
1824 static char *game_text_format(game_state *state)
1826 char *ret, *p, buf[80];
1827 int i, x, y, col, maxlen;
1830 * First determine the number of spaces required to display a
1831 * number. We'll use at least two, because one looks a bit
1835 for (i = 0; i < state->w * state->h; i++) {
1836 x = sprintf(buf, "%d", state->grid[i]);
1837 if (col < x) col = x;
1841 * Now we know the exact total size of the grid we're going to
1842 * produce: it's got 2*h+1 rows, each containing w lots of col,
1843 * w+1 boundary characters and a trailing newline.
1845 maxlen = (2*state->h+1) * (state->w * (col+1) + 2);
1847 ret = snewn(maxlen+1, char);
1850 for (y = 0; y <= 2*state->h; y++) {
1851 for (x = 0; x <= 2*state->w; x++) {
1856 int v = grid(state, x/2, y/2);
1858 sprintf(buf, "%*d", col, v);
1860 sprintf(buf, "%*s", col, "");
1861 memcpy(p, buf, col);
1865 * Display a horizontal edge or nothing.
1867 int h = (y==0 || y==2*state->h ? 1 :
1868 HRANGE(state, x/2, y/2) && hedge(state, x/2, y/2));
1874 for (i = 0; i < col; i++)
1878 * Display a vertical edge or nothing.
1880 int v = (x==0 || x==2*state->w ? 1 :
1881 VRANGE(state, x/2, y/2) && vedge(state, x/2, y/2));
1888 * Display a corner, or a vertical edge, or a
1889 * horizontal edge, or nothing.
1891 int hl = (y==0 || y==2*state->h ? 1 :
1892 HRANGE(state, (x-1)/2, y/2) && hedge(state, (x-1)/2, y/2));
1893 int hr = (y==0 || y==2*state->h ? 1 :
1894 HRANGE(state, (x+1)/2, y/2) && hedge(state, (x+1)/2, y/2));
1895 int vu = (x==0 || x==2*state->w ? 1 :
1896 VRANGE(state, x/2, (y-1)/2) && vedge(state, x/2, (y-1)/2));
1897 int vd = (x==0 || x==2*state->w ? 1 :
1898 VRANGE(state, x/2, (y+1)/2) && vedge(state, x/2, (y+1)/2));
1899 if (!hl && !hr && !vu && !vd)
1901 else if (hl && hr && !vu && !vd)
1903 else if (!hl && !hr && vu && vd)
1912 assert(p - ret == maxlen);
1917 static unsigned char *get_correct(game_state *state)
1922 ret = snewn(state->w * state->h, unsigned char);
1923 memset(ret, 0xFF, state->w * state->h);
1925 for (x = 0; x < state->w; x++)
1926 for (y = 0; y < state->h; y++)
1927 if (index(state,ret,x,y) == 0xFF) {
1930 int num, area, valid;
1933 * Find a rectangle starting at this point.
1936 while (x+rw < state->w && !vedge(state,x+rw,y))
1939 while (y+rh < state->h && !hedge(state,x,y+rh))
1943 * We know what the dimensions of the rectangle
1944 * should be if it's there at all. Find out if we
1945 * really have a valid rectangle.
1948 /* Check the horizontal edges. */
1949 for (xx = x; xx < x+rw; xx++) {
1950 for (yy = y; yy <= y+rh; yy++) {
1951 int e = !HRANGE(state,xx,yy) || hedge(state,xx,yy);
1952 int ec = (yy == y || yy == y+rh);
1957 /* Check the vertical edges. */
1958 for (yy = y; yy < y+rh; yy++) {
1959 for (xx = x; xx <= x+rw; xx++) {
1960 int e = !VRANGE(state,xx,yy) || vedge(state,xx,yy);
1961 int ec = (xx == x || xx == x+rw);
1968 * If this is not a valid rectangle with no other
1969 * edges inside it, we just mark this square as not
1970 * complete and proceed to the next square.
1973 index(state, ret, x, y) = 0;
1978 * We have a rectangle. Now see what its area is,
1979 * and how many numbers are in it.
1983 for (xx = x; xx < x+rw; xx++) {
1984 for (yy = y; yy < y+rh; yy++) {
1986 if (grid(state,xx,yy)) {
1988 valid = FALSE; /* two numbers */
1989 num = grid(state,xx,yy);
1997 * Now fill in the whole rectangle based on the
2000 for (xx = x; xx < x+rw; xx++) {
2001 for (yy = y; yy < y+rh; yy++) {
2002 index(state, ret, xx, yy) = valid;
2012 * These coordinates are 2 times the obvious grid coordinates.
2013 * Hence, the top left of the grid is (0,0), the grid point to
2014 * the right of that is (2,0), the one _below that_ is (2,2)
2015 * and so on. This is so that we can specify a drag start point
2016 * on an edge (one odd coordinate) or in the middle of a square
2017 * (two odd coordinates) rather than always at a corner.
2019 * -1,-1 means no drag is in progress.
2026 * This flag is set as soon as a dragging action moves the
2027 * mouse pointer away from its starting point, so that even if
2028 * the pointer _returns_ to its starting point the action is
2029 * treated as a small drag rather than a click.
2034 static game_ui *new_ui(game_state *state)
2036 game_ui *ui = snew(game_ui);
2037 ui->drag_start_x = -1;
2038 ui->drag_start_y = -1;
2039 ui->drag_end_x = -1;
2040 ui->drag_end_y = -1;
2041 ui->dragged = FALSE;
2045 static void free_ui(game_ui *ui)
2050 static void coord_round(float x, float y, int *xr, int *yr)
2052 float xs, ys, xv, yv, dx, dy, dist;
2055 * Find the nearest square-centre.
2057 xs = (float)floor(x) + 0.5F;
2058 ys = (float)floor(y) + 0.5F;
2061 * And find the nearest grid vertex.
2063 xv = (float)floor(x + 0.5F);
2064 yv = (float)floor(y + 0.5F);
2067 * We allocate clicks in parts of the grid square to either
2068 * corners, edges or square centres, as follows:
2084 * In other words: we measure the square distance (i.e.
2085 * max(dx,dy)) from the click to the nearest corner, and if
2086 * it's within CORNER_TOLERANCE then we return a corner click.
2087 * We measure the square distance from the click to the nearest
2088 * centre, and if that's within CENTRE_TOLERANCE we return a
2089 * centre click. Failing that, we find which of the two edge
2090 * centres is nearer to the click and return that edge.
2094 * Check for corner click.
2096 dx = (float)fabs(x - xv);
2097 dy = (float)fabs(y - yv);
2098 dist = (dx > dy ? dx : dy);
2099 if (dist < CORNER_TOLERANCE) {
2104 * Check for centre click.
2106 dx = (float)fabs(x - xs);
2107 dy = (float)fabs(y - ys);
2108 dist = (dx > dy ? dx : dy);
2109 if (dist < CENTRE_TOLERANCE) {
2110 *xr = 1 + 2 * (int)xs;
2111 *yr = 1 + 2 * (int)ys;
2114 * Failing both of those, see which edge we're closer to.
2115 * Conveniently, this is simply done by testing the relative
2116 * magnitude of dx and dy (which are currently distances from
2117 * the square centre).
2120 /* Vertical edge: x-coord of corner,
2121 * y-coord of square centre. */
2123 *yr = 1 + 2 * (int)ys;
2125 /* Horizontal edge: x-coord of square centre,
2126 * y-coord of corner. */
2127 *xr = 1 + 2 * (int)xs;
2134 static void ui_draw_rect(game_state *state, game_ui *ui,
2135 unsigned char *hedge, unsigned char *vedge, int c)
2137 int x1, x2, y1, y2, x, y, t;
2139 x1 = ui->drag_start_x;
2140 x2 = ui->drag_end_x;
2141 if (x2 < x1) { t = x1; x1 = x2; x2 = t; }
2143 y1 = ui->drag_start_y;
2144 y2 = ui->drag_end_y;
2145 if (y2 < y1) { t = y1; y1 = y2; y2 = t; }
2147 x1 = x1 / 2; /* rounds down */
2148 x2 = (x2+1) / 2; /* rounds up */
2149 y1 = y1 / 2; /* rounds down */
2150 y2 = (y2+1) / 2; /* rounds up */
2153 * Draw horizontal edges of rectangles.
2155 for (x = x1; x < x2; x++)
2156 for (y = y1; y <= y2; y++)
2157 if (HRANGE(state,x,y)) {
2158 int val = index(state,hedge,x,y);
2159 if (y == y1 || y == y2)
2163 index(state,hedge,x,y) = val;
2167 * Draw vertical edges of rectangles.
2169 for (y = y1; y < y2; y++)
2170 for (x = x1; x <= x2; x++)
2171 if (VRANGE(state,x,y)) {
2172 int val = index(state,vedge,x,y);
2173 if (x == x1 || x == x2)
2177 index(state,vedge,x,y) = val;
2181 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
2182 int x, int y, int button) {
2184 int startdrag = FALSE, enddrag = FALSE, active = FALSE;
2187 button &= ~MOD_MASK;
2189 if (button == LEFT_BUTTON) {
2191 } else if (button == LEFT_RELEASE) {
2193 } else if (button != LEFT_DRAG) {
2197 coord_round(FROMCOORD((float)x), FROMCOORD((float)y), &xc, &yc);
2200 ui->drag_start_x = xc;
2201 ui->drag_start_y = yc;
2202 ui->drag_end_x = xc;
2203 ui->drag_end_y = yc;
2204 ui->dragged = FALSE;
2208 if (xc != ui->drag_end_x || yc != ui->drag_end_y) {
2209 ui->drag_end_x = xc;
2210 ui->drag_end_y = yc;
2218 if (xc >= 0 && xc <= 2*from->w &&
2219 yc >= 0 && yc <= 2*from->h) {
2220 ret = dup_game(from);
2223 ui_draw_rect(ret, ui, ret->hedge, ret->vedge, 1);
2225 if ((xc & 1) && !(yc & 1) && HRANGE(from,xc/2,yc/2)) {
2226 hedge(ret,xc/2,yc/2) = !hedge(ret,xc/2,yc/2);
2228 if ((yc & 1) && !(xc & 1) && VRANGE(from,xc/2,yc/2)) {
2229 vedge(ret,xc/2,yc/2) = !vedge(ret,xc/2,yc/2);
2233 if (!memcmp(ret->hedge, from->hedge, from->w*from->h) &&
2234 !memcmp(ret->vedge, from->vedge, from->w*from->h)) {
2240 * We've made a real change to the grid. Check to see
2241 * if the game has been completed.
2243 if (ret && !ret->completed) {
2245 unsigned char *correct = get_correct(ret);
2248 for (x = 0; x < ret->w; x++)
2249 for (y = 0; y < ret->h; y++)
2250 if (!index(ret, correct, x, y))
2256 ret->completed = TRUE;
2260 ui->drag_start_x = -1;
2261 ui->drag_start_y = -1;
2262 ui->drag_end_x = -1;
2263 ui->drag_end_y = -1;
2264 ui->dragged = FALSE;
2269 return ret; /* a move has been made */
2271 return from; /* UI activity has occurred */
2276 /* ----------------------------------------------------------------------
2280 #define CORRECT 65536
2282 #define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG )
2283 #define MAX(x,y) ( (x)>(y) ? (x) : (y) )
2284 #define MAX4(x,y,z,w) ( MAX(MAX(x,y),MAX(z,w)) )
2286 struct game_drawstate {
2289 unsigned int *visible;
2292 static void game_size(game_params *params, int *x, int *y)
2294 *x = params->w * TILE_SIZE + 2*BORDER + 1;
2295 *y = params->h * TILE_SIZE + 2*BORDER + 1;
2298 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
2300 float *ret = snewn(3 * NCOLOURS, float);
2302 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2304 ret[COL_GRID * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
2305 ret[COL_GRID * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
2306 ret[COL_GRID * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
2308 ret[COL_DRAG * 3 + 0] = 1.0F;
2309 ret[COL_DRAG * 3 + 1] = 0.0F;
2310 ret[COL_DRAG * 3 + 2] = 0.0F;
2312 ret[COL_CORRECT * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
2313 ret[COL_CORRECT * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
2314 ret[COL_CORRECT * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
2316 ret[COL_LINE * 3 + 0] = 0.0F;
2317 ret[COL_LINE * 3 + 1] = 0.0F;
2318 ret[COL_LINE * 3 + 2] = 0.0F;
2320 ret[COL_TEXT * 3 + 0] = 0.0F;
2321 ret[COL_TEXT * 3 + 1] = 0.0F;
2322 ret[COL_TEXT * 3 + 2] = 0.0F;
2324 *ncolours = NCOLOURS;
2328 static game_drawstate *game_new_drawstate(game_state *state)
2330 struct game_drawstate *ds = snew(struct game_drawstate);
2333 ds->started = FALSE;
2336 ds->visible = snewn(ds->w * ds->h, unsigned int);
2337 for (i = 0; i < ds->w * ds->h; i++)
2338 ds->visible[i] = 0xFFFF;
2343 static void game_free_drawstate(game_drawstate *ds)
2349 static void draw_tile(frontend *fe, game_state *state, int x, int y,
2350 unsigned char *hedge, unsigned char *vedge,
2351 unsigned char *corners, int correct)
2353 int cx = COORD(x), cy = COORD(y);
2356 draw_rect(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1, COL_GRID);
2357 draw_rect(fe, cx+1, cy+1, TILE_SIZE-1, TILE_SIZE-1,
2358 correct ? COL_CORRECT : COL_BACKGROUND);
2360 if (grid(state,x,y)) {
2361 sprintf(str, "%d", grid(state,x,y));
2362 draw_text(fe, cx+TILE_SIZE/2, cy+TILE_SIZE/2, FONT_VARIABLE,
2363 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, COL_TEXT, str);
2369 if (!HRANGE(state,x,y) || index(state,hedge,x,y))
2370 draw_rect(fe, cx, cy, TILE_SIZE+1, 2,
2371 HRANGE(state,x,y) ? COLOUR(index(state,hedge,x,y)) :
2373 if (!HRANGE(state,x,y+1) || index(state,hedge,x,y+1))
2374 draw_rect(fe, cx, cy+TILE_SIZE-1, TILE_SIZE+1, 2,
2375 HRANGE(state,x,y+1) ? COLOUR(index(state,hedge,x,y+1)) :
2377 if (!VRANGE(state,x,y) || index(state,vedge,x,y))
2378 draw_rect(fe, cx, cy, 2, TILE_SIZE+1,
2379 VRANGE(state,x,y) ? COLOUR(index(state,vedge,x,y)) :
2381 if (!VRANGE(state,x+1,y) || index(state,vedge,x+1,y))
2382 draw_rect(fe, cx+TILE_SIZE-1, cy, 2, TILE_SIZE+1,
2383 VRANGE(state,x+1,y) ? COLOUR(index(state,vedge,x+1,y)) :
2389 if (index(state,corners,x,y))
2390 draw_rect(fe, cx, cy, 2, 2,
2391 COLOUR(index(state,corners,x,y)));
2392 if (x+1 < state->w && index(state,corners,x+1,y))
2393 draw_rect(fe, cx+TILE_SIZE-1, cy, 2, 2,
2394 COLOUR(index(state,corners,x+1,y)));
2395 if (y+1 < state->h && index(state,corners,x,y+1))
2396 draw_rect(fe, cx, cy+TILE_SIZE-1, 2, 2,
2397 COLOUR(index(state,corners,x,y+1)));
2398 if (x+1 < state->w && y+1 < state->h && index(state,corners,x+1,y+1))
2399 draw_rect(fe, cx+TILE_SIZE-1, cy+TILE_SIZE-1, 2, 2,
2400 COLOUR(index(state,corners,x+1,y+1)));
2402 draw_update(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1);
2405 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
2406 game_state *state, int dir, game_ui *ui,
2407 float animtime, float flashtime)
2410 unsigned char *correct;
2411 unsigned char *hedge, *vedge, *corners;
2413 correct = get_correct(state);
2416 hedge = snewn(state->w*state->h, unsigned char);
2417 vedge = snewn(state->w*state->h, unsigned char);
2418 memcpy(hedge, state->hedge, state->w*state->h);
2419 memcpy(vedge, state->vedge, state->w*state->h);
2420 ui_draw_rect(state, ui, hedge, vedge, 2);
2422 hedge = state->hedge;
2423 vedge = state->vedge;
2426 corners = snewn(state->w * state->h, unsigned char);
2427 memset(corners, 0, state->w * state->h);
2428 for (x = 0; x < state->w; x++)
2429 for (y = 0; y < state->h; y++) {
2431 int e = index(state, vedge, x, y);
2432 if (index(state,corners,x,y) < e)
2433 index(state,corners,x,y) = e;
2434 if (y+1 < state->h &&
2435 index(state,corners,x,y+1) < e)
2436 index(state,corners,x,y+1) = e;
2439 int e = index(state, hedge, x, y);
2440 if (index(state,corners,x,y) < e)
2441 index(state,corners,x,y) = e;
2442 if (x+1 < state->w &&
2443 index(state,corners,x+1,y) < e)
2444 index(state,corners,x+1,y) = e;
2450 state->w * TILE_SIZE + 2*BORDER + 1,
2451 state->h * TILE_SIZE + 2*BORDER + 1, COL_BACKGROUND);
2452 draw_rect(fe, COORD(0)-1, COORD(0)-1,
2453 ds->w*TILE_SIZE+3, ds->h*TILE_SIZE+3, COL_LINE);
2455 draw_update(fe, 0, 0,
2456 state->w * TILE_SIZE + 2*BORDER + 1,
2457 state->h * TILE_SIZE + 2*BORDER + 1);
2460 for (x = 0; x < state->w; x++)
2461 for (y = 0; y < state->h; y++) {
2464 if (HRANGE(state,x,y))
2465 c |= index(state,hedge,x,y);
2466 if (HRANGE(state,x,y+1))
2467 c |= index(state,hedge,x,y+1) << 2;
2468 if (VRANGE(state,x,y))
2469 c |= index(state,vedge,x,y) << 4;
2470 if (VRANGE(state,x+1,y))
2471 c |= index(state,vedge,x+1,y) << 6;
2472 c |= index(state,corners,x,y) << 8;
2474 c |= index(state,corners,x+1,y) << 10;
2476 c |= index(state,corners,x,y+1) << 12;
2477 if (x+1 < state->w && y+1 < state->h)
2478 c |= index(state,corners,x+1,y+1) << 14;
2479 if (index(state, correct, x, y) && !flashtime)
2482 if (index(ds,ds->visible,x,y) != c) {
2483 draw_tile(fe, state, x, y, hedge, vedge, corners, c & CORRECT);
2484 index(ds,ds->visible,x,y) = c;
2488 if (hedge != state->hedge) {
2497 static float game_anim_length(game_state *oldstate,
2498 game_state *newstate, int dir, game_ui *ui)
2503 static float game_flash_length(game_state *oldstate,
2504 game_state *newstate, int dir, game_ui *ui)
2506 if (!oldstate->completed && newstate->completed &&
2507 !oldstate->cheated && !newstate->cheated)
2512 static int game_wants_statusbar(void)
2517 static int game_timing_state(game_state *state)
2523 #define thegame rect
2526 const struct game thegame = {
2527 "Rectangles", "games.rectangles",
2534 TRUE, game_configure, custom_params,
2543 TRUE, game_text_format,
2550 game_free_drawstate,
2554 game_wants_statusbar,
2555 FALSE, game_timing_state,