+
+static void game_print(drawing *dr, game_state *state, int tilesize)
+{
+ int w = state->w, h = state->h;
+ int ink = print_mono_colour(dr, 0);
+ int x, y, i;
+
+ /* Ick: fake up `ds->tilesize' for macro expansion purposes */
+ game_drawstate ads, *ds = &ads;
+ game_set_size(dr, ds, NULL, tilesize);
+
+ /*
+ * Border.
+ */
+ print_line_width(dr, TILE_SIZE / 16);
+ draw_rect_outline(dr, TOCOORD(w, 0), TOCOORD(h, 0),
+ w*TILE_SIZE, h*TILE_SIZE, ink);
+
+ /*
+ * Grid.
+ */
+ for (x = 1; x < w; x++) {
+ print_line_width(dr, TILE_SIZE / (x % 5 ? 128 : 24));
+ draw_line(dr, TOCOORD(w, x), TOCOORD(h, 0),
+ TOCOORD(w, x), TOCOORD(h, h), ink);
+ }
+ for (y = 1; y < h; y++) {
+ print_line_width(dr, TILE_SIZE / (y % 5 ? 128 : 24));
+ draw_line(dr, TOCOORD(w, 0), TOCOORD(h, y),
+ TOCOORD(w, w), TOCOORD(h, y), ink);
+ }
+
+ /*
+ * Clues.
+ */
+ for (i = 0; i < state->w + state->h; i++)
+ draw_numbers(dr, ds, state, i, FALSE, ink);
+
+ /*
+ * Solution.
+ */
+ print_line_width(dr, TILE_SIZE / 128);
+ for (y = 0; y < h; y++)
+ for (x = 0; x < w; x++) {
+ if (state->grid[y*w+x] == GRID_FULL)
+ draw_rect(dr, TOCOORD(w, x), TOCOORD(h, y),
+ TILE_SIZE, TILE_SIZE, ink);
+ else if (state->grid[y*w+x] == GRID_EMPTY)
+ draw_circle(dr, TOCOORD(w, x) + TILE_SIZE/2,
+ TOCOORD(h, y) + TILE_SIZE/2,
+ TILE_SIZE/12, ink, ink);
+ }
+}
+
+#ifdef COMBINED
+#define thegame pattern
+#endif
+
+const struct game thegame = {
+ "Pattern", "games.pattern", "pattern",
+ default_params,
+ game_fetch_preset,
+ decode_params,
+ encode_params,
+ free_params,
+ dup_params,
+ TRUE, game_configure, custom_params,
+ validate_params,
+ new_game_desc,
+ validate_desc,
+ new_game,
+ dup_game,
+ free_game,
+ TRUE, solve_game,
+ FALSE, game_can_format_as_text_now, game_text_format,
+ new_ui,
+ free_ui,
+ encode_ui,
+ decode_ui,
+ game_changed_state,
+ interpret_move,
+ execute_move,
+ PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
+ game_colours,
+ game_new_drawstate,
+ game_free_drawstate,
+ game_redraw,
+ game_anim_length,
+ game_flash_length,
+ game_status,
+ TRUE, FALSE, game_print_size, game_print,
+ FALSE, /* wants_statusbar */
+ FALSE, game_timing_state,
+ REQUIRE_RBUTTON, /* flags */
+};
+
+#ifdef STANDALONE_SOLVER
+
+int main(int argc, char **argv)
+{
+ game_params *p;
+ game_state *s;
+ char *id = NULL, *desc, *err;
+
+ while (--argc > 0) {
+ char *p = *++argv;
+ if (*p == '-') {
+ if (!strcmp(p, "-v")) {
+ verbose = TRUE;
+ } else {
+ fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
+ return 1;
+ }
+ } else {
+ id = p;
+ }
+ }
+
+ if (!id) {
+ fprintf(stderr, "usage: %s <game_id>\n", argv[0]);
+ return 1;
+ }
+
+ desc = strchr(id, ':');
+ if (!desc) {
+ fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
+ return 1;
+ }
+ *desc++ = '\0';
+
+ p = default_params();
+ decode_params(p, id);
+ err = validate_desc(p, desc);
+ if (err) {
+ fprintf(stderr, "%s: %s\n", argv[0], err);
+ return 1;
+ }
+ s = new_game(NULL, p, desc);
+
+ {
+ int w = p->w, h = p->h, i, j, done_any, max, cluewid = 0;
+ unsigned char *matrix, *workspace;
+ int *rowdata;
+
+ matrix = snewn(w*h, unsigned char);
+ max = max(w, h);
+ workspace = snewn(max*3, unsigned char);
+ rowdata = snewn(max+1, int);
+
+ memset(matrix, 0, w*h);
+
+ if (verbose) {
+ int thiswid;
+ /*
+ * Work out the maximum text width of the clue numbers
+ * in a row or column, so we can print the solver's
+ * working in a nicely lined up way.
+ */
+ for (i = 0; i < (w+h); i++) {
+ char buf[80];
+ for (thiswid = -1, j = 0; j < s->rowlen[i]; j++)
+ thiswid += sprintf(buf, " %d", s->rowdata[s->rowsize*i+j]);
+ if (cluewid < thiswid)
+ cluewid = thiswid;
+ }
+ }
+
+ do {
+ done_any = 0;
+ for (i=0; i<h; i++) {
+ memcpy(rowdata, s->rowdata + s->rowsize*(w+i),
+ max*sizeof(int));
+ rowdata[s->rowlen[w+i]] = 0;
+ done_any |= do_row(workspace, workspace+max, workspace+2*max,
+ matrix+i*w, w, 1, rowdata
+#ifdef STANDALONE_SOLVER
+ , "row", i+1, cluewid
+#endif
+ );
+ }
+ for (i=0; i<w; i++) {
+ memcpy(rowdata, s->rowdata + s->rowsize*i, max*sizeof(int));
+ rowdata[s->rowlen[i]] = 0;
+ done_any |= do_row(workspace, workspace+max, workspace+2*max,
+ matrix+i, h, w, rowdata
+#ifdef STANDALONE_SOLVER
+ , "col", i+1, cluewid
+#endif
+ );
+ }
+ } while (done_any);
+
+ for (i = 0; i < h; i++) {
+ for (j = 0; j < w; j++) {
+ int c = (matrix[i*w+j] == UNKNOWN ? '?' :
+ matrix[i*w+j] == BLOCK ? '#' :
+ matrix[i*w+j] == DOT ? '.' :
+ '!');
+ putchar(c);
+ }
+ printf("\n");
+ }
+ }
+
+ return 0;
+}
+
+#endif
+
+/* vim: set shiftwidth=4 tabstop=8: */