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