2 * rect.c: Puzzle from nikoli.co.jp. You have a square grid with
3 * numbers in some squares; you must divide the square grid up into
4 * variously sized rectangles, such that every rectangle contains
5 * exactly one numbered square and the area of each rectangle is
6 * equal to the number contained in it.
12 * - Improve on singleton removal by making an aesthetic choice
13 * about which of the options to take.
15 * - When doing the 3x3 trick in singleton removal, limit the size
16 * of the generated rectangles in accordance with the max
19 * - It might be interesting to deliberately try to place
20 * numbers so as to reduce alternative solution patterns. I
21 * doubt we can do a perfect job of this, but we can make a
22 * start by, for example, noticing pairs of 2-rects
23 * alongside one another and _not_ putting their numbers at
26 * - If we start by sorting the rectlist in descending order
27 * of area, we might be able to bias our random number
28 * selection to produce a few large rectangles more often
29 * than oodles of small ones? Unsure, but might be worth a
41 const char *const game_name
= "Rectangles";
42 const int game_can_configure
= TRUE
;
58 #define INDEX(state, x, y) (((y) * (state)->w) + (x))
59 #define index(state, a, x, y) ((a) [ INDEX(state,x,y) ])
60 #define grid(state,x,y) index(state, (state)->grid, x, y)
61 #define vedge(state,x,y) index(state, (state)->vedge, x, y)
62 #define hedge(state,x,y) index(state, (state)->hedge, x, y)
64 #define CRANGE(state,x,y,dx,dy) ( (x) >= dx && (x) < (state)->w && \
65 (y) >= dy && (y) < (state)->h )
66 #define RANGE(state,x,y) CRANGE(state,x,y,0,0)
67 #define HRANGE(state,x,y) CRANGE(state,x,y,0,1)
68 #define VRANGE(state,x,y) CRANGE(state,x,y,1,0)
73 #define CORNER_TOLERANCE 0.15F
74 #define CENTRE_TOLERANCE 0.15F
76 #define FLASH_TIME 0.13F
78 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
79 #define FROMCOORD(x) ( ((x) - BORDER) / TILE_SIZE )
83 int *grid
; /* contains the numbers */
84 unsigned char *vedge
; /* (w+1) x h */
85 unsigned char *hedge
; /* w x (h+1) */
89 game_params
*default_params(void)
91 game_params
*ret
= snew(game_params
);
98 int game_fetch_preset(int i
, char **name
, game_params
**params
)
105 case 0: w
= 7, h
= 7; break;
106 case 1: w
= 11, h
= 11; break;
107 case 2: w
= 15, h
= 15; break;
108 case 3: w
= 19, h
= 19; break;
109 default: return FALSE
;
112 sprintf(buf
, "%dx%d", w
, h
);
114 *params
= ret
= snew(game_params
);
120 void free_params(game_params
*params
)
125 game_params
*dup_params(game_params
*params
)
127 game_params
*ret
= snew(game_params
);
128 *ret
= *params
; /* structure copy */
132 config_item
*game_configure(game_params
*params
)
137 ret
= snewn(5, config_item
);
139 ret
[0].name
= "Width";
140 ret
[0].type
= C_STRING
;
141 sprintf(buf
, "%d", params
->w
);
142 ret
[0].sval
= dupstr(buf
);
145 ret
[1].name
= "Height";
146 ret
[1].type
= C_STRING
;
147 sprintf(buf
, "%d", params
->h
);
148 ret
[1].sval
= dupstr(buf
);
159 game_params
*custom_params(config_item
*cfg
)
161 game_params
*ret
= snew(game_params
);
163 ret
->w
= atoi(cfg
[0].sval
);
164 ret
->h
= atoi(cfg
[1].sval
);
169 char *validate_params(game_params
*params
)
171 if (params
->w
<= 0 && params
->h
<= 0)
172 return "Width and height must both be greater than zero";
173 if (params
->w
< 2 && params
->h
< 2)
174 return "Grid area must be greater than one";
188 static struct rectlist
*get_rectlist(game_params
*params
, int *grid
)
193 struct rect
*rects
= NULL
;
194 int nrects
= 0, rectsize
= 0;
197 * Maximum rectangle area is 1/6 of total grid size, unless
198 * this means we can't place any rectangles at all in which
199 * case we set it to 2 at minimum.
201 maxarea
= params
->w
* params
->h
/ 6;
205 for (rw
= 1; rw
<= params
->w
; rw
++)
206 for (rh
= 1; rh
<= params
->h
; rh
++) {
207 if (rw
* rh
> maxarea
)
211 for (x
= 0; x
<= params
->w
- rw
; x
++)
212 for (y
= 0; y
<= params
->h
- rh
; y
++) {
213 if (nrects
>= rectsize
) {
214 rectsize
= nrects
+ 256;
215 rects
= sresize(rects
, rectsize
, struct rect
);
220 rects
[nrects
].w
= rw
;
221 rects
[nrects
].h
= rh
;
227 struct rectlist
*ret
;
228 ret
= snew(struct rectlist
);
233 assert(rects
== NULL
); /* hence no need to free */
238 static void free_rectlist(struct rectlist
*list
)
244 static void place_rect(game_params
*params
, int *grid
, struct rect r
)
246 int idx
= INDEX(params
, r
.x
, r
.y
);
249 for (x
= r
.x
; x
< r
.x
+r
.w
; x
++)
250 for (y
= r
.y
; y
< r
.y
+r
.h
; y
++) {
251 index(params
, grid
, x
, y
) = idx
;
253 #ifdef GENERATION_DIAGNOSTICS
254 printf(" placing rectangle at (%d,%d) size %d x %d\n",
259 static struct rect
find_rect(game_params
*params
, int *grid
, int x
, int y
)
265 * Find the top left of the rectangle.
267 idx
= index(params
, grid
, x
, y
);
273 return r
; /* 1x1 singleton here */
280 * Find the width and height of the rectangle.
283 (x
+w
< params
->w
&& index(params
,grid
,x
+w
,y
)==idx
);
286 (y
+h
< params
->h
&& index(params
,grid
,x
,y
+h
)==idx
);
297 #ifdef GENERATION_DIAGNOSTICS
298 static void display_grid(game_params
*params
, int *grid
, int *numbers
)
300 unsigned char *egrid
= snewn((params
->w
*2+3) * (params
->h
*2+3),
302 memset(egrid
, 0, (params
->w
*2+3) * (params
->h
*2+3));
304 int r
= (params
->w
*2+3);
306 for (x
= 0; x
< params
->w
; x
++)
307 for (y
= 0; y
< params
->h
; y
++) {
308 int i
= index(params
, grid
, x
, y
);
309 if (x
== 0 || index(params
, grid
, x
-1, y
) != i
)
310 egrid
[(2*y
+2) * r
+ (2*x
+1)] = 1;
311 if (x
== params
->w
-1 || index(params
, grid
, x
+1, y
) != i
)
312 egrid
[(2*y
+2) * r
+ (2*x
+3)] = 1;
313 if (y
== 0 || index(params
, grid
, x
, y
-1) != i
)
314 egrid
[(2*y
+1) * r
+ (2*x
+2)] = 1;
315 if (y
== params
->h
-1 || index(params
, grid
, x
, y
+1) != i
)
316 egrid
[(2*y
+3) * r
+ (2*x
+2)] = 1;
319 for (y
= 1; y
< 2*params
->h
+2; y
++) {
320 for (x
= 1; x
< 2*params
->w
+2; x
++) {
322 int k
= index(params
, numbers
, x
/2-1, y
/2-1);
323 if (k
) printf("%2d", k
); else printf(" ");
324 } else if (!((y
&x
)&1)) {
325 int v
= egrid
[y
*r
+x
];
326 if ((y
&1) && v
) v
= '-';
327 if ((x
&1) && v
) v
= '|';
330 if (!(x
&1)) putchar(v
);
333 if (egrid
[y
*r
+(x
+1)]) d
|= 1;
334 if (egrid
[(y
-1)*r
+x
]) d
|= 2;
335 if (egrid
[y
*r
+(x
-1)]) d
|= 4;
336 if (egrid
[(y
+1)*r
+x
]) d
|= 8;
337 c
= " ??+?-++?+|+++++"[d
];
339 if (!(x
&1)) putchar(c
);
349 char *new_game_seed(game_params
*params
, random_state
*rs
)
352 struct rectlist
*list
;
356 grid
= snewn(params
->w
* params
->h
, int);
357 numbers
= snewn(params
->w
* params
->h
, int);
359 for (y
= 0; y
< params
->h
; y
++)
360 for (x
= 0; x
< params
->w
; x
++) {
361 index(params
, grid
, x
, y
) = -1;
362 index(params
, numbers
, x
, y
) = 0;
365 list
= get_rectlist(params
, grid
);
366 assert(list
!= NULL
);
369 * Place rectangles until we can't any more.
371 while (list
->n
> 0) {
376 * Pick a random rectangle.
378 i
= random_upto(rs
, list
->n
);
384 place_rect(params
, grid
, r
);
387 * Winnow the list by removing any rectangles which
391 for (i
= 0; i
< list
->n
; i
++) {
392 struct rect s
= list
->rects
[i
];
393 if (s
.x
+s
.w
<= r
.x
|| r
.x
+r
.w
<= s
.x
||
394 s
.y
+s
.h
<= r
.y
|| r
.y
+r
.h
<= s
.y
)
395 list
->rects
[m
++] = s
;
403 * Deal with singleton spaces remaining in the grid, one by
406 * We do this by making a local change to the layout. There are
407 * several possibilities:
409 * +-----+-----+ Here, we can remove the singleton by
410 * | | | extending the 1x2 rectangle below it
411 * +--+--+-----+ into a 1x3.
419 * +--+--+--+ Here, that trick doesn't work: there's no
420 * | | | 1 x n rectangle with the singleton at one
421 * | | | end. Instead, we extend a 1 x n rectangle
422 * | | | _out_ from the singleton, shaving a layer
423 * +--+--+ | off the end of another rectangle. So if we
424 * | | | | extended up, we'd make our singleton part
425 * | +--+--+ of a 1x3 and generate a 1x2 where the 2x2
426 * | | | used to be; or we could extend right into
427 * +--+-----+ a 2x1, turning the 1x3 into a 1x2.
429 * +-----+--+ Here, we can't even do _that_, since any
430 * | | | direction we choose to extend the singleton
431 * +--+--+ | will produce a new singleton as a result of
432 * | | | | truncating one of the size-2 rectangles.
433 * | +--+--+ Fortunately, this case can _only_ occur when
434 * | | | a singleton is surrounded by four size-2s
435 * +--+-----+ in this fashion; so instead we can simply
436 * replace the whole section with a single 3x3.
438 for (x
= 0; x
< params
->w
; x
++) {
439 for (y
= 0; y
< params
->h
; y
++) {
440 if (index(params
, grid
, x
, y
) < 0) {
443 #ifdef GENERATION_DIAGNOSTICS
444 display_grid(params
, grid
, numbers
);
445 printf("singleton at %d,%d\n", x
, y
);
449 * Check in which directions we can feasibly extend
450 * the singleton. We can extend in a particular
451 * direction iff either:
453 * - the rectangle on that side of the singleton
454 * is not 2x1, and we are at one end of the edge
455 * of it we are touching
457 * - it is 2x1 but we are on its short side.
459 * FIXME: we could plausibly choose between these
460 * based on the sizes of the rectangles they would
464 if (x
< params
->w
-1) {
465 struct rect r
= find_rect(params
, grid
, x
+1, y
);
466 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
467 dirs
[ndirs
++] = 1; /* right */
470 struct rect r
= find_rect(params
, grid
, x
, y
-1);
471 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
472 dirs
[ndirs
++] = 2; /* up */
475 struct rect r
= find_rect(params
, grid
, x
-1, y
);
476 if ((r
.w
* r
.h
> 2 && (r
.y
==y
|| r
.y
+r
.h
-1==y
)) || r
.h
==1)
477 dirs
[ndirs
++] = 4; /* left */
479 if (y
< params
->h
-1) {
480 struct rect r
= find_rect(params
, grid
, x
, y
+1);
481 if ((r
.w
* r
.h
> 2 && (r
.x
==x
|| r
.x
+r
.w
-1==x
)) || r
.w
==1)
482 dirs
[ndirs
++] = 8; /* down */
489 which
= random_upto(rs
, ndirs
);
494 assert(x
< params
->w
+1);
495 #ifdef GENERATION_DIAGNOSTICS
496 printf("extending right\n");
498 r1
= find_rect(params
, grid
, x
+1, y
);
509 #ifdef GENERATION_DIAGNOSTICS
510 printf("extending up\n");
512 r1
= find_rect(params
, grid
, x
, y
-1);
523 #ifdef GENERATION_DIAGNOSTICS
524 printf("extending left\n");
526 r1
= find_rect(params
, grid
, x
-1, y
);
536 assert(y
< params
->h
+1);
537 #ifdef GENERATION_DIAGNOSTICS
538 printf("extending down\n");
540 r1
= find_rect(params
, grid
, x
, y
+1);
550 if (r1
.h
> 0 && r1
.w
> 0)
551 place_rect(params
, grid
, r1
);
552 place_rect(params
, grid
, r2
);
556 * Sanity-check that there really is a 3x3
557 * rectangle surrounding this singleton and it
558 * contains absolutely everything we could
563 assert(x
> 0 && x
< params
->w
-1);
564 assert(y
> 0 && y
< params
->h
-1);
566 for (xx
= x
-1; xx
<= x
+1; xx
++)
567 for (yy
= y
-1; yy
<= y
+1; yy
++) {
568 struct rect r
= find_rect(params
,grid
,xx
,yy
);
571 assert(r
.x
+r
.w
-1 <= x
+1);
572 assert(r
.y
+r
.h
-1 <= y
+1);
577 #ifdef GENERATION_DIAGNOSTICS
578 printf("need the 3x3 trick\n");
582 * FIXME: If the maximum rectangle area for
583 * this grid is less than 9, we ought to
584 * subdivide the 3x3 in some fashion. There are
585 * five other possibilities:
590 * - a 3 and three 2s (two different arrangements).
598 place_rect(params
, grid
, r
);
608 for (x
= 0; x
< params
->w
; x
++) {
609 for (y
= 0; y
< params
->h
; y
++) {
610 int idx
= INDEX(params
, x
, y
);
611 if (index(params
, grid
, x
, y
) == idx
) {
612 struct rect r
= find_rect(params
, grid
, x
, y
);
616 * Decide where to put the number.
618 n
= random_upto(rs
, r
.w
*r
.h
);
621 index(params
,numbers
,x
+xx
,y
+yy
) = r
.w
*r
.h
;
626 #ifdef GENERATION_DIAGNOSTICS
627 display_grid(params
, grid
, numbers
);
630 seed
= snewn(11 * params
->w
* params
->h
, char);
633 for (i
= 0; i
<= params
->w
* params
->h
; i
++) {
634 int n
= (i
< params
->w
* params
->h ? numbers
[i
] : -1);
641 int c
= 'a' - 1 + run
;
645 run
-= c
- ('a' - 1);
651 p
+= sprintf(p
, "%d", n
);
663 char *validate_seed(game_params
*params
, char *seed
)
665 int area
= params
->w
* params
->h
;
670 if (n
>= 'a' && n
<= 'z') {
671 squares
+= n
- 'a' + 1;
672 } else if (n
== '_') {
674 } else if (n
> '0' && n
<= '9') {
676 while (*seed
>= '0' && *seed
<= '9')
679 return "Invalid character in game specification";
683 return "Not enough data to fill grid";
686 return "Too much data to fit in grid";
691 game_state
*new_game(game_params
*params
, char *seed
)
693 game_state
*state
= snew(game_state
);
696 state
->w
= params
->w
;
697 state
->h
= params
->h
;
699 area
= state
->w
* state
->h
;
701 state
->grid
= snewn(area
, int);
702 state
->vedge
= snewn(area
, unsigned char);
703 state
->hedge
= snewn(area
, unsigned char);
704 state
->completed
= FALSE
;
709 if (n
>= 'a' && n
<= 'z') {
710 int run
= n
- 'a' + 1;
711 assert(i
+ run
<= area
);
713 state
->grid
[i
++] = 0;
714 } else if (n
== '_') {
716 } else if (n
> '0' && n
<= '9') {
718 state
->grid
[i
++] = atoi(seed
-1);
719 while (*seed
>= '0' && *seed
<= '9')
722 assert(!"We can't get here");
727 for (y
= 0; y
< state
->h
; y
++)
728 for (x
= 0; x
< state
->w
; x
++)
729 vedge(state
,x
,y
) = hedge(state
,x
,y
) = 0;
734 game_state
*dup_game(game_state
*state
)
736 game_state
*ret
= snew(game_state
);
741 ret
->vedge
= snewn(state
->w
* state
->h
, unsigned char);
742 ret
->hedge
= snewn(state
->w
* state
->h
, unsigned char);
743 ret
->grid
= snewn(state
->w
* state
->h
, int);
745 ret
->completed
= state
->completed
;
747 memcpy(ret
->grid
, state
->grid
, state
->w
* state
->h
* sizeof(int));
748 memcpy(ret
->vedge
, state
->vedge
, state
->w
*state
->h
*sizeof(unsigned char));
749 memcpy(ret
->hedge
, state
->hedge
, state
->w
*state
->h
*sizeof(unsigned char));
754 void free_game(game_state
*state
)
762 static unsigned char *get_correct(game_state
*state
)
767 ret
= snewn(state
->w
* state
->h
, unsigned char);
768 memset(ret
, 0xFF, state
->w
* state
->h
);
770 for (x
= 0; x
< state
->w
; x
++)
771 for (y
= 0; y
< state
->h
; y
++)
772 if (index(state
,ret
,x
,y
) == 0xFF) {
775 int num
, area
, valid
;
778 * Find a rectangle starting at this point.
781 while (x
+rw
< state
->w
&& !vedge(state
,x
+rw
,y
))
784 while (y
+rh
< state
->h
&& !hedge(state
,x
,y
+rh
))
788 * We know what the dimensions of the rectangle
789 * should be if it's there at all. Find out if we
790 * really have a valid rectangle.
793 /* Check the horizontal edges. */
794 for (xx
= x
; xx
< x
+rw
; xx
++) {
795 for (yy
= y
; yy
<= y
+rh
; yy
++) {
796 int e
= !HRANGE(state
,xx
,yy
) || hedge(state
,xx
,yy
);
797 int ec
= (yy
== y
|| yy
== y
+rh
);
802 /* Check the vertical edges. */
803 for (yy
= y
; yy
< y
+rh
; yy
++) {
804 for (xx
= x
; xx
<= x
+rw
; xx
++) {
805 int e
= !VRANGE(state
,xx
,yy
) || vedge(state
,xx
,yy
);
806 int ec
= (xx
== x
|| xx
== x
+rw
);
813 * If this is not a valid rectangle with no other
814 * edges inside it, we just mark this square as not
815 * complete and proceed to the next square.
818 index(state
, ret
, x
, y
) = 0;
823 * We have a rectangle. Now see what its area is,
824 * and how many numbers are in it.
828 for (xx
= x
; xx
< x
+rw
; xx
++) {
829 for (yy
= y
; yy
< y
+rh
; yy
++) {
831 if (grid(state
,xx
,yy
)) {
833 valid
= FALSE
; /* two numbers */
834 num
= grid(state
,xx
,yy
);
842 * Now fill in the whole rectangle based on the
845 for (xx
= x
; xx
< x
+rw
; xx
++) {
846 for (yy
= y
; yy
< y
+rh
; yy
++) {
847 index(state
, ret
, xx
, yy
) = valid
;
857 * These coordinates are 2 times the obvious grid coordinates.
858 * Hence, the top left of the grid is (0,0), the grid point to
859 * the right of that is (2,0), the one _below that_ is (2,2)
860 * and so on. This is so that we can specify a drag start point
861 * on an edge (one odd coordinate) or in the middle of a square
862 * (two odd coordinates) rather than always at a corner.
864 * -1,-1 means no drag is in progress.
871 * This flag is set as soon as a dragging action moves the
872 * mouse pointer away from its starting point, so that even if
873 * the pointer _returns_ to its starting point the action is
874 * treated as a small drag rather than a click.
879 game_ui
*new_ui(game_state
*state
)
881 game_ui
*ui
= snew(game_ui
);
882 ui
->drag_start_x
= -1;
883 ui
->drag_start_y
= -1;
890 void free_ui(game_ui
*ui
)
895 void coord_round(float x
, float y
, int *xr
, int *yr
)
897 float xs
, ys
, xv
, yv
, dx
, dy
, dist
;
900 * Find the nearest square-centre.
902 xs
= (float)floor(x
) + 0.5F
;
903 ys
= (float)floor(y
) + 0.5F
;
906 * And find the nearest grid vertex.
908 xv
= (float)floor(x
+ 0.5F
);
909 yv
= (float)floor(y
+ 0.5F
);
912 * We allocate clicks in parts of the grid square to either
913 * corners, edges or square centres, as follows:
929 * In other words: we measure the square distance (i.e.
930 * max(dx,dy)) from the click to the nearest corner, and if
931 * it's within CORNER_TOLERANCE then we return a corner click.
932 * We measure the square distance from the click to the nearest
933 * centre, and if that's within CENTRE_TOLERANCE we return a
934 * centre click. Failing that, we find which of the two edge
935 * centres is nearer to the click and return that edge.
939 * Check for corner click.
941 dx
= (float)fabs(x
- xv
);
942 dy
= (float)fabs(y
- yv
);
943 dist
= (dx
> dy ? dx
: dy
);
944 if (dist
< CORNER_TOLERANCE
) {
949 * Check for centre click.
951 dx
= (float)fabs(x
- xs
);
952 dy
= (float)fabs(y
- ys
);
953 dist
= (dx
> dy ? dx
: dy
);
954 if (dist
< CENTRE_TOLERANCE
) {
955 *xr
= 1 + 2 * (int)xs
;
956 *yr
= 1 + 2 * (int)ys
;
959 * Failing both of those, see which edge we're closer to.
960 * Conveniently, this is simply done by testing the relative
961 * magnitude of dx and dy (which are currently distances from
962 * the square centre).
965 /* Vertical edge: x-coord of corner,
966 * y-coord of square centre. */
968 *yr
= 1 + 2 * (int)ys
;
970 /* Horizontal edge: x-coord of square centre,
971 * y-coord of corner. */
972 *xr
= 1 + 2 * (int)xs
;
979 static void ui_draw_rect(game_state
*state
, game_ui
*ui
,
980 unsigned char *hedge
, unsigned char *vedge
, int c
)
982 int x1
, x2
, y1
, y2
, x
, y
, t
;
984 x1
= ui
->drag_start_x
;
986 if (x2
< x1
) { t
= x1
; x1
= x2
; x2
= t
; }
988 y1
= ui
->drag_start_y
;
990 if (y2
< y1
) { t
= y1
; y1
= y2
; y2
= t
; }
992 x1
= x1
/ 2; /* rounds down */
993 x2
= (x2
+1) / 2; /* rounds up */
994 y1
= y1
/ 2; /* rounds down */
995 y2
= (y2
+1) / 2; /* rounds up */
998 * Draw horizontal edges of rectangles.
1000 for (x
= x1
; x
< x2
; x
++)
1001 for (y
= y1
; y
<= y2
; y
++)
1002 if (HRANGE(state
,x
,y
)) {
1003 int val
= index(state
,hedge
,x
,y
);
1004 if (y
== y1
|| y
== y2
)
1008 index(state
,hedge
,x
,y
) = val
;
1012 * Draw vertical edges of rectangles.
1014 for (y
= y1
; y
< y2
; y
++)
1015 for (x
= x1
; x
<= x2
; x
++)
1016 if (VRANGE(state
,x
,y
)) {
1017 int val
= index(state
,vedge
,x
,y
);
1018 if (x
== x1
|| x
== x2
)
1022 index(state
,vedge
,x
,y
) = val
;
1026 game_state
*make_move(game_state
*from
, game_ui
*ui
, int x
, int y
, int button
)
1029 int startdrag
= FALSE
, enddrag
= FALSE
, active
= FALSE
;
1032 if (button
== LEFT_BUTTON
) {
1034 } else if (button
== LEFT_RELEASE
) {
1036 } else if (button
!= LEFT_DRAG
) {
1040 coord_round(FROMCOORD((float)x
), FROMCOORD((float)y
), &xc
, &yc
);
1043 ui
->drag_start_x
= xc
;
1044 ui
->drag_start_y
= yc
;
1045 ui
->drag_end_x
= xc
;
1046 ui
->drag_end_y
= yc
;
1047 ui
->dragged
= FALSE
;
1051 if (xc
!= ui
->drag_end_x
|| yc
!= ui
->drag_end_y
) {
1052 ui
->drag_end_x
= xc
;
1053 ui
->drag_end_y
= yc
;
1061 if (xc
>= 0 && xc
<= 2*from
->w
&&
1062 yc
>= 0 && yc
<= 2*from
->h
) {
1063 ret
= dup_game(from
);
1066 ui_draw_rect(ret
, ui
, ret
->hedge
, ret
->vedge
, 1);
1068 if ((xc
& 1) && !(yc
& 1) && HRANGE(from
,xc
/2,yc
/2)) {
1069 hedge(ret
,xc
/2,yc
/2) = !hedge(ret
,xc
/2,yc
/2);
1071 if ((yc
& 1) && !(xc
& 1) && VRANGE(from
,xc
/2,yc
/2)) {
1072 vedge(ret
,xc
/2,yc
/2) = !vedge(ret
,xc
/2,yc
/2);
1076 if (!memcmp(ret
->hedge
, from
->hedge
, from
->w
*from
->h
) &&
1077 !memcmp(ret
->vedge
, from
->vedge
, from
->w
*from
->h
)) {
1083 * We've made a real change to the grid. Check to see
1084 * if the game has been completed.
1086 if (ret
&& !ret
->completed
) {
1088 unsigned char *correct
= get_correct(ret
);
1091 for (x
= 0; x
< ret
->w
; x
++)
1092 for (y
= 0; y
< ret
->h
; y
++)
1093 if (!index(ret
, correct
, x
, y
))
1099 ret
->completed
= TRUE
;
1103 ui
->drag_start_x
= -1;
1104 ui
->drag_start_y
= -1;
1105 ui
->drag_end_x
= -1;
1106 ui
->drag_end_y
= -1;
1107 ui
->dragged
= FALSE
;
1112 return ret
; /* a move has been made */
1114 return from
; /* UI activity has occurred */
1119 /* ----------------------------------------------------------------------
1123 #define CORRECT 65536
1125 #define COLOUR(k) ( (k)==1 ? COL_LINE : COL_DRAG )
1126 #define MAX(x,y) ( (x)>(y) ? (x) : (y) )
1127 #define MAX4(x,y,z,w) ( MAX(MAX(x,y),MAX(z,w)) )
1129 struct game_drawstate
{
1132 unsigned int *visible
;
1135 void game_size(game_params
*params
, int *x
, int *y
)
1137 *x
= params
->w
* TILE_SIZE
+ 2*BORDER
+ 1;
1138 *y
= params
->h
* TILE_SIZE
+ 2*BORDER
+ 1;
1141 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1143 float *ret
= snewn(3 * NCOLOURS
, float);
1145 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1147 ret
[COL_GRID
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1148 ret
[COL_GRID
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1149 ret
[COL_GRID
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
1151 ret
[COL_DRAG
* 3 + 0] = 1.0F
;
1152 ret
[COL_DRAG
* 3 + 1] = 0.0F
;
1153 ret
[COL_DRAG
* 3 + 2] = 0.0F
;
1155 ret
[COL_CORRECT
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
1156 ret
[COL_CORRECT
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
1157 ret
[COL_CORRECT
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
1159 ret
[COL_LINE
* 3 + 0] = 0.0F
;
1160 ret
[COL_LINE
* 3 + 1] = 0.0F
;
1161 ret
[COL_LINE
* 3 + 2] = 0.0F
;
1163 ret
[COL_TEXT
* 3 + 0] = 0.0F
;
1164 ret
[COL_TEXT
* 3 + 1] = 0.0F
;
1165 ret
[COL_TEXT
* 3 + 2] = 0.0F
;
1167 *ncolours
= NCOLOURS
;
1171 game_drawstate
*game_new_drawstate(game_state
*state
)
1173 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1176 ds
->started
= FALSE
;
1179 ds
->visible
= snewn(ds
->w
* ds
->h
, unsigned int);
1180 for (i
= 0; i
< ds
->w
* ds
->h
; i
++)
1181 ds
->visible
[i
] = 0xFFFF;
1186 void game_free_drawstate(game_drawstate
*ds
)
1192 void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
1193 unsigned char *hedge
, unsigned char *vedge
,
1194 unsigned char *corners
, int correct
)
1196 int cx
= COORD(x
), cy
= COORD(y
);
1199 draw_rect(fe
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1, COL_GRID
);
1200 draw_rect(fe
, cx
+1, cy
+1, TILE_SIZE
-1, TILE_SIZE
-1,
1201 correct ? COL_CORRECT
: COL_BACKGROUND
);
1203 if (grid(state
,x
,y
)) {
1204 sprintf(str
, "%d", grid(state
,x
,y
));
1205 draw_text(fe
, cx
+TILE_SIZE
/2, cy
+TILE_SIZE
/2, FONT_VARIABLE
,
1206 TILE_SIZE
/2, ALIGN_HCENTRE
| ALIGN_VCENTRE
, COL_TEXT
, str
);
1212 if (!HRANGE(state
,x
,y
) || index(state
,hedge
,x
,y
))
1213 draw_rect(fe
, cx
, cy
, TILE_SIZE
+1, 2,
1214 HRANGE(state
,x
,y
) ?
COLOUR(index(state
,hedge
,x
,y
)) :
1216 if (!HRANGE(state
,x
,y
+1) || index(state
,hedge
,x
,y
+1))
1217 draw_rect(fe
, cx
, cy
+TILE_SIZE
-1, TILE_SIZE
+1, 2,
1218 HRANGE(state
,x
,y
+1) ?
COLOUR(index(state
,hedge
,x
,y
+1)) :
1220 if (!VRANGE(state
,x
,y
) || index(state
,vedge
,x
,y
))
1221 draw_rect(fe
, cx
, cy
, 2, TILE_SIZE
+1,
1222 VRANGE(state
,x
,y
) ?
COLOUR(index(state
,vedge
,x
,y
)) :
1224 if (!VRANGE(state
,x
+1,y
) || index(state
,vedge
,x
+1,y
))
1225 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
, 2, TILE_SIZE
+1,
1226 VRANGE(state
,x
+1,y
) ?
COLOUR(index(state
,vedge
,x
+1,y
)) :
1232 if (index(state
,corners
,x
,y
))
1233 draw_rect(fe
, cx
, cy
, 2, 2,
1234 COLOUR(index(state
,corners
,x
,y
)));
1235 if (x
+1 < state
->w
&& index(state
,corners
,x
+1,y
))
1236 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
, 2, 2,
1237 COLOUR(index(state
,corners
,x
+1,y
)));
1238 if (y
+1 < state
->h
&& index(state
,corners
,x
,y
+1))
1239 draw_rect(fe
, cx
, cy
+TILE_SIZE
-1, 2, 2,
1240 COLOUR(index(state
,corners
,x
,y
+1)));
1241 if (x
+1 < state
->w
&& y
+1 < state
->h
&& index(state
,corners
,x
+1,y
+1))
1242 draw_rect(fe
, cx
+TILE_SIZE
-1, cy
+TILE_SIZE
-1, 2, 2,
1243 COLOUR(index(state
,corners
,x
+1,y
+1)));
1245 draw_update(fe
, cx
, cy
, TILE_SIZE
+1, TILE_SIZE
+1);
1248 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1249 game_state
*state
, game_ui
*ui
,
1250 float animtime
, float flashtime
)
1253 unsigned char *correct
;
1254 unsigned char *hedge
, *vedge
, *corners
;
1256 correct
= get_correct(state
);
1259 hedge
= snewn(state
->w
*state
->h
, unsigned char);
1260 vedge
= snewn(state
->w
*state
->h
, unsigned char);
1261 memcpy(hedge
, state
->hedge
, state
->w
*state
->h
);
1262 memcpy(vedge
, state
->vedge
, state
->w
*state
->h
);
1263 ui_draw_rect(state
, ui
, hedge
, vedge
, 2);
1265 hedge
= state
->hedge
;
1266 vedge
= state
->vedge
;
1269 corners
= snewn(state
->w
* state
->h
, unsigned char);
1270 memset(corners
, 0, state
->w
* state
->h
);
1271 for (x
= 0; x
< state
->w
; x
++)
1272 for (y
= 0; y
< state
->h
; y
++) {
1274 int e
= index(state
, vedge
, x
, y
);
1275 if (index(state
,corners
,x
,y
) < e
)
1276 index(state
,corners
,x
,y
) = e
;
1277 if (y
+1 < state
->h
&&
1278 index(state
,corners
,x
,y
+1) < e
)
1279 index(state
,corners
,x
,y
+1) = e
;
1282 int e
= index(state
, hedge
, x
, y
);
1283 if (index(state
,corners
,x
,y
) < e
)
1284 index(state
,corners
,x
,y
) = e
;
1285 if (x
+1 < state
->w
&&
1286 index(state
,corners
,x
+1,y
) < e
)
1287 index(state
,corners
,x
+1,y
) = e
;
1293 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
1294 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1, COL_BACKGROUND
);
1295 draw_rect(fe
, COORD(0)-1, COORD(0)-1,
1296 ds
->w
*TILE_SIZE
+3, ds
->h
*TILE_SIZE
+3, COL_LINE
);
1298 draw_update(fe
, 0, 0,
1299 state
->w
* TILE_SIZE
+ 2*BORDER
+ 1,
1300 state
->h
* TILE_SIZE
+ 2*BORDER
+ 1);
1303 for (x
= 0; x
< state
->w
; x
++)
1304 for (y
= 0; y
< state
->h
; y
++) {
1307 if (HRANGE(state
,x
,y
))
1308 c
|= index(state
,hedge
,x
,y
);
1309 if (HRANGE(state
,x
,y
+1))
1310 c
|= index(state
,hedge
,x
,y
+1) << 2;
1311 if (VRANGE(state
,x
,y
))
1312 c
|= index(state
,vedge
,x
,y
) << 4;
1313 if (VRANGE(state
,x
+1,y
))
1314 c
|= index(state
,vedge
,x
+1,y
) << 6;
1315 c
|= index(state
,corners
,x
,y
) << 8;
1317 c
|= index(state
,corners
,x
+1,y
) << 10;
1319 c
|= index(state
,corners
,x
,y
+1) << 12;
1320 if (x
+1 < state
->w
&& y
+1 < state
->h
)
1321 c
|= index(state
,corners
,x
+1,y
+1) << 14;
1322 if (index(state
, correct
, x
, y
) && !flashtime
)
1325 if (index(ds
,ds
->visible
,x
,y
) != c
) {
1326 draw_tile(fe
, state
, x
, y
, hedge
, vedge
, corners
, c
& CORRECT
);
1327 index(ds
,ds
->visible
,x
,y
) = c
;
1331 if (hedge
!= state
->hedge
) {
1339 float game_anim_length(game_state
*oldstate
, game_state
*newstate
)
1344 float game_flash_length(game_state
*oldstate
, game_state
*newstate
)
1346 if (!oldstate
->completed
&& newstate
->completed
)
1351 int game_wants_statusbar(void)