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 static void do_recurse(unsigned char *known, unsigned char *deduced,
343 unsigned char *row, int *data, int len,
344 int freespace, int ndone, int lowest)
349 for (i=0; i<=freespace; i++) {
351 for (k=0; k<i; k++) row[j++] = DOT;
352 for (k=0; k<data[ndone]; k++) row[j++] = BLOCK;
353 if (j < len) row[j++] = DOT;
354 do_recurse(known, deduced, row, data, len,
355 freespace-i, ndone+1, j);
358 for (i=lowest; i<len; i++)
360 for (i=0; i<len; i++)
361 if (known[i] && known[i] != row[i])
363 for (i=0; i<len; i++)
364 deduced[i] |= row[i];
368 static int do_row(unsigned char *known, unsigned char *deduced,
370 unsigned char *start, int len, int step, int *data)
372 int rowlen, i, freespace, done_any;
375 for (rowlen = 0; data[rowlen]; rowlen++)
376 freespace -= data[rowlen]+1;
378 for (i = 0; i < len; i++) {
379 known[i] = start[i*step];
383 do_recurse(known, deduced, row, data, len, freespace, 0, 0);
385 for (i=0; i<len; i++)
386 if (deduced[i] && deduced[i] != STILL_UNKNOWN && !known[i]) {
387 start[i*step] = deduced[i];
393 static unsigned char *generate_soluble(random_state *rs, int w, int h)
395 int i, j, done_any, ok, ntries, max;
396 unsigned char *grid, *matrix, *workspace;
399 grid = snewn(w*h, unsigned char);
400 matrix = snewn(w*h, unsigned char);
402 workspace = snewn(max*3, unsigned char);
403 rowdata = snewn(max+1, int);
410 generate(rs, w, h, grid);
413 * The game is a bit too easy if any row or column is
414 * completely black or completely white. An exception is
415 * made for rows/columns that are under 3 squares,
416 * otherwise nothing will ever be successfully generated.
420 for (i = 0; i < h; i++) {
422 for (j = 0; j < w; j++)
423 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
429 for (j = 0; j < w; j++) {
431 for (i = 0; i < h; i++)
432 colours |= (grid[i*w+j] == GRID_FULL ? 2 : 1);
440 memset(matrix, 0, w*h);
444 for (i=0; i<h; i++) {
445 rowdata[compute_rowdata(rowdata, grid+i*w, w, 1)] = 0;
446 done_any |= do_row(workspace, workspace+max, workspace+2*max,
447 matrix+i*w, w, 1, rowdata);
449 for (i=0; i<w; i++) {
450 rowdata[compute_rowdata(rowdata, grid+i, h, w)] = 0;
451 done_any |= do_row(workspace, workspace+max, workspace+2*max,
452 matrix+i, h, w, rowdata);
457 for (i=0; i<h; i++) {
458 for (j=0; j<w; j++) {
459 if (matrix[i*w+j] == UNKNOWN)
471 static char *new_game_desc(game_params *params, random_state *rs,
472 char **aux, int interactive)
475 int i, j, max, rowlen, *rowdata;
476 char intbuf[80], *desc;
477 int desclen, descpos;
479 grid = generate_soluble(rs, params->w, params->h);
480 max = max(params->w, params->h);
481 rowdata = snewn(max, int);
484 * Save the solved game in aux.
487 char *ai = snewn(params->w * params->h + 2, char);
490 * String format is exactly the same as a solve move, so we
491 * can just dupstr this in solve_game().
496 for (i = 0; i < params->w * params->h; i++)
497 ai[i+1] = grid[i] ? '1' : '0';
499 ai[params->w * params->h + 1] = '\0';
505 * Seed is a slash-separated list of row contents; each row
506 * contents section is a dot-separated list of integers. Row
507 * contents are listed in the order (columns left to right,
508 * then rows top to bottom).
510 * Simplest way to handle memory allocation is to make two
511 * passes, first computing the seed size and then writing it
515 for (i = 0; i < params->w + params->h; i++) {
517 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w);
519 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w,
522 for (j = 0; j < rowlen; j++) {
523 desclen += 1 + sprintf(intbuf, "%d", rowdata[j]);
529 desc = snewn(desclen, char);
531 for (i = 0; i < params->w + params->h; i++) {
533 rowlen = compute_rowdata(rowdata, grid+i, params->h, params->w);
535 rowlen = compute_rowdata(rowdata, grid+(i-params->w)*params->w,
538 for (j = 0; j < rowlen; j++) {
539 int len = sprintf(desc+descpos, "%d", rowdata[j]);
541 desc[descpos + len] = '.';
543 desc[descpos + len] = '/';
547 desc[descpos++] = '/';
550 assert(descpos == desclen);
551 assert(desc[desclen-1] == '/');
552 desc[desclen-1] = '\0';
558 static char *validate_desc(game_params *params, char *desc)
563 for (i = 0; i < params->w + params->h; i++) {
565 rowspace = params->h + 1;
567 rowspace = params->w + 1;
569 if (*desc && isdigit((unsigned char)*desc)) {
572 while (*desc && isdigit((unsigned char)*desc)) desc++;
578 return "at least one column contains more numbers than will fit";
580 return "at least one row contains more numbers than will fit";
582 } while (*desc++ == '.');
584 desc++; /* expect a slash immediately */
587 if (desc[-1] == '/') {
588 if (i+1 == params->w + params->h)
589 return "too many row/column specifications";
590 } else if (desc[-1] == '\0') {
591 if (i+1 < params->w + params->h)
592 return "too few row/column specifications";
594 return "unrecognised character in game specification";
600 static game_state *new_game(midend *me, game_params *params, char *desc)
604 game_state *state = snew(game_state);
606 state->w = params->w;
607 state->h = params->h;
609 state->grid = snewn(state->w * state->h, unsigned char);
610 memset(state->grid, GRID_UNKNOWN, state->w * state->h);
612 state->rowsize = max(state->w, state->h);
613 state->rowdata = snewn(state->rowsize * (state->w + state->h), int);
614 state->rowlen = snewn(state->w + state->h, int);
616 state->completed = state->cheated = FALSE;
618 for (i = 0; i < params->w + params->h; i++) {
619 state->rowlen[i] = 0;
620 if (*desc && isdigit((unsigned char)*desc)) {
623 while (*desc && isdigit((unsigned char)*desc)) desc++;
624 state->rowdata[state->rowsize * i + state->rowlen[i]++] =
626 } while (*desc++ == '.');
628 desc++; /* expect a slash immediately */
635 static game_state *dup_game(game_state *state)
637 game_state *ret = snew(game_state);
642 ret->grid = snewn(ret->w * ret->h, unsigned char);
643 memcpy(ret->grid, state->grid, ret->w * ret->h);
645 ret->rowsize = state->rowsize;
646 ret->rowdata = snewn(ret->rowsize * (ret->w + ret->h), int);
647 ret->rowlen = snewn(ret->w + ret->h, int);
648 memcpy(ret->rowdata, state->rowdata,
649 ret->rowsize * (ret->w + ret->h) * sizeof(int));
650 memcpy(ret->rowlen, state->rowlen,
651 (ret->w + ret->h) * sizeof(int));
653 ret->completed = state->completed;
654 ret->cheated = state->cheated;
659 static void free_game(game_state *state)
661 sfree(state->rowdata);
662 sfree(state->rowlen);
667 static char *solve_game(game_state *state, game_state *currstate,
668 char *ai, char **error)
670 unsigned char *matrix;
671 int w = state->w, h = state->h;
675 unsigned char *workspace;
679 * If we already have the solved state in ai, copy it out.
684 matrix = snewn(w*h, unsigned char);
686 workspace = snewn(max*3, unsigned char);
687 rowdata = snewn(max+1, int);
689 memset(matrix, 0, w*h);
693 for (i=0; i<h; i++) {
694 memcpy(rowdata, state->rowdata + state->rowsize*(w+i),
696 rowdata[state->rowlen[w+i]] = 0;
697 done_any |= do_row(workspace, workspace+max, workspace+2*max,
698 matrix+i*w, w, 1, rowdata);
700 for (i=0; i<w; i++) {
701 memcpy(rowdata, state->rowdata + state->rowsize*i, max*sizeof(int));
702 rowdata[state->rowlen[i]] = 0;
703 done_any |= do_row(workspace, workspace+max, workspace+2*max,
704 matrix+i, h, w, rowdata);
711 for (i = 0; i < w*h; i++) {
712 if (matrix[i] != BLOCK && matrix[i] != DOT) {
714 *error = "Solving algorithm cannot complete this puzzle";
719 ret = snewn(w*h+2, char);
721 for (i = 0; i < w*h; i++) {
722 assert(matrix[i] == BLOCK || matrix[i] == DOT);
723 ret[i+1] = (matrix[i] == BLOCK ? '1' : '0');
732 static int game_can_format_as_text_now(game_params *params)
737 static char *game_text_format(game_state *state)
748 int drag, release, state;
749 int cur_x, cur_y, cur_visible;
752 static game_ui *new_ui(game_state *state)
757 ret->dragging = FALSE;
758 ret->cur_x = ret->cur_y = ret->cur_visible = 0;
763 static void free_ui(game_ui *ui)
768 static char *encode_ui(game_ui *ui)
773 static void decode_ui(game_ui *ui, char *encoding)
777 static void game_changed_state(game_ui *ui, game_state *oldstate,
778 game_state *newstate)
782 struct game_drawstate {
786 unsigned char *visible;
790 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
791 int x, int y, int button)
795 x = FROMCOORD(state->w, x);
796 y = FROMCOORD(state->h, y);
798 if (x >= 0 && x < state->w && y >= 0 && y < state->h &&
799 (button == LEFT_BUTTON || button == RIGHT_BUTTON ||
800 button == MIDDLE_BUTTON)) {
802 int currstate = state->grid[y * state->w + x];
807 if (button == LEFT_BUTTON) {
808 ui->drag = LEFT_DRAG;
809 ui->release = LEFT_RELEASE;
811 ui->state = currstate == GRID_FULL ? GRID_UNKNOWN : GRID_FULL;
813 ui->state = GRID_FULL;
815 } else if (button == RIGHT_BUTTON) {
816 ui->drag = RIGHT_DRAG;
817 ui->release = RIGHT_RELEASE;
819 ui->state = currstate == GRID_EMPTY ? GRID_UNKNOWN : GRID_EMPTY;
821 ui->state = GRID_EMPTY;
823 } else /* if (button == MIDDLE_BUTTON) */ {
824 ui->drag = MIDDLE_DRAG;
825 ui->release = MIDDLE_RELEASE;
826 ui->state = GRID_UNKNOWN;
829 ui->drag_start_x = ui->drag_end_x = x;
830 ui->drag_start_y = ui->drag_end_y = y;
833 return ""; /* UI activity occurred */
836 if (ui->dragging && button == ui->drag) {
838 * There doesn't seem much point in allowing a rectangle
839 * drag; people will generally only want to drag a single
840 * horizontal or vertical line, so we make that easy by
843 * Exception: if we're _middle_-button dragging to tag
844 * things as UNKNOWN, we may well want to trash an entire
845 * area and start over!
847 if (ui->state != GRID_UNKNOWN) {
848 if (abs(x - ui->drag_start_x) > abs(y - ui->drag_start_y))
849 y = ui->drag_start_y;
851 x = ui->drag_start_x;
856 if (x >= state->w) x = state->w - 1;
857 if (y >= state->h) y = state->h - 1;
862 return ""; /* UI activity occurred */
865 if (ui->dragging && button == ui->release) {
866 int x1, x2, y1, y2, xx, yy;
867 int move_needed = FALSE;
869 x1 = min(ui->drag_start_x, ui->drag_end_x);
870 x2 = max(ui->drag_start_x, ui->drag_end_x);
871 y1 = min(ui->drag_start_y, ui->drag_end_y);
872 y2 = max(ui->drag_start_y, ui->drag_end_y);
874 for (yy = y1; yy <= y2; yy++)
875 for (xx = x1; xx <= x2; xx++)
876 if (state->grid[yy * state->w + xx] != ui->state)
879 ui->dragging = FALSE;
883 sprintf(buf, "%c%d,%d,%d,%d",
884 (char)(ui->state == GRID_FULL ? 'F' :
885 ui->state == GRID_EMPTY ? 'E' : 'U'),
886 x1, y1, x2-x1+1, y2-y1+1);
889 return ""; /* UI activity occurred */
892 if (IS_CURSOR_MOVE(button)) {
893 move_cursor(button, &ui->cur_x, &ui->cur_y, state->w, state->h, 0);
897 if (IS_CURSOR_SELECT(button)) {
898 int currstate = state->grid[ui->cur_y * state->w + ui->cur_x];
902 if (!ui->cur_visible) {
907 if (button == CURSOR_SELECT2)
908 newstate = currstate == GRID_UNKNOWN ? GRID_EMPTY :
909 currstate == GRID_EMPTY ? GRID_FULL : GRID_UNKNOWN;
911 newstate = currstate == GRID_UNKNOWN ? GRID_FULL :
912 currstate == GRID_FULL ? GRID_EMPTY : GRID_UNKNOWN;
914 sprintf(buf, "%c%d,%d,%d,%d",
915 (char)(newstate == GRID_FULL ? 'F' :
916 newstate == GRID_EMPTY ? 'E' : 'U'),
917 ui->cur_x, ui->cur_y, 1, 1);
924 static game_state *execute_move(game_state *from, char *move)
927 int x1, x2, y1, y2, xx, yy;
930 if (move[0] == 'S' && strlen(move) == from->w * from->h + 1) {
933 ret = dup_game(from);
935 for (i = 0; i < ret->w * ret->h; i++)
936 ret->grid[i] = (move[i+1] == '1' ? GRID_FULL : GRID_EMPTY);
938 ret->completed = ret->cheated = TRUE;
941 } else if ((move[0] == 'F' || move[0] == 'E' || move[0] == 'U') &&
942 sscanf(move+1, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) == 4 &&
943 x1 >= 0 && x2 >= 0 && x1+x2 <= from->w &&
944 y1 >= 0 && y2 >= 0 && y1+y2 <= from->h) {
948 val = (move[0] == 'F' ? GRID_FULL :
949 move[0] == 'E' ? GRID_EMPTY : GRID_UNKNOWN);
951 ret = dup_game(from);
952 for (yy = y1; yy < y2; yy++)
953 for (xx = x1; xx < x2; xx++)
954 ret->grid[yy * ret->w + xx] = val;
957 * An actual change, so check to see if we've completed the
960 if (!ret->completed) {
961 int *rowdata = snewn(ret->rowsize, int);
964 ret->completed = TRUE;
966 for (i=0; i<ret->w; i++) {
967 len = compute_rowdata(rowdata,
968 ret->grid+i, ret->h, ret->w);
969 if (len != ret->rowlen[i] ||
970 memcmp(ret->rowdata+i*ret->rowsize, rowdata,
971 len * sizeof(int))) {
972 ret->completed = FALSE;
976 for (i=0; i<ret->h; i++) {
977 len = compute_rowdata(rowdata,
978 ret->grid+i*ret->w, ret->w, 1);
979 if (len != ret->rowlen[i+ret->w] ||
980 memcmp(ret->rowdata+(i+ret->w)*ret->rowsize, rowdata,
981 len * sizeof(int))) {
982 ret->completed = FALSE;
995 /* ----------------------------------------------------------------------
999 static void game_compute_size(game_params *params, int tilesize,
1002 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1003 struct { int tilesize; } ads, *ds = &ads;
1004 ads.tilesize = tilesize;
1006 *x = SIZE(params->w);
1007 *y = SIZE(params->h);
1010 static void game_set_size(drawing *dr, game_drawstate *ds,
1011 game_params *params, int tilesize)
1013 ds->tilesize = tilesize;
1016 static float *game_colours(frontend *fe, int *ncolours)
1018 float *ret = snewn(3 * NCOLOURS, float);
1021 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1023 for (i = 0; i < 3; i++) {
1024 ret[COL_GRID * 3 + i] = 0.3F;
1025 ret[COL_UNKNOWN * 3 + i] = 0.5F;
1026 ret[COL_TEXT * 3 + i] = 0.0F;
1027 ret[COL_FULL * 3 + i] = 0.0F;
1028 ret[COL_EMPTY * 3 + i] = 1.0F;
1030 ret[COL_CURSOR * 3 + 0] = 1.0F;
1031 ret[COL_CURSOR * 3 + 1] = 0.25F;
1032 ret[COL_CURSOR * 3 + 2] = 0.25F;
1034 *ncolours = NCOLOURS;
1038 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1040 struct game_drawstate *ds = snew(struct game_drawstate);
1042 ds->started = FALSE;
1045 ds->visible = snewn(ds->w * ds->h, unsigned char);
1046 ds->tilesize = 0; /* not decided yet */
1047 memset(ds->visible, 255, ds->w * ds->h);
1048 ds->cur_x = ds->cur_y = 0;
1053 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1059 static void grid_square(drawing *dr, game_drawstate *ds,
1060 int y, int x, int state, int cur)
1062 int xl, xr, yt, yb, dx, dy, dw, dh;
1064 draw_rect(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
1065 TILE_SIZE, TILE_SIZE, COL_GRID);
1067 xl = (x % 5 == 0 ? 1 : 0);
1068 yt = (y % 5 == 0 ? 1 : 0);
1069 xr = (x % 5 == 4 || x == ds->w-1 ? 1 : 0);
1070 yb = (y % 5 == 4 || y == ds->h-1 ? 1 : 0);
1072 dx = TOCOORD(ds->w, x) + 1 + xl;
1073 dy = TOCOORD(ds->h, y) + 1 + yt;
1074 dw = TILE_SIZE - xl - xr - 1;
1075 dh = TILE_SIZE - yt - yb - 1;
1077 draw_rect(dr, dx, dy, dw, dh,
1078 (state == GRID_FULL ? COL_FULL :
1079 state == GRID_EMPTY ? COL_EMPTY : COL_UNKNOWN));
1081 draw_rect_outline(dr, dx, dy, dw, dh, COL_CURSOR);
1082 draw_rect_outline(dr, dx+1, dy+1, dw-2, dh-2, COL_CURSOR);
1085 draw_update(dr, TOCOORD(ds->w, x), TOCOORD(ds->h, y),
1086 TILE_SIZE, TILE_SIZE);
1089 static void draw_numbers(drawing *dr, game_drawstate *ds, game_state *state,
1097 for (i = 0; i < state->w + state->h; i++) {
1098 int rowlen = state->rowlen[i];
1099 int *rowdata = state->rowdata + state->rowsize * i;
1103 * Normally I space the numbers out by the same
1104 * distance as the tile size. However, if there are
1105 * more numbers than available spaces, I have to squash
1108 nfit = max(rowlen, TLBORDER(state->h))-1;
1111 for (j = 0; j < rowlen; j++) {
1116 x = TOCOORD(state->w, i);
1117 y = BORDER + TILE_SIZE * (TLBORDER(state->h)-1);
1118 y -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(state->h)-1) / nfit;
1120 y = TOCOORD(state->h, i - state->w);
1121 x = BORDER + TILE_SIZE * (TLBORDER(state->w)-1);
1122 x -= ((rowlen-j-1)*TILE_SIZE) * (TLBORDER(state->h)-1) / nfit;
1125 sprintf(str, "%d", rowdata[j]);
1126 draw_text(dr, x+TILE_SIZE/2, y+TILE_SIZE/2, FONT_VARIABLE,
1127 TILE_SIZE/2, ALIGN_HCENTRE | ALIGN_VCENTRE, colour, str);
1132 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
1133 game_state *state, int dir, game_ui *ui,
1134 float animtime, float flashtime)
1142 * The initial contents of the window are not guaranteed
1143 * and can vary with front ends. To be on the safe side,
1144 * all games should start by drawing a big background-
1145 * colour rectangle covering the whole window.
1147 draw_rect(dr, 0, 0, SIZE(ds->w), SIZE(ds->h), COL_BACKGROUND);
1152 draw_numbers(dr, ds, state, COL_TEXT);
1155 * Draw the grid outline.
1157 draw_rect(dr, TOCOORD(ds->w, 0) - 1, TOCOORD(ds->h, 0) - 1,
1158 ds->w * TILE_SIZE + 3, ds->h * TILE_SIZE + 3,
1163 draw_update(dr, 0, 0, SIZE(ds->w), SIZE(ds->h));
1167 x1 = min(ui->drag_start_x, ui->drag_end_x);
1168 x2 = max(ui->drag_start_x, ui->drag_end_x);
1169 y1 = min(ui->drag_start_y, ui->drag_end_y);
1170 y2 = max(ui->drag_start_y, ui->drag_end_y);
1172 x1 = x2 = y1 = y2 = -1; /* placate gcc warnings */
1175 if (ui->cur_visible) {
1176 cx = ui->cur_x; cy = ui->cur_y;
1180 cmoved = (cx != ds->cur_x || cy != ds->cur_y);
1183 * Now draw any grid squares which have changed since last
1186 for (i = 0; i < ds->h; i++) {
1187 for (j = 0; j < ds->w; j++) {
1191 * Work out what state this square should be drawn in,
1192 * taking any current drag operation into account.
1194 if (ui->dragging && x1 <= j && j <= x2 && y1 <= i && i <= y2)
1197 val = state->grid[i * state->w + j];
1200 /* the cursor has moved; if we were the old or
1201 * the new cursor position we need to redraw. */
1202 if (j == cx && i == cy) cc = 1;
1203 if (j == ds->cur_x && i == ds->cur_y) cc = 1;
1207 * Briefly invert everything twice during a completion
1210 if (flashtime > 0 &&
1211 (flashtime <= FLASH_TIME/3 || flashtime >= FLASH_TIME*2/3) &&
1212 val != GRID_UNKNOWN)
1213 val = (GRID_FULL ^ GRID_EMPTY) ^ val;
1215 if (ds->visible[i * ds->w + j] != val || cc) {
1216 grid_square(dr, ds, i, j, val,
1217 (j == cx && i == cy));
1218 ds->visible[i * ds->w + j] = val;
1222 ds->cur_x = cx; ds->cur_y = cy;
1225 static float game_anim_length(game_state *oldstate,
1226 game_state *newstate, int dir, game_ui *ui)
1231 static float game_flash_length(game_state *oldstate,
1232 game_state *newstate, int dir, game_ui *ui)
1234 if (!oldstate->completed && newstate->completed &&
1235 !oldstate->cheated && !newstate->cheated)
1240 static int game_timing_state(game_state *state, game_ui *ui)
1245 static void game_print_size(game_params *params, float *x, float *y)
1250 * I'll use 5mm squares by default.
1252 game_compute_size(params, 500, &pw, &ph);
1257 static void game_print(drawing *dr, game_state *state, int tilesize)
1259 int w = state->w, h = state->h;
1260 int ink = print_mono_colour(dr, 0);
1263 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1264 game_drawstate ads, *ds = &ads;
1265 game_set_size(dr, ds, NULL, tilesize);
1270 print_line_width(dr, TILE_SIZE / 16);
1271 draw_rect_outline(dr, TOCOORD(w, 0), TOCOORD(h, 0),
1272 w*TILE_SIZE, h*TILE_SIZE, ink);
1277 for (x = 1; x < w; x++) {
1278 print_line_width(dr, TILE_SIZE / (x % 5 ? 128 : 24));
1279 draw_line(dr, TOCOORD(w, x), TOCOORD(h, 0),
1280 TOCOORD(w, x), TOCOORD(h, h), ink);
1282 for (y = 1; y < h; y++) {
1283 print_line_width(dr, TILE_SIZE / (y % 5 ? 128 : 24));
1284 draw_line(dr, TOCOORD(w, 0), TOCOORD(h, y),
1285 TOCOORD(w, w), TOCOORD(h, y), ink);
1291 draw_numbers(dr, ds, state, ink);
1296 print_line_width(dr, TILE_SIZE / 128);
1297 for (y = 0; y < h; y++)
1298 for (x = 0; x < w; x++) {
1299 if (state->grid[y*w+x] == GRID_FULL)
1300 draw_rect(dr, TOCOORD(w, x), TOCOORD(h, y),
1301 TILE_SIZE, TILE_SIZE, ink);
1302 else if (state->grid[y*w+x] == GRID_EMPTY)
1303 draw_circle(dr, TOCOORD(w, x) + TILE_SIZE/2,
1304 TOCOORD(h, y) + TILE_SIZE/2,
1305 TILE_SIZE/12, ink, ink);
1310 #define thegame pattern
1313 const struct game thegame = {
1314 "Pattern", "games.pattern", "pattern",
1321 TRUE, game_configure, custom_params,
1329 FALSE, game_can_format_as_text_now, game_text_format,
1337 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
1340 game_free_drawstate,
1344 TRUE, FALSE, game_print_size, game_print,
1345 FALSE, /* wants_statusbar */
1346 FALSE, game_timing_state,
1347 REQUIRE_RBUTTON, /* flags */
1350 #ifdef STANDALONE_SOLVER
1352 int main(int argc, char **argv)
1356 char *id = NULL, *desc, *err;
1358 while (--argc > 0) {
1361 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
1369 fprintf(stderr, "usage: %s <game_id>\n", argv[0]);
1373 desc = strchr(id, ':');
1375 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
1380 p = default_params();
1381 decode_params(p, id);
1382 err = validate_desc(p, desc);
1384 fprintf(stderr, "%s: %s\n", argv[0], err);
1387 s = new_game(NULL, p, desc);
1390 int w = p->w, h = p->h, i, j, done_any, max;
1391 unsigned char *matrix, *workspace;
1394 matrix = snewn(w*h, unsigned char);
1396 workspace = snewn(max*3, unsigned char);
1397 rowdata = snewn(max+1, int);
1399 memset(matrix, 0, w*h);
1403 for (i=0; i<h; i++) {
1404 memcpy(rowdata, s->rowdata + s->rowsize*(w+i),
1406 rowdata[s->rowlen[w+i]] = 0;
1407 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1408 matrix+i*w, w, 1, rowdata);
1410 for (i=0; i<w; i++) {
1411 memcpy(rowdata, s->rowdata + s->rowsize*i, max*sizeof(int));
1412 rowdata[s->rowlen[i]] = 0;
1413 done_any |= do_row(workspace, workspace+max, workspace+2*max,
1414 matrix+i, h, w, rowdata);
1418 for (i = 0; i < h; i++) {
1419 for (j = 0; j < w; j++) {
1420 int c = (matrix[i*w+j] == UNKNOWN ? '?' :
1421 matrix[i*w+j] == BLOCK ? '#' :
1422 matrix[i*w+j] == DOT ? '.' :
1435 /* vim: set shiftwidth=4 tabstop=8: */