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