+
+#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: */