chiark / gitweb /
Towers: allow marking of clues as done with the keyboard
[sgt-puzzles.git] / towers.c
1 /*
2  * towers.c: the puzzle also known as 'Skyscrapers'.
3  *
4  * Possible future work:
5  *
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
10  *       word to live in
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.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <ctype.h>
26 #include <math.h>
27
28 #include "puzzles.h"
29 #include "latin.h"
30
31 /*
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.
34  */
35 #define DIFFLIST(A) \
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)
48
49 enum {
50     COL_BACKGROUND,
51     COL_GRID,
52     COL_USER,
53     COL_HIGHLIGHT,
54     COL_ERROR,
55     COL_PENCIL,
56     COL_DONE,
57     NCOLOURS
58 };
59
60 struct game_params {
61     int w, diff;
62 };
63
64 struct clues {
65     int refcount;
66     int w;
67     /*
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.
73      */
74     int *clues;
75
76     /*
77      * An array of w*w digits.
78      */
79     digit *immutable;
80 };
81
82 /*
83  * Macros to compute clue indices and coordinates.
84  */
85 #define STARTSTEP(start, step, index, w) do { \
86     if (index < w) \
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; \
92     else \
93         start = w*(index-3*w)+(w-1), step = -1; \
94 } while (0)
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 { \
98     if (index < w) \
99         x = index, y = -1; \
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; \
104     else \
105         x = w, y = index-3*w; \
106 } while (0)
107
108 #ifdef STANDALONE_SOLVER
109 static const char *const cluepos[] = {
110     "above column", "below column", "left of row", "right of row"
111 };
112 #endif
113
114 struct game_state {
115     game_params par;
116     struct clues *clues;
117     unsigned char *clues_done;
118     digit *grid;
119     int *pencil;                       /* bitmaps using bits 1<<1..1<<n */
120     int completed, cheated;
121 };
122
123 static game_params *default_params(void)
124 {
125     game_params *ret = snew(game_params);
126
127     ret->w = 5;
128     ret->diff = DIFF_EASY;
129
130     return ret;
131 }
132
133 const static struct game_params towers_presets[] = {
134     {  4, DIFF_EASY         },
135     {  5, DIFF_EASY         },
136     {  5, DIFF_HARD         },
137     {  6, DIFF_EASY         },
138     {  6, DIFF_HARD         },
139     {  6, DIFF_EXTREME      },
140     {  6, DIFF_UNREASONABLE },
141 };
142
143 static int game_fetch_preset(int i, char **name, game_params **params)
144 {
145     game_params *ret;
146     char buf[80];
147
148     if (i < 0 || i >= lenof(towers_presets))
149         return FALSE;
150
151     ret = snew(game_params);
152     *ret = towers_presets[i]; /* structure copy */
153
154     sprintf(buf, "%dx%d %s", ret->w, ret->w, towers_diffnames[ret->diff]);
155
156     *name = dupstr(buf);
157     *params = ret;
158     return TRUE;
159 }
160
161 static void free_params(game_params *params)
162 {
163     sfree(params);
164 }
165
166 static game_params *dup_params(const game_params *params)
167 {
168     game_params *ret = snew(game_params);
169     *ret = *params;                    /* structure copy */
170     return ret;
171 }
172
173 static void decode_params(game_params *params, char const *string)
174 {
175     char const *p = string;
176
177     params->w = atoi(p);
178     while (*p && isdigit((unsigned char)*p)) p++;
179
180     if (*p == 'd') {
181         int i;
182         p++;
183         params->diff = DIFFCOUNT+1; /* ...which is invalid */
184         if (*p) {
185             for (i = 0; i < DIFFCOUNT; i++) {
186                 if (*p == towers_diffchars[i])
187                     params->diff = i;
188             }
189             p++;
190         }
191     }
192 }
193
194 static char *encode_params(const game_params *params, int full)
195 {
196     char ret[80];
197
198     sprintf(ret, "%d", params->w);
199     if (full)
200         sprintf(ret + strlen(ret), "d%c", towers_diffchars[params->diff]);
201
202     return dupstr(ret);
203 }
204
205 static config_item *game_configure(const game_params *params)
206 {
207     config_item *ret;
208     char buf[80];
209
210     ret = snewn(3, config_item);
211
212     ret[0].name = "Grid size";
213     ret[0].type = C_STRING;
214     sprintf(buf, "%d", params->w);
215     ret[0].sval = dupstr(buf);
216     ret[0].ival = 0;
217
218     ret[1].name = "Difficulty";
219     ret[1].type = C_CHOICES;
220     ret[1].sval = DIFFCONFIG;
221     ret[1].ival = params->diff;
222
223     ret[2].name = NULL;
224     ret[2].type = C_END;
225     ret[2].sval = NULL;
226     ret[2].ival = 0;
227
228     return ret;
229 }
230
231 static game_params *custom_params(const config_item *cfg)
232 {
233     game_params *ret = snew(game_params);
234
235     ret->w = atoi(cfg[0].sval);
236     ret->diff = cfg[1].ival;
237
238     return ret;
239 }
240
241 static char *validate_params(const game_params *params, int full)
242 {
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";
247     return NULL;
248 }
249
250 /* ----------------------------------------------------------------------
251  * Solver.
252  */
253
254 struct solver_ctx {
255     int w, diff;
256     int started;
257     int *clues;
258     long *iscratch;
259     int *dscratch;
260 };
261
262 static int solver_easy(struct latin_solver *solver, void *vctx)
263 {
264     struct solver_ctx *ctx = (struct solver_ctx *)vctx;
265     int w = ctx->w;
266     int c, i, j, n, m, furthest;
267     int start, step, cstart, cstep, clue, pos, cpos;
268     int ret = 0;
269 #ifdef STANDALONE_SOLVER
270     char prefix[256];
271 #endif
272
273     if (!ctx->started) {
274         ctx->started = TRUE;
275         /*
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.
281          */
282         for (c = 0; c < 3*w; c = (c == w-1 ? 2*w : c+1)) {
283             int c2 = c + w;
284
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);
300                     }
301 #endif
302                     latin_solver_place(solver, pos%w, pos/w, w);
303                     ret = 1;
304                 } else {
305                     ret = -1;
306                 }
307             }
308         }
309
310         if (ret)
311             return ret;
312     }
313
314     /*
315      * Go over every clue doing reasonably simple heuristic
316      * deductions.
317      */
318     for (c = 0; c < 4*w; c++) {
319         clue = ctx->clues[c];
320         if (!clue)
321             continue;
322         STARTSTEP(start, step, c, w);
323         CSTARTSTEP(cstart, cstep, c, w);
324
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;
331
332         n = m = 0;
333         furthest = w;
334         for (i = w; i >= 1; i--) {
335             if (ctx->dscratch[i-1] == w) {
336                 break;
337             } else if (ctx->dscratch[i-1] < furthest) {
338                 furthest = ctx->dscratch[i-1];
339                 m = i;
340                 n++;
341             }
342         }
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);
349             else
350                 prefix[0] = '\0';              /* placate optimiser */
351 #endif
352             /*
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.
360              *
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.)
366              */
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 */
371                 j--;
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);
378                         prefix[0] = '\0';
379                     }
380 #endif
381                     solver->cube[cstart*w+i-1] = 0;
382                     ret = 1;
383                 }
384             }
385         }
386
387         if (ret)
388             return ret;
389
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);
395             else
396                 prefix[0] = '\0';              /* placate optimiser */
397 #endif
398
399         i = 0;
400         for (n = w; n > 0; n--) {
401             /*
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
409              * so on.
410              */
411
412             if (ctx->dscratch[n-1] < w) {
413                 for (m = n+1; m < w; m++)
414                     if (ctx->dscratch[m] < ctx->dscratch[n-1])
415                         break;
416                 if (m < w)
417                     continue;          /* this number doesn't count */
418             }
419
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);
428                         prefix[0] = '\0';
429                     }
430 #endif
431                     solver->cube[(cstart + j*cstep)*w+n-1] = 0;
432                     ret = 1;
433                 }
434             i++;
435         }
436     }
437
438     if (ret)
439         return ret;
440
441     return 0;
442 }
443
444 static int solver_hard(struct latin_solver *solver, void *vctx)
445 {
446     struct solver_ctx *ctx = (struct solver_ctx *)vctx;
447     int w = ctx->w;
448     int c, i, j, n, best, clue, start, step, ret;
449     long bitmap;
450 #ifdef STANDALONE_SOLVER
451     char prefix[256];
452 #endif
453
454     /*
455      * Go over every clue analysing all possibilities.
456      */
457     for (c = 0; c < 4*w; c++) {
458         clue = ctx->clues[c];
459         if (!clue)
460             continue;
461         CSTARTSTEP(start, step, c, w);
462
463         for (i = 0; i < w; i++)
464             ctx->iscratch[i] = 0;
465
466         /*
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
470          * be incremented.
471          */
472         i = 0;
473         ctx->dscratch[i] = 0;
474         best = n = 0;
475         bitmap = 0;
476
477         while (1) {
478             if (i < w) {
479                 /*
480                  * Find the next valid value for cell i.
481                  */
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 */
489
490                     /* Found one. */
491                     break;
492                 }
493
494                 if (j > limit) {
495                     /* No valid values left; drop back. */
496                     i--;
497                     if (i < 0)
498                         break;         /* overall iteration is finished */
499                     bitmap &= ~(1L << ctx->dscratch[i]);
500                     if (ctx->dscratch[i] == best) {
501                         n--;
502                         best = 0;
503                         for (j = 0; j < i; j++)
504                             if (best < ctx->dscratch[j])
505                                 best = ctx->dscratch[j];
506                     }
507                 } else {
508                     /* Got a valid value; store it and move on. */
509                     bitmap |= 1L << j;
510                     ctx->dscratch[i++] = j;
511                     if (j > best) {
512                         best = j;
513                         n++;
514                     }
515                     ctx->dscratch[i] = 0;
516                 }
517             } else {
518                 if (n == clue) {
519                     for (j = 0; j < w; j++)
520                         ctx->iscratch[j] |= 1L << ctx->dscratch[j];
521                 }
522                 i--;
523                 bitmap &= ~(1L << ctx->dscratch[i]);
524                 if (ctx->dscratch[i] == best) {
525                     n--;
526                     best = 0;
527                     for (j = 0; j < i; j++)
528                         if (best < ctx->dscratch[j])
529                             best = ctx->dscratch[j];
530                 }
531             }
532         }
533
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);
539         else
540             prefix[0] = '\0';          /* placate optimiser */
541 #endif
542
543         ret = 0;
544
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);
555                         prefix[0] = '\0';
556                     }
557 #endif
558                     solver->cube[pos*w+j-1] = 0;
559                     ret = 1;
560                 }
561             }
562
563             /*
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.
568              */
569             if (ret)
570                 return ret;
571         }
572     }
573
574     return 0;
575 }
576
577 #define SOLVER(upper,title,func,lower) func,
578 static usersolver_t const towers_solvers[] = { DIFFLIST(SOLVER) };
579
580 static int solver(int w, int *clues, digit *soln, int maxdiff)
581 {
582     int ret;
583     struct solver_ctx ctx;
584
585     ctx.w = w;
586     ctx.diff = maxdiff;
587     ctx.clues = clues;
588     ctx.started = FALSE;
589     ctx.iscratch = snewn(w, long);
590     ctx.dscratch = snewn(w+1, int);
591
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);
596
597     sfree(ctx.iscratch);
598     sfree(ctx.dscratch);
599
600     return ret;
601 }
602
603 /* ----------------------------------------------------------------------
604  * Grid generation.
605  */
606
607 static char *new_game_desc(const game_params *params, random_state *rs,
608                            char **aux, int interactive)
609 {
610     int w = params->w, a = w*w;
611     digit *grid, *soln, *soln2;
612     int *clues, *order;
613     int i, ret;
614     int diff = params->diff;
615     char *desc, *p;
616
617     /*
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.
621      *
622      * Remember to re-test this whenever a change is made to the
623      * solver logic!
624      *
625      * I tested it using the following shell command:
626
627 for d in e h x u; do
628   for i in {3..9}; do
629     echo -n "./towers --generate 1 ${i}d${d}: "
630     perl -e 'alarm 30; exec @ARGV' ./towers --generate 1 ${i}d${d} >/dev/null \
631       && echo ok
632   done
633 done
634
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.
638      */
639     if (diff > DIFF_HARD && w <= 3)
640         diff = DIFF_HARD;
641
642     grid = NULL;
643     clues = snewn(4*w, int);
644     soln = snewn(a, digit);
645     soln2 = snewn(a, digit);
646     order = snewn(max(4*w,a), int);
647
648     while (1) {
649         /*
650          * Construct a latin square to be the solution.
651          */
652         sfree(grid);
653         grid = latin_generate(w, rs);
654
655         /*
656          * Fill in the clues.
657          */
658         for (i = 0; i < 4*w; i++) {
659             int start, step, j, k, best;
660             STARTSTEP(start, step, i, w);
661             k = best = 0;
662             for (j = 0; j < w; j++) {
663                 if (grid[start+j*step] > best) {
664                     best = grid[start+j*step];
665                     k++;
666                 }
667             }
668             clues[i] = k;
669         }
670
671         /*
672          * Remove the grid numbers and then the clues, one by one,
673          * for as long as the game remains soluble at the given
674          * difficulty.
675          */
676         memcpy(soln, grid, a);
677
678         if (diff == DIFF_EASY && w <= 5) {
679             /*
680              * Special case: for Easy-mode grids that are small
681              * enough, it's nice to be able to find completely empty
682              * grids.
683              */
684             memset(soln2, 0, a);
685             ret = solver(w, clues, soln2, diff);
686             if (ret > diff)
687                 continue;
688         }
689
690         for (i = 0; i < a; i++)
691             order[i] = i;
692         shuffle(order, a, sizeof(*order), rs);
693         for (i = 0; i < a; i++) {
694             int j = order[i];
695
696             memcpy(soln2, grid, a);
697             soln2[j] = 0;
698             ret = solver(w, clues, soln2, diff);
699             if (ret <= diff)
700                 grid[j] = 0;
701         }
702
703         if (diff > DIFF_EASY) {        /* leave all clues on Easy mode */
704             for (i = 0; i < 4*w; i++)
705                 order[i] = i;
706             shuffle(order, 4*w, sizeof(*order), rs);
707             for (i = 0; i < 4*w; i++) {
708                 int j = order[i];
709                 int clue = clues[j];
710
711                 memcpy(soln2, grid, a);
712                 clues[j] = 0;
713                 ret = solver(w, clues, soln2, diff);
714                 if (ret > diff)
715                     clues[j] = clue;
716             }
717         }
718
719         /*
720          * See if the game can be solved at the specified difficulty
721          * level, but not at the one below.
722          */
723         memcpy(soln2, grid, a);
724         ret = solver(w, clues, soln2, diff);
725         if (ret != diff)
726             continue;                  /* go round again */
727
728         /*
729          * We've got a usable puzzle!
730          */
731         break;
732     }
733
734     /*
735      * Encode the puzzle description.
736      */
737     desc = snewn(40*a, char);
738     p = desc;
739     for (i = 0; i < 4*w; i++) {
740         if (i)
741             *p++ = '/';
742         if (clues[i])
743             p += sprintf(p, "%d", clues[i]);
744     }
745     for (i = 0; i < a; i++)
746         if (grid[i])
747             break;
748     if (i < a) {
749         int run = 0;
750
751         *p++ = ',';
752
753         for (i = 0; i <= a; i++) {
754             int n = (i < a ? grid[i] : -1);
755
756             if (!n)
757                 run++;
758             else {
759                 if (run) {
760                     while (run > 0) {
761                         int thisrun = min(run, 26);
762                         *p++ = thisrun - 1 + 'a';
763                         run -= thisrun;
764                     }
765                 } else {
766                     /*
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.
770                      */
771                     if (i > 0 && n > 0)
772                         *p++ = '_';
773                 }
774                 if (n > 0)
775                     p += sprintf(p, "%d", n);
776                 run = 0;
777             }
778         }
779     }
780     *p++ = '\0';
781     desc = sresize(desc, p - desc, char);
782
783     /*
784      * Encode the solution.
785      */
786     *aux = snewn(a+2, char);
787     (*aux)[0] = 'S';
788     for (i = 0; i < a; i++)
789         (*aux)[i+1] = '0' + soln[i];
790     (*aux)[a+1] = '\0';
791
792     sfree(grid);
793     sfree(clues);
794     sfree(soln);
795     sfree(soln2);
796     sfree(order);
797
798     return desc;
799 }
800
801 /* ----------------------------------------------------------------------
802  * Gameplay.
803  */
804
805 static char *validate_desc(const game_params *params, const char *desc)
806 {
807     int w = params->w, a = w*w;
808     const char *p = desc;
809     int i, clue;
810
811     /*
812      * Verify that the right number of clues are given, and that
813      * they're in range.
814      */
815     for (i = 0; i < 4*w; i++) {
816         if (!*p)
817             return "Too few clues for grid size";
818
819         if (i > 0) {
820             if (*p != '/')
821                 return "Expected commas between clues";
822             p++;
823         }
824
825         if (isdigit((unsigned char)*p)) {
826             clue = atoi(p);
827             while (*p && isdigit((unsigned char)*p)) p++;
828
829             if (clue <= 0 || clue > w)
830                 return "Clue number out of range";
831         }
832     }
833     if (*p == '/')
834         return "Too many clues for grid size";
835
836     if (*p == ',') {
837         /*
838          * Verify that the right amount of grid data is given, and
839          * that any grid elements provided are in range.
840          */
841         int squares = 0;
842
843         p++;
844         while (*p) {
845             int c = *p++;
846             if (c >= 'a' && c <= 'z') {
847                 squares += c - 'a' + 1;
848             } else if (c == '_') {
849                 /* do nothing */;
850             } else if (c > '0' && c <= '9') {
851                 int val = atoi(p-1);
852                 if (val < 1 || val > w)
853                     return "Out-of-range number in grid description";
854                 squares++;
855                 while (*p && isdigit((unsigned char)*p)) p++;
856             } else
857                 return "Invalid character in game description";
858         }
859
860         if (squares < a)
861             return "Not enough data to fill grid";
862
863         if (squares > a)
864             return "Too much data to fit in grid";
865     }
866
867     return NULL;
868 }
869
870 static game_state *new_game(midend *me, const game_params *params,
871                             const char *desc)
872 {
873     int w = params->w, a = w*w;
874     game_state *state = snew(game_state);
875     const char *p = desc;
876     int i;
877
878     state->par = *params;              /* structure copy */
879     state->clues = snew(struct clues);
880     state->clues->refcount = 1;
881     state->clues->w = w;
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);
887
888     for (i = 0; i < a; i++) {
889         state->grid[i] = 0;
890         state->pencil[i] = 0;
891     }
892
893     memset(state->clues->immutable, 0, a);
894     memset(state->clues_done, 0, 4*w*sizeof(unsigned char));
895
896     for (i = 0; i < 4*w; i++) {
897         if (i > 0) {
898             assert(*p == '/');
899             p++;
900         }
901         if (*p && isdigit((unsigned char)*p)) {
902             state->clues->clues[i] = atoi(p);
903             while (*p && isdigit((unsigned char)*p)) p++;
904         } else
905             state->clues->clues[i] = 0;
906     }
907
908     if (*p == ',') {
909         int pos = 0;
910         p++;
911         while (*p) {
912             int c = *p++;
913             if (c >= 'a' && c <= 'z') {
914                 pos += c - 'a' + 1;
915             } else if (c == '_') {
916                 /* do nothing */;
917             } else if (c > '0' && c <= '9') {
918                 int val = atoi(p-1);
919                 assert(val >= 1 && val <= w);
920                 assert(pos < a);
921                 state->grid[pos] = state->clues->immutable[pos] = val;
922                 pos++;
923                 while (*p && isdigit((unsigned char)*p)) p++;
924             } else
925                 assert(!"Corrupt game description");
926         }
927         assert(pos == a);
928     }
929     assert(!*p);
930
931     state->completed = state->cheated = FALSE;
932
933     return state;
934 }
935
936 static game_state *dup_game(const game_state *state)
937 {
938     int w = state->par.w, a = w*w;
939     game_state *ret = snew(game_state);
940
941     ret->par = state->par;             /* structure copy */
942
943     ret->clues = state->clues;
944     ret->clues->refcount++;
945
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));
952
953     ret->completed = state->completed;
954     ret->cheated = state->cheated;
955
956     return ret;
957 }
958
959 static void free_game(game_state *state)
960 {
961     sfree(state->grid);
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);
967         sfree(state->clues);
968     }
969     sfree(state);
970 }
971
972 static char *solve_game(const game_state *state, const game_state *currstate,
973                         const char *aux, char **error)
974 {
975     int w = state->par.w, a = w*w;
976     int i, ret;
977     digit *soln;
978     char *out;
979
980     if (aux)
981         return dupstr(aux);
982
983     soln = snewn(a, digit);
984     memcpy(soln, state->clues->immutable, a);
985
986     ret = solver(w, state->clues->clues, soln, DIFFCOUNT-1);
987
988     if (ret == diff_impossible) {
989         *error = "No solution exists for this puzzle";
990         out = NULL;
991     } else if (ret == diff_ambiguous) {
992         *error = "Multiple solutions exist for this puzzle";
993         out = NULL;
994     } else {
995         out = snewn(a+2, char);
996         out[0] = 'S';
997         for (i = 0; i < a; i++)
998             out[i+1] = '0' + soln[i];
999         out[a+1] = '\0';
1000     }
1001
1002     sfree(soln);
1003     return out;
1004 }
1005
1006 static int game_can_format_as_text_now(const game_params *params)
1007 {
1008     return TRUE;
1009 }
1010
1011 static char *game_text_format(const game_state *state)
1012 {
1013     int w = state->par.w /* , a = w*w */;
1014     char *ret;
1015     char *p;
1016     int x, y;
1017     int total;
1018
1019     /*
1020      * We have:
1021      *  - a top clue row, consisting of three spaces, then w clue
1022      *    digits with spaces between (total 2*w+3 chars including
1023      *    newline)
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.
1031      *
1032      * Total size is therefore 2*(2*w+3) + 2 + w*(2*w+6) + 1
1033      * = 2w^2+10w+9.
1034      */
1035     total = 2*w*w + 10*w + 9;
1036     ret = snewn(total, char);
1037     p = ret;
1038
1039     /* Top clue row. */
1040     *p++ = ' '; *p++ = ' ';
1041     for (x = 0; x < w; x++) {
1042         *p++ = ' ';
1043         *p++ = (state->clues->clues[x] ? '0' + state->clues->clues[x] : ' ');
1044     }
1045     *p++ = '\n';
1046
1047     /* Blank line. */
1048     *p++ = '\n';
1049
1050     /* Main grid. */
1051     for (y = 0; y < w; y++) {
1052         *p++ = (state->clues->clues[y+2*w] ? '0' + state->clues->clues[y+2*w] :
1053                 ' ');
1054         *p++ = ' ';
1055         for (x = 0; x < w; x++) {
1056             *p++ = ' ';
1057             *p++ = (state->grid[y*w+x] ? '0' + state->grid[y*w+x] : ' ');
1058         }
1059         *p++ = ' '; *p++ = ' ';
1060         *p++ = (state->clues->clues[y+3*w] ? '0' + state->clues->clues[y+3*w] :
1061                 ' ');
1062         *p++ = '\n';
1063     }
1064
1065     /* Blank line. */
1066     *p++ = '\n';
1067
1068     /* Bottom clue row. */
1069     *p++ = ' '; *p++ = ' ';
1070     for (x = 0; x < w; x++) {
1071         *p++ = ' ';
1072         *p++ = (state->clues->clues[x+w] ? '0' + state->clues->clues[x+w] :
1073                 ' ');
1074     }
1075     *p++ = '\n';
1076
1077     *p++ = '\0';
1078     assert(p == ret + total);
1079
1080     return ret;
1081 }
1082
1083 struct game_ui {
1084     /*
1085      * These are the coordinates of the currently highlighted
1086      * square on the grid, if hshow = 1.
1087      */
1088     int hx, hy;
1089     /*
1090      * This indicates whether the current highlight is a
1091      * pencil-mark one or a real one.
1092      */
1093     int hpencil;
1094     /*
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.
1100      */
1101     int hshow;
1102     /*
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.
1106      */
1107     int hcursor;
1108 };
1109
1110 static game_ui *new_ui(const game_state *state)
1111 {
1112     game_ui *ui = snew(game_ui);
1113
1114     ui->hx = ui->hy = 0;
1115     ui->hpencil = ui->hshow = ui->hcursor = 0;
1116
1117     return ui;
1118 }
1119
1120 static void free_ui(game_ui *ui)
1121 {
1122     sfree(ui);
1123 }
1124
1125 static char *encode_ui(const game_ui *ui)
1126 {
1127     return NULL;
1128 }
1129
1130 static void decode_ui(game_ui *ui, const char *encoding)
1131 {
1132 }
1133
1134 static void game_changed_state(game_ui *ui, const game_state *oldstate,
1135                                const game_state *newstate)
1136 {
1137     int w = newstate->par.w;
1138     /*
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.
1143      */
1144     if (ui->hshow && ui->hpencil && !ui->hcursor &&
1145         newstate->grid[ui->hy * w + ui->hx] != 0) {
1146         ui->hshow = 0;
1147     }
1148 }
1149
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)
1155
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)))
1159
1160 #define FLASH_TIME 0.4F
1161
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
1170
1171 struct game_drawstate {
1172     int tilesize;
1173     int three_d;                /* default 3D graphics are user-disableable */
1174     int started;
1175     long *tiles;                       /* (w+2)*(w+2) temp space */
1176     long *drawn;                       /* (w+2)*(w+2)*4: current drawn data */
1177     int *errtmp;
1178 };
1179
1180 static int check_errors(const game_state *state, int *errors)
1181 {
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;
1187     int tmp[32];
1188
1189     assert(w < lenof(tmp));
1190
1191     if (errors)
1192         for (i = 0; i < A; i++)
1193             errors[i] = 0;
1194
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);
1200             mask |= bit;
1201         }
1202
1203         if (mask != (1L << (w+1)) - (1L << 1)) {
1204             errs = TRUE;
1205             errmask &= ~1UL;
1206             if (errors) {
1207                 for (x = 0; x < w; x++)
1208                     if (errmask & (1UL << grid[y*w+x]))
1209                         errors[(y+1)*W+(x+1)] = TRUE;
1210             }
1211         }
1212     }
1213
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);
1219             mask |= bit;
1220         }
1221
1222         if (mask != (1 << (w+1)) - (1 << 1)) {
1223             errs = TRUE;
1224             errmask &= ~1UL;
1225             if (errors) {
1226                 for (y = 0; y < w; y++)
1227                     if (errmask & (1UL << grid[y*w+x]))
1228                         errors[(y+1)*W+(x+1)] = TRUE;
1229             }
1230         }
1231     }
1232
1233     for (i = 0; i < 4*w; i++) {
1234         int start, step, j, n, best;
1235         STARTSTEP(start, step, i, w);
1236
1237         if (!clues[i])
1238             continue;
1239
1240         best = n = 0;
1241         for (j = 0; j < w; j++) {
1242             int number = grid[start+j*step];
1243             if (!number)
1244                 break;                 /* can't tell what happens next */
1245             if (number > best) {
1246                 best = number;
1247                 n++;
1248             }
1249         }
1250
1251         if (n > clues[i] || (j == w && n < clues[i])) {
1252             if (errors) {
1253                 int x, y;
1254                 CLUEPOS(x, y, i, w);
1255                 errors[(y+1)*W+(x+1)] = TRUE;
1256             }
1257             errs = TRUE;
1258         }
1259     }
1260
1261     return errs;
1262 }
1263
1264 static int clue_index(const game_state *state, int x, int y)
1265 {
1266     int w = state->par.w;
1267
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;
1272
1273     return -1;
1274 }
1275
1276 static int is_clue(const game_state *state, int x, int y)
1277 {
1278     int w = state->par.w;
1279
1280     if (((x == -1 || x == w) && y >= 0 && y < w) ||
1281         ((y == -1 || y == w) && x >= 0 && x < w))
1282     {
1283         if (state->clues->clues[clue_index(state, x, y)] & DF_DIGIT_MASK)
1284             return TRUE;
1285     }
1286
1287     return FALSE;
1288 }
1289
1290 static char *interpret_move(const game_state *state, game_ui *ui,
1291                             const game_drawstate *ds,
1292                             int x, int y, int button)
1293 {
1294     int w = state->par.w;
1295     int shift_or_control = button & (MOD_SHFT | MOD_CTRL);
1296     int tx, ty;
1297     char buf[80];
1298
1299     button &= ~MOD_MASK;
1300
1301     tx = FROMCOORD(x);
1302     ty = FROMCOORD(y);
1303
1304     if (ds->three_d) {
1305         /*
1306          * In 3D mode, just locating the mouse click in the natural
1307          * square grid may not be sufficient to tell which tower the
1308          * user clicked on. Investigate the _tops_ of the nearby
1309          * towers to see if a click on one grid square was actually
1310          * a click on a tower protruding into that region from
1311          * another.
1312          */
1313         int dx, dy;
1314         for (dy = 0; dy <= 1; dy++)
1315             for (dx = 0; dx >= -1; dx--) {
1316                 int cx = tx + dx, cy = ty + dy;
1317                 if (cx >= 0 && cx < w && cy >= 0 && cy < w) {
1318                     int height = state->grid[cy*w+cx];
1319                     int bx = COORD(cx), by = COORD(cy);
1320                     int ox = bx + X_3D_DISP(height, w);
1321                     int oy = by - Y_3D_DISP(height, w);
1322                     if (/* on top face? */
1323                         (x - ox >= 0 && x - ox < TILESIZE &&
1324                          y - oy >= 0 && y - oy < TILESIZE) ||
1325                         /* in triangle between top-left corners? */
1326                         (ox > bx && x >= bx && x <= ox && y <= by &&
1327                          (by-y) * (ox-bx) <= (by-oy) * (x-bx)) ||
1328                         /* in triangle between bottom-right corners? */
1329                         (ox > bx && x >= bx+TILESIZE && x <= ox+TILESIZE &&
1330                          y >= oy+TILESIZE &&
1331                          (by-y+TILESIZE)*(ox-bx) >= (by-oy)*(x-bx-TILESIZE))) {
1332                         tx = cx;
1333                         ty = cy;
1334                     }
1335                 }
1336             }
1337     }
1338
1339     if (tx >= 0 && tx < w && ty >= 0 && ty < w) {
1340         if (button == LEFT_BUTTON) {
1341             if (tx == ui->hx && ty == ui->hy &&
1342                 ui->hshow && ui->hpencil == 0) {
1343                 ui->hshow = 0;
1344             } else {
1345                 ui->hx = tx;
1346                 ui->hy = ty;
1347                 ui->hshow = !state->clues->immutable[ty*w+tx];
1348                 ui->hpencil = 0;
1349             }
1350             ui->hcursor = 0;
1351             return "";                 /* UI activity occurred */
1352         }
1353         if (button == RIGHT_BUTTON) {
1354             /*
1355              * Pencil-mode highlighting for non filled squares.
1356              */
1357             if (state->grid[ty*w+tx] == 0) {
1358                 if (tx == ui->hx && ty == ui->hy &&
1359                     ui->hshow && ui->hpencil) {
1360                     ui->hshow = 0;
1361                 } else {
1362                     ui->hpencil = 1;
1363                     ui->hx = tx;
1364                     ui->hy = ty;
1365                     ui->hshow = 1;
1366                 }
1367             } else {
1368                 ui->hshow = 0;
1369             }
1370             ui->hcursor = 0;
1371             return "";                 /* UI activity occurred */
1372         }
1373     } else if (button == LEFT_BUTTON) {
1374         if (is_clue(state, tx, ty)) {
1375             sprintf(buf, "%c%d,%d", 'D', tx, ty);
1376             return dupstr(buf);
1377         }
1378     }
1379     if (IS_CURSOR_MOVE(button)) {
1380         if (shift_or_control) {
1381             int x = ui->hx, y = ui->hy;
1382             switch (button) {
1383             case CURSOR_LEFT:   x = -1; break;
1384             case CURSOR_RIGHT:  x =  w; break;
1385             case CURSOR_UP:     y = -1; break;
1386             case CURSOR_DOWN:   y =  w; break;
1387             }
1388             if (is_clue(state, x, y)) {
1389                 sprintf(buf, "%c%d,%d", 'D', x, y);
1390                 return dupstr(buf);
1391             }
1392             return NULL;
1393         }
1394         move_cursor(button, &ui->hx, &ui->hy, w, w, 0);
1395         ui->hshow = ui->hcursor = 1;
1396         return "";
1397     }
1398     if (ui->hshow &&
1399         (button == CURSOR_SELECT)) {
1400         ui->hpencil = 1 - ui->hpencil;
1401         ui->hcursor = 1;
1402         return "";
1403     }
1404
1405     if (ui->hshow &&
1406         ((button >= '0' && button <= '9' && button - '0' <= w) ||
1407          button == CURSOR_SELECT2 || button == '\b')) {
1408         int n = button - '0';
1409         if (button == CURSOR_SELECT2 || button == '\b')
1410             n = 0;
1411
1412         /*
1413          * Can't make pencil marks in a filled square. This can only
1414          * become highlighted if we're using cursor keys.
1415          */
1416         if (ui->hpencil && state->grid[ui->hy*w+ui->hx])
1417             return NULL;
1418
1419         /*
1420          * Can't do anything to an immutable square.
1421          */
1422         if (state->clues->immutable[ui->hy*w+ui->hx])
1423             return NULL;
1424
1425         sprintf(buf, "%c%d,%d,%d",
1426                 (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n);
1427
1428         if (!ui->hcursor) ui->hshow = 0;
1429
1430         return dupstr(buf);
1431     }
1432
1433     if (button == 'M' || button == 'm')
1434         return dupstr("M");
1435
1436     return NULL;
1437 }
1438
1439 static game_state *execute_move(const game_state *from, const char *move)
1440 {
1441     int w = from->par.w, a = w*w;
1442     game_state *ret = dup_game(from);
1443     int x, y, i, n;
1444
1445     if (move[0] == 'S') {
1446         ret->completed = ret->cheated = TRUE;
1447
1448         for (i = 0; i < a; i++) {
1449             if (move[i+1] < '1' || move[i+1] > '0'+w)
1450                 goto badmove;
1451             ret->grid[i] = move[i+1] - '0';
1452             ret->pencil[i] = 0;
1453         }
1454
1455         if (move[a+1] != '\0')
1456             goto badmove;
1457
1458         return ret;
1459     } else if ((move[0] == 'P' || move[0] == 'R') &&
1460         sscanf(move+1, "%d,%d,%d", &x, &y, &n) == 3 &&
1461         x >= 0 && x < w && y >= 0 && y < w && n >= 0 && n <= w) {
1462         if (from->clues->immutable[y*w+x])
1463             goto badmove;
1464
1465         if (move[0] == 'P' && n > 0) {
1466             ret->pencil[y*w+x] ^= 1L << n;
1467         } else {
1468             ret->grid[y*w+x] = n;
1469             ret->pencil[y*w+x] = 0;
1470
1471             if (!ret->completed && !check_errors(ret, NULL))
1472                 ret->completed = TRUE;
1473         }
1474         return ret;
1475     } else if (move[0] == 'M') {
1476         /*
1477          * Fill in absolutely all pencil marks everywhere. (I
1478          * wouldn't use this for actual play, but it's a handy
1479          * starting point when following through a set of
1480          * diagnostics output by the standalone solver.)
1481          */
1482         for (i = 0; i < a; i++) {
1483             if (!ret->grid[i])
1484                 ret->pencil[i] = (1L << (w+1)) - (1L << 1);
1485         }
1486         return ret;
1487     } else if (move[0] == 'D' && sscanf(move+1, "%d,%d", &x, &y) == 2 &&
1488                is_clue(from, x, y)) {
1489         int index = clue_index(from, x, y);
1490         ret->clues_done[index] = !ret->clues_done[index];
1491         return ret;
1492     }
1493
1494   badmove:
1495     /* couldn't parse move string */
1496     free_game(ret);
1497     return NULL;
1498 }
1499
1500 /* ----------------------------------------------------------------------
1501  * Drawing routines.
1502  */
1503
1504 #define SIZE(w) ((w) * TILESIZE + 2*BORDER)
1505
1506 static void game_compute_size(const game_params *params, int tilesize,
1507                               int *x, int *y)
1508 {
1509     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1510     struct { int tilesize; } ads, *ds = &ads;
1511     ads.tilesize = tilesize;
1512
1513     *x = *y = SIZE(params->w);
1514 }
1515
1516 static void game_set_size(drawing *dr, game_drawstate *ds,
1517                           const game_params *params, int tilesize)
1518 {
1519     ds->tilesize = tilesize;
1520 }
1521
1522 static float *game_colours(frontend *fe, int *ncolours)
1523 {
1524     float *ret = snewn(3 * NCOLOURS, float);
1525
1526     frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1527
1528     ret[COL_GRID * 3 + 0] = 0.0F;
1529     ret[COL_GRID * 3 + 1] = 0.0F;
1530     ret[COL_GRID * 3 + 2] = 0.0F;
1531
1532     ret[COL_USER * 3 + 0] = 0.0F;
1533     ret[COL_USER * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1];
1534     ret[COL_USER * 3 + 2] = 0.0F;
1535
1536     ret[COL_HIGHLIGHT * 3 + 0] = 0.78F * ret[COL_BACKGROUND * 3 + 0];
1537     ret[COL_HIGHLIGHT * 3 + 1] = 0.78F * ret[COL_BACKGROUND * 3 + 1];
1538     ret[COL_HIGHLIGHT * 3 + 2] = 0.78F * ret[COL_BACKGROUND * 3 + 2];
1539
1540     ret[COL_ERROR * 3 + 0] = 1.0F;
1541     ret[COL_ERROR * 3 + 1] = 0.0F;
1542     ret[COL_ERROR * 3 + 2] = 0.0F;
1543
1544     ret[COL_PENCIL * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1545     ret[COL_PENCIL * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1546     ret[COL_PENCIL * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
1547
1548     ret[COL_DONE * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] / 1.5F;
1549     ret[COL_DONE * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] / 1.5F;
1550     ret[COL_DONE * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] / 1.5F;
1551
1552     *ncolours = NCOLOURS;
1553     return ret;
1554 }
1555
1556 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1557 {
1558     int w = state->par.w /*, a = w*w */;
1559     struct game_drawstate *ds = snew(struct game_drawstate);
1560     int i;
1561
1562     ds->tilesize = 0;
1563     ds->three_d = !getenv("TOWERS_2D");
1564     ds->started = FALSE;
1565     ds->tiles = snewn((w+2)*(w+2), long);
1566     ds->drawn = snewn((w+2)*(w+2)*4, long);
1567     for (i = 0; i < (w+2)*(w+2)*4; i++)
1568         ds->drawn[i] = -1;
1569     ds->errtmp = snewn((w+2)*(w+2), int);
1570
1571     return ds;
1572 }
1573
1574 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1575 {
1576     sfree(ds->errtmp);
1577     sfree(ds->tiles);
1578     sfree(ds->drawn);
1579     sfree(ds);
1580 }
1581
1582 static void draw_tile(drawing *dr, game_drawstate *ds, struct clues *clues,
1583                       int x, int y, long tile)
1584 {
1585     int w = clues->w /* , a = w*w */;
1586     int tx, ty, bg;
1587     char str[64];
1588
1589     tx = COORD(x);
1590     ty = COORD(y);
1591
1592     bg = (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND;
1593
1594     /* draw tower */
1595     if (ds->three_d && (tile & DF_PLAYAREA) && (tile & DF_DIGIT_MASK)) {
1596         int coords[8];
1597         int xoff = X_3D_DISP(tile & DF_DIGIT_MASK, w);
1598         int yoff = Y_3D_DISP(tile & DF_DIGIT_MASK, w);
1599
1600         /* left face of tower */
1601         coords[0] = tx;
1602         coords[1] = ty - 1;
1603         coords[2] = tx;
1604         coords[3] = ty + TILESIZE - 1;
1605         coords[4] = coords[2] + xoff;
1606         coords[5] = coords[3] - yoff;
1607         coords[6] = coords[0] + xoff;
1608         coords[7] = coords[1] - yoff;
1609         draw_polygon(dr, coords, 4, bg, COL_GRID);
1610
1611         /* bottom face of tower */
1612         coords[0] = tx + TILESIZE;
1613         coords[1] = ty + TILESIZE - 1;
1614         coords[2] = tx;
1615         coords[3] = ty + TILESIZE - 1;
1616         coords[4] = coords[2] + xoff;
1617         coords[5] = coords[3] - yoff;
1618         coords[6] = coords[0] + xoff;
1619         coords[7] = coords[1] - yoff;
1620         draw_polygon(dr, coords, 4, bg, COL_GRID);
1621
1622         /* now offset all subsequent drawing to the top of the tower */
1623         tx += xoff;
1624         ty -= yoff;
1625     }
1626
1627     /* erase background */
1628     draw_rect(dr, tx, ty, TILESIZE, TILESIZE, bg);
1629
1630     /* pencil-mode highlight */
1631     if (tile & DF_HIGHLIGHT_PENCIL) {
1632         int coords[6];
1633         coords[0] = tx;
1634         coords[1] = ty;
1635         coords[2] = tx+TILESIZE/2;
1636         coords[3] = ty;
1637         coords[4] = tx;
1638         coords[5] = ty+TILESIZE/2;
1639         draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
1640     }
1641
1642     /* draw box outline */
1643     if (tile & DF_PLAYAREA) {
1644         int coords[8];
1645         coords[0] = tx;
1646         coords[1] = ty - 1;
1647         coords[2] = tx + TILESIZE;
1648         coords[3] = ty - 1;
1649         coords[4] = tx + TILESIZE;
1650         coords[5] = ty + TILESIZE - 1;
1651         coords[6] = tx;
1652         coords[7] = ty + TILESIZE - 1;
1653         draw_polygon(dr, coords, 4, -1, COL_GRID);
1654     }
1655
1656     /* new number needs drawing? */
1657     if (tile & DF_DIGIT_MASK) {
1658         int color;
1659
1660         str[1] = '\0';
1661         str[0] = (tile & DF_DIGIT_MASK) + '0';
1662
1663         if (tile & DF_ERROR)
1664             color = COL_ERROR;
1665         else if (tile & DF_CLUE_DONE)
1666             color = COL_DONE;
1667         else if (x < 0 || y < 0 || x >= w || y >= w)
1668             color = COL_GRID;
1669         else if (tile & DF_IMMUTABLE)
1670             color = COL_GRID;
1671         else
1672             color = COL_USER;
1673
1674         draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2, FONT_VARIABLE,
1675                   (tile & DF_PLAYAREA ? TILESIZE/2 : TILESIZE*2/5),
1676                   ALIGN_VCENTRE | ALIGN_HCENTRE, color, str);
1677     } else {
1678         int i, j, npencil;
1679         int pl, pr, pt, pb;
1680         float bestsize;
1681         int pw, ph, minph, pbest, fontsize;
1682
1683         /* Count the pencil marks required. */
1684         for (i = 1, npencil = 0; i <= w; i++)
1685             if (tile & (1L << (i + DF_PENCIL_SHIFT)))
1686                 npencil++;
1687         if (npencil) {
1688
1689             minph = 2;
1690
1691             /*
1692              * Determine the bounding rectangle within which we're going
1693              * to put the pencil marks.
1694              */
1695             /* Start with the whole square, minus space for impinging towers */
1696             pl = tx + (ds->three_d ? X_3D_DISP(w,w) : 0);
1697             pr = tx + TILESIZE;
1698             pt = ty;
1699             pb = ty + TILESIZE - (ds->three_d ? Y_3D_DISP(w,w) : 0);
1700
1701             /*
1702              * We arrange our pencil marks in a grid layout, with
1703              * the number of rows and columns adjusted to allow the
1704              * maximum font size.
1705              *
1706              * So now we work out what the grid size ought to be.
1707              */
1708             bestsize = 0.0;
1709             pbest = 0;
1710             /* Minimum */
1711             for (pw = 3; pw < max(npencil,4); pw++) {
1712                 float fw, fh, fs;
1713
1714                 ph = (npencil + pw - 1) / pw;
1715                 ph = max(ph, minph);
1716                 fw = (pr - pl) / (float)pw;
1717                 fh = (pb - pt) / (float)ph;
1718                 fs = min(fw, fh);
1719                 if (fs > bestsize) {
1720                     bestsize = fs;
1721                     pbest = pw;
1722                 }
1723             }
1724             assert(pbest > 0);
1725             pw = pbest;
1726             ph = (npencil + pw - 1) / pw;
1727             ph = max(ph, minph);
1728
1729             /*
1730              * Now we've got our grid dimensions, work out the pixel
1731              * size of a grid element, and round it to the nearest
1732              * pixel. (We don't want rounding errors to make the
1733              * grid look uneven at low pixel sizes.)
1734              */
1735             fontsize = min((pr - pl) / pw, (pb - pt) / ph);
1736
1737             /*
1738              * Centre the resulting figure in the square.
1739              */
1740             pl = pl + (pr - pl - fontsize * pw) / 2;
1741             pt = pt + (pb - pt - fontsize * ph) / 2;
1742
1743             /*
1744              * Now actually draw the pencil marks.
1745              */
1746             for (i = 1, j = 0; i <= w; i++)
1747                 if (tile & (1L << (i + DF_PENCIL_SHIFT))) {
1748                     int dx = j % pw, dy = j / pw;
1749
1750                     str[1] = '\0';
1751                     str[0] = i + '0';
1752                     draw_text(dr, pl + fontsize * (2*dx+1) / 2,
1753                               pt + fontsize * (2*dy+1) / 2,
1754                               FONT_VARIABLE, fontsize,
1755                               ALIGN_VCENTRE | ALIGN_HCENTRE, COL_PENCIL, str);
1756                     j++;
1757                 }
1758         }
1759     }
1760 }
1761
1762 static void game_redraw(drawing *dr, game_drawstate *ds,
1763                         const game_state *oldstate, const game_state *state,
1764                         int dir, const game_ui *ui,
1765                         float animtime, float flashtime)
1766 {
1767     int w = state->par.w /*, a = w*w */;
1768     int i, x, y;
1769
1770     if (!ds->started) {
1771         /*
1772          * The initial contents of the window are not guaranteed and
1773          * can vary with front ends. To be on the safe side, all
1774          * games should start by drawing a big background-colour
1775          * rectangle covering the whole window.
1776          */
1777         draw_rect(dr, 0, 0, SIZE(w), SIZE(w), COL_BACKGROUND);
1778
1779         draw_update(dr, 0, 0, SIZE(w), SIZE(w));
1780
1781         ds->started = TRUE;
1782     }
1783
1784     check_errors(state, ds->errtmp);
1785
1786     /*
1787      * Work out what data each tile should contain.
1788      */
1789     for (i = 0; i < (w+2)*(w+2); i++)
1790         ds->tiles[i] = 0;              /* completely blank square */
1791     /* The clue squares... */
1792     for (i = 0; i < 4*w; i++) {
1793         long tile = state->clues->clues[i];
1794
1795         CLUEPOS(x, y, i, w);
1796
1797         if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1798             tile |= DF_ERROR;
1799         else if (state->clues_done[i])
1800             tile |= DF_CLUE_DONE;
1801
1802         ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1803     }
1804     /* ... and the main grid. */
1805     for (y = 0; y < w; y++) {
1806         for (x = 0; x < w; x++) {
1807             long tile = DF_PLAYAREA;
1808
1809             if (state->grid[y*w+x])
1810                 tile |= state->grid[y*w+x];
1811             else
1812                 tile |= (long)state->pencil[y*w+x] << DF_PENCIL_SHIFT;
1813
1814             if (ui->hshow && ui->hx == x && ui->hy == y)
1815                 tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT);
1816
1817             if (state->clues->immutable[y*w+x])
1818                 tile |= DF_IMMUTABLE;
1819
1820             if (flashtime > 0 &&
1821                 (flashtime <= FLASH_TIME/3 ||
1822                  flashtime >= FLASH_TIME*2/3))
1823                 tile |= DF_HIGHLIGHT;  /* completion flash */
1824
1825             if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1826                 tile |= DF_ERROR;
1827
1828             ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1829         }
1830     }
1831
1832     /*
1833      * Now actually draw anything that needs to be changed.
1834      */
1835     for (y = 0; y < w+2; y++) {
1836         for (x = 0; x < w+2; x++) {
1837             long tl, tr, bl, br;
1838             int i = y*(w+2)+x;
1839
1840             tr = ds->tiles[y*(w+2)+x];
1841             tl = (x == 0 ? 0 : ds->tiles[y*(w+2)+(x-1)]);
1842             br = (y == w+1 ? 0 : ds->tiles[(y+1)*(w+2)+x]);
1843             bl = (x == 0 || y == w+1 ? 0 : ds->tiles[(y+1)*(w+2)+(x-1)]);
1844
1845             if (ds->drawn[i*4] != tl || ds->drawn[i*4+1] != tr ||
1846                 ds->drawn[i*4+2] != bl || ds->drawn[i*4+3] != br) {
1847                 clip(dr, COORD(x-1), COORD(y-1), TILESIZE, TILESIZE);
1848
1849                 draw_tile(dr, ds, state->clues, x-1, y-1, tr);
1850                 if (x > 0)
1851                     draw_tile(dr, ds, state->clues, x-2, y-1, tl);
1852                 if (y <= w)
1853                     draw_tile(dr, ds, state->clues, x-1, y, br);
1854                 if (x > 0 && y <= w)
1855                     draw_tile(dr, ds, state->clues, x-2, y, bl);
1856
1857                 unclip(dr);
1858                 draw_update(dr, COORD(x-1), COORD(y-1), TILESIZE, TILESIZE);
1859
1860                 ds->drawn[i*4] = tl;
1861                 ds->drawn[i*4+1] = tr;
1862                 ds->drawn[i*4+2] = bl;
1863                 ds->drawn[i*4+3] = br;
1864             }
1865         }
1866     }
1867 }
1868
1869 static float game_anim_length(const game_state *oldstate,
1870                               const game_state *newstate, int dir, game_ui *ui)
1871 {
1872     return 0.0F;
1873 }
1874
1875 static float game_flash_length(const game_state *oldstate,
1876                                const game_state *newstate, int dir, game_ui *ui)
1877 {
1878     if (!oldstate->completed && newstate->completed &&
1879         !oldstate->cheated && !newstate->cheated)
1880         return FLASH_TIME;
1881     return 0.0F;
1882 }
1883
1884 static int game_status(const game_state *state)
1885 {
1886     return state->completed ? +1 : 0;
1887 }
1888
1889 static int game_timing_state(const game_state *state, game_ui *ui)
1890 {
1891     if (state->completed)
1892         return FALSE;
1893     return TRUE;
1894 }
1895
1896 static void game_print_size(const game_params *params, float *x, float *y)
1897 {
1898     int pw, ph;
1899
1900     /*
1901      * We use 9mm squares by default, like Solo.
1902      */
1903     game_compute_size(params, 900, &pw, &ph);
1904     *x = pw / 100.0F;
1905     *y = ph / 100.0F;
1906 }
1907
1908 static void game_print(drawing *dr, const game_state *state, int tilesize)
1909 {
1910     int w = state->par.w;
1911     int ink = print_mono_colour(dr, 0);
1912     int i, x, y;
1913
1914     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1915     game_drawstate ads, *ds = &ads;
1916     game_set_size(dr, ds, NULL, tilesize);
1917
1918     /*
1919      * Border.
1920      */
1921     print_line_width(dr, 3 * TILESIZE / 40);
1922     draw_rect_outline(dr, BORDER, BORDER, w*TILESIZE, w*TILESIZE, ink);
1923
1924     /*
1925      * Main grid.
1926      */
1927     for (x = 1; x < w; x++) {
1928         print_line_width(dr, TILESIZE / 40);
1929         draw_line(dr, BORDER+x*TILESIZE, BORDER,
1930                   BORDER+x*TILESIZE, BORDER+w*TILESIZE, ink);
1931     }
1932     for (y = 1; y < w; y++) {
1933         print_line_width(dr, TILESIZE / 40);
1934         draw_line(dr, BORDER, BORDER+y*TILESIZE,
1935                   BORDER+w*TILESIZE, BORDER+y*TILESIZE, ink);
1936     }
1937
1938     /*
1939      * Clues.
1940      */
1941     for (i = 0; i < 4*w; i++) {
1942         char str[128];
1943
1944         if (!state->clues->clues[i])
1945             continue;
1946
1947         CLUEPOS(x, y, i, w);
1948
1949         sprintf (str, "%d", state->clues->clues[i]);
1950
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);
1955     }
1956
1957     /*
1958      * Numbers for the solution, if any.
1959      */
1960     for (y = 0; y < w; y++)
1961         for (x = 0; x < w; x++)
1962             if (state->grid[y*w+x]) {
1963                 char str[2];
1964                 str[1] = '\0';
1965                 str[0] = state->grid[y*w+x] + '0';
1966                 draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
1967                           BORDER + y*TILESIZE + TILESIZE/2,
1968                           FONT_VARIABLE, TILESIZE/2,
1969                           ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
1970             }
1971 }
1972
1973 #ifdef COMBINED
1974 #define thegame towers
1975 #endif
1976
1977 const struct game thegame = {
1978     "Towers", "games.towers", "towers",
1979     default_params,
1980     game_fetch_preset,
1981     decode_params,
1982     encode_params,
1983     free_params,
1984     dup_params,
1985     TRUE, game_configure, custom_params,
1986     validate_params,
1987     new_game_desc,
1988     validate_desc,
1989     new_game,
1990     dup_game,
1991     free_game,
1992     TRUE, solve_game,
1993     TRUE, game_can_format_as_text_now, game_text_format,
1994     new_ui,
1995     free_ui,
1996     encode_ui,
1997     decode_ui,
1998     game_changed_state,
1999     interpret_move,
2000     execute_move,
2001     PREFERRED_TILESIZE, game_compute_size, game_set_size,
2002     game_colours,
2003     game_new_drawstate,
2004     game_free_drawstate,
2005     game_redraw,
2006     game_anim_length,
2007     game_flash_length,
2008     game_status,
2009     TRUE, FALSE, game_print_size, game_print,
2010     FALSE,                             /* wants_statusbar */
2011     FALSE, game_timing_state,
2012     REQUIRE_RBUTTON | REQUIRE_NUMPAD,  /* flags */
2013 };
2014
2015 #ifdef STANDALONE_SOLVER
2016
2017 #include <stdarg.h>
2018
2019 int main(int argc, char **argv)
2020 {
2021     game_params *p;
2022     game_state *s;
2023     char *id = NULL, *desc, *err;
2024     int grade = FALSE;
2025     int ret, diff, really_show_working = FALSE;
2026
2027     while (--argc > 0) {
2028         char *p = *++argv;
2029         if (!strcmp(p, "-v")) {
2030             really_show_working = TRUE;
2031         } else if (!strcmp(p, "-g")) {
2032             grade = TRUE;
2033         } else if (*p == '-') {
2034             fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2035             return 1;
2036         } else {
2037             id = p;
2038         }
2039     }
2040
2041     if (!id) {
2042         fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
2043         return 1;
2044     }
2045
2046     desc = strchr(id, ':');
2047     if (!desc) {
2048         fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
2049         return 1;
2050     }
2051     *desc++ = '\0';
2052
2053     p = default_params();
2054     decode_params(p, id);
2055     err = validate_desc(p, desc);
2056     if (err) {
2057         fprintf(stderr, "%s: %s\n", argv[0], err);
2058         return 1;
2059     }
2060     s = new_game(NULL, p, desc);
2061
2062     /*
2063      * When solving an Easy puzzle, we don't want to bother the
2064      * user with Hard-level deductions. For this reason, we grade
2065      * the puzzle internally before doing anything else.
2066      */
2067     ret = -1;                          /* placate optimiser */
2068     solver_show_working = FALSE;
2069     for (diff = 0; diff < DIFFCOUNT; diff++) {
2070         memcpy(s->grid, s->clues->immutable, p->w * p->w);
2071         ret = solver(p->w, s->clues->clues, s->grid, diff);
2072         if (ret <= diff)
2073             break;
2074     }
2075
2076     if (diff == DIFFCOUNT) {
2077         if (grade)
2078             printf("Difficulty rating: ambiguous\n");
2079         else
2080             printf("Unable to find a unique solution\n");
2081     } else {
2082         if (grade) {
2083             if (ret == diff_impossible)
2084                 printf("Difficulty rating: impossible (no solution exists)\n");
2085             else
2086                 printf("Difficulty rating: %s\n", towers_diffnames[ret]);
2087         } else {
2088             solver_show_working = really_show_working;
2089             memcpy(s->grid, s->clues->immutable, p->w * p->w);
2090             ret = solver(p->w, s->clues->clues, s->grid, diff);
2091             if (ret != diff)
2092                 printf("Puzzle is inconsistent\n");
2093             else
2094                 fputs(game_text_format(s), stdout);
2095         }
2096     }
2097
2098     return 0;
2099 }
2100
2101 #endif
2102
2103 /* vim: set shiftwidth=4 tabstop=8: */