Rogue diagnostic!
[sgt/puzzles] / rect.c
CommitLineData
3870c4d8 1/*
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.
7 */
8
9/*
10 * TODO:
11 *
738d2f61 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
17 * grid.
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
21 * split it up.
3870c4d8 22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <assert.h>
b0e26073 28#include <ctype.h>
3870c4d8 29#include <math.h>
30
31#include "puzzles.h"
32
3870c4d8 33enum {
34 COL_BACKGROUND,
35 COL_CORRECT,
36 COL_LINE,
37 COL_TEXT,
38 COL_GRID,
08dd70c3 39 COL_DRAG,
3870c4d8 40 NCOLOURS
41};
42
43struct game_params {
44 int w, h;
aea3ed9a 45 float expandfactor;
40fde884 46 int unique;
3870c4d8 47};
48
49#define INDEX(state, x, y) (((y) * (state)->w) + (x))
50#define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
51#define grid(state,x,y) index(state, (state)->grid, x, y)
52#define vedge(state,x,y) index(state, (state)->vedge, x, y)
53#define hedge(state,x,y) index(state, (state)->hedge, x, y)
54
55#define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
56 (y) >= dy && (y) < (state)->h )
57#define RANGE(state,x,y) CRANGE(state,x,y,0,0)
58#define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
59#define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
60
1e3e152d 61#define PREFERRED_TILE_SIZE 24
62#define TILE_SIZE (ds->tilesize)
63#define BORDER (TILE_SIZE * 3 / 4)
3870c4d8 64
d4e7900f 65#define CORNER_TOLERANCE 0.15F
66#define CENTRE_TOLERANCE 0.15F
67
ef29354c 68#define FLASH_TIME 0.13F
69
3870c4d8 70#define COORD(x) ( (x) * TILE_SIZE + BORDER )
71#define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
72
73struct game_state {
74 int w, h;
75 int *grid; /* contains the numbers */
76 unsigned char *vedge; /* (w+1) x h */
77 unsigned char *hedge; /* w x (h+1) */
2ac6d24e 78 int completed, cheated;
3870c4d8 79};
80
be8d5aa1 81static game_params *default_params(void)
3870c4d8 82{
83 game_params *ret = snew(game_params);
84
85 ret->w = ret->h = 7;
aea3ed9a 86 ret->expandfactor = 0.0F;
40fde884 87 ret->unique = TRUE;
3870c4d8 88
89 return ret;
90}
91
be8d5aa1 92static int game_fetch_preset(int i, char **name, game_params **params)
3870c4d8 93{
94 game_params *ret;
95 int w, h;
96 char buf[80];
97
98 switch (i) {
99 case 0: w = 7, h = 7; break;
ab53eb64 100 case 1: w = 9, h = 9; break;
101 case 2: w = 11, h = 11; break;
102 case 3: w = 13, h = 13; break;
103 case 4: w = 15, h = 15; break;
ab53eb64 104 case 5: w = 17, h = 17; break;
105 case 6: w = 19, h = 19; break;
3870c4d8 106 default: return FALSE;
107 }
108
109 sprintf(buf, "%dx%d", w, h);
110 *name = dupstr(buf);
111 *params = ret = snew(game_params);
112 ret->w = w;
113 ret->h = h;
aea3ed9a 114 ret->expandfactor = 0.0F;
40fde884 115 ret->unique = TRUE;
3870c4d8 116 return TRUE;
117}
118
be8d5aa1 119static void free_params(game_params *params)
3870c4d8 120{
121 sfree(params);
122}
123
be8d5aa1 124static game_params *dup_params(game_params *params)
3870c4d8 125{
126 game_params *ret = snew(game_params);
127 *ret = *params; /* structure copy */
128 return ret;
129}
130
1185e3c5 131static void decode_params(game_params *ret, char const *string)
b0e26073 132{
b0e26073 133 ret->w = ret->h = atoi(string);
aea3ed9a 134 while (*string && isdigit((unsigned char)*string)) string++;
b0e26073 135 if (*string == 'x') {
136 string++;
137 ret->h = atoi(string);
aea3ed9a 138 while (*string && isdigit((unsigned char)*string)) string++;
139 }
140 if (*string == 'e') {
141 string++;
142 ret->expandfactor = atof(string);
40fde884 143 while (*string &&
144 (*string == '.' || isdigit((unsigned char)*string))) string++;
145 }
146 if (*string == 'a') {
147 string++;
148 ret->unique = FALSE;
b0e26073 149 }
b0e26073 150}
151
1185e3c5 152static char *encode_params(game_params *params, int full)
b0e26073 153{
154 char data[256];
155
156 sprintf(data, "%dx%d", params->w, params->h);
5472ceb6 157 if (full && params->expandfactor)
1185e3c5 158 sprintf(data + strlen(data), "e%g", params->expandfactor);
40fde884 159 if (full && !params->unique)
160 strcat(data, "a");
b0e26073 161
162 return dupstr(data);
163}
164
be8d5aa1 165static config_item *game_configure(game_params *params)
3870c4d8 166{
167 config_item *ret;
168 char buf[80];
169
170 ret = snewn(5, config_item);
171
172 ret[0].name = "Width";
173 ret[0].type = C_STRING;
174 sprintf(buf, "%d", params->w);
175 ret[0].sval = dupstr(buf);
176 ret[0].ival = 0;
177
178 ret[1].name = "Height";
179 ret[1].type = C_STRING;
180 sprintf(buf, "%d", params->h);
181 ret[1].sval = dupstr(buf);
182 ret[1].ival = 0;
183
aea3ed9a 184 ret[2].name = "Expansion factor";
185 ret[2].type = C_STRING;
186 sprintf(buf, "%g", params->expandfactor);
187 ret[2].sval = dupstr(buf);
3870c4d8 188 ret[2].ival = 0;
189
40fde884 190 ret[3].name = "Ensure unique solution";
191 ret[3].type = C_BOOLEAN;
aea3ed9a 192 ret[3].sval = NULL;
40fde884 193 ret[3].ival = params->unique;
194
195 ret[4].name = NULL;
196 ret[4].type = C_END;
197 ret[4].sval = NULL;
198 ret[4].ival = 0;
aea3ed9a 199
3870c4d8 200 return ret;
201}
202
be8d5aa1 203static game_params *custom_params(config_item *cfg)
3870c4d8 204{
205 game_params *ret = snew(game_params);
206
207 ret->w = atoi(cfg[0].sval);
208 ret->h = atoi(cfg[1].sval);
aea3ed9a 209 ret->expandfactor = atof(cfg[2].sval);
40fde884 210 ret->unique = cfg[3].ival;
3870c4d8 211
212 return ret;
213}
214
be8d5aa1 215static char *validate_params(game_params *params)
3870c4d8 216{
ab53eb64 217 if (params->w <= 0 || params->h <= 0)
3870c4d8 218 return "Width and height must both be greater than zero";
ab53eb64 219 if (params->w*params->h < 2)
d4e7900f 220 return "Grid area must be greater than one";
aea3ed9a 221 if (params->expandfactor < 0.0F)
222 return "Expansion factor may not be negative";
3870c4d8 223 return NULL;
224}
225
26801d29 226struct point {
227 int x, y;
228};
229
3870c4d8 230struct rect {
231 int x, y;
232 int w, h;
233};
234
235struct rectlist {
236 struct rect *rects;
237 int n;
238};
239
26801d29 240struct numberdata {
241 int area;
242 int npoints;
243 struct point *points;
244};
245
246/* ----------------------------------------------------------------------
247 * Solver for Rectangles games.
248 *
249 * This solver is souped up beyond the needs of actually _solving_
250 * a puzzle. It is also designed to cope with uncertainty about
251 * where the numbers have been placed. This is because I run it on
252 * my generated grids _before_ placing the numbers, and have it
253 * tell me where I need to place the numbers to ensure a unique
254 * solution.
255 */
256
257static void remove_rect_placement(int w, int h,
258 struct rectlist *rectpositions,
259 int *overlaps,
260 int rectnum, int placement)
261{
262 int x, y, xx, yy;
263
264#ifdef SOLVER_DIAGNOSTICS
265 printf("ruling out rect %d placement at %d,%d w=%d h=%d\n", rectnum,
266 rectpositions[rectnum].rects[placement].x,
267 rectpositions[rectnum].rects[placement].y,
268 rectpositions[rectnum].rects[placement].w,
269 rectpositions[rectnum].rects[placement].h);
270#endif
271
272 /*
273 * Decrement each entry in the overlaps array to reflect the
274 * removal of this rectangle placement.
275 */
276 for (yy = 0; yy < rectpositions[rectnum].rects[placement].h; yy++) {
277 y = yy + rectpositions[rectnum].rects[placement].y;
278 for (xx = 0; xx < rectpositions[rectnum].rects[placement].w; xx++) {
279 x = xx + rectpositions[rectnum].rects[placement].x;
280
281 assert(overlaps[(rectnum * h + y) * w + x] != 0);
282
283 if (overlaps[(rectnum * h + y) * w + x] > 0)
284 overlaps[(rectnum * h + y) * w + x]--;
285 }
286 }
287
288 /*
289 * Remove the placement from the list of positions for that
290 * rectangle, by interchanging it with the one on the end.
291 */
292 if (placement < rectpositions[rectnum].n - 1) {
293 struct rect t;
294
295 t = rectpositions[rectnum].rects[rectpositions[rectnum].n - 1];
296 rectpositions[rectnum].rects[rectpositions[rectnum].n - 1] =
297 rectpositions[rectnum].rects[placement];
298 rectpositions[rectnum].rects[placement] = t;
299 }
300 rectpositions[rectnum].n--;
301}
302
303static void remove_number_placement(int w, int h, struct numberdata *number,
304 int index, int *rectbyplace)
305{
306 /*
307 * Remove the entry from the rectbyplace array.
308 */
309 rectbyplace[number->points[index].y * w + number->points[index].x] = -1;
310
311 /*
312 * Remove the placement from the list of candidates for that
313 * number, by interchanging it with the one on the end.
314 */
315 if (index < number->npoints - 1) {
316 struct point t;
317
318 t = number->points[number->npoints - 1];
319 number->points[number->npoints - 1] = number->points[index];
320 number->points[index] = t;
321 }
322 number->npoints--;
323}
324
325static int rect_solver(int w, int h, int nrects, struct numberdata *numbers,
df11cd4e 326 unsigned char *hedge, unsigned char *vedge,
327 random_state *rs)
26801d29 328{
329 struct rectlist *rectpositions;
330 int *overlaps, *rectbyplace, *workspace;
331 int i, ret;
332
333 /*
334 * Start by setting up a list of candidate positions for each
335 * rectangle.
336 */
337 rectpositions = snewn(nrects, struct rectlist);
338 for (i = 0; i < nrects; i++) {
339 int rw, rh, area = numbers[i].area;
340 int j, minx, miny, maxx, maxy;
341 struct rect *rlist;
342 int rlistn, rlistsize;
343
344 /*
345 * For each rectangle, begin by finding the bounding
346 * rectangle of its candidate number placements.
347 */
348 maxx = maxy = -1;
349 minx = w;
350 miny = h;
351 for (j = 0; j < numbers[i].npoints; j++) {
352 if (minx > numbers[i].points[j].x) minx = numbers[i].points[j].x;
353 if (miny > numbers[i].points[j].y) miny = numbers[i].points[j].y;
354 if (maxx < numbers[i].points[j].x) maxx = numbers[i].points[j].x;
355 if (maxy < numbers[i].points[j].y) maxy = numbers[i].points[j].y;
356 }
357
358 /*
359 * Now loop over all possible rectangle placements
360 * overlapping a point within that bounding rectangle;
361 * ensure each one actually contains a candidate number
362 * placement, and add it to the list.
363 */
364 rlist = NULL;
365 rlistn = rlistsize = 0;
366
367 for (rw = 1; rw <= area && rw <= w; rw++) {
368 int x, y;
369
370 if (area % rw)
371 continue;
372 rh = area / rw;
373 if (rh > h)
374 continue;
375
376 for (y = miny - rh + 1; y <= maxy; y++) {
377 if (y < 0 || y+rh > h)
378 continue;
379
380 for (x = minx - rw + 1; x <= maxx; x++) {
381 if (x < 0 || x+rw > w)
382 continue;
383
384 /*
385 * See if we can find a candidate number
386 * placement within this rectangle.
387 */
388 for (j = 0; j < numbers[i].npoints; j++)
389 if (numbers[i].points[j].x >= x &&
390 numbers[i].points[j].x < x+rw &&
391 numbers[i].points[j].y >= y &&
392 numbers[i].points[j].y < y+rh)
393 break;
394
395 if (j < numbers[i].npoints) {
396 /*
397 * Add this to the list of candidate
398 * placements for this rectangle.
399 */
400 if (rlistn >= rlistsize) {
401 rlistsize = rlistn + 32;
402 rlist = sresize(rlist, rlistsize, struct rect);
403 }
404 rlist[rlistn].x = x;
405 rlist[rlistn].y = y;
406 rlist[rlistn].w = rw;
407 rlist[rlistn].h = rh;
408#ifdef SOLVER_DIAGNOSTICS
409 printf("rect %d [area %d]: candidate position at"
410 " %d,%d w=%d h=%d\n",
411 i, area, x, y, rw, rh);
412#endif
413 rlistn++;
414 }
415 }
416 }
417 }
418
419 rectpositions[i].rects = rlist;
420 rectpositions[i].n = rlistn;
421 }
422
423 /*
424 * Next, construct a multidimensional array tracking how many
425 * candidate positions for each rectangle overlap each square.
426 *
427 * Indexing of this array is by the formula
428 *
429 * overlaps[(rectindex * h + y) * w + x]
430 */
431 overlaps = snewn(nrects * w * h, int);
432 memset(overlaps, 0, nrects * w * h * sizeof(int));
433 for (i = 0; i < nrects; i++) {
434 int j;
435
436 for (j = 0; j < rectpositions[i].n; j++) {
437 int xx, yy;
438
439 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++)
440 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++)
441 overlaps[(i * h + yy+rectpositions[i].rects[j].y) * w +
442 xx+rectpositions[i].rects[j].x]++;
443 }
444 }
445
446 /*
447 * Also we want an array covering the grid once, to make it
448 * easy to figure out which squares are candidate number
449 * placements for which rectangles. (The existence of this
450 * single array assumes that no square starts off as a
451 * candidate number placement for more than one rectangle. This
452 * assumption is justified, because this solver is _either_
453 * used to solve real problems - in which case there is a
454 * single placement for every number - _or_ used to decide on
455 * number placements for a new puzzle, in which case each
456 * number's placements are confined to the intended position of
457 * the rectangle containing that number.)
458 */
459 rectbyplace = snewn(w * h, int);
460 for (i = 0; i < w*h; i++)
461 rectbyplace[i] = -1;
462
463 for (i = 0; i < nrects; i++) {
464 int j;
465
466 for (j = 0; j < numbers[i].npoints; j++) {
467 int x = numbers[i].points[j].x;
468 int y = numbers[i].points[j].y;
469
470 assert(rectbyplace[y * w + x] == -1);
471 rectbyplace[y * w + x] = i;
472 }
473 }
474
475 workspace = snewn(nrects, int);
476
477 /*
478 * Now run the actual deduction loop.
479 */
480 while (1) {
481 int done_something = FALSE;
482
483#ifdef SOLVER_DIAGNOSTICS
484 printf("starting deduction loop\n");
485
486 for (i = 0; i < nrects; i++) {
487 printf("rect %d overlaps:\n", i);
488 {
489 int x, y;
490 for (y = 0; y < h; y++) {
491 for (x = 0; x < w; x++) {
492 printf("%3d", overlaps[(i * h + y) * w + x]);
493 }
494 printf("\n");
495 }
496 }
497 }
498 printf("rectbyplace:\n");
499 {
500 int x, y;
501 for (y = 0; y < h; y++) {
502 for (x = 0; x < w; x++) {
503 printf("%3d", rectbyplace[y * w + x]);
504 }
505 printf("\n");
506 }
507 }
508#endif
509
510 /*
511 * Housekeeping. Look for rectangles whose number has only
512 * one candidate position left, and mark that square as
513 * known if it isn't already.
514 */
515 for (i = 0; i < nrects; i++) {
516 if (numbers[i].npoints == 1) {
517 int x = numbers[i].points[0].x;
518 int y = numbers[i].points[0].y;
519 if (overlaps[(i * h + y) * w + x] >= -1) {
520 int j;
521
522 assert(overlaps[(i * h + y) * w + x] > 0);
523#ifdef SOLVER_DIAGNOSTICS
524 printf("marking %d,%d as known for rect %d"
525 " (sole remaining number position)\n", x, y, i);
526#endif
527
528 for (j = 0; j < nrects; j++)
529 overlaps[(j * h + y) * w + x] = -1;
530
531 overlaps[(i * h + y) * w + x] = -2;
532 }
533 }
534 }
535
536 /*
537 * Now look at the intersection of all possible placements
538 * for each rectangle, and mark all squares in that
539 * intersection as known for that rectangle if they aren't
540 * already.
541 */
542 for (i = 0; i < nrects; i++) {
543 int minx, miny, maxx, maxy, xx, yy, j;
544
545 minx = miny = 0;
546 maxx = w;
547 maxy = h;
548
549 for (j = 0; j < rectpositions[i].n; j++) {
550 int x = rectpositions[i].rects[j].x;
551 int y = rectpositions[i].rects[j].y;
552 int w = rectpositions[i].rects[j].w;
553 int h = rectpositions[i].rects[j].h;
554
555 if (minx < x) minx = x;
556 if (miny < y) miny = y;
557 if (maxx > x+w) maxx = x+w;
558 if (maxy > y+h) maxy = y+h;
559 }
560
561 for (yy = miny; yy < maxy; yy++)
562 for (xx = minx; xx < maxx; xx++)
563 if (overlaps[(i * h + yy) * w + xx] >= -1) {
564 assert(overlaps[(i * h + yy) * w + xx] > 0);
565#ifdef SOLVER_DIAGNOSTICS
566 printf("marking %d,%d as known for rect %d"
567 " (intersection of all placements)\n",
568 xx, yy, i);
569#endif
570
571 for (j = 0; j < nrects; j++)
572 overlaps[(j * h + yy) * w + xx] = -1;
573
574 overlaps[(i * h + yy) * w + xx] = -2;
575 }
576 }
577
578 /*
579 * Rectangle-focused deduction. Look at each rectangle in
580 * turn and try to rule out some of its candidate
581 * placements.
582 */
583 for (i = 0; i < nrects; i++) {
584 int j;
585
586 for (j = 0; j < rectpositions[i].n; j++) {
587 int xx, yy, k;
588 int del = FALSE;
589
590 for (k = 0; k < nrects; k++)
591 workspace[k] = 0;
592
593 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
594 int y = yy + rectpositions[i].rects[j].y;
595 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
596 int x = xx + rectpositions[i].rects[j].x;
597
598 if (overlaps[(i * h + y) * w + x] == -1) {
599 /*
600 * This placement overlaps a square
601 * which is _known_ to be part of
602 * another rectangle. Therefore we must
603 * rule it out.
604 */
605#ifdef SOLVER_DIAGNOSTICS
606 printf("rect %d placement at %d,%d w=%d h=%d "
607 "contains %d,%d which is known-other\n", i,
608 rectpositions[i].rects[j].x,
609 rectpositions[i].rects[j].y,
610 rectpositions[i].rects[j].w,
611 rectpositions[i].rects[j].h,
612 x, y);
613#endif
614 del = TRUE;
615 }
616
617 if (rectbyplace[y * w + x] != -1) {
618 /*
619 * This placement overlaps one of the
620 * candidate number placements for some
621 * rectangle. Count it.
622 */
623 workspace[rectbyplace[y * w + x]]++;
624 }
625 }
626 }
627
628 if (!del) {
629 /*
630 * If we haven't ruled this placement out
631 * already, see if it overlaps _all_ of the
632 * candidate number placements for any
633 * rectangle. If so, we can rule it out.
634 */
635 for (k = 0; k < nrects; k++)
636 if (k != i && workspace[k] == numbers[k].npoints) {
637#ifdef SOLVER_DIAGNOSTICS
638 printf("rect %d placement at %d,%d w=%d h=%d "
639 "contains all number points for rect %d\n",
640 i,
641 rectpositions[i].rects[j].x,
642 rectpositions[i].rects[j].y,
643 rectpositions[i].rects[j].w,
644 rectpositions[i].rects[j].h,
645 k);
646#endif
647 del = TRUE;
648 break;
649 }
650
651 /*
652 * Failing that, see if it overlaps at least
653 * one of the candidate number placements for
654 * itself! (This might not be the case if one
655 * of those number placements has been removed
656 * recently.).
657 */
658 if (!del && workspace[i] == 0) {
659#ifdef SOLVER_DIAGNOSTICS
660 printf("rect %d placement at %d,%d w=%d h=%d "
661 "contains none of its own number points\n",
662 i,
663 rectpositions[i].rects[j].x,
664 rectpositions[i].rects[j].y,
665 rectpositions[i].rects[j].w,
666 rectpositions[i].rects[j].h);
667#endif
668 del = TRUE;
669 }
670 }
671
672 if (del) {
673 remove_rect_placement(w, h, rectpositions, overlaps, i, j);
674
675 j--; /* don't skip over next placement */
676
677 done_something = TRUE;
678 }
679 }
680 }
681
682 /*
683 * Square-focused deduction. Look at each square not marked
684 * as known, and see if there are any which can only be
685 * part of a single rectangle.
686 */
687 {
688 int x, y, n, index;
689 for (y = 0; y < h; y++) for (x = 0; x < w; x++) {
690 /* Known squares are marked as <0 everywhere, so we only need
691 * to check the overlaps entry for rect 0. */
692 if (overlaps[y * w + x] < 0)
693 continue; /* known already */
694
695 n = 0;
696 index = -1;
697 for (i = 0; i < nrects; i++)
698 if (overlaps[(i * h + y) * w + x] > 0)
699 n++, index = i;
700
701 if (n == 1) {
702 int j;
703
704 /*
705 * Now we can rule out all placements for
706 * rectangle `index' which _don't_ contain
707 * square x,y.
708 */
709#ifdef SOLVER_DIAGNOSTICS
710 printf("square %d,%d can only be in rectangle %d\n",
711 x, y, index);
712#endif
713 for (j = 0; j < rectpositions[index].n; j++) {
714 struct rect *r = &rectpositions[index].rects[j];
715 if (x >= r->x && x < r->x + r->w &&
716 y >= r->y && y < r->y + r->h)
717 continue; /* this one is OK */
718 remove_rect_placement(w, h, rectpositions, overlaps,
719 index, j);
720 j--; /* don't skip over next placement */
721 done_something = TRUE;
722 }
723 }
724 }
725 }
726
727 /*
728 * If we've managed to deduce anything by normal means,
729 * loop round again and see if there's more to be done.
730 * Only if normal deduction has completely failed us should
731 * we now move on to narrowing down the possible number
732 * placements.
733 */
734 if (done_something)
735 continue;
736
737 /*
738 * Now we have done everything we can with the current set
739 * of number placements. So we need to winnow the number
740 * placements so as to narrow down the possibilities. We do
741 * this by searching for a candidate placement (of _any_
742 * rectangle) which overlaps a candidate placement of the
743 * number for some other rectangle.
744 */
1507058f 745 if (rs) {
26801d29 746 struct rpn {
747 int rect;
748 int placement;
749 int number;
750 } *rpns = NULL;
64aec339 751 size_t nrpns = 0, rpnsize = 0;
26801d29 752 int j;
753
754 for (i = 0; i < nrects; i++) {
755 for (j = 0; j < rectpositions[i].n; j++) {
756 int xx, yy;
757
758 for (yy = 0; yy < rectpositions[i].rects[j].h; yy++) {
759 int y = yy + rectpositions[i].rects[j].y;
760 for (xx = 0; xx < rectpositions[i].rects[j].w; xx++) {
761 int x = xx + rectpositions[i].rects[j].x;
762
763 if (rectbyplace[y * w + x] >= 0 &&
764 rectbyplace[y * w + x] != i) {
765 /*
766 * Add this to the list of
767 * winnowing possibilities.
768 */
769 if (nrpns >= rpnsize) {
770 rpnsize = rpnsize * 3 / 2 + 32;
771 rpns = sresize(rpns, rpnsize, struct rpn);
772 }
773 rpns[nrpns].rect = i;
774 rpns[nrpns].placement = j;
775 rpns[nrpns].number = rectbyplace[y * w + x];
776 nrpns++;
777 }
778 }
779 }
780
781 }
782 }
783
784#ifdef SOLVER_DIAGNOSTICS
785 printf("%d candidate rect placements we could eliminate\n", nrpns);
786#endif
787 if (nrpns > 0) {
788 /*
789 * Now choose one of these unwanted rectangle
790 * placements, and eliminate it.
791 */
792 int index = random_upto(rs, nrpns);
793 int k, m;
794 struct rpn rpn = rpns[index];
795 struct rect r;
796 sfree(rpns);
797
798 i = rpn.rect;
799 j = rpn.placement;
800 k = rpn.number;
801 r = rectpositions[i].rects[j];
802
803 /*
804 * We rule out placement j of rectangle i by means
805 * of removing all of rectangle k's candidate
806 * number placements which do _not_ overlap it.
807 * This will ensure that it is eliminated during
808 * the next pass of rectangle-focused deduction.
809 */
810#ifdef SOLVER_DIAGNOSTICS
811 printf("ensuring number for rect %d is within"
812 " rect %d's placement at %d,%d w=%d h=%d\n",
813 k, i, r.x, r.y, r.w, r.h);
814#endif
815
816 for (m = 0; m < numbers[k].npoints; m++) {
817 int x = numbers[k].points[m].x;
818 int y = numbers[k].points[m].y;
819
820 if (x < r.x || x >= r.x + r.w ||
821 y < r.y || y >= r.y + r.h) {
822#ifdef SOLVER_DIAGNOSTICS
823 printf("eliminating number for rect %d at %d,%d\n",
824 k, x, y);
825#endif
826 remove_number_placement(w, h, &numbers[k],
827 m, rectbyplace);
828 m--; /* don't skip the next one */
829 done_something = TRUE;
830 }
831 }
832 }
833 }
834
835 if (!done_something) {
836#ifdef SOLVER_DIAGNOSTICS
837 printf("terminating deduction loop\n");
838#endif
839 break;
840 }
841 }
842
843 ret = TRUE;
844 for (i = 0; i < nrects; i++) {
845#ifdef SOLVER_DIAGNOSTICS
846 printf("rect %d has %d possible placements\n",
847 i, rectpositions[i].n);
848#endif
849 assert(rectpositions[i].n > 0);
1507058f 850 if (rectpositions[i].n > 1) {
26801d29 851 ret = FALSE;
df11cd4e 852 } else if (hedge && vedge) {
853 /*
854 * Place the rectangle in its only possible position.
855 */
856 int x, y;
857 struct rect *r = &rectpositions[i].rects[0];
858
859 for (y = 0; y < r->h; y++) {
860 if (r->x > 0)
861 vedge[(r->y+y) * w + r->x] = 1;
862 if (r->x+r->w < w)
863 vedge[(r->y+y) * w + r->x+r->w] = 1;
864 }
865 for (x = 0; x < r->w; x++) {
866 if (r->y > 0)
867 hedge[r->y * w + r->x+x] = 1;
868 if (r->y+r->h < h)
869 hedge[(r->y+r->h) * w + r->x+x] = 1;
870 }
1507058f 871 }
26801d29 872 }
873
874 /*
875 * Free up all allocated storage.
876 */
877 sfree(workspace);
878 sfree(rectbyplace);
879 sfree(overlaps);
880 for (i = 0; i < nrects; i++)
881 sfree(rectpositions[i].rects);
882 sfree(rectpositions);
883
884 return ret;
885}
886
887/* ----------------------------------------------------------------------
888 * Grid generation code.
889 */
890
738d2f61 891/*
892 * This function does one of two things. If passed r==NULL, it
893 * counts the number of possible rectangles which cover the given
894 * square, and returns it in *n. If passed r!=NULL then it _reads_
895 * *n to find an index, counts the possible rectangles until it
896 * reaches the nth, and writes it into r.
897 *
898 * `scratch' is expected to point to an array of 2 * params->w
899 * ints, used internally as scratch space (and passed in like this
900 * to avoid re-allocating and re-freeing it every time round a
901 * tight loop).
902 */
903static void enum_rects(game_params *params, int *grid, struct rect *r, int *n,
904 int sx, int sy, int *scratch)
3870c4d8 905{
738d2f61 906 int rw, rh, mw, mh;
907 int x, y, dx, dy;
908 int maxarea, realmaxarea;
909 int index = 0;
910 int *top, *bottom;
3870c4d8 911
912 /*
d4e7900f 913 * Maximum rectangle area is 1/6 of total grid size, unless
914 * this means we can't place any rectangles at all in which
915 * case we set it to 2 at minimum.
3870c4d8 916 */
917 maxarea = params->w * params->h / 6;
d4e7900f 918 if (maxarea < 2)
919 maxarea = 2;
3870c4d8 920
738d2f61 921 /*
922 * Scan the grid to find the limits of the region within which
923 * any rectangle containing this point must fall. This will
924 * save us trawling the inside of every rectangle later on to
925 * see if it contains any used squares.
926 */
927 top = scratch;
928 bottom = scratch + params->w;
929 for (dy = -1; dy <= +1; dy += 2) {
930 int *array = (dy == -1 ? top : bottom);
931 for (dx = -1; dx <= +1; dx += 2) {
932 for (x = sx; x >= 0 && x < params->w; x += dx) {
933 array[x] = -2 * params->h * dy;
934 for (y = sy; y >= 0 && y < params->h; y += dy) {
935 if (index(params, grid, x, y) == -1 &&
936 (x == sx || dy*y <= dy*array[x-dx]))
937 array[x] = y;
938 else
939 break;
940 }
941 }
942 }
943 }
944
945 /*
946 * Now scan again to work out the largest rectangles we can fit
947 * in the grid, so that we can terminate the following loops
948 * early once we get down to not having much space left in the
949 * grid.
950 */
951 realmaxarea = 0;
952 for (x = 0; x < params->w; x++) {
953 int x2;
954
955 rh = bottom[x] - top[x] + 1;
956 if (rh <= 0)
957 continue; /* no rectangles can start here */
958
959 dx = (x > sx ? -1 : +1);
960 for (x2 = x; x2 >= 0 && x2 < params->w; x2 += dx)
961 if (bottom[x2] < bottom[x] || top[x2] > top[x])
962 break;
963
964 rw = abs(x2 - x);
965 if (realmaxarea < rw * rh)
966 realmaxarea = rw * rh;
967 }
968
969 if (realmaxarea > maxarea)
970 realmaxarea = maxarea;
971
972 /*
973 * Rectangles which go right the way across the grid are
974 * boring, although they can't be helped in the case of
975 * extremely small grids. (Also they might be generated later
976 * on by the singleton-removal process; we can't help that.)
977 */
978 mw = params->w - 1;
979 if (mw < 3) mw++;
980 mh = params->h - 1;
981 if (mh < 3) mh++;
982
983 for (rw = 1; rw <= mw; rw++)
984 for (rh = 1; rh <= mh; rh++) {
985 if (rw * rh > realmaxarea)
3870c4d8 986 continue;
987 if (rw * rh == 1)
988 continue;
738d2f61 989 for (x = max(sx - rw + 1, 0); x <= min(sx, params->w - rw); x++)
990 for (y = max(sy - rh + 1, 0); y <= min(sy, params->h - rh);
991 y++) {
992 /*
993 * Check this rectangle against the region we
994 * defined above.
995 */
996 if (top[x] <= y && top[x+rw-1] <= y &&
997 bottom[x] >= y+rh-1 && bottom[x+rw-1] >= y+rh-1) {
998 if (r && index == *n) {
999 r->x = x;
1000 r->y = y;
1001 r->w = rw;
1002 r->h = rh;
1003 return;
1004 }
1005 index++;
3870c4d8 1006 }
3870c4d8 1007 }
1008 }
1009
738d2f61 1010 assert(!r);
1011 *n = index;
3870c4d8 1012}
1013
1014static void place_rect(game_params *params, int *grid, struct rect r)
1015{
1016 int idx = INDEX(params, r.x, r.y);
1017 int x, y;
1018
1019 for (x = r.x; x < r.x+r.w; x++)
1020 for (y = r.y; y < r.y+r.h; y++) {
1021 index(params, grid, x, y) = idx;
1022 }
1023#ifdef GENERATION_DIAGNOSTICS
1024 printf(" placing rectangle at (%d,%d) size %d x %d\n",
1025 r.x, r.y, r.w, r.h);
1026#endif
1027}
1028
1029static struct rect find_rect(game_params *params, int *grid, int x, int y)
1030{
1031 int idx, w, h;
1032 struct rect r;
1033
1034 /*
1035 * Find the top left of the rectangle.
1036 */
1037 idx = index(params, grid, x, y);
1038
1039 if (idx < 0) {
1040 r.x = x;
1041 r.y = y;
1042 r.w = r.h = 1;
1043 return r; /* 1x1 singleton here */
1044 }
1045
1046 y = idx / params->w;
1047 x = idx % params->w;
1048
1049 /*
1050 * Find the width and height of the rectangle.
1051 */
1052 for (w = 1;
1053 (x+w < params->w && index(params,grid,x+w,y)==idx);
1054 w++);
1055 for (h = 1;
1056 (y+h < params->h && index(params,grid,x,y+h)==idx);
1057 h++);
1058
1059 r.x = x;
1060 r.y = y;
1061 r.w = w;
1062 r.h = h;
1063
1064 return r;
1065}
1066
1067#ifdef GENERATION_DIAGNOSTICS
aea3ed9a 1068static void display_grid(game_params *params, int *grid, int *numbers, int all)
3870c4d8 1069{
1070 unsigned char *egrid = snewn((params->w*2+3) * (params->h*2+3),
1071 unsigned char);
3870c4d8 1072 int x, y;
1073 int r = (params->w*2+3);
1074
aea3ed9a 1075 memset(egrid, 0, (params->w*2+3) * (params->h*2+3));
1076
3870c4d8 1077 for (x = 0; x < params->w; x++)
1078 for (y = 0; y < params->h; y++) {
1079 int i = index(params, grid, x, y);
1080 if (x == 0 || index(params, grid, x-1, y) != i)
1081 egrid[(2*y+2) * r + (2*x+1)] = 1;
1082 if (x == params->w-1 || index(params, grid, x+1, y) != i)
1083 egrid[(2*y+2) * r + (2*x+3)] = 1;
1084 if (y == 0 || index(params, grid, x, y-1) != i)
1085 egrid[(2*y+1) * r + (2*x+2)] = 1;
1086 if (y == params->h-1 || index(params, grid, x, y+1) != i)
1087 egrid[(2*y+3) * r + (2*x+2)] = 1;
1088 }
1089
1090 for (y = 1; y < 2*params->h+2; y++) {
1091 for (x = 1; x < 2*params->w+2; x++) {
1092 if (!((y|x)&1)) {
aea3ed9a 1093 int k = numbers ? index(params, numbers, x/2-1, y/2-1) : 0;
1094 if (k || (all && numbers)) printf("%2d", k); else printf(" ");
3870c4d8 1095 } else if (!((y&x)&1)) {
1096 int v = egrid[y*r+x];
1097 if ((y&1) && v) v = '-';
1098 if ((x&1) && v) v = '|';
1099 if (!v) v = ' ';
1100 putchar(v);
1101 if (!(x&1)) putchar(v);
1102 } else {
1103 int c, d = 0;
1104 if (egrid[y*r+(x+1)]) d |= 1;
1105 if (egrid[(y-1)*r+x]) d |= 2;
1106 if (egrid[y*r+(x-1)]) d |= 4;
1107 if (egrid[(y+1)*r+x]) d |= 8;
1108 c = " ??+?-++?+|+++++"[d];
1109 putchar(c);
1110 if (!(x&1)) putchar(c);
1111 }
1112 }
1113 putchar('\n');
1114 }
1115
1116 sfree(egrid);
1117}
1118#endif
1119
2ac6d24e 1120struct game_aux_info {
1121 int w, h;
1122 unsigned char *vedge; /* (w+1) x h */
1123 unsigned char *hedge; /* w x (h+1) */
1124};
1125
1185e3c5 1126static char *new_game_desc(game_params *params, random_state *rs,
6aa6af4c 1127 game_aux_info **aux, int interactive)
3870c4d8 1128{
26801d29 1129 int *grid, *numbers = NULL;
738d2f61 1130 int x, y, y2, y2last, yx, run, i, nsquares;
1185e3c5 1131 char *desc, *p;
738d2f61 1132 int *enum_rects_scratch;
aea3ed9a 1133 game_params params2real, *params2 = &params2real;
3870c4d8 1134
26801d29 1135 while (1) {
1136 /*
1137 * Set up the smaller width and height which we will use to
1138 * generate the base grid.
1139 */
1140 params2->w = params->w / (1.0F + params->expandfactor);
1141 if (params2->w < 2 && params->w >= 2) params2->w = 2;
1142 params2->h = params->h / (1.0F + params->expandfactor);
1143 if (params2->h < 2 && params->h >= 2) params2->h = 2;
aea3ed9a 1144
26801d29 1145 grid = snewn(params2->w * params2->h, int);
3870c4d8 1146
738d2f61 1147 enum_rects_scratch = snewn(2 * params2->w, int);
1148
1149 nsquares = 0;
26801d29 1150 for (y = 0; y < params2->h; y++)
1151 for (x = 0; x < params2->w; x++) {
1152 index(params2, grid, x, y) = -1;
738d2f61 1153 nsquares++;
26801d29 1154 }
3870c4d8 1155
3870c4d8 1156 /*
738d2f61 1157 * Place rectangles until we can't any more. We do this by
1158 * finding a square we haven't yet covered, and randomly
1159 * choosing a rectangle to cover it.
3870c4d8 1160 */
738d2f61 1161
1162 while (nsquares > 0) {
1163 int square = random_upto(rs, nsquares);
1164 int n;
26801d29 1165 struct rect r;
1166
738d2f61 1167 x = params2->w;
1168 y = params2->h;
1169 for (y = 0; y < params2->h; y++) {
1170 for (x = 0; x < params2->w; x++) {
1171 if (index(params2, grid, x, y) == -1 && square-- == 0)
1172 break;
1173 }
1174 if (x < params2->w)
1175 break;
1176 }
1177 assert(x < params2->w && y < params2->h);
26801d29 1178
1179 /*
738d2f61 1180 * Now see how many rectangles fit around this one.
26801d29 1181 */
738d2f61 1182 enum_rects(params2, grid, NULL, &n, x, y, enum_rects_scratch);
26801d29 1183
738d2f61 1184 if (!n) {
1185 /*
1186 * There are no possible rectangles covering this
1187 * square, meaning it must be a singleton. Mark it
1188 * -2 so we know not to keep trying.
1189 */
1190 index(params2, grid, x, y) = -2;
1191 nsquares--;
1192 } else {
1193 /*
1194 * Pick one at random.
1195 */
1196 n = random_upto(rs, n);
1197 enum_rects(params2, grid, &r, &n, x, y, enum_rects_scratch);
1198
1199 /*
1200 * Place it.
1201 */
1202 place_rect(params2, grid, r);
1203 nsquares -= r.w * r.h;
26801d29 1204 }
26801d29 1205 }
3870c4d8 1206
738d2f61 1207 sfree(enum_rects_scratch);
3870c4d8 1208
1209 /*
26801d29 1210 * Deal with singleton spaces remaining in the grid, one by
1211 * one.
1212 *
1213 * We do this by making a local change to the layout. There are
1214 * several possibilities:
1215 *
1216 * +-----+-----+ Here, we can remove the singleton by
1217 * | | | extending the 1x2 rectangle below it
1218 * +--+--+-----+ into a 1x3.
1219 * | | | |
1220 * | +--+ |
1221 * | | | |
1222 * | | | |
1223 * | | | |
1224 * +--+--+-----+
1225 *
1226 * +--+--+--+ Here, that trick doesn't work: there's no
1227 * | | | 1 x n rectangle with the singleton at one
1228 * | | | end. Instead, we extend a 1 x n rectangle
1229 * | | | _out_ from the singleton, shaving a layer
1230 * +--+--+ | off the end of another rectangle. So if we
1231 * | | | | extended up, we'd make our singleton part
1232 * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2
1233 * | | | used to be; or we could extend right into
1234 * +--+-----+ a 2x1, turning the 1x3 into a 1x2.
1235 *
1236 * +-----+--+ Here, we can't even do _that_, since any
1237 * | | | direction we choose to extend the singleton
1238 * +--+--+ | will produce a new singleton as a result of
1239 * | | | | truncating one of the size-2 rectangles.
1240 * | +--+--+ Fortunately, this case can _only_ occur when
1241 * | | | a singleton is surrounded by four size-2s
1242 * +--+-----+ in this fashion; so instead we can simply
1243 * replace the whole section with a single 3x3.
3870c4d8 1244 */
26801d29 1245 for (x = 0; x < params2->w; x++) {
1246 for (y = 0; y < params2->h; y++) {
1247 if (index(params2, grid, x, y) < 0) {
1248 int dirs[4], ndirs;
3870c4d8 1249
1250#ifdef GENERATION_DIAGNOSTICS
26801d29 1251 display_grid(params2, grid, NULL, FALSE);
1252 printf("singleton at %d,%d\n", x, y);
3870c4d8 1253#endif
1254
26801d29 1255 /*
1256 * Check in which directions we can feasibly extend
1257 * the singleton. We can extend in a particular
1258 * direction iff either:
1259 *
1260 * - the rectangle on that side of the singleton
1261 * is not 2x1, and we are at one end of the edge
1262 * of it we are touching
1263 *
1264 * - it is 2x1 but we are on its short side.
1265 *
1266 * FIXME: we could plausibly choose between these
1267 * based on the sizes of the rectangles they would
1268 * create?
1269 */
1270 ndirs = 0;
1271 if (x < params2->w-1) {
1272 struct rect r = find_rect(params2, grid, x+1, y);
1273 if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1)
1274 dirs[ndirs++] = 1; /* right */
1275 }
1276 if (y > 0) {
1277 struct rect r = find_rect(params2, grid, x, y-1);
1278 if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1)
1279 dirs[ndirs++] = 2; /* up */
1280 }
1281 if (x > 0) {
1282 struct rect r = find_rect(params2, grid, x-1, y);
1283 if ((r.w * r.h > 2 && (r.y==y || r.y+r.h-1==y)) || r.h==1)
1284 dirs[ndirs++] = 4; /* left */
1285 }
1286 if (y < params2->h-1) {
1287 struct rect r = find_rect(params2, grid, x, y+1);
1288 if ((r.w * r.h > 2 && (r.x==x || r.x+r.w-1==x)) || r.w==1)
1289 dirs[ndirs++] = 8; /* down */
1290 }
3870c4d8 1291
26801d29 1292 if (ndirs > 0) {
1293 int which, dir;
1294 struct rect r1, r2;
3870c4d8 1295
26801d29 1296 which = random_upto(rs, ndirs);
1297 dir = dirs[which];
3870c4d8 1298
26801d29 1299 switch (dir) {
1300 case 1: /* right */
1301 assert(x < params2->w+1);
3870c4d8 1302#ifdef GENERATION_DIAGNOSTICS
26801d29 1303 printf("extending right\n");
3870c4d8 1304#endif
26801d29 1305 r1 = find_rect(params2, grid, x+1, y);
1306 r2.x = x;
1307 r2.y = y;
1308 r2.w = 1 + r1.w;
1309 r2.h = 1;
1310 if (r1.y == y)
1311 r1.y++;
1312 r1.h--;
1313 break;
1314 case 2: /* up */
1315 assert(y > 0);
3870c4d8 1316#ifdef GENERATION_DIAGNOSTICS
26801d29 1317 printf("extending up\n");
3870c4d8 1318#endif
26801d29 1319 r1 = find_rect(params2, grid, x, y-1);
1320 r2.x = x;
1321 r2.y = r1.y;
1322 r2.w = 1;
1323 r2.h = 1 + r1.h;
1324 if (r1.x == x)
1325 r1.x++;
1326 r1.w--;
1327 break;
1328 case 4: /* left */
1329 assert(x > 0);
3870c4d8 1330#ifdef GENERATION_DIAGNOSTICS
26801d29 1331 printf("extending left\n");
3870c4d8 1332#endif
26801d29 1333 r1 = find_rect(params2, grid, x-1, y);
1334 r2.x = r1.x;
1335 r2.y = y;
1336 r2.w = 1 + r1.w;
1337 r2.h = 1;
1338 if (r1.y == y)
1339 r1.y++;
1340 r1.h--;
1341 break;
1342 case 8: /* down */
1343 assert(y < params2->h+1);
3870c4d8 1344#ifdef GENERATION_DIAGNOSTICS
26801d29 1345 printf("extending down\n");
3870c4d8 1346#endif
26801d29 1347 r1 = find_rect(params2, grid, x, y+1);
1348 r2.x = x;
1349 r2.y = y;
1350 r2.w = 1;
1351 r2.h = 1 + r1.h;
1352 if (r1.x == x)
1353 r1.x++;
1354 r1.w--;
1355 break;
1356 }
1357 if (r1.h > 0 && r1.w > 0)
1358 place_rect(params2, grid, r1);
1359 place_rect(params2, grid, r2);
1360 } else {
3870c4d8 1361#ifndef NDEBUG
26801d29 1362 /*
1363 * Sanity-check that there really is a 3x3
1364 * rectangle surrounding this singleton and it
1365 * contains absolutely everything we could
1366 * possibly need.
1367 */
1368 {
1369 int xx, yy;
1370 assert(x > 0 && x < params2->w-1);
1371 assert(y > 0 && y < params2->h-1);
1372
1373 for (xx = x-1; xx <= x+1; xx++)
1374 for (yy = y-1; yy <= y+1; yy++) {
1375 struct rect r = find_rect(params2,grid,xx,yy);
1376 assert(r.x >= x-1);
1377 assert(r.y >= y-1);
1378 assert(r.x+r.w-1 <= x+1);
1379 assert(r.y+r.h-1 <= y+1);
1380 }
1381 }
3870c4d8 1382#endif
26801d29 1383
3870c4d8 1384#ifdef GENERATION_DIAGNOSTICS
26801d29 1385 printf("need the 3x3 trick\n");
3870c4d8 1386#endif
1387
26801d29 1388 /*
1389 * FIXME: If the maximum rectangle area for
1390 * this grid is less than 9, we ought to
1391 * subdivide the 3x3 in some fashion. There are
1392 * five other possibilities:
1393 *
1394 * - a 6 and a 3
1395 * - a 4, a 3 and a 2
1396 * - three 3s
1397 * - a 3 and three 2s (two different arrangements).
1398 */
1399
1400 {
1401 struct rect r;
1402 r.x = x-1;
1403 r.y = y-1;
1404 r.w = r.h = 3;
1405 place_rect(params2, grid, r);
1406 }
3870c4d8 1407 }
1408 }
1409 }
1410 }
3870c4d8 1411
26801d29 1412 /*
1413 * We have now constructed a grid of the size specified in
1414 * params2. Now we extend it into a grid of the size specified
1415 * in params. We do this in two passes: we extend it vertically
1416 * until it's the right height, then we transpose it, then
1417 * extend it vertically again (getting it effectively the right
1418 * width), then finally transpose again.
1419 */
1420 for (i = 0; i < 2; i++) {
1421 int *grid2, *expand, *where;
1422 game_params params3real, *params3 = &params3real;
aea3ed9a 1423
1424#ifdef GENERATION_DIAGNOSTICS
26801d29 1425 printf("before expansion:\n");
1426 display_grid(params2, grid, NULL, TRUE);
aea3ed9a 1427#endif
1428
26801d29 1429 /*
1430 * Set up the new grid.
1431 */
1432 grid2 = snewn(params2->w * params->h, int);
1433 expand = snewn(params2->h-1, int);
1434 where = snewn(params2->w, int);
1435 params3->w = params2->w;
1436 params3->h = params->h;
1437
1438 /*
1439 * Decide which horizontal edges are going to get expanded,
1440 * and by how much.
1441 */
1442 for (y = 0; y < params2->h-1; y++)
1443 expand[y] = 0;
1444 for (y = params2->h; y < params->h; y++) {
1445 x = random_upto(rs, params2->h-1);
1446 expand[x]++;
1447 }
aea3ed9a 1448
1449#ifdef GENERATION_DIAGNOSTICS
26801d29 1450 printf("expand[] = {");
1451 for (y = 0; y < params2->h-1; y++)
1452 printf(" %d", expand[y]);
1453 printf(" }\n");
aea3ed9a 1454#endif
1455
26801d29 1456 /*
1457 * Perform the expansion. The way this works is that we
1458 * alternately:
1459 *
1460 * - copy a row from grid into grid2
1461 *
1462 * - invent some number of additional rows in grid2 where
1463 * there was previously only a horizontal line between
1464 * rows in grid, and make random decisions about where
1465 * among these to place each rectangle edge that ran
1466 * along this line.
1467 */
1468 for (y = y2 = y2last = 0; y < params2->h; y++) {
1469 /*
1470 * Copy a single line from row y of grid into row y2 of
1471 * grid2.
1472 */
1473 for (x = 0; x < params2->w; x++) {
1474 int val = index(params2, grid, x, y);
1475 if (val / params2->w == y && /* rect starts on this line */
1476 (y2 == 0 || /* we're at the very top, or... */
1477 index(params3, grid2, x, y2-1) / params3->w < y2last
1478 /* this rect isn't already started */))
1479 index(params3, grid2, x, y2) =
1480 INDEX(params3, val % params2->w, y2);
1481 else
1482 index(params3, grid2, x, y2) =
1483 index(params3, grid2, x, y2-1);
1484 }
1485
1486 /*
1487 * If that was the last line, terminate the loop early.
1488 */
1489 if (++y2 == params3->h)
1490 break;
1491
1492 y2last = y2;
1493
1494 /*
1495 * Invent some number of additional lines. First walk
1496 * along this line working out where to put all the
1497 * edges that coincide with it.
1498 */
1499 yx = -1;
1500 for (x = 0; x < params2->w; x++) {
1501 if (index(params2, grid, x, y) !=
1502 index(params2, grid, x, y+1)) {
1503 /*
1504 * This is a horizontal edge, so it needs
1505 * placing.
1506 */
1507 if (x == 0 ||
1508 (index(params2, grid, x-1, y) !=
1509 index(params2, grid, x, y) &&
1510 index(params2, grid, x-1, y+1) !=
1511 index(params2, grid, x, y+1))) {
1512 /*
1513 * Here we have the chance to make a new
1514 * decision.
1515 */
1516 yx = random_upto(rs, expand[y]+1);
1517 } else {
1518 /*
1519 * Here we just reuse the previous value of
1520 * yx.
1521 */
1522 }
1523 } else
1524 yx = -1;
1525 where[x] = yx;
1526 }
1527
1528 for (yx = 0; yx < expand[y]; yx++) {
1529 /*
1530 * Invent a single row. For each square in the row,
1531 * we copy the grid entry from the square above it,
1532 * unless we're starting the new rectangle here.
1533 */
1534 for (x = 0; x < params2->w; x++) {
1535 if (yx == where[x]) {
1536 int val = index(params2, grid, x, y+1);
1537 val %= params2->w;
1538 val = INDEX(params3, val, y2);
1539 index(params3, grid2, x, y2) = val;
1540 } else
1541 index(params3, grid2, x, y2) =
1542 index(params3, grid2, x, y2-1);
1543 }
1544
1545 y2++;
1546 }
1547 }
1548
1549 sfree(expand);
1550 sfree(where);
aea3ed9a 1551
1552#ifdef GENERATION_DIAGNOSTICS
26801d29 1553 printf("after expansion:\n");
1554 display_grid(params3, grid2, NULL, TRUE);
aea3ed9a 1555#endif
26801d29 1556 /*
1557 * Transpose.
1558 */
1559 params2->w = params3->h;
1560 params2->h = params3->w;
1561 sfree(grid);
1562 grid = snewn(params2->w * params2->h, int);
1563 for (x = 0; x < params2->w; x++)
1564 for (y = 0; y < params2->h; y++) {
1565 int idx1 = INDEX(params2, x, y);
1566 int idx2 = INDEX(params3, y, x);
1567 int tmp;
1568
1569 tmp = grid2[idx2];
1570 tmp = (tmp % params3->w) * params2->w + (tmp / params3->w);
1571 grid[idx1] = tmp;
1572 }
1573
1574 sfree(grid2);
1575
1576 {
1577 int tmp;
1578 tmp = params->w;
1579 params->w = params->h;
1580 params->h = tmp;
1581 }
aea3ed9a 1582
1583#ifdef GENERATION_DIAGNOSTICS
26801d29 1584 printf("after transposition:\n");
1585 display_grid(params2, grid, NULL, TRUE);
aea3ed9a 1586#endif
26801d29 1587 }
aea3ed9a 1588
26801d29 1589 /*
1590 * Run the solver to narrow down the possible number
1591 * placements.
1592 */
1593 {
1594 struct numberdata *nd;
1595 int nnumbers, i, ret;
1596
1597 /* Count the rectangles. */
1598 nnumbers = 0;
1599 for (y = 0; y < params->h; y++) {
1600 for (x = 0; x < params->w; x++) {
1601 int idx = INDEX(params, x, y);
1602 if (index(params, grid, x, y) == idx)
1603 nnumbers++;
1604 }
1605 }
2ac6d24e 1606
26801d29 1607 nd = snewn(nnumbers, struct numberdata);
1608
1609 /* Now set up each number's candidate position list. */
1610 i = 0;
1611 for (y = 0; y < params->h; y++) {
1612 for (x = 0; x < params->w; x++) {
1613 int idx = INDEX(params, x, y);
1614 if (index(params, grid, x, y) == idx) {
1615 struct rect r = find_rect(params, grid, x, y);
1616 int j, k, m;
1617
1618 nd[i].area = r.w * r.h;
1619 nd[i].npoints = nd[i].area;
1620 nd[i].points = snewn(nd[i].npoints, struct point);
1621 m = 0;
1622 for (j = 0; j < r.h; j++)
1623 for (k = 0; k < r.w; k++) {
1624 nd[i].points[m].x = k + r.x;
1625 nd[i].points[m].y = j + r.y;
1626 m++;
1627 }
1628 assert(m == nd[i].npoints);
aea3ed9a 1629
26801d29 1630 i++;
1631 }
1632 }
1633 }
aea3ed9a 1634
40fde884 1635 if (params->unique)
1507058f 1636 ret = rect_solver(params->w, params->h, nnumbers, nd,
df11cd4e 1637 NULL, NULL, rs);
40fde884 1638 else
1639 ret = TRUE; /* allow any number placement at all */
3870c4d8 1640
26801d29 1641 if (ret) {
3870c4d8 1642 /*
26801d29 1643 * Now place the numbers according to the solver's
1644 * recommendations.
3870c4d8 1645 */
26801d29 1646 numbers = snewn(params->w * params->h, int);
1647
1648 for (y = 0; y < params->h; y++)
1649 for (x = 0; x < params->w; x++) {
1650 index(params, numbers, x, y) = 0;
1651 }
1652
1653 for (i = 0; i < nnumbers; i++) {
1654 int idx = random_upto(rs, nd[i].npoints);
1655 int x = nd[i].points[idx].x;
1656 int y = nd[i].points[idx].y;
1657 index(params,numbers,x,y) = nd[i].area;
1658 }
3870c4d8 1659 }
26801d29 1660
1661 /*
1662 * Clean up.
1663 */
1664 for (i = 0; i < nnumbers; i++)
1665 sfree(nd[i].points);
1666 sfree(nd);
1667
1668 /*
1669 * If we've succeeded, then terminate the loop.
1670 */
1671 if (ret)
1672 break;
3870c4d8 1673 }
26801d29 1674
1675 /*
1676 * Give up and go round again.
1677 */
1678 sfree(grid);
1679 }
1680
1681 /*
1682 * Store the rectangle data in the game_aux_info.
1683 */
1684 {
1685 game_aux_info *ai = snew(game_aux_info);
1686
1687 ai->w = params->w;
1688 ai->h = params->h;
1689 ai->vedge = snewn(ai->w * ai->h, unsigned char);
1690 ai->hedge = snewn(ai->w * ai->h, unsigned char);
1691
1692 for (y = 0; y < params->h; y++)
1693 for (x = 1; x < params->w; x++) {
1694 vedge(ai, x, y) =
1695 index(params, grid, x, y) != index(params, grid, x-1, y);
1696 }
1697 for (y = 1; y < params->h; y++)
1698 for (x = 0; x < params->w; x++) {
1699 hedge(ai, x, y) =
1700 index(params, grid, x, y) != index(params, grid, x, y-1);
1701 }
1702
1703 *aux = ai;
3870c4d8 1704 }
1705
1706#ifdef GENERATION_DIAGNOSTICS
aea3ed9a 1707 display_grid(params, grid, numbers, FALSE);
3870c4d8 1708#endif
1709
1185e3c5 1710 desc = snewn(11 * params->w * params->h, char);
1711 p = desc;
3870c4d8 1712 run = 0;
1713 for (i = 0; i <= params->w * params->h; i++) {
1714 int n = (i < params->w * params->h ? numbers[i] : -1);
1715
1716 if (!n)
1717 run++;
1718 else {
1719 if (run) {
1720 while (run > 0) {
1721 int c = 'a' - 1 + run;
1722 if (run > 26)
1723 c = 'z';
1724 *p++ = c;
1725 run -= c - ('a' - 1);
1726 }
1727 } else {
0e87eedc 1728 /*
1729 * If there's a number in the very top left or
1730 * bottom right, there's no point putting an
1731 * unnecessary _ before or after it.
1732 */
1185e3c5 1733 if (p > desc && n > 0)
0e87eedc 1734 *p++ = '_';
3870c4d8 1735 }
1736 if (n > 0)
1737 p += sprintf(p, "%d", n);
1738 run = 0;
1739 }
1740 }
1741 *p = '\0';
1742
1743 sfree(grid);
1744 sfree(numbers);
1745
1185e3c5 1746 return desc;
3870c4d8 1747}
1748
2ac6d24e 1749static void game_free_aux_info(game_aux_info *ai)
6f2d8d7c 1750{
2ac6d24e 1751 sfree(ai->vedge);
1752 sfree(ai->hedge);
1753 sfree(ai);
6f2d8d7c 1754}
1755
1185e3c5 1756static char *validate_desc(game_params *params, char *desc)
3870c4d8 1757{
1758 int area = params->w * params->h;
1759 int squares = 0;
1760
1185e3c5 1761 while (*desc) {
1762 int n = *desc++;
3870c4d8 1763 if (n >= 'a' && n <= 'z') {
1764 squares += n - 'a' + 1;
1765 } else if (n == '_') {
1766 /* do nothing */;
1767 } else if (n > '0' && n <= '9') {
9bb5bf60 1768 squares++;
1185e3c5 1769 while (*desc >= '0' && *desc <= '9')
1770 desc++;
3870c4d8 1771 } else
1185e3c5 1772 return "Invalid character in game description";
3870c4d8 1773 }
1774
1775 if (squares < area)
1776 return "Not enough data to fill grid";
1777
1778 if (squares > area)
1779 return "Too much data to fit in grid";
1780
1781 return NULL;
1782}
1783
c380832d 1784static game_state *new_game(midend_data *me, game_params *params, char *desc)
3870c4d8 1785{
1786 game_state *state = snew(game_state);
1787 int x, y, i, area;
1788
1789 state->w = params->w;
1790 state->h = params->h;
1791
1792 area = state->w * state->h;
1793
1794 state->grid = snewn(area, int);
1795 state->vedge = snewn(area, unsigned char);
1796 state->hedge = snewn(area, unsigned char);
2ac6d24e 1797 state->completed = state->cheated = FALSE;
3870c4d8 1798
1799 i = 0;
1185e3c5 1800 while (*desc) {
1801 int n = *desc++;
3870c4d8 1802 if (n >= 'a' && n <= 'z') {
1803 int run = n - 'a' + 1;
1804 assert(i + run <= area);
1805 while (run-- > 0)
1806 state->grid[i++] = 0;
1807 } else if (n == '_') {
1808 /* do nothing */;
1809 } else if (n > '0' && n <= '9') {
1810 assert(i < area);
1185e3c5 1811 state->grid[i++] = atoi(desc-1);
1812 while (*desc >= '0' && *desc <= '9')
1813 desc++;
3870c4d8 1814 } else {
1815 assert(!"We can't get here");
1816 }
1817 }
1818 assert(i == area);
1819
1820 for (y = 0; y < state->h; y++)
1821 for (x = 0; x < state->w; x++)
1822 vedge(state,x,y) = hedge(state,x,y) = 0;
1823
1824 return state;
1825}
1826
be8d5aa1 1827static game_state *dup_game(game_state *state)
3870c4d8 1828{
1829 game_state *ret = snew(game_state);
1830
1831 ret->w = state->w;
1832 ret->h = state->h;
1833
1834 ret->vedge = snewn(state->w * state->h, unsigned char);
1835 ret->hedge = snewn(state->w * state->h, unsigned char);
1836 ret->grid = snewn(state->w * state->h, int);
1837
ef29354c 1838 ret->completed = state->completed;
2ac6d24e 1839 ret->cheated = state->cheated;
ef29354c 1840
3870c4d8 1841 memcpy(ret->grid, state->grid, state->w * state->h * sizeof(int));
1842 memcpy(ret->vedge, state->vedge, state->w*state->h*sizeof(unsigned char));
1843 memcpy(ret->hedge, state->hedge, state->w*state->h*sizeof(unsigned char));
1844
1845 return ret;
1846}
1847
be8d5aa1 1848static void free_game(game_state *state)
3870c4d8 1849{
1850 sfree(state->grid);
1851 sfree(state->vedge);
1852 sfree(state->hedge);
1853 sfree(state);
1854}
1855
df11cd4e 1856static char *solve_game(game_state *state, game_state *currstate,
1857 game_aux_info *ai, char **error)
2ac6d24e 1858{
df11cd4e 1859 unsigned char *vedge, *hedge;
1860 int edges_need_freeing;
1861 int x, y, len;
1862 char *ret, *p;
2ac6d24e 1863
1864 if (!ai) {
1507058f 1865 int i, j, n;
1866 struct numberdata *nd;
1867
1868 /*
1869 * Attempt the in-built solver.
1870 */
1871
1872 /* Set up each number's (very short) candidate position list. */
1873 for (i = n = 0; i < state->h * state->w; i++)
1874 if (state->grid[i])
1875 n++;
1876
1877 nd = snewn(n, struct numberdata);
1878
1879 for (i = j = 0; i < state->h * state->w; i++)
1880 if (state->grid[i]) {
1881 nd[j].area = state->grid[i];
1882 nd[j].npoints = 1;
1883 nd[j].points = snewn(1, struct point);
1884 nd[j].points[0].x = i % state->w;
1885 nd[j].points[0].y = i / state->w;
1886 j++;
1887 }
1888
1889 assert(j == n);
1890
df11cd4e 1891 vedge = snewn(state->w * state->h, unsigned char);
1892 hedge = snewn(state->w * state->h, unsigned char);
1893 memset(vedge, 0, state->w * state->h);
1894 memset(hedge, 0, state->w * state->h);
1895 edges_need_freeing = TRUE;
1507058f 1896
df11cd4e 1897 rect_solver(state->w, state->h, n, nd, hedge, vedge, NULL);
1507058f 1898
1899 /*
1900 * Clean up.
1901 */
1902 for (i = 0; i < n; i++)
1903 sfree(nd[i].points);
1904 sfree(nd);
df11cd4e 1905 } else {
1906 assert(state->w == ai->w);
1907 assert(state->h == ai->h);
1908 vedge = ai->vedge;
1909 hedge = ai->hedge;
1910 edges_need_freeing = FALSE;
2ac6d24e 1911 }
1912
df11cd4e 1913 len = 2 + (state->w-1)*state->h + (state->h-1)*state->w;
1914 ret = snewn(len, char);
1915
1916 p = ret;
1917 *p++ = 'S';
1918 for (y = 0; y < state->h; y++)
1919 for (x = 1; x < state->w; x++)
1920 *p++ = vedge[y*state->w+x] ? '1' : '0';
1921 for (y = 1; y < state->h; y++)
1922 for (x = 0; x < state->w; x++)
1923 *p++ = hedge[y*state->w+x] ? '1' : '0';
1924 *p++ = '\0';
1925 assert(p - ret == len);
2ac6d24e 1926
df11cd4e 1927 if (edges_need_freeing) {
1928 sfree(vedge);
1929 sfree(hedge);
1930 }
2ac6d24e 1931
1932 return ret;
1933}
1934
9b4b03d3 1935static char *game_text_format(game_state *state)
1936{
6ad5ed74 1937 char *ret, *p, buf[80];
1938 int i, x, y, col, maxlen;
1939
1940 /*
1941 * First determine the number of spaces required to display a
1942 * number. We'll use at least two, because one looks a bit
1943 * silly.
1944 */
1945 col = 2;
1946 for (i = 0; i < state->w * state->h; i++) {
1947 x = sprintf(buf, "%d", state->grid[i]);
1948 if (col < x) col = x;
1949 }
1950
1951 /*
1952 * Now we know the exact total size of the grid we're going to
1953 * produce: it's got 2*h+1 rows, each containing w lots of col,
1954 * w+1 boundary characters and a trailing newline.
1955 */
1956 maxlen = (2*state->h+1) * (state->w * (col+1) + 2);
1957
48a10826 1958 ret = snewn(maxlen+1, char);
6ad5ed74 1959 p = ret;
1960
1961 for (y = 0; y <= 2*state->h; y++) {
1962 for (x = 0; x <= 2*state->w; x++) {
1963 if (x & y & 1) {
1964 /*
1965 * Display a number.
1966 */
1967 int v = grid(state, x/2, y/2);
1968 if (v)
1969 sprintf(buf, "%*d", col, v);
1970 else
1971 sprintf(buf, "%*s", col, "");
1972 memcpy(p, buf, col);
1973 p += col;
1974 } else if (x & 1) {
1975 /*
1976 * Display a horizontal edge or nothing.
1977 */
1978 int h = (y==0 || y==2*state->h ? 1 :
1979 HRANGE(state, x/2, y/2) && hedge(state, x/2, y/2));
1980 int i;
1981 if (h)
1982 h = '-';
1983 else
1984 h = ' ';
1985 for (i = 0; i < col; i++)
1986 *p++ = h;
1987 } else if (y & 1) {
1988 /*
1989 * Display a vertical edge or nothing.
1990 */
1991 int v = (x==0 || x==2*state->w ? 1 :
1992 VRANGE(state, x/2, y/2) && vedge(state, x/2, y/2));
1993 if (v)
1994 *p++ = '|';
1995 else
1996 *p++ = ' ';
1997 } else {
1998 /*
1999 * Display a corner, or a vertical edge, or a
2000 * horizontal edge, or nothing.
2001 */
2002 int hl = (y==0 || y==2*state->h ? 1 :
2003 HRANGE(state, (x-1)/2, y/2) && hedge(state, (x-1)/2, y/2));
2004 int hr = (y==0 || y==2*state->h ? 1 :
2005 HRANGE(state, (x+1)/2, y/2) && hedge(state, (x+1)/2, y/2));
2006 int vu = (x==0 || x==2*state->w ? 1 :
2007 VRANGE(state, x/2, (y-1)/2) && vedge(state, x/2, (y-1)/2));
2008 int vd = (x==0 || x==2*state->w ? 1 :
2009 VRANGE(state, x/2, (y+1)/2) && vedge(state, x/2, (y+1)/2));
2010 if (!hl && !hr && !vu && !vd)
2011 *p++ = ' ';
2012 else if (hl && hr && !vu && !vd)
2013 *p++ = '-';
2014 else if (!hl && !hr && vu && vd)
2015 *p++ = '|';
2016 else
2017 *p++ = '+';
2018 }
2019 }
2020 *p++ = '\n';
2021 }
2022
2023 assert(p - ret == maxlen);
2024 *p = '\0';
2025 return ret;
9b4b03d3 2026}
2027
3870c4d8 2028static unsigned char *get_correct(game_state *state)
2029{
2030 unsigned char *ret;
2031 int x, y;
2032
2033 ret = snewn(state->w * state->h, unsigned char);
2034 memset(ret, 0xFF, state->w * state->h);
2035
2036 for (x = 0; x < state->w; x++)
2037 for (y = 0; y < state->h; y++)
2038 if (index(state,ret,x,y) == 0xFF) {
2039 int rw, rh;
2040 int xx, yy;
2041 int num, area, valid;
2042
2043 /*
2044 * Find a rectangle starting at this point.
2045 */
2046 rw = 1;
2047 while (x+rw < state->w && !vedge(state,x+rw,y))
2048 rw++;
2049 rh = 1;
2050 while (y+rh < state->h && !hedge(state,x,y+rh))
2051 rh++;
2052
2053 /*
2054 * We know what the dimensions of the rectangle
2055 * should be if it's there at all. Find out if we
2056 * really have a valid rectangle.
2057 */
2058 valid = TRUE;
2059 /* Check the horizontal edges. */
2060 for (xx = x; xx < x+rw; xx++) {
2061 for (yy = y; yy <= y+rh; yy++) {
2062 int e = !HRANGE(state,xx,yy) || hedge(state,xx,yy);
2063 int ec = (yy == y || yy == y+rh);
2064 if (e != ec)
2065 valid = FALSE;
2066 }
2067 }
2068 /* Check the vertical edges. */
2069 for (yy = y; yy < y+rh; yy++) {
2070 for (xx = x; xx <= x+rw; xx++) {
2071 int e = !VRANGE(state,xx,yy) || vedge(state,xx,yy);
2072 int ec = (xx == x || xx == x+rw);
2073 if (e != ec)
2074 valid = FALSE;
2075 }
2076 }
2077
2078 /*
2079 * If this is not a valid rectangle with no other
2080 * edges inside it, we just mark this square as not
2081 * complete and proceed to the next square.
2082 */
2083 if (!valid) {
2084 index(state, ret, x, y) = 0;
2085 continue;
2086 }
2087
2088 /*
2089 * We have a rectangle. Now see what its area is,
2090 * and how many numbers are in it.
2091 */
2092 num = 0;
2093 area = 0;
2094 for (xx = x; xx < x+rw; xx++) {
2095 for (yy = y; yy < y+rh; yy++) {
2096 area++;
2097 if (grid(state,xx,yy)) {
2098 if (num > 0)
2099 valid = FALSE; /* two numbers */
2100 num = grid(state,xx,yy);
2101 }
2102 }
2103 }
2104 if (num != area)
2105 valid = FALSE;
2106
2107 /*
2108 * Now fill in the whole rectangle based on the
2109 * value of `valid'.
2110 */
2111 for (xx = x; xx < x+rw; xx++) {
2112 for (yy = y; yy < y+rh; yy++) {
2113 index(state, ret, xx, yy) = valid;
2114 }
2115 }
2116 }
2117
2118 return ret;
2119}
2120
08dd70c3 2121struct game_ui {
2122 /*
2123 * These coordinates are 2 times the obvious grid coordinates.
2124 * Hence, the top left of the grid is (0,0), the grid point to
2125 * the right of that is (2,0), the one _below that_ is (2,2)
2126 * and so on. This is so that we can specify a drag start point
2127 * on an edge (one odd coordinate) or in the middle of a square
2128 * (two odd coordinates) rather than always at a corner.
2129 *
2130 * -1,-1 means no drag is in progress.
2131 */
2132 int drag_start_x;
2133 int drag_start_y;
2134 int drag_end_x;
2135 int drag_end_y;
2136 /*
2137 * This flag is set as soon as a dragging action moves the
2138 * mouse pointer away from its starting point, so that even if
2139 * the pointer _returns_ to its starting point the action is
2140 * treated as a small drag rather than a click.
2141 */
2142 int dragged;
375c9b4d 2143 /*
2144 * These are the co-ordinates of the top-left and bottom-right squares
2145 * in the drag box, respectively, or -1 otherwise.
2146 */
2147 int x1;
2148 int y1;
2149 int x2;
2150 int y2;
08dd70c3 2151};
2152
be8d5aa1 2153static game_ui *new_ui(game_state *state)
74a4e547 2154{
08dd70c3 2155 game_ui *ui = snew(game_ui);
2156 ui->drag_start_x = -1;
2157 ui->drag_start_y = -1;
2158 ui->drag_end_x = -1;
2159 ui->drag_end_y = -1;
2160 ui->dragged = FALSE;
375c9b4d 2161 ui->x1 = -1;
2162 ui->y1 = -1;
2163 ui->x2 = -1;
2164 ui->y2 = -1;
08dd70c3 2165 return ui;
74a4e547 2166}
2167
be8d5aa1 2168static void free_ui(game_ui *ui)
74a4e547 2169{
08dd70c3 2170 sfree(ui);
2171}
2172
ae8290c6 2173char *encode_ui(game_ui *ui)
2174{
2175 return NULL;
2176}
2177
2178void decode_ui(game_ui *ui, char *encoding)
2179{
2180}
2181
be8d5aa1 2182static void coord_round(float x, float y, int *xr, int *yr)
08dd70c3 2183{
d4e7900f 2184 float xs, ys, xv, yv, dx, dy, dist;
08dd70c3 2185
2186 /*
d4e7900f 2187 * Find the nearest square-centre.
08dd70c3 2188 */
d4e7900f 2189 xs = (float)floor(x) + 0.5F;
2190 ys = (float)floor(y) + 0.5F;
08dd70c3 2191
2192 /*
d4e7900f 2193 * And find the nearest grid vertex.
08dd70c3 2194 */
d4e7900f 2195 xv = (float)floor(x + 0.5F);
2196 yv = (float)floor(y + 0.5F);
08dd70c3 2197
2198 /*
d4e7900f 2199 * We allocate clicks in parts of the grid square to either
2200 * corners, edges or square centres, as follows:
2201 *
2202 * +--+--------+--+
2203 * | | | |
2204 * +--+ +--+
2205 * | `. ,' |
2206 * | +--+ |
2207 * | | | |
2208 * | +--+ |
2209 * | ,' `. |
2210 * +--+ +--+
2211 * | | | |
2212 * +--+--------+--+
2213 *
2214 * (Not to scale!)
2215 *
2216 * In other words: we measure the square distance (i.e.
2217 * max(dx,dy)) from the click to the nearest corner, and if
2218 * it's within CORNER_TOLERANCE then we return a corner click.
2219 * We measure the square distance from the click to the nearest
2220 * centre, and if that's within CENTRE_TOLERANCE we return a
2221 * centre click. Failing that, we find which of the two edge
2222 * centres is nearer to the click and return that edge.
08dd70c3 2223 */
d4e7900f 2224
2225 /*
2226 * Check for corner click.
2227 */
2228 dx = (float)fabs(x - xv);
2229 dy = (float)fabs(y - yv);
2230 dist = (dx > dy ? dx : dy);
2231 if (dist < CORNER_TOLERANCE) {
2232 *xr = 2 * (int)xv;
2233 *yr = 2 * (int)yv;
2234 } else {
2235 /*
2236 * Check for centre click.
2237 */
2238 dx = (float)fabs(x - xs);
2239 dy = (float)fabs(y - ys);
2240 dist = (dx > dy ? dx : dy);
2241 if (dist < CENTRE_TOLERANCE) {
2242 *xr = 1 + 2 * (int)xs;
2243 *yr = 1 + 2 * (int)ys;
2244 } else {
2245 /*
2246 * Failing both of those, see which edge we're closer to.
2247 * Conveniently, this is simply done by testing the relative
2248 * magnitude of dx and dy (which are currently distances from
2249 * the square centre).
2250 */
2251 if (dx > dy) {
2252 /* Vertical edge: x-coord of corner,
2253 * y-coord of square centre. */
2254 *xr = 2 * (int)xv;
ee03cb5f 2255 *yr = 1 + 2 * (int)floor(ys);
d4e7900f 2256 } else {
2257 /* Horizontal edge: x-coord of square centre,
2258 * y-coord of corner. */
ee03cb5f 2259 *xr = 1 + 2 * (int)floor(xs);
d4e7900f 2260 *yr = 2 * (int)yv;
2261 }
2262 }
2263 }
08dd70c3 2264}
2265
df11cd4e 2266/*
2267 * Returns TRUE if it has made any change to the grid.
2268 */
2269static int grid_draw_rect(game_state *state,
2270 unsigned char *hedge, unsigned char *vedge,
2271 int c, int really,
2272 int x1, int y1, int x2, int y2)
08dd70c3 2273{
375c9b4d 2274 int x, y;
df11cd4e 2275 int changed = FALSE;
08dd70c3 2276
2277 /*
2278 * Draw horizontal edges of rectangles.
2279 */
2280 for (x = x1; x < x2; x++)
2281 for (y = y1; y <= y2; y++)
2282 if (HRANGE(state,x,y)) {
2283 int val = index(state,hedge,x,y);
2284 if (y == y1 || y == y2)
2285 val = c;
2286 else if (c == 1)
2287 val = 0;
df11cd4e 2288 changed = changed || (index(state,hedge,x,y) != val);
2289 if (really)
2290 index(state,hedge,x,y) = val;
08dd70c3 2291 }
2292
2293 /*
2294 * Draw vertical edges of rectangles.
2295 */
2296 for (y = y1; y < y2; y++)
2297 for (x = x1; x <= x2; x++)
2298 if (VRANGE(state,x,y)) {
2299 int val = index(state,vedge,x,y);
2300 if (x == x1 || x == x2)
2301 val = c;
2302 else if (c == 1)
2303 val = 0;
df11cd4e 2304 changed = changed || (index(state,vedge,x,y) != val);
2305 if (really)
2306 index(state,vedge,x,y) = val;
08dd70c3 2307 }
df11cd4e 2308
2309 return changed;
2310}
2311
2312static int ui_draw_rect(game_state *state, game_ui *ui,
2313 unsigned char *hedge, unsigned char *vedge, int c,
2314 int really)
2315{
2316 return grid_draw_rect(state, hedge, vedge, c, really,
2317 ui->x1, ui->y1, ui->x2, ui->y2);
74a4e547 2318}
2319
07dfb697 2320static void game_changed_state(game_ui *ui, game_state *oldstate,
2321 game_state *newstate)
2322{
2323}
2324
1e3e152d 2325struct game_drawstate {
2326 int started;
2327 int w, h, tilesize;
2328 unsigned long *visible;
2329};
2330
df11cd4e 2331static char *interpret_move(game_state *from, game_ui *ui, game_drawstate *ds,
2332 int x, int y, int button)
2333{
08dd70c3 2334 int xc, yc;
2335 int startdrag = FALSE, enddrag = FALSE, active = FALSE;
df11cd4e 2336 char buf[80], *ret;
3870c4d8 2337
f0ee053c 2338 button &= ~MOD_MASK;
2339
08dd70c3 2340 if (button == LEFT_BUTTON) {
2341 startdrag = TRUE;
2342 } else if (button == LEFT_RELEASE) {
2343 enddrag = TRUE;
2344 } else if (button != LEFT_DRAG) {
2345 return NULL;
2346 }
2347
d4e7900f 2348 coord_round(FROMCOORD((float)x), FROMCOORD((float)y), &xc, &yc);
08dd70c3 2349
2350 if (startdrag) {
2351 ui->drag_start_x = xc;
2352 ui->drag_start_y = yc;
2353 ui->drag_end_x = xc;
2354 ui->drag_end_y = yc;
2355 ui->dragged = FALSE;
2356 active = TRUE;
2357 }
3870c4d8 2358
08dd70c3 2359 if (xc != ui->drag_end_x || yc != ui->drag_end_y) {
375c9b4d 2360 int t;
2361
08dd70c3 2362 ui->drag_end_x = xc;
2363 ui->drag_end_y = yc;
2364 ui->dragged = TRUE;
2365 active = TRUE;
375c9b4d 2366
ee03cb5f 2367 if (xc >= 0 && xc <= 2*from->w &&
2368 yc >= 0 && yc <= 2*from->h) {
2369 ui->x1 = ui->drag_start_x;
2370 ui->x2 = ui->drag_end_x;
2371 if (ui->x2 < ui->x1) { t = ui->x1; ui->x1 = ui->x2; ui->x2 = t; }
2372
2373 ui->y1 = ui->drag_start_y;
2374 ui->y2 = ui->drag_end_y;
2375 if (ui->y2 < ui->y1) { t = ui->y1; ui->y1 = ui->y2; ui->y2 = t; }
2376
2377 ui->x1 = ui->x1 / 2; /* rounds down */
2378 ui->x2 = (ui->x2+1) / 2; /* rounds up */
2379 ui->y1 = ui->y1 / 2; /* rounds down */
2380 ui->y2 = (ui->y2+1) / 2; /* rounds up */
2381 } else {
2382 ui->x1 = -1;
2383 ui->y1 = -1;
2384 ui->x2 = -1;
2385 ui->y2 = -1;
2386 }
08dd70c3 2387 }
3870c4d8 2388
934797c7 2389 ret = NULL;
2390
2391 if (enddrag) {
2392 if (xc >= 0 && xc <= 2*from->w &&
2393 yc >= 0 && yc <= 2*from->h) {
934797c7 2394
2395 if (ui->dragged) {
df11cd4e 2396 if (ui_draw_rect(from, ui, from->hedge,
2397 from->vedge, 1, FALSE)) {
2398 sprintf(buf, "R%d,%d,%d,%d",
2399 ui->x1, ui->y1, ui->x2 - ui->x1, ui->y2 - ui->y1);
2400 ret = dupstr(buf);
2401 }
934797c7 2402 } else {
2403 if ((xc & 1) && !(yc & 1) && HRANGE(from,xc/2,yc/2)) {
df11cd4e 2404 sprintf(buf, "H%d,%d", xc/2, yc/2);
2405 ret = dupstr(buf);
934797c7 2406 }
2407 if ((yc & 1) && !(xc & 1) && VRANGE(from,xc/2,yc/2)) {
df11cd4e 2408 sprintf(buf, "V%d,%d", xc/2, yc/2);
2409 ret = dupstr(buf);
934797c7 2410 }
2411 }
934797c7 2412 }
2413
2414 ui->drag_start_x = -1;
2415 ui->drag_start_y = -1;
2416 ui->drag_end_x = -1;
2417 ui->drag_end_y = -1;
375c9b4d 2418 ui->x1 = -1;
2419 ui->y1 = -1;
2420 ui->x2 = -1;
2421 ui->y2 = -1;
934797c7 2422 ui->dragged = FALSE;
2423 active = TRUE;
3870c4d8 2424 }
2425
934797c7 2426 if (ret)
2427 return ret; /* a move has been made */
2428 else if (active)
df11cd4e 2429 return ""; /* UI activity has occurred */
934797c7 2430 else
2431 return NULL;
3870c4d8 2432}
2433
df11cd4e 2434static game_state *execute_move(game_state *from, char *move)
2435{
2436 game_state *ret;
2437 int x1, y1, x2, y2, mode;
2438
2439 if (move[0] == 'S') {
2440 char *p = move+1;
2441 int x, y;
2442
2443 ret = dup_game(from);
2444 ret->cheated = TRUE;
2445
2446 for (y = 0; y < ret->h; y++)
2447 for (x = 1; x < ret->w; x++) {
2448 vedge(ret, x, y) = (*p == '1');
2449 if (*p) p++;
2450 }
2451 for (y = 1; y < ret->h; y++)
2452 for (x = 0; x < ret->w; x++) {
2453 hedge(ret, x, y) = (*p == '1');
2454 if (*p) p++;
2455 }
2456
2457 return ret;
2458
2459 } else if (move[0] == 'R' &&
2460 sscanf(move+1, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) == 4 &&
2461 x1 >= 0 && x2 >= 0 && x1+x2 <= from->w &&
2462 y1 >= 0 && y2 >= 0 && y1+y2 <= from->h) {
2463 x2 += x1;
2464 y2 += y1;
2465 mode = move[0];
2466 } else if ((move[0] == 'H' || move[0] == 'V') &&
2467 sscanf(move+1, "%d,%d", &x1, &y1) == 2 &&
2468 (move[0] == 'H' ? HRANGE(from, x1, y1) :
2469 VRANGE(from, x1, y1))) {
2470 mode = move[0];
2471 } else
2472 return NULL; /* can't parse move string */
2473
2474 ret = dup_game(from);
2475
2476 if (mode == 'R') {
2477 grid_draw_rect(ret, ret->hedge, ret->vedge, 1, TRUE, x1, y1, x2, y2);
2478 } else if (mode == 'H') {
2479 hedge(ret,x1,y1) = !hedge(ret,x1,y1);
2480 } else if (mode == 'V') {
2481 vedge(ret,x1,y1) = !vedge(ret,x1,y1);
2482 }
2483
2484 /*
2485 * We've made a real change to the grid. Check to see
2486 * if the game has been completed.
2487 */
2488 if (!ret->completed) {
2489 int x, y, ok;
2490 unsigned char *correct = get_correct(ret);
2491
2492 ok = TRUE;
2493 for (x = 0; x < ret->w; x++)
2494 for (y = 0; y < ret->h; y++)
2495 if (!index(ret, correct, x, y))
2496 ok = FALSE;
2497
2498 sfree(correct);
2499
2500 if (ok)
2501 ret->completed = TRUE;
2502 }
2503
2504 return ret;
2505}
2506
3870c4d8 2507/* ----------------------------------------------------------------------
2508 * Drawing routines.
2509 */
2510
ab53eb64 2511#define CORRECT (1L<<16)
08dd70c3 2512
2513#define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG )
ab53eb64 2514#define MAX4(x,y,z,w) ( max(max(x,y),max(z,w)) )
3870c4d8 2515
1e3e152d 2516static void game_size(game_params *params, game_drawstate *ds,
2517 int *x, int *y, int expand)
3870c4d8 2518{
1e3e152d 2519 int tsx, tsy, ts;
2520 /*
2521 * Each window dimension equals the tile size times 1.5 more
2522 * than the grid dimension (the border is 3/4 the width of the
2523 * tiles).
a65c0e54 2524 *
2525 * We must cast to unsigned before multiplying by two, because
2526 * *x might be INT_MAX.
1e3e152d 2527 */
a65c0e54 2528 tsx = 2 * (unsigned)*x / (2 * params->w + 3);
2529 tsy = 2 * (unsigned)*y / (2 * params->h + 3);
1e3e152d 2530 ts = min(tsx, tsy);
2531 if (expand)
2532 ds->tilesize = ts;
2533 else
2534 ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
2535
3870c4d8 2536 *x = params->w * TILE_SIZE + 2*BORDER + 1;
2537 *y = params->h * TILE_SIZE + 2*BORDER + 1;
2538}
2539
be8d5aa1 2540static float *game_colours(frontend *fe, game_state *state, int *ncolours)
3870c4d8 2541{
2542 float *ret = snewn(3 * NCOLOURS, float);
2543
2544 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2545
2546 ret[COL_GRID * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
2547 ret[COL_GRID * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
2548 ret[COL_GRID * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
2549
08dd70c3 2550 ret[COL_DRAG * 3 + 0] = 1.0F;
2551 ret[COL_DRAG * 3 + 1] = 0.0F;
2552 ret[COL_DRAG * 3 + 2] = 0.0F;
2553
3870c4d8 2554 ret[COL_CORRECT * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
2555 ret[COL_CORRECT * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
2556 ret[COL_CORRECT * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
2557
2558 ret[COL_LINE * 3 + 0] = 0.0F;
2559 ret[COL_LINE * 3 + 1] = 0.0F;
2560 ret[COL_LINE * 3 + 2] = 0.0F;
2561
2562 ret[COL_TEXT * 3 + 0] = 0.0F;
2563 ret[COL_TEXT * 3 + 1] = 0.0F;
2564 ret[COL_TEXT * 3 + 2] = 0.0F;
2565
2566 *ncolours = NCOLOURS;
2567 return ret;
2568}
2569
be8d5aa1 2570static game_drawstate *game_new_drawstate(game_state *state)
3870c4d8 2571{
2572 struct game_drawstate *ds = snew(struct game_drawstate);
08dd70c3 2573 int i;
3870c4d8 2574
2575 ds->started = FALSE;
2576 ds->w = state->w;
2577 ds->h = state->h;
ab53eb64 2578 ds->visible = snewn(ds->w * ds->h, unsigned long);
1e3e152d 2579 ds->tilesize = 0; /* not decided yet */
08dd70c3 2580 for (i = 0; i < ds->w * ds->h; i++)
2581 ds->visible[i] = 0xFFFF;
3870c4d8 2582
2583 return ds;
2584}
2585
be8d5aa1 2586static void game_free_drawstate(game_drawstate *ds)
3870c4d8 2587{
2588 sfree(ds->visible);
2589 sfree(ds);
2590}
2591
1e3e152d 2592static void draw_tile(frontend *fe, game_drawstate *ds, game_state *state,
2593 int x, int y, unsigned char *hedge, unsigned char *vedge,
2594 unsigned char *corners, int correct)
3870c4d8 2595{
2596 int cx = COORD(x), cy = COORD(y);
2597 char str[80];
2598
2599 draw_rect(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1, COL_GRID);
2600 draw_rect(fe, cx+1, cy+1, TILE_SIZE-1, TILE_SIZE-1,
2601 correct ? COL_CORRECT : COL_BACKGROUND);
2602
2603 if (grid(state,x,y)) {
2604 sprintf(str, "%d", grid(state,x,y));
2605 draw_text(fe, cx+TILE_SIZE/2, cy+TILE_SIZE/2, FONT_VARIABLE,
105a00d0 2606 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, COL_TEXT, str);
3870c4d8 2607 }
2608
2609 /*
2610 * Draw edges.
2611 */
08dd70c3 2612 if (!HRANGE(state,x,y) || index(state,hedge,x,y))
2613 draw_rect(fe, cx, cy, TILE_SIZE+1, 2,
2614 HRANGE(state,x,y) ? COLOUR(index(state,hedge,x,y)) :
2615 COL_LINE);
2616 if (!HRANGE(state,x,y+1) || index(state,hedge,x,y+1))
2617 draw_rect(fe, cx, cy+TILE_SIZE-1, TILE_SIZE+1, 2,
2618 HRANGE(state,x,y+1) ? COLOUR(index(state,hedge,x,y+1)) :
2619 COL_LINE);
2620 if (!VRANGE(state,x,y) || index(state,vedge,x,y))
2621 draw_rect(fe, cx, cy, 2, TILE_SIZE+1,
2622 VRANGE(state,x,y) ? COLOUR(index(state,vedge,x,y)) :
2623 COL_LINE);
2624 if (!VRANGE(state,x+1,y) || index(state,vedge,x+1,y))
2625 draw_rect(fe, cx+TILE_SIZE-1, cy, 2, TILE_SIZE+1,
2626 VRANGE(state,x+1,y) ? COLOUR(index(state,vedge,x+1,y)) :
2627 COL_LINE);
3870c4d8 2628
2629 /*
2630 * Draw corners.
2631 */
ec9a0f09 2632 if (index(state,corners,x,y))
08dd70c3 2633 draw_rect(fe, cx, cy, 2, 2,
ec9a0f09 2634 COLOUR(index(state,corners,x,y)));
2635 if (x+1 < state->w && index(state,corners,x+1,y))
08dd70c3 2636 draw_rect(fe, cx+TILE_SIZE-1, cy, 2, 2,
ec9a0f09 2637 COLOUR(index(state,corners,x+1,y)));
2638 if (y+1 < state->h && index(state,corners,x,y+1))
08dd70c3 2639 draw_rect(fe, cx, cy+TILE_SIZE-1, 2, 2,
ec9a0f09 2640 COLOUR(index(state,corners,x,y+1)));
2641 if (x+1 < state->w && y+1 < state->h && index(state,corners,x+1,y+1))
08dd70c3 2642 draw_rect(fe, cx+TILE_SIZE-1, cy+TILE_SIZE-1, 2, 2,
ec9a0f09 2643 COLOUR(index(state,corners,x+1,y+1)));
3870c4d8 2644
2645 draw_update(fe, cx, cy, TILE_SIZE+1, TILE_SIZE+1);
2646}
2647
be8d5aa1 2648static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
c822de4a 2649 game_state *state, int dir, game_ui *ui,
74a4e547 2650 float animtime, float flashtime)
3870c4d8 2651{
2652 int x, y;
2653 unsigned char *correct;
ec9a0f09 2654 unsigned char *hedge, *vedge, *corners;
3870c4d8 2655
2656 correct = get_correct(state);
2657
08dd70c3 2658 if (ui->dragged) {
2659 hedge = snewn(state->w*state->h, unsigned char);
2660 vedge = snewn(state->w*state->h, unsigned char);
2661 memcpy(hedge, state->hedge, state->w*state->h);
2662 memcpy(vedge, state->vedge, state->w*state->h);
df11cd4e 2663 ui_draw_rect(state, ui, hedge, vedge, 2, TRUE);
08dd70c3 2664 } else {
2665 hedge = state->hedge;
2666 vedge = state->vedge;
2667 }
2668
ec9a0f09 2669 corners = snewn(state->w * state->h, unsigned char);
2670 memset(corners, 0, state->w * state->h);
2671 for (x = 0; x < state->w; x++)
2672 for (y = 0; y < state->h; y++) {
2673 if (x > 0) {
2674 int e = index(state, vedge, x, y);
2675 if (index(state,corners,x,y) < e)
2676 index(state,corners,x,y) = e;
2677 if (y+1 < state->h &&
2678 index(state,corners,x,y+1) < e)
2679 index(state,corners,x,y+1) = e;
2680 }
2681 if (y > 0) {
2682 int e = index(state, hedge, x, y);
2683 if (index(state,corners,x,y) < e)
2684 index(state,corners,x,y) = e;
2685 if (x+1 < state->w &&
2686 index(state,corners,x+1,y) < e)
2687 index(state,corners,x+1,y) = e;
2688 }
2689 }
2690
3870c4d8 2691 if (!ds->started) {
105a00d0 2692 draw_rect(fe, 0, 0,
2693 state->w * TILE_SIZE + 2*BORDER + 1,
2694 state->h * TILE_SIZE + 2*BORDER + 1, COL_BACKGROUND);
3870c4d8 2695 draw_rect(fe, COORD(0)-1, COORD(0)-1,
2696 ds->w*TILE_SIZE+3, ds->h*TILE_SIZE+3, COL_LINE);
2697 ds->started = TRUE;
863c3945 2698 draw_update(fe, 0, 0,
2699 state->w * TILE_SIZE + 2*BORDER + 1,
2700 state->h * TILE_SIZE + 2*BORDER + 1);
3870c4d8 2701 }
2702
2703 for (x = 0; x < state->w; x++)
2704 for (y = 0; y < state->h; y++) {
ab53eb64 2705 unsigned long c = 0;
08dd70c3 2706
2707 if (HRANGE(state,x,y))
2708 c |= index(state,hedge,x,y);
eddb22e8 2709 if (HRANGE(state,x,y+1))
2710 c |= index(state,hedge,x,y+1) << 2;
08dd70c3 2711 if (VRANGE(state,x,y))
2712 c |= index(state,vedge,x,y) << 4;
eddb22e8 2713 if (VRANGE(state,x+1,y))
2714 c |= index(state,vedge,x+1,y) << 6;
ec9a0f09 2715 c |= index(state,corners,x,y) << 8;
2716 if (x+1 < state->w)
2717 c |= index(state,corners,x+1,y) << 10;
2718 if (y+1 < state->h)
2719 c |= index(state,corners,x,y+1) << 12;
2720 if (x+1 < state->w && y+1 < state->h)
ab53eb64 2721 /* cast to prevent 2<<14 sign-extending on promotion to long */
2722 c |= (unsigned long)index(state,corners,x+1,y+1) << 14;
ef29354c 2723 if (index(state, correct, x, y) && !flashtime)
3870c4d8 2724 c |= CORRECT;
2725
2726 if (index(ds,ds->visible,x,y) != c) {
1e3e152d 2727 draw_tile(fe, ds, state, x, y, hedge, vedge, corners,
ab53eb64 2728 (c & CORRECT) ? 1 : 0);
ec9a0f09 2729 index(ds,ds->visible,x,y) = c;
3870c4d8 2730 }
2731 }
2732
375c9b4d 2733 {
2734 char buf[256];
2735
2736 if (ui->x1 >= 0 && ui->y1 >= 0 &&
2737 ui->x2 >= 0 && ui->y2 >= 0) {
2738 sprintf(buf, "%dx%d ",
2739 ui->x2-ui->x1,
2740 ui->y2-ui->y1);
2741 } else {
2742 buf[0] = '\0';
2743 }
2744
2745 if (state->cheated)
2746 strcat(buf, "Auto-solved.");
2747 else if (state->completed)
2748 strcat(buf, "COMPLETED!");
2749
2750 status_bar(fe, buf);
2751 }
2752
08dd70c3 2753 if (hedge != state->hedge) {
2754 sfree(hedge);
2755 sfree(vedge);
375c9b4d 2756 }
08dd70c3 2757
11c44cf5 2758 sfree(corners);
3870c4d8 2759 sfree(correct);
2760}
2761
be8d5aa1 2762static float game_anim_length(game_state *oldstate,
e3f21163 2763 game_state *newstate, int dir, game_ui *ui)
3870c4d8 2764{
2765 return 0.0F;
2766}
2767
be8d5aa1 2768static float game_flash_length(game_state *oldstate,
e3f21163 2769 game_state *newstate, int dir, game_ui *ui)
3870c4d8 2770{
2ac6d24e 2771 if (!oldstate->completed && newstate->completed &&
2772 !oldstate->cheated && !newstate->cheated)
ef29354c 2773 return FLASH_TIME;
3870c4d8 2774 return 0.0F;
2775}
2776
be8d5aa1 2777static int game_wants_statusbar(void)
3870c4d8 2778{
375c9b4d 2779 return TRUE;
3870c4d8 2780}
be8d5aa1 2781
48dcdd62 2782static int game_timing_state(game_state *state)
2783{
2784 return TRUE;
2785}
2786
be8d5aa1 2787#ifdef COMBINED
2788#define thegame rect
2789#endif
2790
2791const struct game thegame = {
1d228b10 2792 "Rectangles", "games.rectangles",
be8d5aa1 2793 default_params,
2794 game_fetch_preset,
2795 decode_params,
2796 encode_params,
2797 free_params,
2798 dup_params,
1d228b10 2799 TRUE, game_configure, custom_params,
be8d5aa1 2800 validate_params,
1185e3c5 2801 new_game_desc,
6f2d8d7c 2802 game_free_aux_info,
1185e3c5 2803 validate_desc,
be8d5aa1 2804 new_game,
2805 dup_game,
2806 free_game,
2ac6d24e 2807 TRUE, solve_game,
6ad5ed74 2808 TRUE, game_text_format,
be8d5aa1 2809 new_ui,
2810 free_ui,
ae8290c6 2811 encode_ui,
2812 decode_ui,
07dfb697 2813 game_changed_state,
df11cd4e 2814 interpret_move,
2815 execute_move,
be8d5aa1 2816 game_size,
2817 game_colours,
2818 game_new_drawstate,
2819 game_free_drawstate,
2820 game_redraw,
2821 game_anim_length,
2822 game_flash_length,
2823 game_wants_statusbar,
48dcdd62 2824 FALSE, game_timing_state,
93b1da3d 2825 0, /* mouse_priorities */
be8d5aa1 2826};