From 1b16ddeda405e022ea6502aded3aa606b065f791 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 2 Apr 2010 10:20:58 +0000 Subject: [PATCH] Add '-v' option to patternsolver, to make it show its working. git-svn-id: svn://svn.tartarus.org/sgt/puzzles@8914 cda61777-01e9-0310-a592-d414129be87e --- pattern.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/pattern.c b/pattern.c index 7c1b0f2..544c10b 100644 --- a/pattern.c +++ b/pattern.c @@ -339,6 +339,10 @@ static int compute_rowdata(int *ret, unsigned char *start, int len, int step) #define DOT 2 #define STILL_UNKNOWN 3 +#ifdef STANDALONE_SOLVER +int verbose = FALSE; +#endif + static void do_recurse(unsigned char *known, unsigned char *deduced, unsigned char *row, int *data, int len, int freespace, int ndone, int lowest) @@ -367,7 +371,11 @@ static void do_recurse(unsigned char *known, unsigned char *deduced, static int do_row(unsigned char *known, unsigned char *deduced, unsigned char *row, - unsigned char *start, int len, int step, int *data) + unsigned char *start, int len, int step, int *data +#ifdef STANDALONE_SOLVER + , const char *rowcol, int index, int cluewid +#endif + ) { int rowlen, i, freespace, done_any; @@ -387,6 +395,27 @@ static int do_row(unsigned char *known, unsigned char *deduced, start[i*step] = deduced[i]; done_any = TRUE; } +#ifdef STANDALONE_SOLVER + if (verbose && done_any) { + char buf[80]; + int thiscluewid; + printf("%s %2d: [", rowcol, index); + for (thiscluewid = -1, i = 0; data[i]; i++) + thiscluewid += sprintf(buf, " %d", data[i]); + printf("%*s", cluewid - thiscluewid, ""); + for (i = 0; data[i]; i++) + printf(" %d", data[i]); + printf(" ] "); + for (i = 0; i < len; i++) + putchar(known[i] == BLOCK ? '#' : + known[i] == DOT ? '.' : '?'); + printf(" -> "); + for (i = 0; i < len; i++) + putchar(start[i*step] == BLOCK ? '#' : + start[i*step] == DOT ? '.' : '?'); + putchar('\n'); + } +#endif return done_any; } @@ -444,12 +473,20 @@ static unsigned char *generate_soluble(random_state *rs, int w, int h) for (i=0; irowlen[w+i]] = 0; done_any |= do_row(workspace, workspace+max, workspace+2*max, - matrix+i*w, w, 1, rowdata); + matrix+i*w, w, 1, rowdata +#ifdef STANDALONE_SOLVER + , NULL, 0, 0 /* never do diagnostics here */ +#endif + ); } for (i=0; irowdata + state->rowsize*i, max*sizeof(int)); rowdata[state->rowlen[i]] = 0; done_any |= do_row(workspace, workspace+max, workspace+2*max, - matrix+i, h, w, rowdata); + matrix+i, h, w, rowdata +#ifdef STANDALONE_SOLVER + , NULL, 0, 0 /* never do diagnostics here */ +#endif + ); } } while (done_any); @@ -1358,8 +1403,12 @@ int main(int argc, char **argv) while (--argc > 0) { char *p = *++argv; if (*p == '-') { - fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); - return 1; + if (!strcmp(p, "-v")) { + verbose = TRUE; + } else { + fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); + return 1; + } } else { id = p; } @@ -1387,7 +1436,7 @@ int main(int argc, char **argv) s = new_game(NULL, p, desc); { - int w = p->w, h = p->h, i, j, done_any, max; + int w = p->w, h = p->h, i, j, done_any, max, cluewid = 0; unsigned char *matrix, *workspace; int *rowdata; @@ -1398,6 +1447,22 @@ int main(int argc, char **argv) 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; irowlen[w+i]] = 0; done_any |= do_row(workspace, workspace+max, workspace+2*max, - matrix+i*w, w, 1, rowdata); + matrix+i*w, w, 1, rowdata +#ifdef STANDALONE_SOLVER + , "row", i+1, cluewid +#endif + ); } for (i=0; irowdata + 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); + matrix+i, h, w, rowdata +#ifdef STANDALONE_SOLVER + , "col", i+1, cluewid +#endif + ); } } while (done_any); -- 2.11.0