2 * pattern.c: the pattern-reconstruction game known as `nonograms'.
14 #define max(x,y) ( (x)>(y) ? (x):(y) )
15 #define min(x,y) ( (x)<(y) ? (x):(y) )
27 #define TLBORDER(d) ( (d) / 5 + 2 )
31 #define FROMCOORD(d, x) \
32 ( ((x) - (BORDER + GUTTER + TILE_SIZE * TLBORDER(d))) / TILE_SIZE )
34 #define SIZE(d) (2*BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (d)))
36 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
42 #define GRID_UNKNOWN 2
50 int *rowdata, *rowlen;
51 int completed, cheated;
54 #define FLASH_TIME 0.13F
56 static game_params *default_params(void)
58 game_params *ret = snew(game_params);
65 static int game_fetch_preset(int i, char **name, game_params **params)
69 static const struct { int x, y; } values[] = {
77 if (i < 0 || i >= lenof(values))
80 ret = snew(game_params);
84 sprintf(str, "%dx%d", ret->w, ret->h);
91 static void free_params(game_params *params)
96 static game_params *dup_params(game_params *params)
98 game_params *ret = snew(game_params);
99 *ret = *params; /* structure copy */
103 static game_params *decode_params(char const *string)
105 game_params *ret = default_params();
106 char const *p = string;
109 while (*p && isdigit(*p)) p++;
113 while (*p && isdigit(*p)) p++;
121 static char *encode_params(game_params *params)
126 len = sprintf(ret, "%dx%d", params->w, params->h);
127 assert(len < lenof(ret));
133 static config_item *game_configure(game_params *params)
138 ret = snewn(3, config_item);
140 ret[0].name = "Width";
141 ret[0].type = C_STRING;
142 sprintf(buf, "%d", params->w);
143 ret[0].sval = dupstr(buf);
146 ret[1].name = "Height";
147 ret[1].type = C_STRING;
148 sprintf(buf, "%d", params->h);
149 ret[1].sval = dupstr(buf);
160 static game_params *custom_params(config_item *cfg)
162 game_params *ret = snew(game_params);
164 ret->w = atoi(cfg[0].sval);
165 ret->h = atoi(cfg[1].sval);
170 static char *validate_params(game_params *params)
172 if (params->w <= 0 && params->h <= 0)
173 return "Width and height must both be greater than zero";
175 return "Width must be greater than zero";
177 return "Height must be greater than zero";
181 /* ----------------------------------------------------------------------
182 * Puzzle generation code.
184 * For this particular puzzle, it seemed important to me to ensure
185 * a unique solution. I do this the brute-force way, by having a
186 * solver algorithm alongside the generator, and repeatedly
187 * generating a random grid until I find one whose solution is
188 * unique. It turns out that this isn't too onerous on a modern PC
189 * provided you keep grid size below around 30. Any offers of
190 * better algorithms, however, will be very gratefully received.
192 * Another annoyance of this approach is that it limits the
193 * available puzzles to those solvable by the algorithm I've used.
194 * My algorithm only ever considers a single row or column at any
195 * one time, which means it's incapable of solving the following
196 * difficult example (found by Bella Image around 1995/6, when she
197 * and I were both doing maths degrees):
211 * Obviously this cannot be solved by a one-row-or-column-at-a-time
212 * algorithm (it would require at least one row or column reading
213 * `2 1', `1 2', `3' or `4' to get started). However, it can be
214 * proved to have a unique solution: if the top left square were
215 * empty, then the only option for the top row would be to fill the
216 * two squares in the 1 columns, which would imply the squares
217 * below those were empty, leaving no place for the 2 in the second
218 * row. Contradiction. Hence the top left square is full, and the
219 * unique solution follows easily from that starting point.
221 * (The game ID for this puzzle is 4x4:2/1/2/1/1.1/2/1/1 , in case
222 * it's useful to anyone.)
225 static int float_compare(const void *av, const void *bv)
227 const float *a = (const float *)av;
228 const float *b = (const float *)bv;
237 static void generate(random_state *rs, int w, int h, unsigned char *retgrid)
244 fgrid = snewn(w*h, float);
246 for (i = 0; i < h; i++) {
247 for (j = 0; j < w; j++) {
248 fgrid[i*w+j] = random_upto(rs, 100000000UL) / 100000000.F;
253 * The above gives a completely random splattering of black and
254 * white cells. We want to gently bias this in favour of _some_
255 * reasonably thick areas of white and black, while retaining
256 * some randomness and fine detail.
258 * So we evolve the starting grid using a cellular automaton.
259 * Currently, I'm doing something very simple indeed, which is
260 * to set each square to the average of the surrounding nine
261 * cells (or the average of fewer, if we're on a corner).
263 for (step = 0; step < 1; step++) {
264 fgrid2 = snewn(w*h, float);
266 for (i = 0; i < h; i++) {
267 for (j = 0; j < w; j++) {
272 * Compute the average of the surrounding cells.
276 for (p = -1; p <= +1; p++) {
277 for (q = -1; q <= +1; q++) {
278 if (i+p < 0 || i+p >= h || j+q < 0 || j+q >= w)
281 * An additional special case not mentioned
282 * above: if a grid dimension is 2xn then
283 * we do not average across that dimension
284 * at all. Otherwise a 2x2 grid would
285 * contain four identical squares.
287 if ((h==2 && p!=0) || (w==2 && q!=0))
290 sx += fgrid[(i+p)*w+(j+q)];
295 fgrid2[i*w+j] = xbar;
303 fgrid2 = snewn(w*h, float);
304 memcpy(fgrid2, fgrid, w*h*sizeof(float));
305 qsort(fgrid2, w*h, sizeof(float), float_compare);
306 threshold = fgrid2[w*h/2];
309 for (i = 0; i < h; i++) {
310 for (j = 0; j < w; j++) {
311 retgrid[i*w+j] = (fgrid[i*w+j] >= threshold ? GRID_FULL :
319 static int compute_rowdata(int *ret, unsigned char *start, int len, int step)
325 for (i = 0; i < len; i++) {
326 if (start[i*step] == GRID_FULL) {
328 while (i+runlen < len && start[(i+runlen)*step] == GRID_FULL)
334 if (i < len && start[i*step] == GRID_UNKNOWN)
344 #define STILL_UNKNOWN 3
346 static void do_recurse(unsigned char *known, unsigned char *deduced,
347 unsigned char *row, int *data, int len,
348 int freespace, int ndone, int lowest)
353 for (i=0; i<=freespace; i++) {
355 for (k=0; k<i; k++) row[j++] = DOT;
356 for (k=0; k<data[ndone]; k++) row[j++] = BLOCK;
357 if (j < len) row[j++] = DOT;
358 do_recurse(known, deduced, row, data, len,
359 freespace-i, ndone+1, j);
362 for (i=lowest; i<len; i++)
364 for (i=0; i<len; i++)
365 if (known[i] && known[i] != row[i])
367 for (i=0; i<len; i++)
368 deduced[i] |= row[i];
372 static int do_row(unsigned char *known, unsigned char *deduced,
374 unsigned char *start, int len, int step, int *data)
376 int rowlen, i, freespace, done_any;
379 for (rowlen = 0; data[rowlen]; rowlen++)
380 freespace -= data[rowlen]+1;
382 for (i = 0; i < len; i++) {
383 known[i] = start[i*step];
387 do_recurse(known, deduced, row, data, len, freespace, 0, 0);
389 for (i=0; i<len; i++)
390 if (deduced[i] && deduced[i] != STILL_UNKNOWN && !known[i]) {
391 start[i*step] = deduced[i];
397 static unsigned char *generate_soluble(random_state *rs, int w, int h)
399 int i, j, done_any, ok, ntries, max;
400 unsigned char *grid, *matrix, *workspace;
403 grid = snewn(w*h, unsigned char);
404 matrix = snewn(w*h, unsigned char);
406 workspace = snewn(max*3, unsigned char);
407 rowdata = snewn(max+1, int);
414 generate(rs, w, h, grid);
417 * The game is a bit too easy if any row or column is
418 * completely black or completely white. An exception is
419 * made for rows/columns that are under 3 squares,
420 * otherwise nothing will ever be successfully generated.
424 for (i = 0; i < h; i++) {
426 for (j = 0; j < w; j++)
427 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
433 for (j = 0; j < w; j++) {
435 for (i = 0; i < h; i++)
436 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
444 memset(matrix, 0, w*h);
448 for (i=0; i<h; i++) {
449 rowdata[compute_rowdata(rowdata, grid+i*w, w, 1)] = 0;
450 done_any |= do_row(workspace, workspace+max, workspace+2*max,
451 matrix+i*w, w, 1, rowdata);
453 for (i=0; i<w; i++) {
454 rowdata[compute_rowdata(rowdata, grid+i, h, w)] = 0;
455 done_any |= do_row(workspace, workspace+max, workspace+2*max,
456 matrix+i, h, w, rowdata);
461 for (i=0; i<h; i++) {
462 for (j=0; j<w; j++) {
463 if (matrix[i*w+j] == UNKNOWN)
475 static char *new_game_seed(game_params *params, random_state *rs,
479 int i, j, max, rowlen, *rowdata;
480 char intbuf[80], *seed;
481 int seedlen, seedpos;
483 grid = generate_soluble(rs, params->w, params->h);
484 max = max(params->w, params->h);
485 rowdata = snewn(max, int);
488 * Seed is a slash-separated list of row contents; each row
489 * contents section is a dot-separated list of integers. Row
490 * contents are listed in the order (columns left to right,
491 * then rows top to bottom).
493 * Simplest way to handle memory allocation is to make two
494 * passes, first computing the seed size and then writing it
498 for (i = 0; i < params->w + params->h; i++) {
500 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w);
502 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w,
505 for (j = 0; j < rowlen; j++) {
506 seedlen += 1 + sprintf(intbuf, "%d", rowdata[j]);
512 seed = snewn(seedlen, char);
514 for (i = 0; i < params->w + params->h; i++) {
516 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w);
518 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w,
521 for (j = 0; j < rowlen; j++) {
522 int len = sprintf(seed+seedpos, "%d", rowdata[j]);
524 seed[seedpos + len] = '.';
526 seed[seedpos + len] = '/';
530 seed[seedpos++] = '/';
533 assert(seedpos == seedlen);
534 assert(seed[seedlen-1] == '/');
535 seed[seedlen-1] = '\0';
540 static void game_free_aux_info(game_aux_info *aux)
542 assert(!"Shouldn't happen");
545 static char *validate_seed(game_params *params, char *seed)
550 for (i = 0; i < params->w + params->h; i++) {
552 rowspace = params->h + 1;
554 rowspace = params->w + 1;
556 if (*seed && isdigit((unsigned char)*seed)) {
559 while (seed && isdigit((unsigned char)*seed)) seed++;
565 return "at least one column contains more numbers than will fit";
567 return "at least one row contains more numbers than will fit";
569 } while (*seed++ == '.');
571 seed++; /* expect a slash immediately */
574 if (seed[-1] == '/') {
575 if (i+1 == params->w + params->h)
576 return "too many row/column specifications";
577 } else if (seed[-1] == '\0') {
578 if (i+1 < params->w + params->h)
579 return "too few row/column specifications";
581 return "unrecognised character in game specification";
587 static game_state *new_game(game_params *params, char *seed)
591 game_state *state = snew(game_state);
593 state->w = params->w;
594 state->h = params->h;
596 state->grid = snewn(state->w * state->h, unsigned char);
597 memset(state->grid, GRID_UNKNOWN, state->w * state->h);
599 state->rowsize = max(state->w, state->h);
600 state->rowdata = snewn(state->rowsize * (state->w + state->h), int);
601 state->rowlen = snewn(state->w + state->h, int);
603 state->completed = state->cheated = FALSE;
605 for (i = 0; i < params->w + params->h; i++) {
606 state->rowlen[i] = 0;
607 if (*seed && isdigit((unsigned char)*seed)) {
610 while (seed && isdigit((unsigned char)*seed)) seed++;
611 state->rowdata[state->rowsize * i + state->rowlen[i]++] =
613 } while (*seed++ == '.');
615 seed++; /* expect a slash immediately */
622 static game_state *dup_game(game_state *state)
624 game_state *ret = snew(game_state);
629 ret->grid = snewn(ret->w * ret->h, unsigned char);
630 memcpy(ret->grid, state->grid, ret->w * ret->h);
632 ret->rowsize = state->rowsize;
633 ret->rowdata = snewn(ret->rowsize * (ret->w + ret->h), int);
634 ret->rowlen = snewn(ret->w + ret->h, int);
635 memcpy(ret->rowdata, state->rowdata,
636 ret->rowsize * (ret->w + ret->h) * sizeof(int));
637 memcpy(ret->rowlen, state->rowlen,
638 (ret->w + ret->h) * sizeof(int));
640 ret->completed = state->completed;
641 ret->cheated = state->cheated;
646 static void free_game(game_state *state)
648 sfree(state->rowdata);
649 sfree(state->rowlen);
654 static game_state *solve_game(game_state *state, game_aux_info *aux,
660 * I could have stored the grid I invented in the game_aux_info
661 * and extracted it here where available, but it seems easier
662 * just to run my internal solver in all cases.
665 ret = dup_game(state);
666 ret->completed = ret->cheated = TRUE;
669 int w = state->w, h = state->h, i, j, done_any, max;
670 unsigned char *matrix, *workspace;
673 matrix = snewn(w*h, unsigned char);
675 workspace = snewn(max*3, unsigned char);
676 rowdata = snewn(max+1, int);
678 memset(matrix, 0, w*h);
682 for (i=0; i<h; i++) {
683 memcpy(rowdata, state->rowdata + state->rowsize*(w+i),
685 rowdata[state->rowlen[w+i]] = 0;
686 done_any |= do_row(workspace, workspace+max, workspace+2*max,
687 matrix+i*w, w, 1, rowdata);
689 for (i=0; i<w; i++) {
690 memcpy(rowdata, state->rowdata + state->rowsize*i, max*sizeof(int));
691 rowdata[state->rowlen[i]] = 0;
692 done_any |= do_row(workspace, workspace+max, workspace+2*max,
693 matrix+i, h, w, rowdata);
697 for (i = 0; i < h; i++) {
698 for (j = 0; j < w; j++) {
699 int c = (matrix[i*w+j] == BLOCK ? GRID_FULL :
700 matrix[i*w+j] == DOT ? GRID_EMPTY : GRID_UNKNOWN);
701 ret->grid[i*w+j] = c;
702 if (c == GRID_UNKNOWN)
703 ret->completed = FALSE;
707 if (!ret->completed) {
709 *error = "Solving algorithm cannot complete this puzzle";
717 static char *game_text_format(game_state *state)
728 int drag, release, state;
731 static game_ui *new_ui(game_state *state)
736 ret->dragging = FALSE;
741 static void free_ui(game_ui *ui)
746 static game_state *make_move(game_state *from, game_ui *ui,
747 int x, int y, int button)
751 x = FROMCOORD(from->w, x);
752 y = FROMCOORD(from->h, y);
754 if (x >= 0 && x < from->w && y >= 0 && y < from->h &&
755 (button == LEFT_BUTTON || button == RIGHT_BUTTON ||
756 button == MIDDLE_BUTTON)) {
760 if (button == LEFT_BUTTON) {
761 ui->drag = LEFT_DRAG;
762 ui->release = LEFT_RELEASE;
763 ui->state = GRID_FULL;
764 } else if (button == RIGHT_BUTTON) {
765 ui->drag = RIGHT_DRAG;
766 ui->release = RIGHT_RELEASE;
767 ui->state = GRID_EMPTY;
768 } else /* if (button == MIDDLE_BUTTON) */ {
769 ui->drag = MIDDLE_DRAG;
770 ui->release = MIDDLE_RELEASE;
771 ui->state = GRID_UNKNOWN;
774 ui->drag_start_x = ui->drag_end_x = x;
775 ui->drag_start_y = ui->drag_end_y = y;
777 return from; /* UI activity occurred */
780 if (ui->dragging && button == ui->drag) {
782 * There doesn't seem much point in allowing a rectangle
783 * drag; people will generally only want to drag a single
784 * horizontal or vertical line, so we make that easy by
787 * Exception: if we're _middle_-button dragging to tag
788 * things as UNKNOWN, we may well want to trash an entire
789 * area and start over!
791 if (ui->state != GRID_UNKNOWN) {
792 if (abs(x - ui->drag_start_x) > abs(y - ui->drag_start_y))
793 y = ui->drag_start_y;
795 x = ui->drag_start_x;
800 if (x >= from->w) x = from->w - 1;
801 if (y >= from->h) y = from->h - 1;
806 return from; /* UI activity occurred */
809 if (ui->dragging && button == ui->release) {
810 int x1, x2, y1, y2, xx, yy;
811 int move_needed = FALSE;
813 x1 = min(ui->drag_start_x, ui->drag_end_x);
814 x2 = max(ui->drag_start_x, ui->drag_end_x);
815 y1 = min(ui->drag_start_y, ui->drag_end_y);
816 y2 = max(ui->drag_start_y, ui->drag_end_y);
818 for (yy = y1; yy <= y2; yy++)
819 for (xx = x1; xx <= x2; xx++)
820 if (from->grid[yy * from->w + xx] != ui->state)
823 ui->dragging = FALSE;
826 ret = dup_game(from);
827 for (yy = y1; yy <= y2; yy++)
828 for (xx = x1; xx <= x2; xx++)
829 ret->grid[yy * ret->w + xx] = ui->state;
832 * An actual change, so check to see if we've completed
835 if (!ret->completed) {
836 int *rowdata = snewn(ret->rowsize, int);
839 ret->completed = TRUE;
841 for (i=0; i<ret->w; i++) {
842 len = compute_rowdata(rowdata,
843 ret->grid+i, ret->h, ret->w);
844 if (len != ret->rowlen[i] ||
845 memcmp(ret->rowdata+i*ret->rowsize, rowdata,
846 len * sizeof(int))) {
847 ret->completed = FALSE;
851 for (i=0; i<ret->h; i++) {
852 len = compute_rowdata(rowdata,
853 ret->grid+i*ret->w, ret->w, 1);
854 if (len != ret->rowlen[i+ret->w] ||
855 memcmp(ret->rowdata+(i+ret->w)*ret->rowsize, rowdata,
856 len * sizeof(int))) {
857 ret->completed = FALSE;
867 return from; /* UI activity occurred */
873 /* ----------------------------------------------------------------------
877 struct game_drawstate {
880 unsigned char *visible;
883 static void game_size(game_params *params, int *x, int *y)
885 *x = SIZE(params->w);
886 *y = SIZE(params->h);
889 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
891 float *ret = snewn(3 * NCOLOURS, float);
893 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
895 ret[COL_GRID * 3 + 0] = 0.3F;
896 ret[COL_GRID * 3 + 1] = 0.3F;
897 ret[COL_GRID * 3 + 2] = 0.3F;
899 ret[COL_UNKNOWN * 3 + 0] = 0.5F;
900 ret[COL_UNKNOWN * 3 + 1] = 0.5F;
901 ret[COL_UNKNOWN * 3 + 2] = 0.5F;
903 ret[COL_FULL * 3 + 0] = 0.0F;
904 ret[COL_FULL * 3 + 1] = 0.0F;
905 ret[COL_FULL * 3 + 2] = 0.0F;
907 ret[COL_EMPTY * 3 + 0] = 1.0F;
908 ret[COL_EMPTY * 3 + 1] = 1.0F;
909 ret[COL_EMPTY * 3 + 2] = 1.0F;
911 *ncolours = NCOLOURS;
915 static game_drawstate *game_new_drawstate(game_state *state)
917 struct game_drawstate *ds = snew(struct game_drawstate);
922 ds->visible = snewn(ds->w * ds->h, unsigned char);
923 memset(ds->visible, 255, ds->w * ds->h);
928 static void game_free_drawstate(game_drawstate *ds)
934 static void grid_square(frontend *fe, game_drawstate *ds,
935 int y, int x, int state)
939 draw_rect(fe, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
940 TILE_SIZE, TILE_SIZE, COL_GRID);
942 xl = (x % 5 == 0 ? 1 : 0);
943 yt = (y % 5 == 0 ? 1 : 0);
944 xr = (x % 5 == 4 || x == ds->w-1 ? 1 : 0);
945 yb = (y % 5 == 4 || y == ds->h-1 ? 1 : 0);
947 draw_rect(fe, TOCOORD(ds->w, x) + 1 + xl, TOCOORD(ds->h, y) + 1 + yt,
948 TILE_SIZE - xl - xr - 1, TILE_SIZE - yt - yb - 1,
949 (state == GRID_FULL ? COL_FULL :
950 state == GRID_EMPTY ? COL_EMPTY : COL_UNKNOWN));
952 draw_update(fe, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
953 TILE_SIZE, TILE_SIZE);
956 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
957 game_state *state, int dir, game_ui *ui,
958 float animtime, float flashtime)
965 * The initial contents of the window are not guaranteed
966 * and can vary with front ends. To be on the safe side,
967 * all games should start by drawing a big background-
968 * colour rectangle covering the whole window.
970 draw_rect(fe, 0, 0, SIZE(ds->w), SIZE(ds->h), COL_BACKGROUND);
975 for (i = 0; i < ds->w + ds->h; i++) {
976 int rowlen = state->rowlen[i];
977 int *rowdata = state->rowdata + state->rowsize * i;
981 * Normally I space the numbers out by the same
982 * distance as the tile size. However, if there are
983 * more numbers than available spaces, I have to squash
986 nfit = max(rowlen, TLBORDER(ds->h))-1;
989 for (j = 0; j < rowlen; j++) {
994 x = TOCOORD(ds->w, i);
995 y = BORDER + TILE_SIZE * (TLBORDER(ds->h)-1);
996 y -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(ds->h)-1) / nfit;
998 y = TOCOORD(ds->h, i - ds->w);
999 x = BORDER + TILE_SIZE * (TLBORDER(ds->w)-1);
1000 x -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(ds->h)-1) / nfit;
1003 sprintf(str, "%d", rowdata[j]);
1004 draw_text(fe, x+TILE_SIZE/2, y+TILE_SIZE/2, FONT_VARIABLE,
1005 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE,
1006 COL_FULL, str); /* FIXME: COL_TEXT */
1011 * Draw the grid outline.
1013 draw_rect(fe, TOCOORD(ds->w, 0) - 1, TOCOORD(ds->h, 0) - 1,
1014 ds->w * TILE_SIZE + 3, ds->h * TILE_SIZE + 3,
1019 draw_update(fe, 0, 0, SIZE(ds->w), SIZE(ds->h));
1023 x1 = min(ui->drag_start_x, ui->drag_end_x);
1024 x2 = max(ui->drag_start_x, ui->drag_end_x);
1025 y1 = min(ui->drag_start_y, ui->drag_end_y);
1026 y2 = max(ui->drag_start_y, ui->drag_end_y);
1028 x1 = x2 = y1 = y2 = -1; /* placate gcc warnings */
1032 * Now draw any grid squares which have changed since last
1035 for (i = 0; i < ds->h; i++) {
1036 for (j = 0; j < ds->w; j++) {
1040 * Work out what state this square should be drawn in,
1041 * taking any current drag operation into account.
1043 if (ui->dragging && x1 <= j && j <= x2 && y1 <= i && i <= y2)
1046 val = state->grid[i * state->w + j];
1049 * Briefly invert everything twice during a completion
1052 if (flashtime > 0 &&
1053 (flashtime <= FLASH_TIME/3 || flashtime >= FLASH_TIME*2/3) &&
1054 val != GRID_UNKNOWN)
1055 val = (GRID_FULL ^ GRID_EMPTY) ^ val;
1057 if (ds->visible[i * ds->w + j] != val) {
1058 grid_square(fe, ds, i, j, val);
1059 ds->visible[i * ds->w + j] = val;
1065 static float game_anim_length(game_state *oldstate,
1066 game_state *newstate, int dir)
1071 static float game_flash_length(game_state *oldstate,
1072 game_state *newstate, int dir)
1074 if (!oldstate->completed && newstate->completed &&
1075 !oldstate->cheated && !newstate->cheated)
1080 static int game_wants_statusbar(void)
1086 #define thegame pattern
1089 const struct game thegame = {
1090 "Pattern", "games.pattern",
1097 TRUE, game_configure, custom_params,
1106 FALSE, game_text_format,
1113 game_free_drawstate,
1117 game_wants_statusbar,
1120 #ifdef STANDALONE_SOLVER
1123 * gcc -DSTANDALONE_SOLVER -o patternsolver pattern.c malloc.c
1128 void frontend_default_colour(frontend *fe, float *output) {}
1129 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
1130 int align, int colour, char *text) {}
1131 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) {}
1132 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) {}
1133 void draw_polygon(frontend *fe, int *coords, int npoints,
1134 int fill, int colour) {}
1135 void clip(frontend *fe, int x, int y, int w, int h) {}
1136 void unclip(frontend *fe) {}
1137 void start_draw(frontend *fe) {}
1138 void draw_update(frontend *fe, int x, int y, int w, int h) {}
1139 void end_draw(frontend *fe) {}
1140 unsigned long random_upto(random_state *state, unsigned long limit)
1141 { assert(!"Shouldn't get randomness"); return 0; }
1143 void fatal(char *fmt, ...)
1147 fprintf(stderr, "fatal error: ");
1150 vfprintf(stderr, fmt, ap);
1153 fprintf(stderr, "\n");
1157 int main(int argc, char **argv)
1162 char *id = NULL, *seed, *err;
1166 while (--argc > 0) {
1169 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0]);
1177 fprintf(stderr, "usage: %s <game_id>\n", argv[0]);
1181 seed = strchr(id, ':');
1183 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
1188 p = decode_params(id);
1189 err = validate_seed(p, seed);
1191 fprintf(stderr, "%s: %s\n", argv[0], err);
1194 s = new_game(p, seed);
1197 int w = p->w, h = p->h, i, j, done_any, max;
1198 unsigned char *matrix, *workspace;
1201 matrix = snewn(w*h, unsigned char);
1203 workspace = snewn(max*3, unsigned char);
1204 rowdata = snewn(max+1, int);
1206 memset(matrix, 0, w*h);
1210 for (i=0; i<h; i++) {
1211 memcpy(rowdata, s->rowdata + s->rowsize*(w+i),
1213 rowdata[s->rowlen[w+i]] = 0;
1214 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1215 matrix+i*w, w, 1, rowdata);
1217 for (i=0; i<w; i++) {
1218 memcpy(rowdata, s->rowdata + s->rowsize*i, max*sizeof(int));
1219 rowdata[s->rowlen[i]] = 0;
1220 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1221 matrix+i, h, w, rowdata);
1225 for (i = 0; i < h; i++) {
1226 for (j = 0; j < w; j++) {
1227 int c = (matrix[i*w+j] == UNKNOWN ? '?' :
1228 matrix[i*w+j] == BLOCK ? '#' :
1229 matrix[i*w+j] == DOT ? '.' :