2 * pattern.c: the pattern-reconstruction game known as `nonograms'.
25 #define PREFERRED_TILE_SIZE 24
26 #define TILE_SIZE (ds->tilesize)
27 #define BORDER (3 * TILE_SIZE / 4)
28 #define TLBORDER(d) ( (d) / 5 + 2 )
29 #define GUTTER (TILE_SIZE / 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)))
35 #define GETTILESIZE(d, w) ((double)w / (2.0 + (double)TLBORDER(d) + (double)(d)))
37 #define TOCOORD(d, x) (BORDER + GUTTER + TILE_SIZE * (TLBORDER(d) + (x)))
43 #define GRID_UNKNOWN 2
51 int *rowdata, *rowlen;
52 int completed, cheated;
55 #define FLASH_TIME 0.13F
57 static game_params *default_params(void)
59 game_params *ret = snew(game_params);
66 static const struct game_params pattern_presets[] = {
76 static int game_fetch_preset(int i, char **name, game_params **params)
81 if (i < 0 || i >= lenof(pattern_presets))
84 ret = snew(game_params);
85 *ret = pattern_presets[i];
87 sprintf(str, "%dx%d", ret->w, ret->h);
94 static void free_params(game_params *params)
99 static game_params *dup_params(game_params *params)
101 game_params *ret = snew(game_params);
102 *ret = *params; /* structure copy */
106 static void decode_params(game_params *ret, char const *string)
108 char const *p = string;
111 while (*p && isdigit((unsigned char)*p)) p++;
115 while (*p && isdigit((unsigned char)*p)) p++;
121 static char *encode_params(game_params *params, int full)
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, int full)
172 if (params->w <= 0 || params->h <= 0)
173 return "Width and height must both be greater than zero";
177 /* ----------------------------------------------------------------------
178 * Puzzle generation code.
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.
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):
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.
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.)
221 static int float_compare(const void *av, const void *bv)
223 const float *a = (const float *)av;
224 const float *b = (const float *)bv;
233 static void generate(random_state *rs, int w, int h, unsigned char *retgrid)
240 fgrid = snewn(w*h, float);
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;
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.
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).
259 for (step = 0; step < 1; step++) {
260 fgrid2 = snewn(w*h, float);
262 for (i = 0; i < h; i++) {
263 for (j = 0; j < w; j++) {
268 * Compute the average of the surrounding cells.
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)
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.
283 if ((h==2 && p!=0) || (w==2 && q!=0))
286 sx += fgrid[(i+p)*w+(j+q)];
291 fgrid2[i*w+j] = xbar;
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];
305 for (i = 0; i < h; i++) {
306 for (j = 0; j < w; j++) {
307 retgrid[i*w+j] = (fgrid[i*w+j] >= threshold ? GRID_FULL :
315 static int compute_rowdata(int *ret, unsigned char *start, int len, int step)
321 for (i = 0; i < len; i++) {
322 if (start[i*step] == GRID_FULL) {
324 while (i+runlen < len && start[(i+runlen)*step] == GRID_FULL)
330 if (i < len && start[i*step] == GRID_UNKNOWN)
340 #define STILL_UNKNOWN 3
342 #ifdef STANDALONE_SOLVER
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
375 #ifdef STANDALONE_SOLVER
376 , const char *rowcol, int index, int cluewid
380 int rowlen, i, freespace, done_any;
383 for (rowlen = 0; data[rowlen]; rowlen++)
384 freespace -= data[rowlen]+1;
386 for (i = 0; i < len; i++) {
387 known[i] = start[i*step];
391 do_recurse(known, deduced, row, data, len, freespace, 0, 0);
393 for (i=0; i<len; i++)
394 if (deduced[i] && deduced[i] != STILL_UNKNOWN && !known[i]) {
395 start[i*step] = deduced[i];
398 #ifdef STANDALONE_SOLVER
399 if (verbose && done_any) {
402 printf("%s %2d: [", rowcol, index);
403 for (thiscluewid = -1, i = 0; data[i]; i++)
404 thiscluewid += sprintf(buf, " %d", data[i]);
405 printf("%*s", cluewid - thiscluewid, "");
406 for (i = 0; data[i]; i++)
407 printf(" %d", data[i]);
409 for (i = 0; i < len; i++)
410 putchar(known[i] == BLOCK ? '#' :
411 known[i] == DOT ? '.' : '?');
413 for (i = 0; i < len; i++)
414 putchar(start[i*step] == BLOCK ? '#' :
415 start[i*step] == DOT ? '.' : '?');
422 static unsigned char *generate_soluble(random_state *rs, int w, int h)
424 int i, j, done_any, ok, ntries, max;
425 unsigned char *grid, *matrix, *workspace;
428 grid = snewn(w*h, unsigned char);
429 matrix = snewn(w*h, unsigned char);
431 workspace = snewn(max*3, unsigned char);
432 rowdata = snewn(max+1, int);
439 generate(rs, w, h, grid);
442 * The game is a bit too easy if any row or column is
443 * completely black or completely white. An exception is
444 * made for rows/columns that are under 3 squares,
445 * otherwise nothing will ever be successfully generated.
449 for (i = 0; i < h; i++) {
451 for (j = 0; j < w; j++)
452 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
458 for (j = 0; j < w; j++) {
460 for (i = 0; i < h; i++)
461 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
469 memset(matrix, 0, w*h);
473 for (i=0; i<h; i++) {
474 rowdata[compute_rowdata(rowdata, grid+i*w, w, 1)] = 0;
475 done_any |= do_row(workspace, workspace+max, workspace+2*max,
476 matrix+i*w, w, 1, rowdata
477 #ifdef STANDALONE_SOLVER
478 , NULL, 0, 0 /* never do diagnostics here */
482 for (i=0; i<w; i++) {
483 rowdata[compute_rowdata(rowdata, grid+i, h, w)] = 0;
484 done_any |= do_row(workspace, workspace+max, workspace+2*max,
485 matrix+i, h, w, rowdata
486 #ifdef STANDALONE_SOLVER
487 , NULL, 0, 0 /* never do diagnostics here */
494 for (i=0; i<h; i++) {
495 for (j=0; j<w; j++) {
496 if (matrix[i*w+j] == UNKNOWN)
508 static char *new_game_desc(game_params *params, random_state *rs,
509 char **aux, int interactive)
512 int i, j, max, rowlen, *rowdata;
513 char intbuf[80], *desc;
514 int desclen, descpos;
516 grid = generate_soluble(rs, params->w, params->h);
517 max = max(params->w, params->h);
518 rowdata = snewn(max, int);
521 * Save the solved game in aux.
524 char *ai = snewn(params->w * params->h + 2, char);
527 * String format is exactly the same as a solve move, so we
528 * can just dupstr this in solve_game().
533 for (i = 0; i < params->w * params->h; i++)
534 ai[i+1] = grid[i] ? '1' : '0';
536 ai[params->w * params->h + 1] = '\0';
542 * Seed is a slash-separated list of row contents; each row
543 * contents section is a dot-separated list of integers. Row
544 * contents are listed in the order (columns left to right,
545 * then rows top to bottom).
547 * Simplest way to handle memory allocation is to make two
548 * passes, first computing the seed size and then writing it
552 for (i = 0; i < params->w + params->h; i++) {
554 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w);
556 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w,
559 for (j = 0; j < rowlen; j++) {
560 desclen += 1 + sprintf(intbuf, "%d", rowdata[j]);
566 desc = snewn(desclen, char);
568 for (i = 0; i < params->w + params->h; i++) {
570 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w);
572 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w,
575 for (j = 0; j < rowlen; j++) {
576 int len = sprintf(desc+descpos, "%d", rowdata[j]);
578 desc[descpos + len] = '.';
580 desc[descpos + len] = '/';
584 desc[descpos++] = '/';
587 assert(descpos == desclen);
588 assert(desc[desclen-1] == '/');
589 desc[desclen-1] = '\0';
595 static char *validate_desc(game_params *params, char *desc)
600 for (i = 0; i < params->w + params->h; i++) {
602 rowspace = params->h + 1;
604 rowspace = params->w + 1;
606 if (*desc && isdigit((unsigned char)*desc)) {
609 while (*desc && isdigit((unsigned char)*desc)) desc++;
615 return "at least one column contains more numbers than will fit";
617 return "at least one row contains more numbers than will fit";
619 } while (*desc++ == '.');
621 desc++; /* expect a slash immediately */
624 if (desc[-1] == '/') {
625 if (i+1 == params->w + params->h)
626 return "too many row/column specifications";
627 } else if (desc[-1] == '\0') {
628 if (i+1 < params->w + params->h)
629 return "too few row/column specifications";
631 return "unrecognised character in game specification";
637 static game_state *new_game(midend *me, game_params *params, char *desc)
641 game_state *state = snew(game_state);
643 state->w = params->w;
644 state->h = params->h;
646 state->grid = snewn(state->w * state->h, unsigned char);
647 memset(state->grid, GRID_UNKNOWN, state->w * state->h);
649 state->rowsize = max(state->w, state->h);
650 state->rowdata = snewn(state->rowsize * (state->w + state->h), int);
651 state->rowlen = snewn(state->w + state->h, int);
653 state->completed = state->cheated = FALSE;
655 for (i = 0; i < params->w + params->h; i++) {
656 state->rowlen[i] = 0;
657 if (*desc && isdigit((unsigned char)*desc)) {
660 while (*desc && isdigit((unsigned char)*desc)) desc++;
661 state->rowdata[state->rowsize * i + state->rowlen[i]++] =
663 } while (*desc++ == '.');
665 desc++; /* expect a slash immediately */
672 static game_state *dup_game(game_state *state)
674 game_state *ret = snew(game_state);
679 ret->grid = snewn(ret->w * ret->h, unsigned char);
680 memcpy(ret->grid, state->grid, ret->w * ret->h);
682 ret->rowsize = state->rowsize;
683 ret->rowdata = snewn(ret->rowsize * (ret->w + ret->h), int);
684 ret->rowlen = snewn(ret->w + ret->h, int);
685 memcpy(ret->rowdata, state->rowdata,
686 ret->rowsize * (ret->w + ret->h) * sizeof(int));
687 memcpy(ret->rowlen, state->rowlen,
688 (ret->w + ret->h) * sizeof(int));
690 ret->completed = state->completed;
691 ret->cheated = state->cheated;
696 static void free_game(game_state *state)
698 sfree(state->rowdata);
699 sfree(state->rowlen);
704 static char *solve_game(game_state *state, game_state *currstate,
705 char *ai, char **error)
707 unsigned char *matrix;
708 int w = state->w, h = state->h;
712 unsigned char *workspace;
716 * If we already have the solved state in ai, copy it out.
721 matrix = snewn(w*h, unsigned char);
723 workspace = snewn(max*3, unsigned char);
724 rowdata = snewn(max+1, int);
726 memset(matrix, 0, w*h);
730 for (i=0; i<h; i++) {
731 memcpy(rowdata, state->rowdata + state->rowsize*(w+i),
733 rowdata[state->rowlen[w+i]] = 0;
734 done_any |= do_row(workspace, workspace+max, workspace+2*max,
735 matrix+i*w, w, 1, rowdata
736 #ifdef STANDALONE_SOLVER
737 , NULL, 0, 0 /* never do diagnostics here */
741 for (i=0; i<w; i++) {
742 memcpy(rowdata, state->rowdata + state->rowsize*i, max*sizeof(int));
743 rowdata[state->rowlen[i]] = 0;
744 done_any |= do_row(workspace, workspace+max, workspace+2*max,
745 matrix+i, h, w, rowdata
746 #ifdef STANDALONE_SOLVER
747 , NULL, 0, 0 /* never do diagnostics here */
756 for (i = 0; i < w*h; i++) {
757 if (matrix[i] != BLOCK && matrix[i] != DOT) {
759 *error = "Solving algorithm cannot complete this puzzle";
764 ret = snewn(w*h+2, char);
766 for (i = 0; i < w*h; i++) {
767 assert(matrix[i] == BLOCK || matrix[i] == DOT);
768 ret[i+1] = (matrix[i] == BLOCK ? '1' : '0');
777 static int game_can_format_as_text_now(game_params *params)
782 static char *game_text_format(game_state *state)
793 int drag, release, state;
794 int cur_x, cur_y, cur_visible;
797 static game_ui *new_ui(game_state *state)
802 ret->dragging = FALSE;
803 ret->cur_x = ret->cur_y = ret->cur_visible = 0;
808 static void free_ui(game_ui *ui)
813 static char *encode_ui(game_ui *ui)
818 static void decode_ui(game_ui *ui, char *encoding)
822 static void game_changed_state(game_ui *ui, game_state *oldstate,
823 game_state *newstate)
827 struct game_drawstate {
831 unsigned char *visible;
835 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
836 int x, int y, int button)
840 x = FROMCOORD(state->w, x);
841 y = FROMCOORD(state->h, y);
843 if (x >= 0 && x < state->w && y >= 0 && y < state->h &&
844 (button == LEFT_BUTTON || button == RIGHT_BUTTON ||
845 button == MIDDLE_BUTTON)) {
847 int currstate = state->grid[y * state->w + x];
852 if (button == LEFT_BUTTON) {
853 ui->drag = LEFT_DRAG;
854 ui->release = LEFT_RELEASE;
856 ui->state = (currstate + 2) % 3; /* FULL -> EMPTY -> UNKNOWN */
858 ui->state = GRID_FULL;
860 } else if (button == RIGHT_BUTTON) {
861 ui->drag = RIGHT_DRAG;
862 ui->release = RIGHT_RELEASE;
864 ui->state = (currstate + 1) % 3; /* EMPTY -> FULL -> UNKNOWN */
866 ui->state = GRID_EMPTY;
868 } else /* if (button == MIDDLE_BUTTON) */ {
869 ui->drag = MIDDLE_DRAG;
870 ui->release = MIDDLE_RELEASE;
871 ui->state = GRID_UNKNOWN;
874 ui->drag_start_x = ui->drag_end_x = x;
875 ui->drag_start_y = ui->drag_end_y = y;
878 return ""; /* UI activity occurred */
881 if (ui->dragging && button == ui->drag) {
883 * There doesn't seem much point in allowing a rectangle
884 * drag; people will generally only want to drag a single
885 * horizontal or vertical line, so we make that easy by
888 * Exception: if we're _middle_-button dragging to tag
889 * things as UNKNOWN, we may well want to trash an entire
890 * area and start over!
892 if (ui->state != GRID_UNKNOWN) {
893 if (abs(x - ui->drag_start_x) > abs(y - ui->drag_start_y))
894 y = ui->drag_start_y;
896 x = ui->drag_start_x;
901 if (x >= state->w) x = state->w - 1;
902 if (y >= state->h) y = state->h - 1;
907 return ""; /* UI activity occurred */
910 if (ui->dragging && button == ui->release) {
911 int x1, x2, y1, y2, xx, yy;
912 int move_needed = FALSE;
914 x1 = min(ui->drag_start_x, ui->drag_end_x);
915 x2 = max(ui->drag_start_x, ui->drag_end_x);
916 y1 = min(ui->drag_start_y, ui->drag_end_y);
917 y2 = max(ui->drag_start_y, ui->drag_end_y);
919 for (yy = y1; yy <= y2; yy++)
920 for (xx = x1; xx <= x2; xx++)
921 if (state->grid[yy * state->w + xx] != ui->state)
924 ui->dragging = FALSE;
928 sprintf(buf, "%c%d,%d,%d,%d",
929 (char)(ui->state == GRID_FULL ? 'F' :
930 ui->state == GRID_EMPTY ? 'E' : 'U'),
931 x1, y1, x2-x1+1, y2-y1+1);
934 return ""; /* UI activity occurred */
937 if (IS_CURSOR_MOVE(button)) {
938 move_cursor(button, &ui->cur_x, &ui->cur_y, state->w, state->h, 0);
942 if (IS_CURSOR_SELECT(button)) {
943 int currstate = state->grid[ui->cur_y * state->w + ui->cur_x];
947 if (!ui->cur_visible) {
952 if (button == CURSOR_SELECT2)
953 newstate = currstate == GRID_UNKNOWN ? GRID_EMPTY :
954 currstate == GRID_EMPTY ? GRID_FULL : GRID_UNKNOWN;
956 newstate = currstate == GRID_UNKNOWN ? GRID_FULL :
957 currstate == GRID_FULL ? GRID_EMPTY : GRID_UNKNOWN;
959 sprintf(buf, "%c%d,%d,%d,%d",
960 (char)(newstate == GRID_FULL ? 'F' :
961 newstate == GRID_EMPTY ? 'E' : 'U'),
962 ui->cur_x, ui->cur_y, 1, 1);
969 static game_state *execute_move(game_state *from, char *move)
972 int x1, x2, y1, y2, xx, yy;
975 if (move[0] == 'S' && strlen(move) == from->w * from->h + 1) {
978 ret = dup_game(from);
980 for (i = 0; i < ret->w * ret->h; i++)
981 ret->grid[i] = (move[i+1] == '1' ? GRID_FULL : GRID_EMPTY);
983 ret->completed = ret->cheated = TRUE;
986 } else if ((move[0] == 'F' || move[0] == 'E' || move[0] == 'U') &&
987 sscanf(move+1, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) == 4 &&
988 x1 >= 0 && x2 >= 0 && x1+x2 <= from->w &&
989 y1 >= 0 && y2 >= 0 && y1+y2 <= from->h) {
993 val = (move[0] == 'F' ? GRID_FULL :
994 move[0] == 'E' ? GRID_EMPTY : GRID_UNKNOWN);
996 ret = dup_game(from);
997 for (yy = y1; yy < y2; yy++)
998 for (xx = x1; xx < x2; xx++)
999 ret->grid[yy * ret->w + xx] = val;
1002 * An actual change, so check to see if we've completed the
1005 if (!ret->completed) {
1006 int *rowdata = snewn(ret->rowsize, int);
1009 ret->completed = TRUE;
1011 for (i=0; i<ret->w; i++) {
1012 len = compute_rowdata(rowdata,
1013 ret->grid+i, ret->h, ret->w);
1014 if (len != ret->rowlen[i] ||
1015 memcmp(ret->rowdata+i*ret->rowsize, rowdata,
1016 len * sizeof(int))) {
1017 ret->completed = FALSE;
1021 for (i=0; i<ret->h; i++) {
1022 len = compute_rowdata(rowdata,
1023 ret->grid+i*ret->w, ret->w, 1);
1024 if (len != ret->rowlen[i+ret->w] ||
1025 memcmp(ret->rowdata+(i+ret->w)*ret->rowsize, rowdata,
1026 len * sizeof(int))) {
1027 ret->completed = FALSE;
1040 /* ----------------------------------------------------------------------
1044 static void game_compute_size(game_params *params, int tilesize,
1047 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1048 struct { int tilesize; } ads, *ds = &ads;
1049 ads.tilesize = tilesize;
1051 *x = SIZE(params->w);
1052 *y = SIZE(params->h);
1055 static void game_set_size(drawing *dr, game_drawstate *ds,
1056 game_params *params, int tilesize)
1058 ds->tilesize = tilesize;
1061 static float *game_colours(frontend *fe, int *ncolours)
1063 float *ret = snewn(3 * NCOLOURS, float);
1066 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1068 for (i = 0; i < 3; i++) {
1069 ret[COL_GRID * 3 + i] = 0.3F;
1070 ret[COL_UNKNOWN * 3 + i] = 0.5F;
1071 ret[COL_TEXT * 3 + i] = 0.0F;
1072 ret[COL_FULL * 3 + i] = 0.0F;
1073 ret[COL_EMPTY * 3 + i] = 1.0F;
1075 ret[COL_CURSOR * 3 + 0] = 1.0F;
1076 ret[COL_CURSOR * 3 + 1] = 0.25F;
1077 ret[COL_CURSOR * 3 + 2] = 0.25F;
1079 *ncolours = NCOLOURS;
1083 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1085 struct game_drawstate *ds = snew(struct game_drawstate);
1087 ds->started = FALSE;
1090 ds->visible = snewn(ds->w * ds->h, unsigned char);
1091 ds->tilesize = 0; /* not decided yet */
1092 memset(ds->visible, 255, ds->w * ds->h);
1093 ds->cur_x = ds->cur_y = 0;
1098 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1104 static void grid_square(drawing *dr, game_drawstate *ds,
1105 int y, int x, int state, int cur)
1107 int xl, xr, yt, yb, dx, dy, dw, dh;
1109 draw_rect(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
1110 TILE_SIZE, TILE_SIZE, COL_GRID);
1112 xl = (x % 5 == 0 ? 1 : 0);
1113 yt = (y % 5 == 0 ? 1 : 0);
1114 xr = (x % 5 == 4 || x == ds->w-1 ? 1 : 0);
1115 yb = (y % 5 == 4 || y == ds->h-1 ? 1 : 0);
1117 dx = TOCOORD(ds->w, x) + 1 + xl;
1118 dy = TOCOORD(ds->h, y) + 1 + yt;
1119 dw = TILE_SIZE - xl - xr - 1;
1120 dh = TILE_SIZE - yt - yb - 1;
1122 draw_rect(dr, dx, dy, dw, dh,
1123 (state == GRID_FULL ? COL_FULL :
1124 state == GRID_EMPTY ? COL_EMPTY : COL_UNKNOWN));
1126 draw_rect_outline(dr, dx, dy, dw, dh, COL_CURSOR);
1127 draw_rect_outline(dr, dx+1, dy+1, dw-2, dh-2, COL_CURSOR);
1130 draw_update(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
1131 TILE_SIZE, TILE_SIZE);
1134 static void draw_numbers(drawing *dr, game_drawstate *ds, game_state *state,
1142 for (i = 0; i < state->w + state->h; i++) {
1143 int rowlen = state->rowlen[i];
1144 int *rowdata = state->rowdata + state->rowsize * i;
1148 * Normally I space the numbers out by the same
1149 * distance as the tile size. However, if there are
1150 * more numbers than available spaces, I have to squash
1153 nfit = max(rowlen, TLBORDER(state->h))-1;
1156 for (j = 0; j < rowlen; j++) {
1161 x = TOCOORD(state->w, i);
1162 y = BORDER + TILE_SIZE * (TLBORDER(state->h)-1);
1163 y -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(state->h)-1) / nfit;
1165 y = TOCOORD(state->h, i - state->w);
1166 x = BORDER + TILE_SIZE * (TLBORDER(state->w)-1);
1167 x -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(state->h)-1) / nfit;
1170 sprintf(str, "%d", rowdata[j]);
1171 draw_text(dr, x+TILE_SIZE/2, y+TILE_SIZE/2, FONT_VARIABLE,
1172 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, colour, str);
1177 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
1178 game_state *state, int dir, game_ui *ui,
1179 float animtime, float flashtime)
1187 * The initial contents of the window are not guaranteed
1188 * and can vary with front ends. To be on the safe side,
1189 * all games should start by drawing a big background-
1190 * colour rectangle covering the whole window.
1192 draw_rect(dr, 0, 0, SIZE(ds->w), SIZE(ds->h), COL_BACKGROUND);
1197 draw_numbers(dr, ds, state, COL_TEXT);
1200 * Draw the grid outline.
1202 draw_rect(dr, TOCOORD(ds->w, 0) - 1, TOCOORD(ds->h, 0) - 1,
1203 ds->w * TILE_SIZE + 3, ds->h * TILE_SIZE + 3,
1208 draw_update(dr, 0, 0, SIZE(ds->w), SIZE(ds->h));
1212 x1 = min(ui->drag_start_x, ui->drag_end_x);
1213 x2 = max(ui->drag_start_x, ui->drag_end_x);
1214 y1 = min(ui->drag_start_y, ui->drag_end_y);
1215 y2 = max(ui->drag_start_y, ui->drag_end_y);
1217 x1 = x2 = y1 = y2 = -1; /* placate gcc warnings */
1220 if (ui->cur_visible) {
1221 cx = ui->cur_x; cy = ui->cur_y;
1225 cmoved = (cx != ds->cur_x || cy != ds->cur_y);
1228 * Now draw any grid squares which have changed since last
1231 for (i = 0; i < ds->h; i++) {
1232 for (j = 0; j < ds->w; j++) {
1236 * Work out what state this square should be drawn in,
1237 * taking any current drag operation into account.
1239 if (ui->dragging && x1 <= j && j <= x2 && y1 <= i && i <= y2)
1242 val = state->grid[i * state->w + j];
1245 /* the cursor has moved; if we were the old or
1246 * the new cursor position we need to redraw. */
1247 if (j == cx && i == cy) cc = 1;
1248 if (j == ds->cur_x && i == ds->cur_y) cc = 1;
1252 * Briefly invert everything twice during a completion
1255 if (flashtime > 0 &&
1256 (flashtime <= FLASH_TIME/3 || flashtime >= FLASH_TIME*2/3) &&
1257 val != GRID_UNKNOWN)
1258 val = (GRID_FULL ^ GRID_EMPTY) ^ val;
1260 if (ds->visible[i * ds->w + j] != val || cc) {
1261 grid_square(dr, ds, i, j, val,
1262 (j == cx && i == cy));
1263 ds->visible[i * ds->w + j] = val;
1267 ds->cur_x = cx; ds->cur_y = cy;
1270 static float game_anim_length(game_state *oldstate,
1271 game_state *newstate, int dir, game_ui *ui)
1276 static float game_flash_length(game_state *oldstate,
1277 game_state *newstate, int dir, game_ui *ui)
1279 if (!oldstate->completed && newstate->completed &&
1280 !oldstate->cheated && !newstate->cheated)
1285 static int game_timing_state(game_state *state, game_ui *ui)
1290 static void game_print_size(game_params *params, float *x, float *y)
1295 * I'll use 5mm squares by default.
1297 game_compute_size(params, 500, &pw, &ph);
1302 static void game_print(drawing *dr, game_state *state, int tilesize)
1304 int w = state->w, h = state->h;
1305 int ink = print_mono_colour(dr, 0);
1308 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1309 game_drawstate ads, *ds = &ads;
1310 game_set_size(dr, ds, NULL, tilesize);
1315 print_line_width(dr, TILE_SIZE / 16);
1316 draw_rect_outline(dr, TOCOORD(w, 0), TOCOORD(h, 0),
1317 w*TILE_SIZE, h*TILE_SIZE, ink);
1322 for (x = 1; x < w; x++) {
1323 print_line_width(dr, TILE_SIZE / (x % 5 ? 128 : 24));
1324 draw_line(dr, TOCOORD(w, x), TOCOORD(h, 0),
1325 TOCOORD(w, x), TOCOORD(h, h), ink);
1327 for (y = 1; y < h; y++) {
1328 print_line_width(dr, TILE_SIZE / (y % 5 ? 128 : 24));
1329 draw_line(dr, TOCOORD(w, 0), TOCOORD(h, y),
1330 TOCOORD(w, w), TOCOORD(h, y), ink);
1336 draw_numbers(dr, ds, state, ink);
1341 print_line_width(dr, TILE_SIZE / 128);
1342 for (y = 0; y < h; y++)
1343 for (x = 0; x < w; x++) {
1344 if (state->grid[y*w+x] == GRID_FULL)
1345 draw_rect(dr, TOCOORD(w, x), TOCOORD(h, y),
1346 TILE_SIZE, TILE_SIZE, ink);
1347 else if (state->grid[y*w+x] == GRID_EMPTY)
1348 draw_circle(dr, TOCOORD(w, x) + TILE_SIZE/2,
1349 TOCOORD(h, y) + TILE_SIZE/2,
1350 TILE_SIZE/12, ink, ink);
1355 #define thegame pattern
1358 const struct game thegame = {
1359 "Pattern", "games.pattern", "pattern",
1366 TRUE, game_configure, custom_params,
1374 FALSE, game_can_format_as_text_now, game_text_format,
1382 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
1385 game_free_drawstate,
1389 TRUE, FALSE, game_print_size, game_print,
1390 FALSE, /* wants_statusbar */
1391 FALSE, game_timing_state,
1392 REQUIRE_RBUTTON, /* flags */
1395 #ifdef STANDALONE_SOLVER
1397 int main(int argc, char **argv)
1401 char *id = NULL, *desc, *err;
1403 while (--argc > 0) {
1406 if (!strcmp(p, "-v")) {
1409 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
1418 fprintf(stderr, "usage: %s <game_id>\n", argv[0]);
1422 desc = strchr(id, ':');
1424 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
1429 p = default_params();
1430 decode_params(p, id);
1431 err = validate_desc(p, desc);
1433 fprintf(stderr, "%s: %s\n", argv[0], err);
1436 s = new_game(NULL, p, desc);
1439 int w = p->w, h = p->h, i, j, done_any, max, cluewid = 0;
1440 unsigned char *matrix, *workspace;
1443 matrix = snewn(w*h, unsigned char);
1445 workspace = snewn(max*3, unsigned char);
1446 rowdata = snewn(max+1, int);
1448 memset(matrix, 0, w*h);
1453 * Work out the maximum text width of the clue numbers
1454 * in a row or column, so we can print the solver's
1455 * working in a nicely lined up way.
1457 for (i = 0; i < (w+h); i++) {
1459 for (thiswid = -1, j = 0; j < s->rowlen[i]; j++)
1460 thiswid += sprintf(buf, " %d", s->rowdata[s->rowsize*i+j]);
1461 if (cluewid < thiswid)
1468 for (i=0; i<h; i++) {
1469 memcpy(rowdata, s->rowdata + s->rowsize*(w+i),
1471 rowdata[s->rowlen[w+i]] = 0;
1472 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1473 matrix+i*w, w, 1, rowdata
1474 #ifdef STANDALONE_SOLVER
1475 , "row", i+1, cluewid
1479 for (i=0; i<w; i++) {
1480 memcpy(rowdata, s->rowdata + s->rowsize*i, max*sizeof(int));
1481 rowdata[s->rowlen[i]] = 0;
1482 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1483 matrix+i, h, w, rowdata
1484 #ifdef STANDALONE_SOLVER
1485 , "col", i+1, cluewid
1491 for (i = 0; i < h; i++) {
1492 for (j = 0; j < w; j++) {
1493 int c = (matrix[i*w+j] == UNKNOWN ? '?' :
1494 matrix[i*w+j] == BLOCK ? '#' :
1495 matrix[i*w+j] == DOT ? '.' :
1508 /* vim: set shiftwidth=4 tabstop=8: */