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] || (j == w && n < clues[i])) {
1254 CLUEPOS(x, y, i, w);
1255 errors[(y+1)*W+(x+1)] = TRUE;
1264 static int clue_index(const game_state *state, int x, int y)
1266 int w = state->par.w;
1268 if (x == -1 || x == w)
1269 return w * (x == -1 ? 2 : 3) + y;
1270 else if (y == -1 || y == w)
1271 return (y == -1 ? 0 : w) + x;
1276 static int is_clue(const game_state *state, int x, int y)
1278 int w = state->par.w;
1280 if (((x == -1 || x == w) && y >= 0 && y < w) ||
1281 ((y == -1 || y == w) && x >= 0 && x < w))
1283 if (state->clues->clues[clue_index(state, x, y)] & DF_DIGIT_MASK)
1290 static char *interpret_move(const game_state *state, game_ui *ui,
1291 const game_drawstate *ds,
1292 int x, int y, int button)
1294 int w = state->par.w;
1298 button &= ~MOD_MASK;
1305 * In 3D mode, just locating the mouse click in the natural
1306 * square grid may not be sufficient to tell which tower the
1307 * user clicked on. Investigate the _tops_ of the nearby
1308 * towers to see if a click on one grid square was actually
1309 * a click on a tower protruding into that region from
1313 for (dy = 0; dy <= 1; dy++)
1314 for (dx = 0; dx >= -1; dx--) {
1315 int cx = tx + dx, cy = ty + dy;
1316 if (cx >= 0 && cx < w && cy >= 0 && cy < w) {
1317 int height = state->grid[cy*w+cx];
1318 int bx = COORD(cx), by = COORD(cy);
1319 int ox = bx + X_3D_DISP(height, w);
1320 int oy = by - Y_3D_DISP(height, w);
1321 if (/* on top face? */
1322 (x - ox >= 0 && x - ox < TILESIZE &&
1323 y - oy >= 0 && y - oy < TILESIZE) ||
1324 /* in triangle between top-left corners? */
1325 (ox > bx && x >= bx && x <= ox && y <= by &&
1326 (by-y) * (ox-bx) <= (by-oy) * (x-bx)) ||
1327 /* in triangle between bottom-right corners? */
1328 (ox > bx && x >= bx+TILESIZE && x <= ox+TILESIZE &&
1330 (by-y+TILESIZE)*(ox-bx) >= (by-oy)*(x-bx-TILESIZE))) {
1338 if (tx >= 0 && tx < w && ty >= 0 && ty < w) {
1339 if (button == LEFT_BUTTON) {
1340 if (tx == ui->hx && ty == ui->hy &&
1341 ui->hshow && ui->hpencil == 0) {
1346 ui->hshow = !state->clues->immutable[ty*w+tx];
1350 return ""; /* UI activity occurred */
1352 if (button == RIGHT_BUTTON) {
1354 * Pencil-mode highlighting for non filled squares.
1356 if (state->grid[ty*w+tx] == 0) {
1357 if (tx == ui->hx && ty == ui->hy &&
1358 ui->hshow && ui->hpencil) {
1370 return ""; /* UI activity occurred */
1372 } else if (button == LEFT_BUTTON) {
1373 if (is_clue(state, tx, ty)) {
1374 sprintf(buf, "%c%d,%d", 'D', tx, ty);
1378 if (IS_CURSOR_MOVE(button)) {
1379 move_cursor(button, &ui->hx, &ui->hy, w, w, 0);
1380 ui->hshow = ui->hcursor = 1;
1384 (button == CURSOR_SELECT)) {
1385 ui->hpencil = 1 - ui->hpencil;
1391 ((button >= '0' && button <= '9' && button - '0' <= w) ||
1392 button == CURSOR_SELECT2 || button == '\b')) {
1393 int n = button - '0';
1394 if (button == CURSOR_SELECT2 || button == '\b')
1398 * Can't make pencil marks in a filled square. This can only
1399 * become highlighted if we're using cursor keys.
1401 if (ui->hpencil && state->grid[ui->hy*w+ui->hx])
1405 * Can't do anything to an immutable square.
1407 if (state->clues->immutable[ui->hy*w+ui->hx])
1410 sprintf(buf, "%c%d,%d,%d",
1411 (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n);
1413 if (!ui->hcursor) ui->hshow = 0;
1418 if (button == 'M' || button == 'm')
1424 static game_state *execute_move(const game_state *from, const char *move)
1426 int w = from->par.w, a = w*w;
1427 game_state *ret = dup_game(from);
1430 if (move[0] == 'S') {
1431 ret->completed = ret->cheated = TRUE;
1433 for (i = 0; i < a; i++) {
1434 if (move[i+1] < '1' || move[i+1] > '0'+w)
1436 ret->grid[i] = move[i+1] - '0';
1440 if (move[a+1] != '\0')
1444 } else if ((move[0] == 'P' || move[0] == 'R') &&
1445 sscanf(move+1, "%d,%d,%d", &x, &y, &n) == 3 &&
1446 x >= 0 && x < w && y >= 0 && y < w && n >= 0 && n <= w) {
1447 if (from->clues->immutable[y*w+x])
1450 if (move[0] == 'P' && n > 0) {
1451 ret->pencil[y*w+x] ^= 1L << n;
1453 ret->grid[y*w+x] = n;
1454 ret->pencil[y*w+x] = 0;
1456 if (!ret->completed && !check_errors(ret, NULL))
1457 ret->completed = TRUE;
1460 } else if (move[0] == 'M') {
1462 * Fill in absolutely all pencil marks everywhere. (I
1463 * wouldn't use this for actual play, but it's a handy
1464 * starting point when following through a set of
1465 * diagnostics output by the standalone solver.)
1467 for (i = 0; i < a; i++) {
1469 ret->pencil[i] = (1L << (w+1)) - (1L << 1);
1472 } else if (move[0] == 'D' && sscanf(move+1, "%d,%d", &x, &y) == 2 &&
1473 is_clue(from, x, y)) {
1474 int index = clue_index(from, x, y);
1475 ret->clues_done[index] = !ret->clues_done[index];
1480 /* couldn't parse move string */
1485 /* ----------------------------------------------------------------------
1489 #define SIZE(w) ((w) * TILESIZE + 2*BORDER)
1491 static void game_compute_size(const game_params *params, int tilesize,
1494 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1495 struct { int tilesize; } ads, *ds = &ads;
1496 ads.tilesize = tilesize;
1498 *x = *y = SIZE(params->w);
1501 static void game_set_size(drawing *dr, game_drawstate *ds,
1502 const game_params *params, int tilesize)
1504 ds->tilesize = tilesize;
1507 static float *game_colours(frontend *fe, int *ncolours)
1509 float *ret = snewn(3 * NCOLOURS, float);
1511 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1513 ret[COL_GRID * 3 + 0] = 0.0F;
1514 ret[COL_GRID * 3 + 1] = 0.0F;
1515 ret[COL_GRID * 3 + 2] = 0.0F;
1517 ret[COL_USER * 3 + 0] = 0.0F;
1518 ret[COL_USER * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1];
1519 ret[COL_USER * 3 + 2] = 0.0F;
1521 ret[COL_HIGHLIGHT * 3 + 0] = 0.78F * ret[COL_BACKGROUND * 3 + 0];
1522 ret[COL_HIGHLIGHT * 3 + 1] = 0.78F * ret[COL_BACKGROUND * 3 + 1];
1523 ret[COL_HIGHLIGHT * 3 + 2] = 0.78F * ret[COL_BACKGROUND * 3 + 2];
1525 ret[COL_ERROR * 3 + 0] = 1.0F;
1526 ret[COL_ERROR * 3 + 1] = 0.0F;
1527 ret[COL_ERROR * 3 + 2] = 0.0F;
1529 ret[COL_PENCIL * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1530 ret[COL_PENCIL * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1531 ret[COL_PENCIL * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
1533 ret[COL_DONE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] / 1.5F;
1534 ret[COL_DONE * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] / 1.5F;
1535 ret[COL_DONE * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] / 1.5F;
1537 *ncolours = NCOLOURS;
1541 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1543 int w = state->par.w /*, a = w*w */;
1544 struct game_drawstate *ds = snew(struct game_drawstate);
1548 ds->three_d = !getenv("TOWERS_2D");
1549 ds->started = FALSE;
1550 ds->tiles = snewn((w+2)*(w+2), long);
1551 ds->drawn = snewn((w+2)*(w+2)*4, long);
1552 for (i = 0; i < (w+2)*(w+2)*4; i++)
1554 ds->errtmp = snewn((w+2)*(w+2), int);
1559 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1567 static void draw_tile(drawing *dr, game_drawstate *ds, struct clues *clues,
1568 int x, int y, long tile)
1570 int w = clues->w /* , a = w*w */;
1577 bg = (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND;
1580 if (ds->three_d && (tile & DF_PLAYAREA) && (tile & DF_DIGIT_MASK)) {
1582 int xoff = X_3D_DISP(tile & DF_DIGIT_MASK, w);
1583 int yoff = Y_3D_DISP(tile & DF_DIGIT_MASK, w);
1585 /* left face of tower */
1589 coords[3] = ty + TILESIZE - 1;
1590 coords[4] = coords[2] + xoff;
1591 coords[5] = coords[3] - yoff;
1592 coords[6] = coords[0] + xoff;
1593 coords[7] = coords[1] - yoff;
1594 draw_polygon(dr, coords, 4, bg, COL_GRID);
1596 /* bottom face of tower */
1597 coords[0] = tx + TILESIZE;
1598 coords[1] = ty + TILESIZE - 1;
1600 coords[3] = ty + TILESIZE - 1;
1601 coords[4] = coords[2] + xoff;
1602 coords[5] = coords[3] - yoff;
1603 coords[6] = coords[0] + xoff;
1604 coords[7] = coords[1] - yoff;
1605 draw_polygon(dr, coords, 4, bg, COL_GRID);
1607 /* now offset all subsequent drawing to the top of the tower */
1612 /* erase background */
1613 draw_rect(dr, tx, ty, TILESIZE, TILESIZE, bg);
1615 /* pencil-mode highlight */
1616 if (tile & DF_HIGHLIGHT_PENCIL) {
1620 coords[2] = tx+TILESIZE/2;
1623 coords[5] = ty+TILESIZE/2;
1624 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
1627 /* draw box outline */
1628 if (tile & DF_PLAYAREA) {
1632 coords[2] = tx + TILESIZE;
1634 coords[4] = tx + TILESIZE;
1635 coords[5] = ty + TILESIZE - 1;
1637 coords[7] = ty + TILESIZE - 1;
1638 draw_polygon(dr, coords, 4, -1, COL_GRID);
1641 /* new number needs drawing? */
1642 if (tile & DF_DIGIT_MASK) {
1646 str[0] = (tile & DF_DIGIT_MASK) + '0';
1648 if (tile & DF_ERROR)
1650 else if (tile & DF_CLUE_DONE)
1652 else if (x < 0 || y < 0 || x >= w || y >= w)
1654 else if (tile & DF_IMMUTABLE)
1659 draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2, FONT_VARIABLE,
1660 (tile & DF_PLAYAREA ? TILESIZE/2 : TILESIZE*2/5),
1661 ALIGN_VCENTRE | ALIGN_HCENTRE, color, str);
1666 int pw, ph, minph, pbest, fontsize;
1668 /* Count the pencil marks required. */
1669 for (i = 1, npencil = 0; i <= w; i++)
1670 if (tile & (1L << (i + DF_PENCIL_SHIFT)))
1677 * Determine the bounding rectangle within which we're going
1678 * to put the pencil marks.
1680 /* Start with the whole square, minus space for impinging towers */
1681 pl = tx + (ds->three_d ? X_3D_DISP(w,w) : 0);
1684 pb = ty + TILESIZE - (ds->three_d ? Y_3D_DISP(w,w) : 0);
1687 * We arrange our pencil marks in a grid layout, with
1688 * the number of rows and columns adjusted to allow the
1689 * maximum font size.
1691 * So now we work out what the grid size ought to be.
1696 for (pw = 3; pw < max(npencil,4); pw++) {
1699 ph = (npencil + pw - 1) / pw;
1700 ph = max(ph, minph);
1701 fw = (pr - pl) / (float)pw;
1702 fh = (pb - pt) / (float)ph;
1704 if (fs > bestsize) {
1711 ph = (npencil + pw - 1) / pw;
1712 ph = max(ph, minph);
1715 * Now we've got our grid dimensions, work out the pixel
1716 * size of a grid element, and round it to the nearest
1717 * pixel. (We don't want rounding errors to make the
1718 * grid look uneven at low pixel sizes.)
1720 fontsize = min((pr - pl) / pw, (pb - pt) / ph);
1723 * Centre the resulting figure in the square.
1725 pl = pl + (pr - pl - fontsize * pw) / 2;
1726 pt = pt + (pb - pt - fontsize * ph) / 2;
1729 * Now actually draw the pencil marks.
1731 for (i = 1, j = 0; i <= w; i++)
1732 if (tile & (1L << (i + DF_PENCIL_SHIFT))) {
1733 int dx = j % pw, dy = j / pw;
1737 draw_text(dr, pl + fontsize * (2*dx+1) / 2,
1738 pt + fontsize * (2*dy+1) / 2,
1739 FONT_VARIABLE, fontsize,
1740 ALIGN_VCENTRE | ALIGN_HCENTRE, COL_PENCIL, str);
1747 static void game_redraw(drawing *dr, game_drawstate *ds,
1748 const game_state *oldstate, const game_state *state,
1749 int dir, const game_ui *ui,
1750 float animtime, float flashtime)
1752 int w = state->par.w /*, a = w*w */;
1757 * The initial contents of the window are not guaranteed and
1758 * can vary with front ends. To be on the safe side, all
1759 * games should start by drawing a big background-colour
1760 * rectangle covering the whole window.
1762 draw_rect(dr, 0, 0, SIZE(w), SIZE(w), COL_BACKGROUND);
1764 draw_update(dr, 0, 0, SIZE(w), SIZE(w));
1769 check_errors(state, ds->errtmp);
1772 * Work out what data each tile should contain.
1774 for (i = 0; i < (w+2)*(w+2); i++)
1775 ds->tiles[i] = 0; /* completely blank square */
1776 /* The clue squares... */
1777 for (i = 0; i < 4*w; i++) {
1778 long tile = state->clues->clues[i];
1780 CLUEPOS(x, y, i, w);
1782 if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1784 else if (state->clues_done[i])
1785 tile |= DF_CLUE_DONE;
1787 ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1789 /* ... and the main grid. */
1790 for (y = 0; y < w; y++) {
1791 for (x = 0; x < w; x++) {
1792 long tile = DF_PLAYAREA;
1794 if (state->grid[y*w+x])
1795 tile |= state->grid[y*w+x];
1797 tile |= (long)state->pencil[y*w+x] << DF_PENCIL_SHIFT;
1799 if (ui->hshow && ui->hx == x && ui->hy == y)
1800 tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT);
1802 if (state->clues->immutable[y*w+x])
1803 tile |= DF_IMMUTABLE;
1805 if (flashtime > 0 &&
1806 (flashtime <= FLASH_TIME/3 ||
1807 flashtime >= FLASH_TIME*2/3))
1808 tile |= DF_HIGHLIGHT; /* completion flash */
1810 if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1813 ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1818 * Now actually draw anything that needs to be changed.
1820 for (y = 0; y < w+2; y++) {
1821 for (x = 0; x < w+2; x++) {
1822 long tl, tr, bl, br;
1825 tr = ds->tiles[y*(w+2)+x];
1826 tl = (x == 0 ? 0 : ds->tiles[y*(w+2)+(x-1)]);
1827 br = (y == w+1 ? 0 : ds->tiles[(y+1)*(w+2)+x]);
1828 bl = (x == 0 || y == w+1 ? 0 : ds->tiles[(y+1)*(w+2)+(x-1)]);
1830 if (ds->drawn[i*4] != tl || ds->drawn[i*4+1] != tr ||
1831 ds->drawn[i*4+2] != bl || ds->drawn[i*4+3] != br) {
1832 clip(dr, COORD(x-1), COORD(y-1), TILESIZE, TILESIZE);
1834 draw_tile(dr, ds, state->clues, x-1, y-1, tr);
1836 draw_tile(dr, ds, state->clues, x-2, y-1, tl);
1838 draw_tile(dr, ds, state->clues, x-1, y, br);
1839 if (x > 0 && y <= w)
1840 draw_tile(dr, ds, state->clues, x-2, y, bl);
1843 draw_update(dr, COORD(x-1), COORD(y-1), TILESIZE, TILESIZE);
1845 ds->drawn[i*4] = tl;
1846 ds->drawn[i*4+1] = tr;
1847 ds->drawn[i*4+2] = bl;
1848 ds->drawn[i*4+3] = br;
1854 static float game_anim_length(const game_state *oldstate,
1855 const game_state *newstate, int dir, game_ui *ui)
1860 static float game_flash_length(const game_state *oldstate,
1861 const game_state *newstate, int dir, game_ui *ui)
1863 if (!oldstate->completed && newstate->completed &&
1864 !oldstate->cheated && !newstate->cheated)
1869 static int game_status(const game_state *state)
1871 return state->completed ? +1 : 0;
1874 static int game_timing_state(const game_state *state, game_ui *ui)
1876 if (state->completed)
1881 static void game_print_size(const game_params *params, float *x, float *y)
1886 * We use 9mm squares by default, like Solo.
1888 game_compute_size(params, 900, &pw, &ph);
1893 static void game_print(drawing *dr, const game_state *state, int tilesize)
1895 int w = state->par.w;
1896 int ink = print_mono_colour(dr, 0);
1899 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1900 game_drawstate ads, *ds = &ads;
1901 game_set_size(dr, ds, NULL, tilesize);
1906 print_line_width(dr, 3 * TILESIZE / 40);
1907 draw_rect_outline(dr, BORDER, BORDER, w*TILESIZE, w*TILESIZE, ink);
1912 for (x = 1; x < w; x++) {
1913 print_line_width(dr, TILESIZE / 40);
1914 draw_line(dr, BORDER+x*TILESIZE, BORDER,
1915 BORDER+x*TILESIZE, BORDER+w*TILESIZE, ink);
1917 for (y = 1; y < w; y++) {
1918 print_line_width(dr, TILESIZE / 40);
1919 draw_line(dr, BORDER, BORDER+y*TILESIZE,
1920 BORDER+w*TILESIZE, BORDER+y*TILESIZE, ink);
1926 for (i = 0; i < 4*w; i++) {
1929 if (!state->clues->clues[i])
1932 CLUEPOS(x, y, i, w);
1934 sprintf (str, "%d", state->clues->clues[i]);
1936 draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
1937 BORDER + y*TILESIZE + TILESIZE/2,
1938 FONT_VARIABLE, TILESIZE/2,
1939 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
1943 * Numbers for the solution, if any.
1945 for (y = 0; y < w; y++)
1946 for (x = 0; x < w; x++)
1947 if (state->grid[y*w+x]) {
1950 str[0] = state->grid[y*w+x] + '0';
1951 draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
1952 BORDER + y*TILESIZE + TILESIZE/2,
1953 FONT_VARIABLE, TILESIZE/2,
1954 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
1959 #define thegame towers
1962 const struct game thegame = {
1963 "Towers", "games.towers", "towers",
1970 TRUE, game_configure, custom_params,
1978 TRUE, game_can_format_as_text_now, game_text_format,
1986 PREFERRED_TILESIZE, game_compute_size, game_set_size,
1989 game_free_drawstate,
1994 TRUE, FALSE, game_print_size, game_print,
1995 FALSE, /* wants_statusbar */
1996 FALSE, game_timing_state,
1997 REQUIRE_RBUTTON | REQUIRE_NUMPAD, /* flags */
2000 #ifdef STANDALONE_SOLVER
2004 int main(int argc, char **argv)
2008 char *id = NULL, *desc, *err;
2010 int ret, diff, really_show_working = FALSE;
2012 while (--argc > 0) {
2014 if (!strcmp(p, "-v")) {
2015 really_show_working = TRUE;
2016 } else if (!strcmp(p, "-g")) {
2018 } else if (*p == '-') {
2019 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2027 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
2031 desc = strchr(id, ':');
2033 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
2038 p = default_params();
2039 decode_params(p, id);
2040 err = validate_desc(p, desc);
2042 fprintf(stderr, "%s: %s\n", argv[0], err);
2045 s = new_game(NULL, p, desc);
2048 * When solving an Easy puzzle, we don't want to bother the
2049 * user with Hard-level deductions. For this reason, we grade
2050 * the puzzle internally before doing anything else.
2052 ret = -1; /* placate optimiser */
2053 solver_show_working = FALSE;
2054 for (diff = 0; diff < DIFFCOUNT; diff++) {
2055 memcpy(s->grid, s->clues->immutable, p->w * p->w);
2056 ret = solver(p->w, s->clues->clues, s->grid, diff);
2061 if (diff == DIFFCOUNT) {
2063 printf("Difficulty rating: ambiguous\n");
2065 printf("Unable to find a unique solution\n");
2068 if (ret == diff_impossible)
2069 printf("Difficulty rating: impossible (no solution exists)\n");
2071 printf("Difficulty rating: %s\n", towers_diffnames[ret]);
2073 solver_show_working = really_show_working;
2074 memcpy(s->grid, s->clues->immutable, p->w * p->w);
2075 ret = solver(p->w, s->clues->clues, s->grid, diff);
2077 printf("Puzzle is inconsistent\n");
2079 fputs(game_text_format(s), stdout);
2088 /* vim: set shiftwidth=4 tabstop=8: */