2 * towers.c: the puzzle also known as 'Skyscrapers'.
4 * Possible future work:
6 * - Relax the upper bound on grid size at 9?
7 * + I'd need TOCHAR and FROMCHAR macros a bit like group's, to
8 * be used wherever this code has +'0' or -'0'
9 * + the pencil marks in the drawstate would need a separate
11 * + the clues outside the grid would have to cope with being
12 * multi-digit, meaning in particular that the text formatting
13 * would become more unpleasant
14 * + most importantly, though, the solver just isn't fast
15 * enough. Even at size 9 it can't really do the solver_hard
16 * factorial-time enumeration at a sensible rate. Easy puzzles
17 * higher than that would be possible, but more latin-squarey
18 * than skyscrapery, as it were.
32 * Difficulty levels. I do some macro ickery here to ensure that my
33 * enum and the various forms of my name list always match up.
36 A(EASY,Easy,solver_easy,e) \
37 A(HARD,Hard,solver_hard,h) \
38 A(EXTREME,Extreme,NULL,x) \
39 A(UNREASONABLE,Unreasonable,NULL,u)
40 #define ENUM(upper,title,func,lower) DIFF_ ## upper,
41 #define TITLE(upper,title,func,lower) #title,
42 #define ENCODE(upper,title,func,lower) #lower
43 #define CONFIG(upper,title,func,lower) ":" #title
44 enum { DIFFLIST(ENUM) DIFFCOUNT };
45 static char const *const towers_diffnames[] = { DIFFLIST(TITLE) };
46 static char const towers_diffchars[] = DIFFLIST(ENCODE);
47 #define DIFFCONFIG DIFFLIST(CONFIG)
68 * An array of 4w integers, of which:
69 * - the first w run across the top
70 * - the next w across the bottom
71 * - the third w down the left
72 * - the last w down the right.
77 * An array of w*w digits.
83 * Macros to compute clue indices and coordinates.
85 #define STARTSTEP(start, step, index, w) do { \
87 start = index, step = w; \
88 else if (index < 2*w) \
89 start = (w-1)*w+(index-w), step = -w; \
90 else if (index < 3*w) \
91 start = w*(index-2*w), step = 1; \
93 start = w*(index-3*w)+(w-1), step = -1; \
95 #define CSTARTSTEP(start, step, index, w) \
96 STARTSTEP(start, step, (((index)+2*w)%(4*w)), w)
97 #define CLUEPOS(x, y, index, w) do { \
100 else if (index < 2*w) \
101 x = index-w, y = w; \
102 else if (index < 3*w) \
103 x = -1, y = index-2*w; \
105 x = w, y = index-3*w; \
108 #ifdef STANDALONE_SOLVER
109 static const char *const cluepos[] = {
110 "above column", "below column", "left of row", "right of row"
117 unsigned char *clues_done;
119 int *pencil; /* bitmaps using bits 1<<1..1<<n */
120 int completed, cheated;
123 static game_params *default_params(void)
125 game_params *ret = snew(game_params);
128 ret->diff = DIFF_EASY;
133 const static struct game_params towers_presets[] = {
140 { 6, DIFF_UNREASONABLE },
143 static int game_fetch_preset(int i, char **name, game_params **params)
148 if (i < 0 || i >= lenof(towers_presets))
151 ret = snew(game_params);
152 *ret = towers_presets[i]; /* structure copy */
154 sprintf(buf, "%dx%d %s", ret->w, ret->w, towers_diffnames[ret->diff]);
161 static void free_params(game_params *params)
166 static game_params *dup_params(const game_params *params)
168 game_params *ret = snew(game_params);
169 *ret = *params; /* structure copy */
173 static void decode_params(game_params *params, char const *string)
175 char const *p = string;
178 while (*p && isdigit((unsigned char)*p)) p++;
183 params->diff = DIFFCOUNT+1; /* ...which is invalid */
185 for (i = 0; i < DIFFCOUNT; i++) {
186 if (*p == towers_diffchars[i])
194 static char *encode_params(const game_params *params, int full)
198 sprintf(ret, "%d", params->w);
200 sprintf(ret + strlen(ret), "d%c", towers_diffchars[params->diff]);
205 static config_item *game_configure(const game_params *params)
210 ret = snewn(3, config_item);
212 ret[0].name = "Grid size";
213 ret[0].type = C_STRING;
214 sprintf(buf, "%d", params->w);
215 ret[0].sval = dupstr(buf);
218 ret[1].name = "Difficulty";
219 ret[1].type = C_CHOICES;
220 ret[1].sval = DIFFCONFIG;
221 ret[1].ival = params->diff;
231 static game_params *custom_params(const config_item *cfg)
233 game_params *ret = snew(game_params);
235 ret->w = atoi(cfg[0].sval);
236 ret->diff = cfg[1].ival;
241 static char *validate_params(const game_params *params, int full)
243 if (params->w < 3 || params->w > 9)
244 return "Grid size must be between 3 and 9";
245 if (params->diff >= DIFFCOUNT)
246 return "Unknown difficulty rating";
250 /* ----------------------------------------------------------------------
262 static int solver_easy(struct latin_solver *solver, void *vctx)
264 struct solver_ctx *ctx = (struct solver_ctx *)vctx;
266 int c, i, j, n, m, furthest;
267 int start, step, cstart, cstep, clue, pos, cpos;
269 #ifdef STANDALONE_SOLVER
276 * One-off loop to help get started: when a pair of facing
277 * clues sum to w+1, it must mean that the row consists of
278 * two increasing sequences back to back, so we can
279 * immediately place the highest digit by knowing the
280 * lengths of those two sequences.
282 for (c = 0; c < 3*w; c = (c == w-1 ? 2*w : c+1)) {
285 if (ctx->clues[c] && ctx->clues[c2] &&
286 ctx->clues[c] + ctx->clues[c2] == w+1) {
287 STARTSTEP(start, step, c, w);
288 CSTARTSTEP(cstart, cstep, c, w);
289 pos = start + (ctx->clues[c]-1)*step;
290 cpos = cstart + (ctx->clues[c]-1)*cstep;
291 if (solver->cube[cpos*w+w-1]) {
292 #ifdef STANDALONE_SOLVER
293 if (solver_show_working) {
294 printf("%*sfacing clues on %s %d are maximal:\n",
295 solver_recurse_depth*4, "",
296 c>=2*w ? "row" : "column", c % w + 1);
297 printf("%*s placing %d at (%d,%d)\n",
298 solver_recurse_depth*4, "",
299 w, pos%w+1, pos/w+1);
302 latin_solver_place(solver, pos%w, pos/w, w);
315 * Go over every clue doing reasonably simple heuristic
318 for (c = 0; c < 4*w; c++) {
319 clue = ctx->clues[c];
322 STARTSTEP(start, step, c, w);
323 CSTARTSTEP(cstart, cstep, c, w);
325 /* Find the location of each number in the row. */
326 for (i = 0; i < w; i++)
327 ctx->dscratch[i] = w;
328 for (i = 0; i < w; i++)
329 if (solver->grid[start+i*step])
330 ctx->dscratch[solver->grid[start+i*step]-1] = i;
334 for (i = w; i >= 1; i--) {
335 if (ctx->dscratch[i-1] == w) {
337 } else if (ctx->dscratch[i-1] < furthest) {
338 furthest = ctx->dscratch[i-1];
343 if (clue == n+1 && furthest > 1) {
344 #ifdef STANDALONE_SOLVER
345 if (solver_show_working)
346 sprintf(prefix, "%*sclue %s %d is nearly filled:\n",
347 solver_recurse_depth*4, "",
348 cluepos[c/w], c%w+1);
350 prefix[0] = '\0'; /* placate optimiser */
353 * We can already see an increasing sequence of the very
354 * highest numbers, of length one less than that
355 * specified in the clue. All of those numbers _must_ be
356 * part of the clue sequence, so the number right next
357 * to the clue must be the final one - i.e. it must be
358 * bigger than any of the numbers between it and m. This
359 * allows us to rule out small numbers in that square.
361 * (This is a generalisation of the obvious deduction
362 * that when you see a clue saying 1, it must be right
363 * next to the largest possible number; and similarly,
364 * when you see a clue saying 2 opposite that, it must
365 * be right next to the second-largest.)
367 j = furthest-1; /* number of small numbers we can rule out */
368 for (i = 1; i <= w && j > 0; i++) {
369 if (ctx->dscratch[i-1] < w && ctx->dscratch[i-1] >= furthest)
370 continue; /* skip this number, it's elsewhere */
372 if (solver->cube[cstart*w+i-1]) {
373 #ifdef STANDALONE_SOLVER
374 if (solver_show_working) {
375 printf("%s%*s ruling out %d at (%d,%d)\n",
376 prefix, solver_recurse_depth*4, "",
377 i, start%w+1, start/w+1);
381 solver->cube[cstart*w+i-1] = 0;
390 #ifdef STANDALONE_SOLVER
391 if (solver_show_working)
392 sprintf(prefix, "%*slower bounds for clue %s %d:\n",
393 solver_recurse_depth*4, "",
394 cluepos[c/w], c%w+1);
396 prefix[0] = '\0'; /* placate optimiser */
400 for (n = w; n > 0; n--) {
402 * The largest number cannot occur in the first (clue-1)
403 * squares of the row, or else there wouldn't be space
404 * for a sufficiently long increasing sequence which it
405 * terminated. The second-largest number (not counting
406 * any that are known to be on the far side of a larger
407 * number and hence excluded from this sequence) cannot
408 * occur in the first (clue-2) squares, similarly, and
412 if (ctx->dscratch[n-1] < w) {
413 for (m = n+1; m < w; m++)
414 if (ctx->dscratch[m] < ctx->dscratch[n-1])
417 continue; /* this number doesn't count */
420 for (j = 0; j < clue - i - 1; j++)
421 if (solver->cube[(cstart + j*cstep)*w+n-1]) {
422 #ifdef STANDALONE_SOLVER
423 if (solver_show_working) {
424 int pos = start+j*step;
425 printf("%s%*s ruling out %d at (%d,%d)\n",
426 prefix, solver_recurse_depth*4, "",
427 n, pos%w+1, pos/w+1);
431 solver->cube[(cstart + j*cstep)*w+n-1] = 0;
444 static int solver_hard(struct latin_solver *solver, void *vctx)
446 struct solver_ctx *ctx = (struct solver_ctx *)vctx;
448 int c, i, j, n, best, clue, start, step, ret;
450 #ifdef STANDALONE_SOLVER
455 * Go over every clue analysing all possibilities.
457 for (c = 0; c < 4*w; c++) {
458 clue = ctx->clues[c];
461 CSTARTSTEP(start, step, c, w);
463 for (i = 0; i < w; i++)
464 ctx->iscratch[i] = 0;
467 * Instead of a tedious physical recursion, I iterate in the
468 * scratch array through all possibilities. At any given
469 * moment, i indexes the element of the box that will next
473 ctx->dscratch[i] = 0;
480 * Find the next valid value for cell i.
482 int limit = (n == clue ? best : w);
483 int pos = start + step * i;
484 for (j = ctx->dscratch[i] + 1; j <= limit; j++) {
485 if (bitmap & (1L << j))
486 continue; /* used this one already */
487 if (!solver->cube[pos*w+j-1])
488 continue; /* ruled out already */
495 /* No valid values left; drop back. */
498 break; /* overall iteration is finished */
499 bitmap &= ~(1L << ctx->dscratch[i]);
500 if (ctx->dscratch[i] == best) {
503 for (j = 0; j < i; j++)
504 if (best < ctx->dscratch[j])
505 best = ctx->dscratch[j];
508 /* Got a valid value; store it and move on. */
510 ctx->dscratch[i++] = j;
515 ctx->dscratch[i] = 0;
519 for (j = 0; j < w; j++)
520 ctx->iscratch[j] |= 1L << ctx->dscratch[j];
523 bitmap &= ~(1L << ctx->dscratch[i]);
524 if (ctx->dscratch[i] == best) {
527 for (j = 0; j < i; j++)
528 if (best < ctx->dscratch[j])
529 best = ctx->dscratch[j];
534 #ifdef STANDALONE_SOLVER
535 if (solver_show_working)
536 sprintf(prefix, "%*sexhaustive analysis of clue %s %d:\n",
537 solver_recurse_depth*4, "",
538 cluepos[c/w], c%w+1);
540 prefix[0] = '\0'; /* placate optimiser */
545 for (i = 0; i < w; i++) {
546 int pos = start + step * i;
547 for (j = 1; j <= w; j++) {
548 if (solver->cube[pos*w+j-1] &&
549 !(ctx->iscratch[i] & (1L << j))) {
550 #ifdef STANDALONE_SOLVER
551 if (solver_show_working) {
552 printf("%s%*s ruling out %d at (%d,%d)\n",
553 prefix, solver_recurse_depth*4, "",
554 j, pos/w+1, pos%w+1);
558 solver->cube[pos*w+j-1] = 0;
564 * Once we find one clue we can do something with in
565 * this way, revert to trying easier deductions, so as
566 * not to generate solver diagnostics that make the
567 * problem look harder than it is.
577 #define SOLVER(upper,title,func,lower) func,
578 static usersolver_t const towers_solvers[] = { DIFFLIST(SOLVER) };
580 static int solver(int w, int *clues, digit *soln, int maxdiff)
583 struct solver_ctx ctx;
589 ctx.iscratch = snewn(w, long);
590 ctx.dscratch = snewn(w+1, int);
592 ret = latin_solver(soln, w, maxdiff,
593 DIFF_EASY, DIFF_HARD, DIFF_EXTREME,
594 DIFF_EXTREME, DIFF_UNREASONABLE,
595 towers_solvers, &ctx, NULL, NULL);
603 /* ----------------------------------------------------------------------
607 static char *new_game_desc(const game_params *params, random_state *rs,
608 char **aux, int interactive)
610 int w = params->w, a = w*w;
611 digit *grid, *soln, *soln2;
614 int diff = params->diff;
618 * Difficulty exceptions: some combinations of size and
619 * difficulty cannot be satisfied, because all puzzles of at
620 * most that difficulty are actually even easier.
622 * Remember to re-test this whenever a change is made to the
625 * I tested it using the following shell command:
629 echo -n "./towers --generate 1 ${i}d${d}: "
630 perl -e 'alarm 30; exec @ARGV' ./towers --generate 1 ${i}d${d} >/dev/null \
635 * Of course, it's better to do that after taking the exceptions
636 * _out_, so as to detect exceptions that should be removed as
637 * well as those which should be added.
639 if (diff > DIFF_HARD && w <= 3)
643 clues = snewn(4*w, int);
644 soln = snewn(a, digit);
645 soln2 = snewn(a, digit);
646 order = snewn(max(4*w,a), int);
650 * Construct a latin square to be the solution.
653 grid = latin_generate(w, rs);
658 for (i = 0; i < 4*w; i++) {
659 int start, step, j, k, best;
660 STARTSTEP(start, step, i, w);
662 for (j = 0; j < w; j++) {
663 if (grid[start+j*step] > best) {
664 best = grid[start+j*step];
672 * Remove the grid numbers and then the clues, one by one,
673 * for as long as the game remains soluble at the given
676 memcpy(soln, grid, a);
678 if (diff == DIFF_EASY && w <= 5) {
680 * Special case: for Easy-mode grids that are small
681 * enough, it's nice to be able to find completely empty
685 ret = solver(w, clues, soln2, diff);
690 for (i = 0; i < a; i++)
692 shuffle(order, a, sizeof(*order), rs);
693 for (i = 0; i < a; i++) {
696 memcpy(soln2, grid, a);
698 ret = solver(w, clues, soln2, diff);
703 if (diff > DIFF_EASY) { /* leave all clues on Easy mode */
704 for (i = 0; i < 4*w; i++)
706 shuffle(order, 4*w, sizeof(*order), rs);
707 for (i = 0; i < 4*w; i++) {
711 memcpy(soln2, grid, a);
713 ret = solver(w, clues, soln2, diff);
720 * See if the game can be solved at the specified difficulty
721 * level, but not at the one below.
723 memcpy(soln2, grid, a);
724 ret = solver(w, clues, soln2, diff);
726 continue; /* go round again */
729 * We've got a usable puzzle!
735 * Encode the puzzle description.
737 desc = snewn(40*a, char);
739 for (i = 0; i < 4*w; i++) {
743 p += sprintf(p, "%d", clues[i]);
745 for (i = 0; i < a; i++)
753 for (i = 0; i <= a; i++) {
754 int n = (i < a ? grid[i] : -1);
761 int thisrun = min(run, 26);
762 *p++ = thisrun - 1 + 'a';
767 * If there's a number in the very top left or
768 * bottom right, there's no point putting an
769 * unnecessary _ before or after it.
775 p += sprintf(p, "%d", n);
781 desc = sresize(desc, p - desc, char);
784 * Encode the solution.
786 *aux = snewn(a+2, char);
788 for (i = 0; i < a; i++)
789 (*aux)[i+1] = '0' + soln[i];
801 /* ----------------------------------------------------------------------
805 static char *validate_desc(const game_params *params, const char *desc)
807 int w = params->w, a = w*w;
808 const char *p = desc;
812 * Verify that the right number of clues are given, and that
815 for (i = 0; i < 4*w; i++) {
817 return "Too few clues for grid size";
821 return "Expected commas between clues";
825 if (isdigit((unsigned char)*p)) {
827 while (*p && isdigit((unsigned char)*p)) p++;
829 if (clue <= 0 || clue > w)
830 return "Clue number out of range";
834 return "Too many clues for grid size";
838 * Verify that the right amount of grid data is given, and
839 * that any grid elements provided are in range.
846 if (c >= 'a' && c <= 'z') {
847 squares += c - 'a' + 1;
848 } else if (c == '_') {
850 } else if (c > '0' && c <= '9') {
852 if (val < 1 || val > w)
853 return "Out-of-range number in grid description";
855 while (*p && isdigit((unsigned char)*p)) p++;
857 return "Invalid character in game description";
861 return "Not enough data to fill grid";
864 return "Too much data to fit in grid";
870 static game_state *new_game(midend *me, const game_params *params,
873 int w = params->w, a = w*w;
874 game_state *state = snew(game_state);
875 const char *p = desc;
878 state->par = *params; /* structure copy */
879 state->clues = snew(struct clues);
880 state->clues->refcount = 1;
882 state->clues->clues = snewn(4*w, int);
883 state->clues->immutable = snewn(a, digit);
884 state->grid = snewn(a, digit);
885 state->clues_done = snewn(4*w, unsigned char);
886 state->pencil = snewn(a, int);
888 for (i = 0; i < a; i++) {
890 state->pencil[i] = 0;
893 memset(state->clues->immutable, 0, a);
894 memset(state->clues_done, 0, 4*w*sizeof(unsigned char));
896 for (i = 0; i < 4*w; i++) {
901 if (*p && isdigit((unsigned char)*p)) {
902 state->clues->clues[i] = atoi(p);
903 while (*p && isdigit((unsigned char)*p)) p++;
905 state->clues->clues[i] = 0;
913 if (c >= 'a' && c <= 'z') {
915 } else if (c == '_') {
917 } else if (c > '0' && c <= '9') {
919 assert(val >= 1 && val <= w);
921 state->grid[pos] = state->clues->immutable[pos] = val;
923 while (*p && isdigit((unsigned char)*p)) p++;
925 assert(!"Corrupt game description");
931 state->completed = state->cheated = FALSE;
936 static game_state *dup_game(const game_state *state)
938 int w = state->par.w, a = w*w;
939 game_state *ret = snew(game_state);
941 ret->par = state->par; /* structure copy */
943 ret->clues = state->clues;
944 ret->clues->refcount++;
946 ret->grid = snewn(a, digit);
947 ret->pencil = snewn(a, int);
948 ret->clues_done = snewn(4*w, unsigned char);
949 memcpy(ret->grid, state->grid, a*sizeof(digit));
950 memcpy(ret->pencil, state->pencil, a*sizeof(int));
951 memcpy(ret->clues_done, state->clues_done, 4*w*sizeof(unsigned char));
953 ret->completed = state->completed;
954 ret->cheated = state->cheated;
959 static void free_game(game_state *state)
962 sfree(state->pencil);
963 sfree(state->clues_done);
964 if (--state->clues->refcount <= 0) {
965 sfree(state->clues->immutable);
966 sfree(state->clues->clues);
972 static char *solve_game(const game_state *state, const game_state *currstate,
973 const char *aux, char **error)
975 int w = state->par.w, a = w*w;
983 soln = snewn(a, digit);
984 memcpy(soln, state->clues->immutable, a);
986 ret = solver(w, state->clues->clues, soln, DIFFCOUNT-1);
988 if (ret == diff_impossible) {
989 *error = "No solution exists for this puzzle";
991 } else if (ret == diff_ambiguous) {
992 *error = "Multiple solutions exist for this puzzle";
995 out = snewn(a+2, char);
997 for (i = 0; i < a; i++)
998 out[i+1] = '0' + soln[i];
1006 static int game_can_format_as_text_now(const game_params *params)
1011 static char *game_text_format(const game_state *state)
1013 int w = state->par.w /* , a = w*w */;
1021 * - a top clue row, consisting of three spaces, then w clue
1022 * digits with spaces between (total 2*w+3 chars including
1024 * - a blank line (one newline)
1025 * - w main rows, consisting of a left clue digit, two spaces,
1026 * w grid digits with spaces between, two spaces and a right
1027 * clue digit (total 2*w+6 chars each including newline)
1028 * - a blank line (one newline)
1029 * - a bottom clue row (same as top clue row)
1030 * - terminating NUL.
1032 * Total size is therefore 2*(2*w+3) + 2 + w*(2*w+6) + 1
1035 total = 2*w*w + 10*w + 9;
1036 ret = snewn(total, char);
1040 *p++ = ' '; *p++ = ' ';
1041 for (x = 0; x < w; x++) {
1043 *p++ = (state->clues->clues[x] ? '0' + state->clues->clues[x] : ' ');
1051 for (y = 0; y < w; y++) {
1052 *p++ = (state->clues->clues[y+2*w] ? '0' + state->clues->clues[y+2*w] :
1055 for (x = 0; x < w; x++) {
1057 *p++ = (state->grid[y*w+x] ? '0' + state->grid[y*w+x] : ' ');
1059 *p++ = ' '; *p++ = ' ';
1060 *p++ = (state->clues->clues[y+3*w] ? '0' + state->clues->clues[y+3*w] :
1068 /* Bottom clue row. */
1069 *p++ = ' '; *p++ = ' ';
1070 for (x = 0; x < w; x++) {
1072 *p++ = (state->clues->clues[x+w] ? '0' + state->clues->clues[x+w] :
1078 assert(p == ret + total);
1085 * These are the coordinates of the currently highlighted
1086 * square on the grid, if hshow = 1.
1090 * This indicates whether the current highlight is a
1091 * pencil-mark one or a real one.
1095 * This indicates whether or not we're showing the highlight
1096 * (used to be hx = hy = -1); important so that when we're
1097 * using the cursor keys it doesn't keep coming back at a
1098 * fixed position. When hshow = 1, pressing a valid number
1099 * or letter key or Space will enter that number or letter in the grid.
1103 * This indicates whether we're using the highlight as a cursor;
1104 * it means that it doesn't vanish on a keypress, and that it is
1105 * allowed on immutable squares.
1110 static game_ui *new_ui(const game_state *state)
1112 game_ui *ui = snew(game_ui);
1114 ui->hx = ui->hy = 0;
1115 ui->hpencil = ui->hshow = ui->hcursor = 0;
1120 static void free_ui(game_ui *ui)
1125 static char *encode_ui(const game_ui *ui)
1130 static void decode_ui(game_ui *ui, const char *encoding)
1134 static void game_changed_state(game_ui *ui, const game_state *oldstate,
1135 const game_state *newstate)
1137 int w = newstate->par.w;
1139 * We prevent pencil-mode highlighting of a filled square, unless
1140 * we're using the cursor keys. So if the user has just filled in
1141 * a square which we had a pencil-mode highlight in (by Undo, or
1142 * by Redo, or by Solve), then we cancel the highlight.
1144 if (ui->hshow && ui->hpencil && !ui->hcursor &&
1145 newstate->grid[ui->hy * w + ui->hx] != 0) {
1150 #define PREFERRED_TILESIZE 48
1151 #define TILESIZE (ds->tilesize)
1152 #define BORDER (TILESIZE * 9 / 8)
1153 #define COORD(x) ((x)*TILESIZE + BORDER)
1154 #define FROMCOORD(x) (((x)+(TILESIZE-BORDER)) / TILESIZE - 1)
1156 /* These always return positive values, though y offsets are actually -ve */
1157 #define X_3D_DISP(height, w) ((height) * TILESIZE / (8 * (w)))
1158 #define Y_3D_DISP(height, w) ((height) * TILESIZE / (4 * (w)))
1160 #define FLASH_TIME 0.4F
1162 #define DF_PENCIL_SHIFT 16
1163 #define DF_CLUE_DONE 0x10000
1164 #define DF_ERROR 0x8000
1165 #define DF_HIGHLIGHT 0x4000
1166 #define DF_HIGHLIGHT_PENCIL 0x2000
1167 #define DF_IMMUTABLE 0x1000
1168 #define DF_PLAYAREA 0x0800
1169 #define DF_DIGIT_MASK 0x00FF
1171 struct game_drawstate {
1173 int three_d; /* default 3D graphics are user-disableable */
1175 long *tiles; /* (w+2)*(w+2) temp space */
1176 long *drawn; /* (w+2)*(w+2)*4: current drawn data */
1180 static int check_errors(const game_state *state, int *errors)
1182 int w = state->par.w /*, a = w*w */;
1183 int W = w+2, A = W*W; /* the errors array is (w+2) square */
1184 int *clues = state->clues->clues;
1185 digit *grid = state->grid;
1186 int i, x, y, errs = FALSE;
1189 assert(w < lenof(tmp));
1192 for (i = 0; i < A; i++)
1195 for (y = 0; y < w; y++) {
1196 unsigned long mask = 0, errmask = 0;
1197 for (x = 0; x < w; x++) {
1198 unsigned long bit = 1UL << grid[y*w+x];
1199 errmask |= (mask & bit);
1203 if (mask != (1L << (w+1)) - (1L << 1)) {
1207 for (x = 0; x < w; x++)
1208 if (errmask & (1UL << grid[y*w+x]))
1209 errors[(y+1)*W+(x+1)] = TRUE;
1214 for (x = 0; x < w; x++) {
1215 unsigned long mask = 0, errmask = 0;
1216 for (y = 0; y < w; y++) {
1217 unsigned long bit = 1UL << grid[y*w+x];
1218 errmask |= (mask & bit);
1222 if (mask != (1 << (w+1)) - (1 << 1)) {
1226 for (y = 0; y < w; y++)
1227 if (errmask & (1UL << grid[y*w+x]))
1228 errors[(y+1)*W+(x+1)] = TRUE;
1233 for (i = 0; i < 4*w; i++) {
1234 int start, step, j, n, best;
1235 STARTSTEP(start, step, i, w);
1241 for (j = 0; j < w; j++) {
1242 int number = grid[start+j*step];
1244 break; /* can't tell what happens next */
1245 if (number > best) {
1251 if (n > clues[i] || (best == w && n < clues[i]) ||
1252 (best < w && n == clues[i])) {
1255 CLUEPOS(x, y, i, w);
1256 errors[(y+1)*W+(x+1)] = TRUE;
1265 static int clue_index(const game_state *state, int x, int y)
1267 int w = state->par.w;
1269 if (x == -1 || x == w)
1270 return w * (x == -1 ? 2 : 3) + y;
1271 else if (y == -1 || y == w)
1272 return (y == -1 ? 0 : w) + x;
1277 static int is_clue(const game_state *state, int x, int y)
1279 int w = state->par.w;
1281 if (((x == -1 || x == w) && y >= 0 && y < w) ||
1282 ((y == -1 || y == w) && x >= 0 && x < w))
1284 if (state->clues->clues[clue_index(state, x, y)] & DF_DIGIT_MASK)
1291 static char *interpret_move(const game_state *state, game_ui *ui,
1292 const game_drawstate *ds,
1293 int x, int y, int button)
1295 int w = state->par.w;
1296 int shift_or_control = button & (MOD_SHFT | MOD_CTRL);
1300 button &= ~MOD_MASK;
1307 * In 3D mode, just locating the mouse click in the natural
1308 * square grid may not be sufficient to tell which tower the
1309 * user clicked on. Investigate the _tops_ of the nearby
1310 * towers to see if a click on one grid square was actually
1311 * a click on a tower protruding into that region from
1315 for (dy = 0; dy <= 1; dy++)
1316 for (dx = 0; dx >= -1; dx--) {
1317 int cx = tx + dx, cy = ty + dy;
1318 if (cx >= 0 && cx < w && cy >= 0 && cy < w) {
1319 int height = state->grid[cy*w+cx];
1320 int bx = COORD(cx), by = COORD(cy);
1321 int ox = bx + X_3D_DISP(height, w);
1322 int oy = by - Y_3D_DISP(height, w);
1323 if (/* on top face? */
1324 (x - ox >= 0 && x - ox < TILESIZE &&
1325 y - oy >= 0 && y - oy < TILESIZE) ||
1326 /* in triangle between top-left corners? */
1327 (ox > bx && x >= bx && x <= ox && y <= by &&
1328 (by-y) * (ox-bx) <= (by-oy) * (x-bx)) ||
1329 /* in triangle between bottom-right corners? */
1330 (ox > bx && x >= bx+TILESIZE && x <= ox+TILESIZE &&
1332 (by-y+TILESIZE)*(ox-bx) >= (by-oy)*(x-bx-TILESIZE))) {
1340 if (tx >= 0 && tx < w && ty >= 0 && ty < w) {
1341 if (button == LEFT_BUTTON) {
1342 if (tx == ui->hx && ty == ui->hy &&
1343 ui->hshow && ui->hpencil == 0) {
1348 ui->hshow = !state->clues->immutable[ty*w+tx];
1352 return ""; /* UI activity occurred */
1354 if (button == RIGHT_BUTTON) {
1356 * Pencil-mode highlighting for non filled squares.
1358 if (state->grid[ty*w+tx] == 0) {
1359 if (tx == ui->hx && ty == ui->hy &&
1360 ui->hshow && ui->hpencil) {
1372 return ""; /* UI activity occurred */
1374 } else if (button == LEFT_BUTTON) {
1375 if (is_clue(state, tx, ty)) {
1376 sprintf(buf, "%c%d,%d", 'D', tx, ty);
1380 if (IS_CURSOR_MOVE(button)) {
1381 if (shift_or_control) {
1382 int x = ui->hx, y = ui->hy;
1384 case CURSOR_LEFT: x = -1; break;
1385 case CURSOR_RIGHT: x = w; break;
1386 case CURSOR_UP: y = -1; break;
1387 case CURSOR_DOWN: y = w; break;
1389 if (is_clue(state, x, y)) {
1390 sprintf(buf, "%c%d,%d", 'D', x, y);
1395 move_cursor(button, &ui->hx, &ui->hy, w, w, 0);
1396 ui->hshow = ui->hcursor = 1;
1400 (button == CURSOR_SELECT)) {
1401 ui->hpencil = 1 - ui->hpencil;
1407 ((button >= '0' && button <= '9' && button - '0' <= w) ||
1408 button == CURSOR_SELECT2 || button == '\b')) {
1409 int n = button - '0';
1410 if (button == CURSOR_SELECT2 || button == '\b')
1414 * Can't make pencil marks in a filled square. This can only
1415 * become highlighted if we're using cursor keys.
1417 if (ui->hpencil && state->grid[ui->hy*w+ui->hx])
1421 * Can't do anything to an immutable square.
1423 if (state->clues->immutable[ui->hy*w+ui->hx])
1426 sprintf(buf, "%c%d,%d,%d",
1427 (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n);
1429 if (!ui->hcursor) ui->hshow = 0;
1434 if (button == 'M' || button == 'm')
1440 static game_state *execute_move(const game_state *from, const char *move)
1442 int w = from->par.w, a = w*w;
1443 game_state *ret = dup_game(from);
1446 if (move[0] == 'S') {
1447 ret->completed = ret->cheated = TRUE;
1449 for (i = 0; i < a; i++) {
1450 if (move[i+1] < '1' || move[i+1] > '0'+w)
1452 ret->grid[i] = move[i+1] - '0';
1456 if (move[a+1] != '\0')
1460 } else if ((move[0] == 'P' || move[0] == 'R') &&
1461 sscanf(move+1, "%d,%d,%d", &x, &y, &n) == 3 &&
1462 x >= 0 && x < w && y >= 0 && y < w && n >= 0 && n <= w) {
1463 if (from->clues->immutable[y*w+x])
1466 if (move[0] == 'P' && n > 0) {
1467 ret->pencil[y*w+x] ^= 1L << n;
1469 ret->grid[y*w+x] = n;
1470 ret->pencil[y*w+x] = 0;
1472 if (!ret->completed && !check_errors(ret, NULL))
1473 ret->completed = TRUE;
1476 } else if (move[0] == 'M') {
1478 * Fill in absolutely all pencil marks everywhere. (I
1479 * wouldn't use this for actual play, but it's a handy
1480 * starting point when following through a set of
1481 * diagnostics output by the standalone solver.)
1483 for (i = 0; i < a; i++) {
1485 ret->pencil[i] = (1L << (w+1)) - (1L << 1);
1488 } else if (move[0] == 'D' && sscanf(move+1, "%d,%d", &x, &y) == 2 &&
1489 is_clue(from, x, y)) {
1490 int index = clue_index(from, x, y);
1491 ret->clues_done[index] = !ret->clues_done[index];
1496 /* couldn't parse move string */
1501 /* ----------------------------------------------------------------------
1505 #define SIZE(w) ((w) * TILESIZE + 2*BORDER)
1507 static void game_compute_size(const game_params *params, int tilesize,
1510 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1511 struct { int tilesize; } ads, *ds = &ads;
1512 ads.tilesize = tilesize;
1514 *x = *y = SIZE(params->w);
1517 static void game_set_size(drawing *dr, game_drawstate *ds,
1518 const game_params *params, int tilesize)
1520 ds->tilesize = tilesize;
1523 static float *game_colours(frontend *fe, int *ncolours)
1525 float *ret = snewn(3 * NCOLOURS, float);
1527 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1529 ret[COL_GRID * 3 + 0] = 0.0F;
1530 ret[COL_GRID * 3 + 1] = 0.0F;
1531 ret[COL_GRID * 3 + 2] = 0.0F;
1533 ret[COL_USER * 3 + 0] = 0.0F;
1534 ret[COL_USER * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1];
1535 ret[COL_USER * 3 + 2] = 0.0F;
1537 ret[COL_HIGHLIGHT * 3 + 0] = 0.78F * ret[COL_BACKGROUND * 3 + 0];
1538 ret[COL_HIGHLIGHT * 3 + 1] = 0.78F * ret[COL_BACKGROUND * 3 + 1];
1539 ret[COL_HIGHLIGHT * 3 + 2] = 0.78F * ret[COL_BACKGROUND * 3 + 2];
1541 ret[COL_ERROR * 3 + 0] = 1.0F;
1542 ret[COL_ERROR * 3 + 1] = 0.0F;
1543 ret[COL_ERROR * 3 + 2] = 0.0F;
1545 ret[COL_PENCIL * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1546 ret[COL_PENCIL * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1547 ret[COL_PENCIL * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
1549 ret[COL_DONE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] / 1.5F;
1550 ret[COL_DONE * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] / 1.5F;
1551 ret[COL_DONE * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] / 1.5F;
1553 *ncolours = NCOLOURS;
1557 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1559 int w = state->par.w /*, a = w*w */;
1560 struct game_drawstate *ds = snew(struct game_drawstate);
1564 ds->three_d = !getenv("TOWERS_2D");
1565 ds->started = FALSE;
1566 ds->tiles = snewn((w+2)*(w+2), long);
1567 ds->drawn = snewn((w+2)*(w+2)*4, long);
1568 for (i = 0; i < (w+2)*(w+2)*4; i++)
1570 ds->errtmp = snewn((w+2)*(w+2), int);
1575 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1583 static void draw_tile(drawing *dr, game_drawstate *ds, struct clues *clues,
1584 int x, int y, long tile)
1586 int w = clues->w /* , a = w*w */;
1593 bg = (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND;
1596 if (ds->three_d && (tile & DF_PLAYAREA) && (tile & DF_DIGIT_MASK)) {
1598 int xoff = X_3D_DISP(tile & DF_DIGIT_MASK, w);
1599 int yoff = Y_3D_DISP(tile & DF_DIGIT_MASK, w);
1601 /* left face of tower */
1605 coords[3] = ty + TILESIZE - 1;
1606 coords[4] = coords[2] + xoff;
1607 coords[5] = coords[3] - yoff;
1608 coords[6] = coords[0] + xoff;
1609 coords[7] = coords[1] - yoff;
1610 draw_polygon(dr, coords, 4, bg, COL_GRID);
1612 /* bottom face of tower */
1613 coords[0] = tx + TILESIZE;
1614 coords[1] = ty + TILESIZE - 1;
1616 coords[3] = ty + TILESIZE - 1;
1617 coords[4] = coords[2] + xoff;
1618 coords[5] = coords[3] - yoff;
1619 coords[6] = coords[0] + xoff;
1620 coords[7] = coords[1] - yoff;
1621 draw_polygon(dr, coords, 4, bg, COL_GRID);
1623 /* now offset all subsequent drawing to the top of the tower */
1628 /* erase background */
1629 draw_rect(dr, tx, ty, TILESIZE, TILESIZE, bg);
1631 /* pencil-mode highlight */
1632 if (tile & DF_HIGHLIGHT_PENCIL) {
1636 coords[2] = tx+TILESIZE/2;
1639 coords[5] = ty+TILESIZE/2;
1640 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
1643 /* draw box outline */
1644 if (tile & DF_PLAYAREA) {
1648 coords[2] = tx + TILESIZE;
1650 coords[4] = tx + TILESIZE;
1651 coords[5] = ty + TILESIZE - 1;
1653 coords[7] = ty + TILESIZE - 1;
1654 draw_polygon(dr, coords, 4, -1, COL_GRID);
1657 /* new number needs drawing? */
1658 if (tile & DF_DIGIT_MASK) {
1662 str[0] = (tile & DF_DIGIT_MASK) + '0';
1664 if (tile & DF_ERROR)
1666 else if (tile & DF_CLUE_DONE)
1668 else if (x < 0 || y < 0 || x >= w || y >= w)
1670 else if (tile & DF_IMMUTABLE)
1675 draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2, FONT_VARIABLE,
1676 (tile & DF_PLAYAREA ? TILESIZE/2 : TILESIZE*2/5),
1677 ALIGN_VCENTRE | ALIGN_HCENTRE, color, str);
1682 int pw, ph, minph, pbest, fontsize;
1684 /* Count the pencil marks required. */
1685 for (i = 1, npencil = 0; i <= w; i++)
1686 if (tile & (1L << (i + DF_PENCIL_SHIFT)))
1693 * Determine the bounding rectangle within which we're going
1694 * to put the pencil marks.
1696 /* Start with the whole square, minus space for impinging towers */
1697 pl = tx + (ds->three_d ? X_3D_DISP(w,w) : 0);
1700 pb = ty + TILESIZE - (ds->three_d ? Y_3D_DISP(w,w) : 0);
1703 * We arrange our pencil marks in a grid layout, with
1704 * the number of rows and columns adjusted to allow the
1705 * maximum font size.
1707 * So now we work out what the grid size ought to be.
1712 for (pw = 3; pw < max(npencil,4); pw++) {
1715 ph = (npencil + pw - 1) / pw;
1716 ph = max(ph, minph);
1717 fw = (pr - pl) / (float)pw;
1718 fh = (pb - pt) / (float)ph;
1720 if (fs > bestsize) {
1727 ph = (npencil + pw - 1) / pw;
1728 ph = max(ph, minph);
1731 * Now we've got our grid dimensions, work out the pixel
1732 * size of a grid element, and round it to the nearest
1733 * pixel. (We don't want rounding errors to make the
1734 * grid look uneven at low pixel sizes.)
1736 fontsize = min((pr - pl) / pw, (pb - pt) / ph);
1739 * Centre the resulting figure in the square.
1741 pl = pl + (pr - pl - fontsize * pw) / 2;
1742 pt = pt + (pb - pt - fontsize * ph) / 2;
1745 * Now actually draw the pencil marks.
1747 for (i = 1, j = 0; i <= w; i++)
1748 if (tile & (1L << (i + DF_PENCIL_SHIFT))) {
1749 int dx = j % pw, dy = j / pw;
1753 draw_text(dr, pl + fontsize * (2*dx+1) / 2,
1754 pt + fontsize * (2*dy+1) / 2,
1755 FONT_VARIABLE, fontsize,
1756 ALIGN_VCENTRE | ALIGN_HCENTRE, COL_PENCIL, str);
1763 static void game_redraw(drawing *dr, game_drawstate *ds,
1764 const game_state *oldstate, const game_state *state,
1765 int dir, const game_ui *ui,
1766 float animtime, float flashtime)
1768 int w = state->par.w /*, a = w*w */;
1773 * The initial contents of the window are not guaranteed and
1774 * can vary with front ends. To be on the safe side, all
1775 * games should start by drawing a big background-colour
1776 * rectangle covering the whole window.
1778 draw_rect(dr, 0, 0, SIZE(w), SIZE(w), COL_BACKGROUND);
1780 draw_update(dr, 0, 0, SIZE(w), SIZE(w));
1785 check_errors(state, ds->errtmp);
1788 * Work out what data each tile should contain.
1790 for (i = 0; i < (w+2)*(w+2); i++)
1791 ds->tiles[i] = 0; /* completely blank square */
1792 /* The clue squares... */
1793 for (i = 0; i < 4*w; i++) {
1794 long tile = state->clues->clues[i];
1796 CLUEPOS(x, y, i, w);
1798 if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1800 else if (state->clues_done[i])
1801 tile |= DF_CLUE_DONE;
1803 ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1805 /* ... and the main grid. */
1806 for (y = 0; y < w; y++) {
1807 for (x = 0; x < w; x++) {
1808 long tile = DF_PLAYAREA;
1810 if (state->grid[y*w+x])
1811 tile |= state->grid[y*w+x];
1813 tile |= (long)state->pencil[y*w+x] << DF_PENCIL_SHIFT;
1815 if (ui->hshow && ui->hx == x && ui->hy == y)
1816 tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT);
1818 if (state->clues->immutable[y*w+x])
1819 tile |= DF_IMMUTABLE;
1821 if (flashtime > 0 &&
1822 (flashtime <= FLASH_TIME/3 ||
1823 flashtime >= FLASH_TIME*2/3))
1824 tile |= DF_HIGHLIGHT; /* completion flash */
1826 if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1829 ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1834 * Now actually draw anything that needs to be changed.
1836 for (y = 0; y < w+2; y++) {
1837 for (x = 0; x < w+2; x++) {
1838 long tl, tr, bl, br;
1841 tr = ds->tiles[y*(w+2)+x];
1842 tl = (x == 0 ? 0 : ds->tiles[y*(w+2)+(x-1)]);
1843 br = (y == w+1 ? 0 : ds->tiles[(y+1)*(w+2)+x]);
1844 bl = (x == 0 || y == w+1 ? 0 : ds->tiles[(y+1)*(w+2)+(x-1)]);
1846 if (ds->drawn[i*4] != tl || ds->drawn[i*4+1] != tr ||
1847 ds->drawn[i*4+2] != bl || ds->drawn[i*4+3] != br) {
1848 clip(dr, COORD(x-1), COORD(y-1), TILESIZE, TILESIZE);
1850 draw_tile(dr, ds, state->clues, x-1, y-1, tr);
1852 draw_tile(dr, ds, state->clues, x-2, y-1, tl);
1854 draw_tile(dr, ds, state->clues, x-1, y, br);
1855 if (x > 0 && y <= w)
1856 draw_tile(dr, ds, state->clues, x-2, y, bl);
1859 draw_update(dr, COORD(x-1), COORD(y-1), TILESIZE, TILESIZE);
1861 ds->drawn[i*4] = tl;
1862 ds->drawn[i*4+1] = tr;
1863 ds->drawn[i*4+2] = bl;
1864 ds->drawn[i*4+3] = br;
1870 static float game_anim_length(const game_state *oldstate,
1871 const game_state *newstate, int dir, game_ui *ui)
1876 static float game_flash_length(const game_state *oldstate,
1877 const game_state *newstate, int dir, game_ui *ui)
1879 if (!oldstate->completed && newstate->completed &&
1880 !oldstate->cheated && !newstate->cheated)
1885 static int game_status(const game_state *state)
1887 return state->completed ? +1 : 0;
1890 static int game_timing_state(const game_state *state, game_ui *ui)
1892 if (state->completed)
1897 static void game_print_size(const game_params *params, float *x, float *y)
1902 * We use 9mm squares by default, like Solo.
1904 game_compute_size(params, 900, &pw, &ph);
1909 static void game_print(drawing *dr, const game_state *state, int tilesize)
1911 int w = state->par.w;
1912 int ink = print_mono_colour(dr, 0);
1915 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1916 game_drawstate ads, *ds = &ads;
1917 game_set_size(dr, ds, NULL, tilesize);
1922 print_line_width(dr, 3 * TILESIZE / 40);
1923 draw_rect_outline(dr, BORDER, BORDER, w*TILESIZE, w*TILESIZE, ink);
1928 for (x = 1; x < w; x++) {
1929 print_line_width(dr, TILESIZE / 40);
1930 draw_line(dr, BORDER+x*TILESIZE, BORDER,
1931 BORDER+x*TILESIZE, BORDER+w*TILESIZE, ink);
1933 for (y = 1; y < w; y++) {
1934 print_line_width(dr, TILESIZE / 40);
1935 draw_line(dr, BORDER, BORDER+y*TILESIZE,
1936 BORDER+w*TILESIZE, BORDER+y*TILESIZE, ink);
1942 for (i = 0; i < 4*w; i++) {
1945 if (!state->clues->clues[i])
1948 CLUEPOS(x, y, i, w);
1950 sprintf (str, "%d", state->clues->clues[i]);
1952 draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
1953 BORDER + y*TILESIZE + TILESIZE/2,
1954 FONT_VARIABLE, TILESIZE/2,
1955 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
1959 * Numbers for the solution, if any.
1961 for (y = 0; y < w; y++)
1962 for (x = 0; x < w; x++)
1963 if (state->grid[y*w+x]) {
1966 str[0] = state->grid[y*w+x] + '0';
1967 draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
1968 BORDER + y*TILESIZE + TILESIZE/2,
1969 FONT_VARIABLE, TILESIZE/2,
1970 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
1975 #define thegame towers
1978 const struct game thegame = {
1979 "Towers", "games.towers", "towers",
1981 game_fetch_preset, NULL,
1986 TRUE, game_configure, custom_params,
1994 TRUE, game_can_format_as_text_now, game_text_format,
2002 PREFERRED_TILESIZE, game_compute_size, game_set_size,
2005 game_free_drawstate,
2010 TRUE, FALSE, game_print_size, game_print,
2011 FALSE, /* wants_statusbar */
2012 FALSE, game_timing_state,
2013 REQUIRE_RBUTTON | REQUIRE_NUMPAD, /* flags */
2016 #ifdef STANDALONE_SOLVER
2020 int main(int argc, char **argv)
2024 char *id = NULL, *desc, *err;
2026 int ret, diff, really_show_working = FALSE;
2028 while (--argc > 0) {
2030 if (!strcmp(p, "-v")) {
2031 really_show_working = TRUE;
2032 } else if (!strcmp(p, "-g")) {
2034 } else if (*p == '-') {
2035 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2043 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
2047 desc = strchr(id, ':');
2049 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
2054 p = default_params();
2055 decode_params(p, id);
2056 err = validate_desc(p, desc);
2058 fprintf(stderr, "%s: %s\n", argv[0], err);
2061 s = new_game(NULL, p, desc);
2064 * When solving an Easy puzzle, we don't want to bother the
2065 * user with Hard-level deductions. For this reason, we grade
2066 * the puzzle internally before doing anything else.
2068 ret = -1; /* placate optimiser */
2069 solver_show_working = FALSE;
2070 for (diff = 0; diff < DIFFCOUNT; diff++) {
2071 memcpy(s->grid, s->clues->immutable, p->w * p->w);
2072 ret = solver(p->w, s->clues->clues, s->grid, diff);
2077 if (diff == DIFFCOUNT) {
2079 printf("Difficulty rating: ambiguous\n");
2081 printf("Unable to find a unique solution\n");
2084 if (ret == diff_impossible)
2085 printf("Difficulty rating: impossible (no solution exists)\n");
2087 printf("Difficulty rating: %s\n", towers_diffnames[ret]);
2089 solver_show_working = really_show_working;
2090 memcpy(s->grid, s->clues->immutable, p->w * p->w);
2091 ret = solver(p->w, s->clues->clues, s->grid, diff);
2093 printf("Puzzle is inconsistent\n");
2095 fputs(game_text_format(s), stdout);
2104 /* vim: set shiftwidth=4 tabstop=8: */