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 void decode_params(game_params *ret, char const *string)
105 char const *p = string;
108 while (*p && isdigit(*p)) p++;
112 while (*p && isdigit(*p)) p++;
118 static char *encode_params(game_params *params, int full)
123 len = sprintf(ret, "%dx%d", params->w, params->h);
124 assert(len < lenof(ret));
130 static config_item *game_configure(game_params *params)
135 ret = snewn(3, config_item);
137 ret[0].name = "Width";
138 ret[0].type = C_STRING;
139 sprintf(buf, "%d", params->w);
140 ret[0].sval = dupstr(buf);
143 ret[1].name = "Height";
144 ret[1].type = C_STRING;
145 sprintf(buf, "%d", params->h);
146 ret[1].sval = dupstr(buf);
157 static game_params *custom_params(config_item *cfg)
159 game_params *ret = snew(game_params);
161 ret->w = atoi(cfg[0].sval);
162 ret->h = atoi(cfg[1].sval);
167 static char *validate_params(game_params *params)
169 if (params->w <= 0 && params->h <= 0)
170 return "Width and height must both be greater than zero";
172 return "Width must be greater than zero";
174 return "Height must be greater than zero";
178 /* ----------------------------------------------------------------------
179 * Puzzle generation code.
181 * For this particular puzzle, it seemed important to me to ensure
182 * a unique solution. I do this the brute-force way, by having a
183 * solver algorithm alongside the generator, and repeatedly
184 * generating a random grid until I find one whose solution is
185 * unique. It turns out that this isn't too onerous on a modern PC
186 * provided you keep grid size below around 30. Any offers of
187 * better algorithms, however, will be very gratefully received.
189 * Another annoyance of this approach is that it limits the
190 * available puzzles to those solvable by the algorithm I've used.
191 * My algorithm only ever considers a single row or column at any
192 * one time, which means it's incapable of solving the following
193 * difficult example (found by Bella Image around 1995/6, when she
194 * and I were both doing maths degrees):
208 * Obviously this cannot be solved by a one-row-or-column-at-a-time
209 * algorithm (it would require at least one row or column reading
210 * `2 1', `1 2', `3' or `4' to get started). However, it can be
211 * proved to have a unique solution: if the top left square were
212 * empty, then the only option for the top row would be to fill the
213 * two squares in the 1 columns, which would imply the squares
214 * below those were empty, leaving no place for the 2 in the second
215 * row. Contradiction. Hence the top left square is full, and the
216 * unique solution follows easily from that starting point.
218 * (The game ID for this puzzle is 4x4:2/1/2/1/1.1/2/1/1 , in case
219 * it's useful to anyone.)
222 static int float_compare(const void *av, const void *bv)
224 const float *a = (const float *)av;
225 const float *b = (const float *)bv;
234 static void generate(random_state *rs, int w, int h, unsigned char *retgrid)
241 fgrid = snewn(w*h, float);
243 for (i = 0; i < h; i++) {
244 for (j = 0; j < w; j++) {
245 fgrid[i*w+j] = random_upto(rs, 100000000UL) / 100000000.F;
250 * The above gives a completely random splattering of black and
251 * white cells. We want to gently bias this in favour of _some_
252 * reasonably thick areas of white and black, while retaining
253 * some randomness and fine detail.
255 * So we evolve the starting grid using a cellular automaton.
256 * Currently, I'm doing something very simple indeed, which is
257 * to set each square to the average of the surrounding nine
258 * cells (or the average of fewer, if we're on a corner).
260 for (step = 0; step < 1; step++) {
261 fgrid2 = snewn(w*h, float);
263 for (i = 0; i < h; i++) {
264 for (j = 0; j < w; j++) {
269 * Compute the average of the surrounding cells.
273 for (p = -1; p <= +1; p++) {
274 for (q = -1; q <= +1; q++) {
275 if (i+p < 0 || i+p >= h || j+q < 0 || j+q >= w)
278 * An additional special case not mentioned
279 * above: if a grid dimension is 2xn then
280 * we do not average across that dimension
281 * at all. Otherwise a 2x2 grid would
282 * contain four identical squares.
284 if ((h==2 && p!=0) || (w==2 && q!=0))
287 sx += fgrid[(i+p)*w+(j+q)];
292 fgrid2[i*w+j] = xbar;
300 fgrid2 = snewn(w*h, float);
301 memcpy(fgrid2, fgrid, w*h*sizeof(float));
302 qsort(fgrid2, w*h, sizeof(float), float_compare);
303 threshold = fgrid2[w*h/2];
306 for (i = 0; i < h; i++) {
307 for (j = 0; j < w; j++) {
308 retgrid[i*w+j] = (fgrid[i*w+j] >= threshold ? GRID_FULL :
316 static int compute_rowdata(int *ret, unsigned char *start, int len, int step)
322 for (i = 0; i < len; i++) {
323 if (start[i*step] == GRID_FULL) {
325 while (i+runlen < len && start[(i+runlen)*step] == GRID_FULL)
331 if (i < len && start[i*step] == GRID_UNKNOWN)
341 #define STILL_UNKNOWN 3
343 static void do_recurse(unsigned char *known, unsigned char *deduced,
344 unsigned char *row, int *data, int len,
345 int freespace, int ndone, int lowest)
350 for (i=0; i<=freespace; i++) {
352 for (k=0; k<i; k++) row[j++] = DOT;
353 for (k=0; k<data[ndone]; k++) row[j++] = BLOCK;
354 if (j < len) row[j++] = DOT;
355 do_recurse(known, deduced, row, data, len,
356 freespace-i, ndone+1, j);
359 for (i=lowest; i<len; i++)
361 for (i=0; i<len; i++)
362 if (known[i] && known[i] != row[i])
364 for (i=0; i<len; i++)
365 deduced[i] |= row[i];
369 static int do_row(unsigned char *known, unsigned char *deduced,
371 unsigned char *start, int len, int step, int *data)
373 int rowlen, i, freespace, done_any;
376 for (rowlen = 0; data[rowlen]; rowlen++)
377 freespace -= data[rowlen]+1;
379 for (i = 0; i < len; i++) {
380 known[i] = start[i*step];
384 do_recurse(known, deduced, row, data, len, freespace, 0, 0);
386 for (i=0; i<len; i++)
387 if (deduced[i] && deduced[i] != STILL_UNKNOWN && !known[i]) {
388 start[i*step] = deduced[i];
394 static unsigned char *generate_soluble(random_state *rs, int w, int h)
396 int i, j, done_any, ok, ntries, max;
397 unsigned char *grid, *matrix, *workspace;
400 grid = snewn(w*h, unsigned char);
401 matrix = snewn(w*h, unsigned char);
403 workspace = snewn(max*3, unsigned char);
404 rowdata = snewn(max+1, int);
411 generate(rs, w, h, grid);
414 * The game is a bit too easy if any row or column is
415 * completely black or completely white. An exception is
416 * made for rows/columns that are under 3 squares,
417 * otherwise nothing will ever be successfully generated.
421 for (i = 0; i < h; i++) {
423 for (j = 0; j < w; j++)
424 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
430 for (j = 0; j < w; j++) {
432 for (i = 0; i < h; i++)
433 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
441 memset(matrix, 0, w*h);
445 for (i=0; i<h; i++) {
446 rowdata[compute_rowdata(rowdata, grid+i*w, w, 1)] = 0;
447 done_any |= do_row(workspace, workspace+max, workspace+2*max,
448 matrix+i*w, w, 1, rowdata);
450 for (i=0; i<w; i++) {
451 rowdata[compute_rowdata(rowdata, grid+i, h, w)] = 0;
452 done_any |= do_row(workspace, workspace+max, workspace+2*max,
453 matrix+i, h, w, rowdata);
458 for (i=0; i<h; i++) {
459 for (j=0; j<w; j++) {
460 if (matrix[i*w+j] == UNKNOWN)
472 struct game_aux_info {
477 static char *new_game_desc(game_params *params, random_state *rs,
481 int i, j, max, rowlen, *rowdata;
482 char intbuf[80], *desc;
483 int desclen, descpos;
485 grid = generate_soluble(rs, params->w, params->h);
486 max = max(params->w, params->h);
487 rowdata = snewn(max, int);
490 * Save the solved game in an aux_info.
493 game_aux_info *ai = snew(game_aux_info);
497 ai->grid = snewn(ai->w * ai->h, unsigned char);
498 memcpy(ai->grid, grid, ai->w * ai->h);
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).
509 * Simplest way to handle memory allocation is to make two
510 * passes, first computing the seed size and then writing it
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 desclen += 1 + sprintf(intbuf, "%d", rowdata[j]);
528 desc = snewn(desclen, char);
530 for (i = 0; i < params->w + params->h; i++) {
532 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w);
534 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w,
537 for (j = 0; j < rowlen; j++) {
538 int len = sprintf(desc+descpos, "%d", rowdata[j]);
540 desc[descpos + len] = '.';
542 desc[descpos + len] = '/';
546 desc[descpos++] = '/';
549 assert(descpos == desclen);
550 assert(desc[desclen-1] == '/');
551 desc[desclen-1] = '\0';
556 static void game_free_aux_info(game_aux_info *aux)
562 static char *validate_desc(game_params *params, char *desc)
567 for (i = 0; i < params->w + params->h; i++) {
569 rowspace = params->h + 1;
571 rowspace = params->w + 1;
573 if (*desc && isdigit((unsigned char)*desc)) {
576 while (desc && isdigit((unsigned char)*desc)) desc++;
582 return "at least one column contains more numbers than will fit";
584 return "at least one row contains more numbers than will fit";
586 } while (*desc++ == '.');
588 desc++; /* expect a slash immediately */
591 if (desc[-1] == '/') {
592 if (i+1 == params->w + params->h)
593 return "too many row/column specifications";
594 } else if (desc[-1] == '\0') {
595 if (i+1 < params->w + params->h)
596 return "too few row/column specifications";
598 return "unrecognised character in game specification";
604 static game_state *new_game(midend_data *me, game_params *params, char *desc)
608 game_state *state = snew(game_state);
610 state->w = params->w;
611 state->h = params->h;
613 state->grid = snewn(state->w * state->h, unsigned char);
614 memset(state->grid, GRID_UNKNOWN, state->w * state->h);
616 state->rowsize = max(state->w, state->h);
617 state->rowdata = snewn(state->rowsize * (state->w + state->h), int);
618 state->rowlen = snewn(state->w + state->h, int);
620 state->completed = state->cheated = FALSE;
622 for (i = 0; i < params->w + params->h; i++) {
623 state->rowlen[i] = 0;
624 if (*desc && isdigit((unsigned char)*desc)) {
627 while (desc && isdigit((unsigned char)*desc)) desc++;
628 state->rowdata[state->rowsize * i + state->rowlen[i]++] =
630 } while (*desc++ == '.');
632 desc++; /* expect a slash immediately */
639 static game_state *dup_game(game_state *state)
641 game_state *ret = snew(game_state);
646 ret->grid = snewn(ret->w * ret->h, unsigned char);
647 memcpy(ret->grid, state->grid, ret->w * ret->h);
649 ret->rowsize = state->rowsize;
650 ret->rowdata = snewn(ret->rowsize * (ret->w + ret->h), int);
651 ret->rowlen = snewn(ret->w + ret->h, int);
652 memcpy(ret->rowdata, state->rowdata,
653 ret->rowsize * (ret->w + ret->h) * sizeof(int));
654 memcpy(ret->rowlen, state->rowlen,
655 (ret->w + ret->h) * sizeof(int));
657 ret->completed = state->completed;
658 ret->cheated = state->cheated;
663 static void free_game(game_state *state)
665 sfree(state->rowdata);
666 sfree(state->rowlen);
671 static game_state *solve_game(game_state *state, game_aux_info *ai,
676 ret = dup_game(state);
677 ret->completed = ret->cheated = TRUE;
680 * If we already have the solved state in an aux_info, copy it
685 assert(ret->w == ai->w);
686 assert(ret->h == ai->h);
687 memcpy(ret->grid, ai->grid, ai->w * ai->h);
690 int w = state->w, h = state->h, i, j, done_any, max;
691 unsigned char *matrix, *workspace;
694 matrix = snewn(w*h, unsigned char);
696 workspace = snewn(max*3, unsigned char);
697 rowdata = snewn(max+1, int);
699 memset(matrix, 0, w*h);
703 for (i=0; i<h; i++) {
704 memcpy(rowdata, state->rowdata + state->rowsize*(w+i),
706 rowdata[state->rowlen[w+i]] = 0;
707 done_any |= do_row(workspace, workspace+max, workspace+2*max,
708 matrix+i*w, w, 1, rowdata);
710 for (i=0; i<w; i++) {
711 memcpy(rowdata, state->rowdata + state->rowsize*i, max*sizeof(int));
712 rowdata[state->rowlen[i]] = 0;
713 done_any |= do_row(workspace, workspace+max, workspace+2*max,
714 matrix+i, h, w, rowdata);
718 for (i = 0; i < h; i++) {
719 for (j = 0; j < w; j++) {
720 int c = (matrix[i*w+j] == BLOCK ? GRID_FULL :
721 matrix[i*w+j] == DOT ? GRID_EMPTY : GRID_UNKNOWN);
722 ret->grid[i*w+j] = c;
723 if (c == GRID_UNKNOWN)
724 ret->completed = FALSE;
728 if (!ret->completed) {
730 *error = "Solving algorithm cannot complete this puzzle";
738 static char *game_text_format(game_state *state)
749 int drag, release, state;
752 static game_ui *new_ui(game_state *state)
757 ret->dragging = FALSE;
762 static void free_ui(game_ui *ui)
767 static game_state *make_move(game_state *from, game_ui *ui,
768 int x, int y, int button)
774 x = FROMCOORD(from->w, x);
775 y = FROMCOORD(from->h, y);
777 if (x >= 0 && x < from->w && y >= 0 && y < from->h &&
778 (button == LEFT_BUTTON || button == RIGHT_BUTTON ||
779 button == MIDDLE_BUTTON)) {
783 if (button == LEFT_BUTTON) {
784 ui->drag = LEFT_DRAG;
785 ui->release = LEFT_RELEASE;
786 ui->state = GRID_FULL;
787 } else if (button == RIGHT_BUTTON) {
788 ui->drag = RIGHT_DRAG;
789 ui->release = RIGHT_RELEASE;
790 ui->state = GRID_EMPTY;
791 } else /* if (button == MIDDLE_BUTTON) */ {
792 ui->drag = MIDDLE_DRAG;
793 ui->release = MIDDLE_RELEASE;
794 ui->state = GRID_UNKNOWN;
797 ui->drag_start_x = ui->drag_end_x = x;
798 ui->drag_start_y = ui->drag_end_y = y;
800 return from; /* UI activity occurred */
803 if (ui->dragging && button == ui->drag) {
805 * There doesn't seem much point in allowing a rectangle
806 * drag; people will generally only want to drag a single
807 * horizontal or vertical line, so we make that easy by
810 * Exception: if we're _middle_-button dragging to tag
811 * things as UNKNOWN, we may well want to trash an entire
812 * area and start over!
814 if (ui->state != GRID_UNKNOWN) {
815 if (abs(x - ui->drag_start_x) > abs(y - ui->drag_start_y))
816 y = ui->drag_start_y;
818 x = ui->drag_start_x;
823 if (x >= from->w) x = from->w - 1;
824 if (y >= from->h) y = from->h - 1;
829 return from; /* UI activity occurred */
832 if (ui->dragging && button == ui->release) {
833 int x1, x2, y1, y2, xx, yy;
834 int move_needed = FALSE;
836 x1 = min(ui->drag_start_x, ui->drag_end_x);
837 x2 = max(ui->drag_start_x, ui->drag_end_x);
838 y1 = min(ui->drag_start_y, ui->drag_end_y);
839 y2 = max(ui->drag_start_y, ui->drag_end_y);
841 for (yy = y1; yy <= y2; yy++)
842 for (xx = x1; xx <= x2; xx++)
843 if (from->grid[yy * from->w + xx] != ui->state)
846 ui->dragging = FALSE;
849 ret = dup_game(from);
850 for (yy = y1; yy <= y2; yy++)
851 for (xx = x1; xx <= x2; xx++)
852 ret->grid[yy * ret->w + xx] = ui->state;
855 * An actual change, so check to see if we've completed
858 if (!ret->completed) {
859 int *rowdata = snewn(ret->rowsize, int);
862 ret->completed = TRUE;
864 for (i=0; i<ret->w; i++) {
865 len = compute_rowdata(rowdata,
866 ret->grid+i, ret->h, ret->w);
867 if (len != ret->rowlen[i] ||
868 memcmp(ret->rowdata+i*ret->rowsize, rowdata,
869 len * sizeof(int))) {
870 ret->completed = FALSE;
874 for (i=0; i<ret->h; i++) {
875 len = compute_rowdata(rowdata,
876 ret->grid+i*ret->w, ret->w, 1);
877 if (len != ret->rowlen[i+ret->w] ||
878 memcmp(ret->rowdata+(i+ret->w)*ret->rowsize, rowdata,
879 len * sizeof(int))) {
880 ret->completed = FALSE;
890 return from; /* UI activity occurred */
896 /* ----------------------------------------------------------------------
900 struct game_drawstate {
903 unsigned char *visible;
906 static void game_size(game_params *params, int *x, int *y)
908 *x = SIZE(params->w);
909 *y = SIZE(params->h);
912 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
914 float *ret = snewn(3 * NCOLOURS, float);
916 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
918 ret[COL_GRID * 3 + 0] = 0.3F;
919 ret[COL_GRID * 3 + 1] = 0.3F;
920 ret[COL_GRID * 3 + 2] = 0.3F;
922 ret[COL_UNKNOWN * 3 + 0] = 0.5F;
923 ret[COL_UNKNOWN * 3 + 1] = 0.5F;
924 ret[COL_UNKNOWN * 3 + 2] = 0.5F;
926 ret[COL_FULL * 3 + 0] = 0.0F;
927 ret[COL_FULL * 3 + 1] = 0.0F;
928 ret[COL_FULL * 3 + 2] = 0.0F;
930 ret[COL_EMPTY * 3 + 0] = 1.0F;
931 ret[COL_EMPTY * 3 + 1] = 1.0F;
932 ret[COL_EMPTY * 3 + 2] = 1.0F;
934 *ncolours = NCOLOURS;
938 static game_drawstate *game_new_drawstate(game_state *state)
940 struct game_drawstate *ds = snew(struct game_drawstate);
945 ds->visible = snewn(ds->w * ds->h, unsigned char);
946 memset(ds->visible, 255, ds->w * ds->h);
951 static void game_free_drawstate(game_drawstate *ds)
957 static void grid_square(frontend *fe, game_drawstate *ds,
958 int y, int x, int state)
962 draw_rect(fe, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
963 TILE_SIZE, TILE_SIZE, COL_GRID);
965 xl = (x % 5 == 0 ? 1 : 0);
966 yt = (y % 5 == 0 ? 1 : 0);
967 xr = (x % 5 == 4 || x == ds->w-1 ? 1 : 0);
968 yb = (y % 5 == 4 || y == ds->h-1 ? 1 : 0);
970 draw_rect(fe, TOCOORD(ds->w, x) + 1 + xl, TOCOORD(ds->h, y) + 1 + yt,
971 TILE_SIZE - xl - xr - 1, TILE_SIZE - yt - yb - 1,
972 (state == GRID_FULL ? COL_FULL :
973 state == GRID_EMPTY ? COL_EMPTY : COL_UNKNOWN));
975 draw_update(fe, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
976 TILE_SIZE, TILE_SIZE);
979 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
980 game_state *state, int dir, game_ui *ui,
981 float animtime, float flashtime)
988 * The initial contents of the window are not guaranteed
989 * and can vary with front ends. To be on the safe side,
990 * all games should start by drawing a big background-
991 * colour rectangle covering the whole window.
993 draw_rect(fe, 0, 0, SIZE(ds->w), SIZE(ds->h), COL_BACKGROUND);
998 for (i = 0; i < ds->w + ds->h; i++) {
999 int rowlen = state->rowlen[i];
1000 int *rowdata = state->rowdata + state->rowsize * i;
1004 * Normally I space the numbers out by the same
1005 * distance as the tile size. However, if there are
1006 * more numbers than available spaces, I have to squash
1009 nfit = max(rowlen, TLBORDER(ds->h))-1;
1012 for (j = 0; j < rowlen; j++) {
1017 x = TOCOORD(ds->w, i);
1018 y = BORDER + TILE_SIZE * (TLBORDER(ds->h)-1);
1019 y -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(ds->h)-1) / nfit;
1021 y = TOCOORD(ds->h, i - ds->w);
1022 x = BORDER + TILE_SIZE * (TLBORDER(ds->w)-1);
1023 x -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(ds->h)-1) / nfit;
1026 sprintf(str, "%d", rowdata[j]);
1027 draw_text(fe, x+TILE_SIZE/2, y+TILE_SIZE/2, FONT_VARIABLE,
1028 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE,
1029 COL_FULL, str); /* FIXME: COL_TEXT */
1034 * Draw the grid outline.
1036 draw_rect(fe, TOCOORD(ds->w, 0) - 1, TOCOORD(ds->h, 0) - 1,
1037 ds->w * TILE_SIZE + 3, ds->h * TILE_SIZE + 3,
1042 draw_update(fe, 0, 0, SIZE(ds->w), SIZE(ds->h));
1046 x1 = min(ui->drag_start_x, ui->drag_end_x);
1047 x2 = max(ui->drag_start_x, ui->drag_end_x);
1048 y1 = min(ui->drag_start_y, ui->drag_end_y);
1049 y2 = max(ui->drag_start_y, ui->drag_end_y);
1051 x1 = x2 = y1 = y2 = -1; /* placate gcc warnings */
1055 * Now draw any grid squares which have changed since last
1058 for (i = 0; i < ds->h; i++) {
1059 for (j = 0; j < ds->w; j++) {
1063 * Work out what state this square should be drawn in,
1064 * taking any current drag operation into account.
1066 if (ui->dragging && x1 <= j && j <= x2 && y1 <= i && i <= y2)
1069 val = state->grid[i * state->w + j];
1072 * Briefly invert everything twice during a completion
1075 if (flashtime > 0 &&
1076 (flashtime <= FLASH_TIME/3 || flashtime >= FLASH_TIME*2/3) &&
1077 val != GRID_UNKNOWN)
1078 val = (GRID_FULL ^ GRID_EMPTY) ^ val;
1080 if (ds->visible[i * ds->w + j] != val) {
1081 grid_square(fe, ds, i, j, val);
1082 ds->visible[i * ds->w + j] = val;
1088 static float game_anim_length(game_state *oldstate,
1089 game_state *newstate, int dir, game_ui *ui)
1094 static float game_flash_length(game_state *oldstate,
1095 game_state *newstate, int dir, game_ui *ui)
1097 if (!oldstate->completed && newstate->completed &&
1098 !oldstate->cheated && !newstate->cheated)
1103 static int game_wants_statusbar(void)
1109 #define thegame pattern
1112 const struct game thegame = {
1113 "Pattern", "games.pattern",
1120 TRUE, game_configure, custom_params,
1129 FALSE, game_text_format,
1136 game_free_drawstate,
1140 game_wants_statusbar,
1143 #ifdef STANDALONE_SOLVER
1146 * gcc -DSTANDALONE_SOLVER -o patternsolver pattern.c malloc.c
1151 void frontend_default_colour(frontend *fe, float *output) {}
1152 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
1153 int align, int colour, char *text) {}
1154 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour) {}
1155 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour) {}
1156 void draw_polygon(frontend *fe, int *coords, int npoints,
1157 int fill, int colour) {}
1158 void clip(frontend *fe, int x, int y, int w, int h) {}
1159 void unclip(frontend *fe) {}
1160 void start_draw(frontend *fe) {}
1161 void draw_update(frontend *fe, int x, int y, int w, int h) {}
1162 void end_draw(frontend *fe) {}
1163 unsigned long random_upto(random_state *state, unsigned long limit)
1164 { assert(!"Shouldn't get randomness"); return 0; }
1166 void fatal(char *fmt, ...)
1170 fprintf(stderr, "fatal error: ");
1173 vfprintf(stderr, fmt, ap);
1176 fprintf(stderr, "\n");
1180 int main(int argc, char **argv)
1185 char *id = NULL, *desc, *err;
1189 while (--argc > 0) {
1192 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0]);
1200 fprintf(stderr, "usage: %s <game_id>\n", argv[0]);
1204 desc = strchr(id, ':');
1206 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
1211 p = default_params();
1212 decode_params(p, id);
1213 err = validate_desc(p, desc);
1215 fprintf(stderr, "%s: %s\n", argv[0], err);
1218 s = new_game(p, desc);
1221 int w = p->w, h = p->h, i, j, done_any, max;
1222 unsigned char *matrix, *workspace;
1225 matrix = snewn(w*h, unsigned char);
1227 workspace = snewn(max*3, unsigned char);
1228 rowdata = snewn(max+1, int);
1230 memset(matrix, 0, w*h);
1234 for (i=0; i<h; i++) {
1235 memcpy(rowdata, s->rowdata + s->rowsize*(w+i),
1237 rowdata[s->rowlen[w+i]] = 0;
1238 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1239 matrix+i*w, w, 1, rowdata);
1241 for (i=0; i<w; i++) {
1242 memcpy(rowdata, s->rowdata + s->rowsize*i, max*sizeof(int));
1243 rowdata[s->rowlen[i]] = 0;
1244 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1245 matrix+i, h, w, rowdata);
1249 for (i = 0; i < h; i++) {
1250 for (j = 0; j < w; j++) {
1251 int c = (matrix[i*w+j] == UNKNOWN ? '?' :
1252 matrix[i*w+j] == BLOCK ? '#' :
1253 matrix[i*w+j] == DOT ? '.' :