b6b0369e |
1 | /* |
2 | * pattern.c: the pattern-reconstruction game known as `nonograms'. |
b6b0369e |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | #include <assert.h> |
9 | #include <ctype.h> |
10 | #include <math.h> |
11 | |
12 | #include "puzzles.h" |
13 | |
b6b0369e |
14 | enum { |
15 | COL_BACKGROUND, |
16 | COL_EMPTY, |
17 | COL_FULL, |
dafd6cf6 |
18 | COL_TEXT, |
b6b0369e |
19 | COL_UNKNOWN, |
20 | COL_GRID, |
b2ae5b05 |
21 | COL_CURSOR, |
b6b0369e |
22 | NCOLOURS |
23 | }; |
24 | |
1e3e152d |
25 | #define PREFERRED_TILE_SIZE 24 |
26 | #define TILE_SIZE (ds->tilesize) |
27 | #define BORDER (3 * TILE_SIZE / 4) |
b6b0369e |
28 | #define TLBORDER(d) ( (d) / 5 + 2 ) |
1e3e152d |
29 | #define GUTTER (TILE_SIZE / 2) |
b6b0369e |
30 | |
31 | #define FROMCOORD(d, x) \ |
32 | ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE ) |
33 | |
34 | #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d))) |
871bf294 |
35 | #define GETTILESIZE(d, w) ((double)w / (2.0 + (double)TLBORDER(d) + (double)(d))) |
b6b0369e |
36 | |
37 | #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x))) |
38 | |
39 | struct game_params { |
40 | int w, h; |
41 | }; |
42 | |
43 | #define GRID_UNKNOWN 2 |
44 | #define GRID_FULL 1 |
45 | #define GRID_EMPTY 0 |
46 | |
47 | struct game_state { |
48 | int w, h; |
49 | unsigned char *grid; |
50 | int rowsize; |
51 | int *rowdata, *rowlen; |
2ac6d24e |
52 | int completed, cheated; |
b6b0369e |
53 | }; |
54 | |
55 | #define FLASH_TIME 0.13F |
56 | |
be8d5aa1 |
57 | static game_params *default_params(void) |
b6b0369e |
58 | { |
59 | game_params *ret = snew(game_params); |
60 | |
61 | ret->w = ret->h = 15; |
62 | |
63 | return ret; |
64 | } |
65 | |
ab53eb64 |
66 | static const struct game_params pattern_presets[] = { |
67 | {10, 10}, |
68 | {15, 15}, |
69 | {20, 20}, |
70 | #ifndef SLOW_SYSTEM |
71 | {25, 25}, |
72 | {30, 30}, |
73 | #endif |
74 | }; |
75 | |
be8d5aa1 |
76 | static int game_fetch_preset(int i, char **name, game_params **params) |
b6b0369e |
77 | { |
78 | game_params *ret; |
79 | char str[80]; |
ab53eb64 |
80 | |
81 | if (i < 0 || i >= lenof(pattern_presets)) |
b6b0369e |
82 | return FALSE; |
83 | |
84 | ret = snew(game_params); |
ab53eb64 |
85 | *ret = pattern_presets[i]; |
b6b0369e |
86 | |
87 | sprintf(str, "%dx%d", ret->w, ret->h); |
88 | |
89 | *name = dupstr(str); |
90 | *params = ret; |
91 | return TRUE; |
92 | } |
93 | |
be8d5aa1 |
94 | static void free_params(game_params *params) |
b6b0369e |
95 | { |
96 | sfree(params); |
97 | } |
98 | |
be8d5aa1 |
99 | static game_params *dup_params(game_params *params) |
b6b0369e |
100 | { |
101 | game_params *ret = snew(game_params); |
102 | *ret = *params; /* structure copy */ |
103 | return ret; |
104 | } |
105 | |
1185e3c5 |
106 | static void decode_params(game_params *ret, char const *string) |
b6b0369e |
107 | { |
b6b0369e |
108 | char const *p = string; |
109 | |
110 | ret->w = atoi(p); |
89167dad |
111 | while (*p && isdigit((unsigned char)*p)) p++; |
b6b0369e |
112 | if (*p == 'x') { |
113 | p++; |
114 | ret->h = atoi(p); |
89167dad |
115 | while (*p && isdigit((unsigned char)*p)) p++; |
b6b0369e |
116 | } else { |
117 | ret->h = ret->w; |
118 | } |
b6b0369e |
119 | } |
120 | |
1185e3c5 |
121 | static char *encode_params(game_params *params, int full) |
b6b0369e |
122 | { |
123 | char ret[400]; |
124 | int len; |
125 | |
126 | len = sprintf(ret, "%dx%d", params->w, params->h); |
127 | assert(len < lenof(ret)); |
128 | ret[len] = '\0'; |
129 | |
130 | return dupstr(ret); |
131 | } |
132 | |
be8d5aa1 |
133 | static config_item *game_configure(game_params *params) |
b6b0369e |
134 | { |
135 | config_item *ret; |
136 | char buf[80]; |
137 | |
138 | ret = snewn(3, config_item); |
139 | |
140 | ret[0].name = "Width"; |
141 | ret[0].type = C_STRING; |
142 | sprintf(buf, "%d", params->w); |
143 | ret[0].sval = dupstr(buf); |
144 | ret[0].ival = 0; |
145 | |
146 | ret[1].name = "Height"; |
147 | ret[1].type = C_STRING; |
148 | sprintf(buf, "%d", params->h); |
149 | ret[1].sval = dupstr(buf); |
150 | ret[1].ival = 0; |
151 | |
152 | ret[2].name = NULL; |
153 | ret[2].type = C_END; |
154 | ret[2].sval = NULL; |
155 | ret[2].ival = 0; |
156 | |
157 | return ret; |
158 | } |
159 | |
be8d5aa1 |
160 | static game_params *custom_params(config_item *cfg) |
b6b0369e |
161 | { |
162 | game_params *ret = snew(game_params); |
163 | |
164 | ret->w = atoi(cfg[0].sval); |
165 | ret->h = atoi(cfg[1].sval); |
166 | |
167 | return ret; |
168 | } |
169 | |
3ff276f2 |
170 | static char *validate_params(game_params *params, int full) |
b6b0369e |
171 | { |
ab53eb64 |
172 | if (params->w <= 0 || params->h <= 0) |
b6b0369e |
173 | return "Width and height must both be greater than zero"; |
b6b0369e |
174 | return NULL; |
175 | } |
176 | |
177 | /* ---------------------------------------------------------------------- |
178 | * Puzzle generation code. |
179 | * |
180 | * For this particular puzzle, it seemed important to me to ensure |
181 | * a unique solution. I do this the brute-force way, by having a |
182 | * solver algorithm alongside the generator, and repeatedly |
183 | * generating a random grid until I find one whose solution is |
184 | * unique. It turns out that this isn't too onerous on a modern PC |
185 | * provided you keep grid size below around 30. Any offers of |
186 | * better algorithms, however, will be very gratefully received. |
187 | * |
188 | * Another annoyance of this approach is that it limits the |
189 | * available puzzles to those solvable by the algorithm I've used. |
190 | * My algorithm only ever considers a single row or column at any |
191 | * one time, which means it's incapable of solving the following |
192 | * difficult example (found by Bella Image around 1995/6, when she |
193 | * and I were both doing maths degrees): |
194 | * |
195 | * 2 1 2 1 |
196 | * |
197 | * +--+--+--+--+ |
198 | * 1 1 | | | | | |
199 | * +--+--+--+--+ |
200 | * 2 | | | | | |
201 | * +--+--+--+--+ |
202 | * 1 | | | | | |
203 | * +--+--+--+--+ |
204 | * 1 | | | | | |
205 | * +--+--+--+--+ |
206 | * |
207 | * Obviously this cannot be solved by a one-row-or-column-at-a-time |
208 | * algorithm (it would require at least one row or column reading |
209 | * `2 1', `1 2', `3' or `4' to get started). However, it can be |
210 | * proved to have a unique solution: if the top left square were |
211 | * empty, then the only option for the top row would be to fill the |
212 | * two squares in the 1 columns, which would imply the squares |
213 | * below those were empty, leaving no place for the 2 in the second |
214 | * row. Contradiction. Hence the top left square is full, and the |
215 | * unique solution follows easily from that starting point. |
216 | * |
217 | * (The game ID for this puzzle is 4x4:2/1/2/1/1.1/2/1/1 , in case |
218 | * it's useful to anyone.) |
219 | */ |
220 | |
221 | static int float_compare(const void *av, const void *bv) |
222 | { |
223 | const float *a = (const float *)av; |
224 | const float *b = (const float *)bv; |
225 | if (*a < *b) |
226 | return -1; |
227 | else if (*a > *b) |
228 | return +1; |
229 | else |
230 | return 0; |
231 | } |
232 | |
233 | static void generate(random_state *rs, int w, int h, unsigned char *retgrid) |
234 | { |
235 | float *fgrid; |
236 | float *fgrid2; |
237 | int step, i, j; |
238 | float threshold; |
239 | |
240 | fgrid = snewn(w*h, float); |
241 | |
242 | for (i = 0; i < h; i++) { |
243 | for (j = 0; j < w; j++) { |
244 | fgrid[i*w+j] = random_upto(rs, 100000000UL) / 100000000.F; |
245 | } |
246 | } |
247 | |
248 | /* |
249 | * The above gives a completely random splattering of black and |
250 | * white cells. We want to gently bias this in favour of _some_ |
251 | * reasonably thick areas of white and black, while retaining |
252 | * some randomness and fine detail. |
253 | * |
254 | * So we evolve the starting grid using a cellular automaton. |
255 | * Currently, I'm doing something very simple indeed, which is |
256 | * to set each square to the average of the surrounding nine |
257 | * cells (or the average of fewer, if we're on a corner). |
258 | */ |
259 | for (step = 0; step < 1; step++) { |
260 | fgrid2 = snewn(w*h, float); |
261 | |
262 | for (i = 0; i < h; i++) { |
263 | for (j = 0; j < w; j++) { |
264 | float sx, xbar; |
265 | int n, p, q; |
266 | |
267 | /* |
268 | * Compute the average of the surrounding cells. |
269 | */ |
270 | n = 0; |
271 | sx = 0.F; |
272 | for (p = -1; p <= +1; p++) { |
273 | for (q = -1; q <= +1; q++) { |
274 | if (i+p < 0 || i+p >= h || j+q < 0 || j+q >= w) |
275 | continue; |
29caa839 |
276 | /* |
277 | * An additional special case not mentioned |
278 | * above: if a grid dimension is 2xn then |
279 | * we do not average across that dimension |
280 | * at all. Otherwise a 2x2 grid would |
281 | * contain four identical squares. |
282 | */ |
283 | if ((h==2 && p!=0) || (w==2 && q!=0)) |
284 | continue; |
b6b0369e |
285 | n++; |
286 | sx += fgrid[(i+p)*w+(j+q)]; |
287 | } |
288 | } |
289 | xbar = sx / n; |
290 | |
291 | fgrid2[i*w+j] = xbar; |
292 | } |
293 | } |
294 | |
295 | sfree(fgrid); |
296 | fgrid = fgrid2; |
297 | } |
298 | |
299 | fgrid2 = snewn(w*h, float); |
300 | memcpy(fgrid2, fgrid, w*h*sizeof(float)); |
301 | qsort(fgrid2, w*h, sizeof(float), float_compare); |
302 | threshold = fgrid2[w*h/2]; |
303 | sfree(fgrid2); |
304 | |
305 | for (i = 0; i < h; i++) { |
306 | for (j = 0; j < w; j++) { |
29caa839 |
307 | retgrid[i*w+j] = (fgrid[i*w+j] >= threshold ? GRID_FULL : |
b6b0369e |
308 | GRID_EMPTY); |
309 | } |
310 | } |
311 | |
312 | sfree(fgrid); |
313 | } |
314 | |
be8d5aa1 |
315 | static int compute_rowdata(int *ret, unsigned char *start, int len, int step) |
b6b0369e |
316 | { |
317 | int i, n; |
318 | |
319 | n = 0; |
320 | |
321 | for (i = 0; i < len; i++) { |
b6b0369e |
322 | if (start[i*step] == GRID_FULL) { |
323 | int runlen = 1; |
0526a222 |
324 | while (i+runlen < len && start[(i+runlen)*step] == GRID_FULL) |
b6b0369e |
325 | runlen++; |
326 | ret[n++] = runlen; |
327 | i += runlen; |
328 | } |
0526a222 |
329 | |
c87ce51a |
330 | if (i < len && start[i*step] == GRID_UNKNOWN) |
0526a222 |
331 | return -1; |
b6b0369e |
332 | } |
333 | |
334 | return n; |
335 | } |
336 | |
337 | #define UNKNOWN 0 |
338 | #define BLOCK 1 |
339 | #define DOT 2 |
340 | #define STILL_UNKNOWN 3 |
341 | |
342 | static void do_recurse(unsigned char *known, unsigned char *deduced, |
343 | unsigned char *row, int *data, int len, |
344 | int freespace, int ndone, int lowest) |
345 | { |
346 | int i, j, k; |
347 | |
348 | if (data[ndone]) { |
349 | for (i=0; i<=freespace; i++) { |
350 | j = lowest; |
351 | for (k=0; k<i; k++) row[j++] = DOT; |
352 | for (k=0; k<data[ndone]; k++) row[j++] = BLOCK; |
353 | if (j < len) row[j++] = DOT; |
354 | do_recurse(known, deduced, row, data, len, |
355 | freespace-i, ndone+1, j); |
356 | } |
357 | } else { |
358 | for (i=lowest; i<len; i++) |
359 | row[i] = DOT; |
360 | for (i=0; i<len; i++) |
361 | if (known[i] && known[i] != row[i]) |
362 | return; |
363 | for (i=0; i<len; i++) |
364 | deduced[i] |= row[i]; |
365 | } |
366 | } |
367 | |
368 | static int do_row(unsigned char *known, unsigned char *deduced, |
369 | unsigned char *row, |
370 | unsigned char *start, int len, int step, int *data) |
371 | { |
372 | int rowlen, i, freespace, done_any; |
373 | |
374 | freespace = len+1; |
375 | for (rowlen = 0; data[rowlen]; rowlen++) |
376 | freespace -= data[rowlen]+1; |
377 | |
378 | for (i = 0; i < len; i++) { |
379 | known[i] = start[i*step]; |
380 | deduced[i] = 0; |
381 | } |
382 | |
383 | do_recurse(known, deduced, row, data, len, freespace, 0, 0); |
384 | done_any = FALSE; |
385 | for (i=0; i<len; i++) |
386 | if (deduced[i] && deduced[i] != STILL_UNKNOWN && !known[i]) { |
387 | start[i*step] = deduced[i]; |
388 | done_any = TRUE; |
389 | } |
390 | return done_any; |
391 | } |
392 | |
393 | static unsigned char *generate_soluble(random_state *rs, int w, int h) |
394 | { |
395 | int i, j, done_any, ok, ntries, max; |
396 | unsigned char *grid, *matrix, *workspace; |
397 | int *rowdata; |
398 | |
399 | grid = snewn(w*h, unsigned char); |
400 | matrix = snewn(w*h, unsigned char); |
401 | max = max(w, h); |
402 | workspace = snewn(max*3, unsigned char); |
403 | rowdata = snewn(max+1, int); |
404 | |
405 | ntries = 0; |
406 | |
407 | do { |
408 | ntries++; |
409 | |
410 | generate(rs, w, h, grid); |
411 | |
15f00e06 |
412 | /* |
413 | * The game is a bit too easy if any row or column is |
414 | * completely black or completely white. An exception is |
415 | * made for rows/columns that are under 3 squares, |
416 | * otherwise nothing will ever be successfully generated. |
417 | */ |
418 | ok = TRUE; |
419 | if (w > 2) { |
420 | for (i = 0; i < h; i++) { |
421 | int colours = 0; |
422 | for (j = 0; j < w; j++) |
423 | colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1); |
424 | if (colours != 3) |
425 | ok = FALSE; |
426 | } |
427 | } |
428 | if (h > 2) { |
429 | for (j = 0; j < w; j++) { |
430 | int colours = 0; |
431 | for (i = 0; i < h; i++) |
432 | colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1); |
433 | if (colours != 3) |
434 | ok = FALSE; |
435 | } |
436 | } |
437 | if (!ok) |
438 | continue; |
439 | |
b6b0369e |
440 | memset(matrix, 0, w*h); |
441 | |
442 | do { |
443 | done_any = 0; |
444 | for (i=0; i<h; i++) { |
445 | rowdata[compute_rowdata(rowdata, grid+i*w, w, 1)] = 0; |
446 | done_any |= do_row(workspace, workspace+max, workspace+2*max, |
447 | matrix+i*w, w, 1, rowdata); |
448 | } |
449 | for (i=0; i<w; i++) { |
450 | rowdata[compute_rowdata(rowdata, grid+i, h, w)] = 0; |
451 | done_any |= do_row(workspace, workspace+max, workspace+2*max, |
452 | matrix+i, h, w, rowdata); |
453 | } |
454 | } while (done_any); |
455 | |
456 | ok = TRUE; |
457 | for (i=0; i<h; i++) { |
458 | for (j=0; j<w; j++) { |
459 | if (matrix[i*w+j] == UNKNOWN) |
460 | ok = FALSE; |
461 | } |
462 | } |
463 | } while (!ok); |
464 | |
465 | sfree(matrix); |
466 | sfree(workspace); |
467 | sfree(rowdata); |
468 | return grid; |
469 | } |
470 | |
1185e3c5 |
471 | static char *new_game_desc(game_params *params, random_state *rs, |
c566778e |
472 | char **aux, int interactive) |
b6b0369e |
473 | { |
474 | unsigned char *grid; |
475 | int i, j, max, rowlen, *rowdata; |
1185e3c5 |
476 | char intbuf[80], *desc; |
477 | int desclen, descpos; |
b6b0369e |
478 | |
479 | grid = generate_soluble(rs, params->w, params->h); |
480 | max = max(params->w, params->h); |
481 | rowdata = snewn(max, int); |
482 | |
483 | /* |
c566778e |
484 | * Save the solved game in aux. |
3220eba4 |
485 | */ |
486 | { |
c566778e |
487 | char *ai = snewn(params->w * params->h + 2, char); |
3220eba4 |
488 | |
c566778e |
489 | /* |
490 | * String format is exactly the same as a solve move, so we |
491 | * can just dupstr this in solve_game(). |
492 | */ |
493 | |
494 | ai[0] = 'S'; |
495 | |
496 | for (i = 0; i < params->w * params->h; i++) |
497 | ai[i+1] = grid[i] ? '1' : '0'; |
498 | |
499 | ai[params->w * params->h + 1] = '\0'; |
3220eba4 |
500 | |
501 | *aux = ai; |
502 | } |
503 | |
504 | /* |
b6b0369e |
505 | * Seed is a slash-separated list of row contents; each row |
506 | * contents section is a dot-separated list of integers. Row |
507 | * contents are listed in the order (columns left to right, |
508 | * then rows top to bottom). |
509 | * |
510 | * Simplest way to handle memory allocation is to make two |
511 | * passes, first computing the seed size and then writing it |
512 | * out. |
513 | */ |
1185e3c5 |
514 | desclen = 0; |
b6b0369e |
515 | for (i = 0; i < params->w + params->h; i++) { |
516 | if (i < params->w) |
517 | rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w); |
518 | else |
519 | rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w, |
520 | params->w, 1); |
521 | if (rowlen > 0) { |
522 | for (j = 0; j < rowlen; j++) { |
1185e3c5 |
523 | desclen += 1 + sprintf(intbuf, "%d", rowdata[j]); |
b6b0369e |
524 | } |
525 | } else { |
1185e3c5 |
526 | desclen++; |
b6b0369e |
527 | } |
528 | } |
1185e3c5 |
529 | desc = snewn(desclen, char); |
530 | descpos = 0; |
b6b0369e |
531 | for (i = 0; i < params->w + params->h; i++) { |
532 | if (i < params->w) |
533 | rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w); |
534 | else |
535 | rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w, |
536 | params->w, 1); |
537 | if (rowlen > 0) { |
538 | for (j = 0; j < rowlen; j++) { |
1185e3c5 |
539 | int len = sprintf(desc+descpos, "%d", rowdata[j]); |
b6b0369e |
540 | if (j+1 < rowlen) |
1185e3c5 |
541 | desc[descpos + len] = '.'; |
b6b0369e |
542 | else |
1185e3c5 |
543 | desc[descpos + len] = '/'; |
544 | descpos += len+1; |
b6b0369e |
545 | } |
546 | } else { |
1185e3c5 |
547 | desc[descpos++] = '/'; |
b6b0369e |
548 | } |
549 | } |
1185e3c5 |
550 | assert(descpos == desclen); |
551 | assert(desc[desclen-1] == '/'); |
552 | desc[desclen-1] = '\0'; |
b6b0369e |
553 | sfree(rowdata); |
871bf294 |
554 | sfree(grid); |
1185e3c5 |
555 | return desc; |
b6b0369e |
556 | } |
557 | |
1185e3c5 |
558 | static char *validate_desc(game_params *params, char *desc) |
b6b0369e |
559 | { |
560 | int i, n, rowspace; |
561 | char *p; |
562 | |
563 | for (i = 0; i < params->w + params->h; i++) { |
564 | if (i < params->w) |
565 | rowspace = params->h + 1; |
566 | else |
567 | rowspace = params->w + 1; |
568 | |
1185e3c5 |
569 | if (*desc && isdigit((unsigned char)*desc)) { |
b6b0369e |
570 | do { |
1185e3c5 |
571 | p = desc; |
9a6d429a |
572 | while (*desc && isdigit((unsigned char)*desc)) desc++; |
b6b0369e |
573 | n = atoi(p); |
574 | rowspace -= n+1; |
575 | |
576 | if (rowspace < 0) { |
577 | if (i < params->w) |
578 | return "at least one column contains more numbers than will fit"; |
579 | else |
580 | return "at least one row contains more numbers than will fit"; |
581 | } |
1185e3c5 |
582 | } while (*desc++ == '.'); |
b6b0369e |
583 | } else { |
1185e3c5 |
584 | desc++; /* expect a slash immediately */ |
b6b0369e |
585 | } |
586 | |
1185e3c5 |
587 | if (desc[-1] == '/') { |
b6b0369e |
588 | if (i+1 == params->w + params->h) |
589 | return "too many row/column specifications"; |
1185e3c5 |
590 | } else if (desc[-1] == '\0') { |
b6b0369e |
591 | if (i+1 < params->w + params->h) |
592 | return "too few row/column specifications"; |
593 | } else |
594 | return "unrecognised character in game specification"; |
595 | } |
596 | |
597 | return NULL; |
598 | } |
599 | |
dafd6cf6 |
600 | static game_state *new_game(midend *me, game_params *params, char *desc) |
b6b0369e |
601 | { |
602 | int i; |
603 | char *p; |
604 | game_state *state = snew(game_state); |
605 | |
606 | state->w = params->w; |
607 | state->h = params->h; |
608 | |
609 | state->grid = snewn(state->w * state->h, unsigned char); |
610 | memset(state->grid, GRID_UNKNOWN, state->w * state->h); |
611 | |
612 | state->rowsize = max(state->w, state->h); |
613 | state->rowdata = snewn(state->rowsize * (state->w + state->h), int); |
614 | state->rowlen = snewn(state->w + state->h, int); |
615 | |
2ac6d24e |
616 | state->completed = state->cheated = FALSE; |
b6b0369e |
617 | |
618 | for (i = 0; i < params->w + params->h; i++) { |
619 | state->rowlen[i] = 0; |
1185e3c5 |
620 | if (*desc && isdigit((unsigned char)*desc)) { |
b6b0369e |
621 | do { |
1185e3c5 |
622 | p = desc; |
9a6d429a |
623 | while (*desc && isdigit((unsigned char)*desc)) desc++; |
b6b0369e |
624 | state->rowdata[state->rowsize * i + state->rowlen[i]++] = |
625 | atoi(p); |
1185e3c5 |
626 | } while (*desc++ == '.'); |
b6b0369e |
627 | } else { |
1185e3c5 |
628 | desc++; /* expect a slash immediately */ |
b6b0369e |
629 | } |
630 | } |
631 | |
632 | return state; |
633 | } |
634 | |
be8d5aa1 |
635 | static game_state *dup_game(game_state *state) |
b6b0369e |
636 | { |
637 | game_state *ret = snew(game_state); |
638 | |
639 | ret->w = state->w; |
640 | ret->h = state->h; |
641 | |
642 | ret->grid = snewn(ret->w * ret->h, unsigned char); |
643 | memcpy(ret->grid, state->grid, ret->w * ret->h); |
644 | |
645 | ret->rowsize = state->rowsize; |
646 | ret->rowdata = snewn(ret->rowsize * (ret->w + ret->h), int); |
647 | ret->rowlen = snewn(ret->w + ret->h, int); |
648 | memcpy(ret->rowdata, state->rowdata, |
649 | ret->rowsize * (ret->w + ret->h) * sizeof(int)); |
650 | memcpy(ret->rowlen, state->rowlen, |
651 | (ret->w + ret->h) * sizeof(int)); |
652 | |
653 | ret->completed = state->completed; |
2ac6d24e |
654 | ret->cheated = state->cheated; |
b6b0369e |
655 | |
656 | return ret; |
657 | } |
658 | |
be8d5aa1 |
659 | static void free_game(game_state *state) |
b6b0369e |
660 | { |
661 | sfree(state->rowdata); |
662 | sfree(state->rowlen); |
663 | sfree(state->grid); |
664 | sfree(state); |
665 | } |
666 | |
df11cd4e |
667 | static char *solve_game(game_state *state, game_state *currstate, |
c566778e |
668 | char *ai, char **error) |
2ac6d24e |
669 | { |
df11cd4e |
670 | unsigned char *matrix; |
df11cd4e |
671 | int w = state->w, h = state->h; |
672 | int i; |
673 | char *ret; |
c566778e |
674 | int done_any, max; |
675 | unsigned char *workspace; |
676 | int *rowdata; |
3220eba4 |
677 | |
2ac6d24e |
678 | /* |
c566778e |
679 | * If we already have the solved state in ai, copy it out. |
2ac6d24e |
680 | */ |
c566778e |
681 | if (ai) |
682 | return dupstr(ai); |
2ac6d24e |
683 | |
c566778e |
684 | matrix = snewn(w*h, unsigned char); |
685 | max = max(w, h); |
686 | workspace = snewn(max*3, unsigned char); |
687 | rowdata = snewn(max+1, int); |
2ac6d24e |
688 | |
c566778e |
689 | memset(matrix, 0, w*h); |
2ac6d24e |
690 | |
c566778e |
691 | do { |
692 | done_any = 0; |
693 | for (i=0; i<h; i++) { |
694 | memcpy(rowdata, state->rowdata + state->rowsize*(w+i), |
695 | max*sizeof(int)); |
696 | rowdata[state->rowlen[w+i]] = 0; |
697 | done_any |= do_row(workspace, workspace+max, workspace+2*max, |
698 | matrix+i*w, w, 1, rowdata); |
699 | } |
700 | for (i=0; i<w; i++) { |
701 | memcpy(rowdata, state->rowdata + state->rowsize*i, max*sizeof(int)); |
702 | rowdata[state->rowlen[i]] = 0; |
703 | done_any |= do_row(workspace, workspace+max, workspace+2*max, |
704 | matrix+i, h, w, rowdata); |
705 | } |
706 | } while (done_any); |
df11cd4e |
707 | |
c566778e |
708 | sfree(workspace); |
709 | sfree(rowdata); |
2ac6d24e |
710 | |
c566778e |
711 | for (i = 0; i < w*h; i++) { |
712 | if (matrix[i] != BLOCK && matrix[i] != DOT) { |
713 | sfree(matrix); |
714 | *error = "Solving algorithm cannot complete this puzzle"; |
715 | return NULL; |
716 | } |
df11cd4e |
717 | } |
718 | |
719 | ret = snewn(w*h+2, char); |
720 | ret[0] = 'S'; |
721 | for (i = 0; i < w*h; i++) { |
c566778e |
722 | assert(matrix[i] == BLOCK || matrix[i] == DOT); |
723 | ret[i+1] = (matrix[i] == BLOCK ? '1' : '0'); |
2ac6d24e |
724 | } |
df11cd4e |
725 | ret[w*h+1] = '\0'; |
726 | |
c566778e |
727 | sfree(matrix); |
2ac6d24e |
728 | |
729 | return ret; |
730 | } |
731 | |
fa3abef5 |
732 | static int game_can_format_as_text_now(game_params *params) |
733 | { |
734 | return TRUE; |
735 | } |
736 | |
9b4b03d3 |
737 | static char *game_text_format(game_state *state) |
738 | { |
739 | return NULL; |
740 | } |
741 | |
b6b0369e |
742 | struct game_ui { |
743 | int dragging; |
744 | int drag_start_x; |
745 | int drag_start_y; |
746 | int drag_end_x; |
747 | int drag_end_y; |
748 | int drag, release, state; |
b2ae5b05 |
749 | int cur_x, cur_y, cur_visible; |
b6b0369e |
750 | }; |
751 | |
be8d5aa1 |
752 | static game_ui *new_ui(game_state *state) |
b6b0369e |
753 | { |
754 | game_ui *ret; |
755 | |
756 | ret = snew(game_ui); |
757 | ret->dragging = FALSE; |
b2ae5b05 |
758 | ret->cur_x = ret->cur_y = ret->cur_visible = 0; |
b6b0369e |
759 | |
760 | return ret; |
761 | } |
762 | |
be8d5aa1 |
763 | static void free_ui(game_ui *ui) |
b6b0369e |
764 | { |
765 | sfree(ui); |
766 | } |
767 | |
844f605f |
768 | static char *encode_ui(game_ui *ui) |
ae8290c6 |
769 | { |
770 | return NULL; |
771 | } |
772 | |
844f605f |
773 | static void decode_ui(game_ui *ui, char *encoding) |
ae8290c6 |
774 | { |
775 | } |
776 | |
07dfb697 |
777 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
778 | game_state *newstate) |
779 | { |
780 | } |
781 | |
1e3e152d |
782 | struct game_drawstate { |
783 | int started; |
784 | int w, h; |
785 | int tilesize; |
786 | unsigned char *visible; |
b2ae5b05 |
787 | int cur_x, cur_y; |
1e3e152d |
788 | }; |
789 | |
df11cd4e |
790 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
791 | int x, int y, int button) |
792 | { |
f0ee053c |
793 | button &= ~MOD_MASK; |
794 | |
df11cd4e |
795 | x = FROMCOORD(state->w, x); |
796 | y = FROMCOORD(state->h, y); |
b6b0369e |
797 | |
df11cd4e |
798 | if (x >= 0 && x < state->w && y >= 0 && y < state->h && |
b6b0369e |
799 | (button == LEFT_BUTTON || button == RIGHT_BUTTON || |
800 | button == MIDDLE_BUTTON)) { |
242a7d91 |
801 | #ifdef STYLUS_BASED |
802 | int currstate = state->grid[y * state->w + x]; |
803 | #endif |
b6b0369e |
804 | |
805 | ui->dragging = TRUE; |
806 | |
807 | if (button == LEFT_BUTTON) { |
808 | ui->drag = LEFT_DRAG; |
809 | ui->release = LEFT_RELEASE; |
242a7d91 |
810 | #ifdef STYLUS_BASED |
80e7e37c |
811 | ui->state = (currstate + 2) % 3; /* FULL -> EMPTY -> UNKNOWN */ |
242a7d91 |
812 | #else |
b6b0369e |
813 | ui->state = GRID_FULL; |
242a7d91 |
814 | #endif |
b6b0369e |
815 | } else if (button == RIGHT_BUTTON) { |
816 | ui->drag = RIGHT_DRAG; |
817 | ui->release = RIGHT_RELEASE; |
242a7d91 |
818 | #ifdef STYLUS_BASED |
80e7e37c |
819 | ui->state = (currstate + 1) % 3; /* EMPTY -> FULL -> UNKNOWN */ |
242a7d91 |
820 | #else |
b6b0369e |
821 | ui->state = GRID_EMPTY; |
242a7d91 |
822 | #endif |
b6b0369e |
823 | } else /* if (button == MIDDLE_BUTTON) */ { |
824 | ui->drag = MIDDLE_DRAG; |
825 | ui->release = MIDDLE_RELEASE; |
826 | ui->state = GRID_UNKNOWN; |
827 | } |
828 | |
829 | ui->drag_start_x = ui->drag_end_x = x; |
830 | ui->drag_start_y = ui->drag_end_y = y; |
b2ae5b05 |
831 | ui->cur_visible = 0; |
b6b0369e |
832 | |
df11cd4e |
833 | return ""; /* UI activity occurred */ |
b6b0369e |
834 | } |
835 | |
836 | if (ui->dragging && button == ui->drag) { |
837 | /* |
838 | * There doesn't seem much point in allowing a rectangle |
839 | * drag; people will generally only want to drag a single |
840 | * horizontal or vertical line, so we make that easy by |
841 | * snapping to it. |
842 | * |
843 | * Exception: if we're _middle_-button dragging to tag |
844 | * things as UNKNOWN, we may well want to trash an entire |
845 | * area and start over! |
846 | */ |
847 | if (ui->state != GRID_UNKNOWN) { |
848 | if (abs(x - ui->drag_start_x) > abs(y - ui->drag_start_y)) |
849 | y = ui->drag_start_y; |
850 | else |
851 | x = ui->drag_start_x; |
852 | } |
853 | |
854 | if (x < 0) x = 0; |
855 | if (y < 0) y = 0; |
df11cd4e |
856 | if (x >= state->w) x = state->w - 1; |
857 | if (y >= state->h) y = state->h - 1; |
b6b0369e |
858 | |
859 | ui->drag_end_x = x; |
860 | ui->drag_end_y = y; |
861 | |
df11cd4e |
862 | return ""; /* UI activity occurred */ |
b6b0369e |
863 | } |
864 | |
865 | if (ui->dragging && button == ui->release) { |
866 | int x1, x2, y1, y2, xx, yy; |
867 | int move_needed = FALSE; |
868 | |
869 | x1 = min(ui->drag_start_x, ui->drag_end_x); |
870 | x2 = max(ui->drag_start_x, ui->drag_end_x); |
871 | y1 = min(ui->drag_start_y, ui->drag_end_y); |
872 | y2 = max(ui->drag_start_y, ui->drag_end_y); |
873 | |
874 | for (yy = y1; yy <= y2; yy++) |
875 | for (xx = x1; xx <= x2; xx++) |
df11cd4e |
876 | if (state->grid[yy * state->w + xx] != ui->state) |
b6b0369e |
877 | move_needed = TRUE; |
878 | |
879 | ui->dragging = FALSE; |
880 | |
881 | if (move_needed) { |
df11cd4e |
882 | char buf[80]; |
883 | sprintf(buf, "%c%d,%d,%d,%d", |
871bf294 |
884 | (char)(ui->state == GRID_FULL ? 'F' : |
885 | ui->state == GRID_EMPTY ? 'E' : 'U'), |
df11cd4e |
886 | x1, y1, x2-x1+1, y2-y1+1); |
887 | return dupstr(buf); |
b6b0369e |
888 | } else |
df11cd4e |
889 | return ""; /* UI activity occurred */ |
b6b0369e |
890 | } |
891 | |
b2ae5b05 |
892 | if (IS_CURSOR_MOVE(button)) { |
893 | move_cursor(button, &ui->cur_x, &ui->cur_y, state->w, state->h, 0); |
894 | ui->cur_visible = 1; |
895 | return ""; |
896 | } |
897 | if (IS_CURSOR_SELECT(button)) { |
898 | int currstate = state->grid[ui->cur_y * state->w + ui->cur_x]; |
899 | int newstate; |
900 | char buf[80]; |
901 | |
902 | if (!ui->cur_visible) { |
903 | ui->cur_visible = 1; |
904 | return ""; |
905 | } |
906 | |
907 | if (button == CURSOR_SELECT2) |
908 | newstate = currstate == GRID_UNKNOWN ? GRID_EMPTY : |
909 | currstate == GRID_EMPTY ? GRID_FULL : GRID_UNKNOWN; |
910 | else |
911 | newstate = currstate == GRID_UNKNOWN ? GRID_FULL : |
912 | currstate == GRID_FULL ? GRID_EMPTY : GRID_UNKNOWN; |
913 | |
914 | sprintf(buf, "%c%d,%d,%d,%d", |
915 | (char)(newstate == GRID_FULL ? 'F' : |
916 | newstate == GRID_EMPTY ? 'E' : 'U'), |
917 | ui->cur_x, ui->cur_y, 1, 1); |
918 | return dupstr(buf); |
919 | } |
920 | |
b6b0369e |
921 | return NULL; |
922 | } |
923 | |
df11cd4e |
924 | static game_state *execute_move(game_state *from, char *move) |
925 | { |
926 | game_state *ret; |
927 | int x1, x2, y1, y2, xx, yy; |
928 | int val; |
929 | |
930 | if (move[0] == 'S' && strlen(move) == from->w * from->h + 1) { |
931 | int i; |
932 | |
933 | ret = dup_game(from); |
934 | |
935 | for (i = 0; i < ret->w * ret->h; i++) |
936 | ret->grid[i] = (move[i+1] == '1' ? GRID_FULL : GRID_EMPTY); |
937 | |
938 | ret->completed = ret->cheated = TRUE; |
939 | |
940 | return ret; |
941 | } else if ((move[0] == 'F' || move[0] == 'E' || move[0] == 'U') && |
942 | sscanf(move+1, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) == 4 && |
943 | x1 >= 0 && x2 >= 0 && x1+x2 <= from->w && |
944 | y1 >= 0 && y2 >= 0 && y1+y2 <= from->h) { |
945 | |
946 | x2 += x1; |
947 | y2 += y1; |
948 | val = (move[0] == 'F' ? GRID_FULL : |
949 | move[0] == 'E' ? GRID_EMPTY : GRID_UNKNOWN); |
950 | |
951 | ret = dup_game(from); |
952 | for (yy = y1; yy < y2; yy++) |
953 | for (xx = x1; xx < x2; xx++) |
954 | ret->grid[yy * ret->w + xx] = val; |
955 | |
956 | /* |
957 | * An actual change, so check to see if we've completed the |
958 | * game. |
959 | */ |
960 | if (!ret->completed) { |
961 | int *rowdata = snewn(ret->rowsize, int); |
962 | int i, len; |
963 | |
964 | ret->completed = TRUE; |
965 | |
966 | for (i=0; i<ret->w; i++) { |
967 | len = compute_rowdata(rowdata, |
968 | ret->grid+i, ret->h, ret->w); |
969 | if (len != ret->rowlen[i] || |
970 | memcmp(ret->rowdata+i*ret->rowsize, rowdata, |
971 | len * sizeof(int))) { |
972 | ret->completed = FALSE; |
973 | break; |
974 | } |
975 | } |
976 | for (i=0; i<ret->h; i++) { |
977 | len = compute_rowdata(rowdata, |
978 | ret->grid+i*ret->w, ret->w, 1); |
979 | if (len != ret->rowlen[i+ret->w] || |
980 | memcmp(ret->rowdata+(i+ret->w)*ret->rowsize, rowdata, |
981 | len * sizeof(int))) { |
982 | ret->completed = FALSE; |
983 | break; |
984 | } |
985 | } |
986 | |
987 | sfree(rowdata); |
988 | } |
989 | |
990 | return ret; |
991 | } else |
992 | return NULL; |
993 | } |
994 | |
b6b0369e |
995 | /* ---------------------------------------------------------------------- |
996 | * Drawing routines. |
997 | */ |
998 | |
1f3ee4ee |
999 | static void game_compute_size(game_params *params, int tilesize, |
1000 | int *x, int *y) |
b6b0369e |
1001 | { |
1f3ee4ee |
1002 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1003 | struct { int tilesize; } ads, *ds = &ads; |
1004 | ads.tilesize = tilesize; |
1e3e152d |
1005 | |
b6b0369e |
1006 | *x = SIZE(params->w); |
1007 | *y = SIZE(params->h); |
1008 | } |
1009 | |
dafd6cf6 |
1010 | static void game_set_size(drawing *dr, game_drawstate *ds, |
1011 | game_params *params, int tilesize) |
1f3ee4ee |
1012 | { |
1013 | ds->tilesize = tilesize; |
1014 | } |
1015 | |
8266f3fc |
1016 | static float *game_colours(frontend *fe, int *ncolours) |
b6b0369e |
1017 | { |
1018 | float *ret = snewn(3 * NCOLOURS, float); |
b2ae5b05 |
1019 | int i; |
b6b0369e |
1020 | |
1021 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
1022 | |
b2ae5b05 |
1023 | for (i = 0; i < 3; i++) { |
1024 | ret[COL_GRID * 3 + i] = 0.3F; |
1025 | ret[COL_UNKNOWN * 3 + i] = 0.5F; |
1026 | ret[COL_TEXT * 3 + i] = 0.0F; |
1027 | ret[COL_FULL * 3 + i] = 0.0F; |
1028 | ret[COL_EMPTY * 3 + i] = 1.0F; |
1029 | } |
1030 | ret[COL_CURSOR * 3 + 0] = 1.0F; |
1031 | ret[COL_CURSOR * 3 + 1] = 0.25F; |
1032 | ret[COL_CURSOR * 3 + 2] = 0.25F; |
b6b0369e |
1033 | |
1034 | *ncolours = NCOLOURS; |
1035 | return ret; |
1036 | } |
1037 | |
dafd6cf6 |
1038 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
b6b0369e |
1039 | { |
1040 | struct game_drawstate *ds = snew(struct game_drawstate); |
1041 | |
1042 | ds->started = FALSE; |
1043 | ds->w = state->w; |
1044 | ds->h = state->h; |
1045 | ds->visible = snewn(ds->w * ds->h, unsigned char); |
1e3e152d |
1046 | ds->tilesize = 0; /* not decided yet */ |
b6b0369e |
1047 | memset(ds->visible, 255, ds->w * ds->h); |
8719c2e7 |
1048 | ds->cur_x = ds->cur_y = 0; |
b6b0369e |
1049 | |
1050 | return ds; |
1051 | } |
1052 | |
dafd6cf6 |
1053 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
b6b0369e |
1054 | { |
1055 | sfree(ds->visible); |
1056 | sfree(ds); |
1057 | } |
1058 | |
dafd6cf6 |
1059 | static void grid_square(drawing *dr, game_drawstate *ds, |
b2ae5b05 |
1060 | int y, int x, int state, int cur) |
b6b0369e |
1061 | { |
b2ae5b05 |
1062 | int xl, xr, yt, yb, dx, dy, dw, dh; |
b6b0369e |
1063 | |
dafd6cf6 |
1064 | draw_rect(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y), |
b6b0369e |
1065 | TILE_SIZE, TILE_SIZE, COL_GRID); |
1066 | |
1067 | xl = (x % 5 == 0 ? 1 : 0); |
1068 | yt = (y % 5 == 0 ? 1 : 0); |
1069 | xr = (x % 5 == 4 || x == ds->w-1 ? 1 : 0); |
1070 | yb = (y % 5 == 4 || y == ds->h-1 ? 1 : 0); |
1071 | |
b2ae5b05 |
1072 | dx = TOCOORD(ds->w, x) + 1 + xl; |
1073 | dy = TOCOORD(ds->h, y) + 1 + yt; |
1074 | dw = TILE_SIZE - xl - xr - 1; |
1075 | dh = TILE_SIZE - yt - yb - 1; |
1076 | |
1077 | draw_rect(dr, dx, dy, dw, dh, |
b6b0369e |
1078 | (state == GRID_FULL ? COL_FULL : |
1079 | state == GRID_EMPTY ? COL_EMPTY : COL_UNKNOWN)); |
b2ae5b05 |
1080 | if (cur) { |
1081 | draw_rect_outline(dr, dx, dy, dw, dh, COL_CURSOR); |
1082 | draw_rect_outline(dr, dx+1, dy+1, dw-2, dh-2, COL_CURSOR); |
1083 | } |
b6b0369e |
1084 | |
dafd6cf6 |
1085 | draw_update(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y), |
b6b0369e |
1086 | TILE_SIZE, TILE_SIZE); |
1087 | } |
1088 | |
dafd6cf6 |
1089 | static void draw_numbers(drawing *dr, game_drawstate *ds, game_state *state, |
1090 | int colour) |
1091 | { |
1092 | int i, j; |
1093 | |
1094 | /* |
1095 | * Draw the numbers. |
1096 | */ |
1097 | for (i = 0; i < state->w + state->h; i++) { |
1098 | int rowlen = state->rowlen[i]; |
1099 | int *rowdata = state->rowdata + state->rowsize * i; |
1100 | int nfit; |
1101 | |
1102 | /* |
1103 | * Normally I space the numbers out by the same |
1104 | * distance as the tile size. However, if there are |
1105 | * more numbers than available spaces, I have to squash |
1106 | * them up a bit. |
1107 | */ |
1108 | nfit = max(rowlen, TLBORDER(state->h))-1; |
1109 | assert(nfit > 0); |
1110 | |
1111 | for (j = 0; j < rowlen; j++) { |
1112 | int x, y; |
1113 | char str[80]; |
1114 | |
1115 | if (i < state->w) { |
1116 | x = TOCOORD(state->w, i); |
1117 | y = BORDER + TILE_SIZE * (TLBORDER(state->h)-1); |
1118 | y -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(state->h)-1) / nfit; |
1119 | } else { |
1120 | y = TOCOORD(state->h, i - state->w); |
1121 | x = BORDER + TILE_SIZE * (TLBORDER(state->w)-1); |
1122 | x -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(state->h)-1) / nfit; |
1123 | } |
1124 | |
1125 | sprintf(str, "%d", rowdata[j]); |
1126 | draw_text(dr, x+TILE_SIZE/2, y+TILE_SIZE/2, FONT_VARIABLE, |
1127 | TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, colour, str); |
1128 | } |
1129 | } |
1130 | } |
1131 | |
1132 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
1e3e152d |
1133 | game_state *state, int dir, game_ui *ui, |
1134 | float animtime, float flashtime) |
b6b0369e |
1135 | { |
1136 | int i, j; |
1137 | int x1, x2, y1, y2; |
b2ae5b05 |
1138 | int cx, cy, cmoved; |
b6b0369e |
1139 | |
1140 | if (!ds->started) { |
1141 | /* |
1142 | * The initial contents of the window are not guaranteed |
1143 | * and can vary with front ends. To be on the safe side, |
1144 | * all games should start by drawing a big background- |
1145 | * colour rectangle covering the whole window. |
1146 | */ |
dafd6cf6 |
1147 | draw_rect(dr, 0, 0, SIZE(ds->w), SIZE(ds->h), COL_BACKGROUND); |
b6b0369e |
1148 | |
dafd6cf6 |
1149 | /* |
1150 | * Draw the numbers. |
1151 | */ |
1152 | draw_numbers(dr, ds, state, COL_TEXT); |
b6b0369e |
1153 | |
1154 | /* |
1155 | * Draw the grid outline. |
1156 | */ |
dafd6cf6 |
1157 | draw_rect(dr, TOCOORD(ds->w, 0) - 1, TOCOORD(ds->h, 0) - 1, |
95eedaa6 |
1158 | ds->w * TILE_SIZE + 3, ds->h * TILE_SIZE + 3, |
b6b0369e |
1159 | COL_GRID); |
1160 | |
1161 | ds->started = TRUE; |
1162 | |
dafd6cf6 |
1163 | draw_update(dr, 0, 0, SIZE(ds->w), SIZE(ds->h)); |
b6b0369e |
1164 | } |
1165 | |
1166 | if (ui->dragging) { |
1167 | x1 = min(ui->drag_start_x, ui->drag_end_x); |
1168 | x2 = max(ui->drag_start_x, ui->drag_end_x); |
1169 | y1 = min(ui->drag_start_y, ui->drag_end_y); |
1170 | y2 = max(ui->drag_start_y, ui->drag_end_y); |
1171 | } else { |
1172 | x1 = x2 = y1 = y2 = -1; /* placate gcc warnings */ |
1173 | } |
1174 | |
b2ae5b05 |
1175 | if (ui->cur_visible) { |
1176 | cx = ui->cur_x; cy = ui->cur_y; |
1177 | } else { |
1178 | cx = cy = -1; |
1179 | } |
1180 | cmoved = (cx != ds->cur_x || cy != ds->cur_y); |
1181 | |
b6b0369e |
1182 | /* |
1183 | * Now draw any grid squares which have changed since last |
1184 | * redraw. |
1185 | */ |
1186 | for (i = 0; i < ds->h; i++) { |
1187 | for (j = 0; j < ds->w; j++) { |
b2ae5b05 |
1188 | int val, cc = 0; |
b6b0369e |
1189 | |
1190 | /* |
1191 | * Work out what state this square should be drawn in, |
1192 | * taking any current drag operation into account. |
1193 | */ |
1194 | if (ui->dragging && x1 <= j && j <= x2 && y1 <= i && i <= y2) |
1195 | val = ui->state; |
1196 | else |
1197 | val = state->grid[i * state->w + j]; |
1198 | |
b2ae5b05 |
1199 | if (cmoved) { |
1200 | /* the cursor has moved; if we were the old or |
1201 | * the new cursor position we need to redraw. */ |
1202 | if (j == cx && i == cy) cc = 1; |
1203 | if (j == ds->cur_x && i == ds->cur_y) cc = 1; |
1204 | } |
1205 | |
b6b0369e |
1206 | /* |
1207 | * Briefly invert everything twice during a completion |
1208 | * flash. |
1209 | */ |
1210 | if (flashtime > 0 && |
1211 | (flashtime <= FLASH_TIME/3 || flashtime >= FLASH_TIME*2/3) && |
1212 | val != GRID_UNKNOWN) |
1213 | val = (GRID_FULL ^ GRID_EMPTY) ^ val; |
1214 | |
b2ae5b05 |
1215 | if (ds->visible[i * ds->w + j] != val || cc) { |
1216 | grid_square(dr, ds, i, j, val, |
1217 | (j == cx && i == cy)); |
b6b0369e |
1218 | ds->visible[i * ds->w + j] = val; |
1219 | } |
1220 | } |
1221 | } |
b2ae5b05 |
1222 | ds->cur_x = cx; ds->cur_y = cy; |
b6b0369e |
1223 | } |
1224 | |
be8d5aa1 |
1225 | static float game_anim_length(game_state *oldstate, |
e3f21163 |
1226 | game_state *newstate, int dir, game_ui *ui) |
b6b0369e |
1227 | { |
1228 | return 0.0F; |
1229 | } |
1230 | |
be8d5aa1 |
1231 | static float game_flash_length(game_state *oldstate, |
e3f21163 |
1232 | game_state *newstate, int dir, game_ui *ui) |
b6b0369e |
1233 | { |
2ac6d24e |
1234 | if (!oldstate->completed && newstate->completed && |
1235 | !oldstate->cheated && !newstate->cheated) |
b6b0369e |
1236 | return FLASH_TIME; |
1237 | return 0.0F; |
1238 | } |
1239 | |
4d08de49 |
1240 | static int game_timing_state(game_state *state, game_ui *ui) |
48dcdd62 |
1241 | { |
1242 | return TRUE; |
1243 | } |
1244 | |
dafd6cf6 |
1245 | static void game_print_size(game_params *params, float *x, float *y) |
1246 | { |
1247 | int pw, ph; |
1248 | |
1249 | /* |
1250 | * I'll use 5mm squares by default. |
1251 | */ |
1252 | game_compute_size(params, 500, &pw, &ph); |
b2ae5b05 |
1253 | *x = pw / 100.0F; |
1254 | *y = ph / 100.0F; |
dafd6cf6 |
1255 | } |
1256 | |
1257 | static void game_print(drawing *dr, game_state *state, int tilesize) |
1258 | { |
1259 | int w = state->w, h = state->h; |
1260 | int ink = print_mono_colour(dr, 0); |
1261 | int x, y; |
1262 | |
1263 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1264 | game_drawstate ads, *ds = &ads; |
4413ef0f |
1265 | game_set_size(dr, ds, NULL, tilesize); |
dafd6cf6 |
1266 | |
1267 | /* |
1268 | * Border. |
1269 | */ |
1270 | print_line_width(dr, TILE_SIZE / 16); |
1271 | draw_rect_outline(dr, TOCOORD(w, 0), TOCOORD(h, 0), |
1272 | w*TILE_SIZE, h*TILE_SIZE, ink); |
1273 | |
1274 | /* |
1275 | * Grid. |
1276 | */ |
1277 | for (x = 1; x < w; x++) { |
1278 | print_line_width(dr, TILE_SIZE / (x % 5 ? 128 : 24)); |
1279 | draw_line(dr, TOCOORD(w, x), TOCOORD(h, 0), |
1280 | TOCOORD(w, x), TOCOORD(h, h), ink); |
1281 | } |
1282 | for (y = 1; y < h; y++) { |
1283 | print_line_width(dr, TILE_SIZE / (y % 5 ? 128 : 24)); |
1284 | draw_line(dr, TOCOORD(w, 0), TOCOORD(h, y), |
1285 | TOCOORD(w, w), TOCOORD(h, y), ink); |
1286 | } |
1287 | |
1288 | /* |
1289 | * Clues. |
1290 | */ |
1291 | draw_numbers(dr, ds, state, ink); |
1292 | |
1293 | /* |
1294 | * Solution. |
1295 | */ |
1296 | print_line_width(dr, TILE_SIZE / 128); |
1297 | for (y = 0; y < h; y++) |
1298 | for (x = 0; x < w; x++) { |
1299 | if (state->grid[y*w+x] == GRID_FULL) |
1300 | draw_rect(dr, TOCOORD(w, x), TOCOORD(h, y), |
1301 | TILE_SIZE, TILE_SIZE, ink); |
1302 | else if (state->grid[y*w+x] == GRID_EMPTY) |
1303 | draw_circle(dr, TOCOORD(w, x) + TILE_SIZE/2, |
1304 | TOCOORD(h, y) + TILE_SIZE/2, |
1305 | TILE_SIZE/12, ink, ink); |
1306 | } |
1307 | } |
1308 | |
be8d5aa1 |
1309 | #ifdef COMBINED |
1310 | #define thegame pattern |
1311 | #endif |
1312 | |
1313 | const struct game thegame = { |
750037d7 |
1314 | "Pattern", "games.pattern", "pattern", |
be8d5aa1 |
1315 | default_params, |
1316 | game_fetch_preset, |
1317 | decode_params, |
1318 | encode_params, |
1319 | free_params, |
1320 | dup_params, |
1d228b10 |
1321 | TRUE, game_configure, custom_params, |
be8d5aa1 |
1322 | validate_params, |
1185e3c5 |
1323 | new_game_desc, |
1185e3c5 |
1324 | validate_desc, |
be8d5aa1 |
1325 | new_game, |
1326 | dup_game, |
1327 | free_game, |
2ac6d24e |
1328 | TRUE, solve_game, |
fa3abef5 |
1329 | FALSE, game_can_format_as_text_now, game_text_format, |
be8d5aa1 |
1330 | new_ui, |
1331 | free_ui, |
ae8290c6 |
1332 | encode_ui, |
1333 | decode_ui, |
07dfb697 |
1334 | game_changed_state, |
df11cd4e |
1335 | interpret_move, |
1336 | execute_move, |
1f3ee4ee |
1337 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
be8d5aa1 |
1338 | game_colours, |
1339 | game_new_drawstate, |
1340 | game_free_drawstate, |
1341 | game_redraw, |
1342 | game_anim_length, |
1343 | game_flash_length, |
dafd6cf6 |
1344 | TRUE, FALSE, game_print_size, game_print, |
ac9f41c4 |
1345 | FALSE, /* wants_statusbar */ |
48dcdd62 |
1346 | FALSE, game_timing_state, |
cb0c7d4a |
1347 | REQUIRE_RBUTTON, /* flags */ |
be8d5aa1 |
1348 | }; |
329b3f06 |
1349 | |
1350 | #ifdef STANDALONE_SOLVER |
1351 | |
329b3f06 |
1352 | int main(int argc, char **argv) |
1353 | { |
1354 | game_params *p; |
1355 | game_state *s; |
1185e3c5 |
1356 | char *id = NULL, *desc, *err; |
329b3f06 |
1357 | |
1358 | while (--argc > 0) { |
1359 | char *p = *++argv; |
1360 | if (*p == '-') { |
8317499a |
1361 | fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); |
329b3f06 |
1362 | return 1; |
1363 | } else { |
1364 | id = p; |
1365 | } |
1366 | } |
1367 | |
1368 | if (!id) { |
1369 | fprintf(stderr, "usage: %s <game_id>\n", argv[0]); |
1370 | return 1; |
1371 | } |
1372 | |
1185e3c5 |
1373 | desc = strchr(id, ':'); |
1374 | if (!desc) { |
329b3f06 |
1375 | fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); |
1376 | return 1; |
1377 | } |
1185e3c5 |
1378 | *desc++ = '\0'; |
329b3f06 |
1379 | |
1733f4ca |
1380 | p = default_params(); |
1381 | decode_params(p, id); |
1185e3c5 |
1382 | err = validate_desc(p, desc); |
329b3f06 |
1383 | if (err) { |
1384 | fprintf(stderr, "%s: %s\n", argv[0], err); |
1385 | return 1; |
1386 | } |
39d682c9 |
1387 | s = new_game(NULL, p, desc); |
329b3f06 |
1388 | |
1389 | { |
1390 | int w = p->w, h = p->h, i, j, done_any, max; |
1391 | unsigned char *matrix, *workspace; |
1392 | int *rowdata; |
1393 | |
1394 | matrix = snewn(w*h, unsigned char); |
1395 | max = max(w, h); |
1396 | workspace = snewn(max*3, unsigned char); |
1397 | rowdata = snewn(max+1, int); |
1398 | |
1399 | memset(matrix, 0, w*h); |
1400 | |
1401 | do { |
1402 | done_any = 0; |
1403 | for (i=0; i<h; i++) { |
1404 | memcpy(rowdata, s->rowdata + s->rowsize*(w+i), |
1405 | max*sizeof(int)); |
1406 | rowdata[s->rowlen[w+i]] = 0; |
1407 | done_any |= do_row(workspace, workspace+max, workspace+2*max, |
1408 | matrix+i*w, w, 1, rowdata); |
1409 | } |
1410 | for (i=0; i<w; i++) { |
1411 | memcpy(rowdata, s->rowdata + s->rowsize*i, max*sizeof(int)); |
1412 | rowdata[s->rowlen[i]] = 0; |
1413 | done_any |= do_row(workspace, workspace+max, workspace+2*max, |
1414 | matrix+i, h, w, rowdata); |
1415 | } |
1416 | } while (done_any); |
1417 | |
1418 | for (i = 0; i < h; i++) { |
1419 | for (j = 0; j < w; j++) { |
1420 | int c = (matrix[i*w+j] == UNKNOWN ? '?' : |
1421 | matrix[i*w+j] == BLOCK ? '#' : |
1422 | matrix[i*w+j] == DOT ? '.' : |
1423 | '!'); |
1424 | putchar(c); |
1425 | } |
1426 | printf("\n"); |
1427 | } |
1428 | } |
1429 | |
1430 | return 0; |
1431 | } |
1432 | |
1433 | #endif |
b2ae5b05 |
1434 | |
1435 | /* vim: set shiftwidth=4 tabstop=8: */ |