New puzzle, again using the revised latin.c: 'Towers', a clone of a
[sgt/puzzles] / 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 * - UI work?
21 * + Allow the user to mark a clue as 'spent' in some way once
22 * it's no longer interesting (typically because no
23 * arrangement of the remaining possibilities _can_ violate
24 * it)?
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <math.h>
33
34 #include "puzzles.h"
35 #include "latin.h"
36
37 /*
38 * Difficulty levels. I do some macro ickery here to ensure that my
39 * enum and the various forms of my name list always match up.
40 */
41 #define DIFFLIST(A) \
42 A(EASY,Easy,solver_easy,e) \
43 A(HARD,Hard,solver_hard,h) \
44 A(EXTREME,Extreme,NULL,x) \
45 A(UNREASONABLE,Unreasonable,NULL,u)
46 #define ENUM(upper,title,func,lower) DIFF_ ## upper,
47 #define TITLE(upper,title,func,lower) #title,
48 #define ENCODE(upper,title,func,lower) #lower
49 #define CONFIG(upper,title,func,lower) ":" #title
50 enum { DIFFLIST(ENUM) DIFFCOUNT };
51 static char const *const towers_diffnames[] = { DIFFLIST(TITLE) };
52 static char const towers_diffchars[] = DIFFLIST(ENCODE);
53 #define DIFFCONFIG DIFFLIST(CONFIG)
54
55 enum {
56 COL_BACKGROUND,
57 COL_GRID,
58 COL_USER,
59 COL_HIGHLIGHT,
60 COL_ERROR,
61 COL_PENCIL,
62 NCOLOURS
63 };
64
65 struct game_params {
66 int w, diff;
67 };
68
69 struct clues {
70 int refcount;
71 int w;
72 /*
73 * An array of 4w integers, of which:
74 * - the first w run across the top
75 * - the next w across the bottom
76 * - the third w down the left
77 * - the last w down the right.
78 */
79 int *clues;
80
81 /*
82 * An array of w*w digits.
83 */
84 digit *immutable;
85 };
86
87 /*
88 * Macros to compute clue indices and coordinates.
89 */
90 #define STARTSTEP(start, step, index, w) do { \
91 if (index < w) \
92 start = index, step = w; \
93 else if (index < 2*w) \
94 start = (w-1)*w+(index-w), step = -w; \
95 else if (index < 3*w) \
96 start = w*(index-2*w), step = 1; \
97 else \
98 start = w*(index-3*w)+(w-1), step = -1; \
99 } while (0)
100 #define CSTARTSTEP(start, step, index, w) \
101 STARTSTEP(start, step, (((index)+2*w)%(4*w)), w)
102 #define CLUEPOS(x, y, index, w) do { \
103 if (index < w) \
104 x = index, y = -1; \
105 else if (index < 2*w) \
106 x = index-w, y = w; \
107 else if (index < 3*w) \
108 x = -1, y = index-2*w; \
109 else \
110 x = w, y = index-3*w; \
111 } while (0)
112
113 #ifdef STANDALONE_SOLVER
114 static const char *const cluepos[] = {
115 "above column", "below column", "left of row", "right of row"
116 };
117 #endif
118
119 struct game_state {
120 game_params par;
121 struct clues *clues;
122 digit *grid;
123 int *pencil; /* bitmaps using bits 1<<1..1<<n */
124 int completed, cheated;
125 };
126
127 static game_params *default_params(void)
128 {
129 game_params *ret = snew(game_params);
130
131 ret->w = 5;
132 ret->diff = DIFF_EASY;
133
134 return ret;
135 }
136
137 const static struct game_params towers_presets[] = {
138 { 4, DIFF_EASY },
139 { 5, DIFF_EASY },
140 { 5, DIFF_HARD },
141 { 6, DIFF_EASY },
142 { 6, DIFF_HARD },
143 { 6, DIFF_EXTREME },
144 { 6, DIFF_UNREASONABLE },
145 };
146
147 static int game_fetch_preset(int i, char **name, game_params **params)
148 {
149 game_params *ret;
150 char buf[80];
151
152 if (i < 0 || i >= lenof(towers_presets))
153 return FALSE;
154
155 ret = snew(game_params);
156 *ret = towers_presets[i]; /* structure copy */
157
158 sprintf(buf, "%dx%d %s", ret->w, ret->w, towers_diffnames[ret->diff]);
159
160 *name = dupstr(buf);
161 *params = ret;
162 return TRUE;
163 }
164
165 static void free_params(game_params *params)
166 {
167 sfree(params);
168 }
169
170 static game_params *dup_params(game_params *params)
171 {
172 game_params *ret = snew(game_params);
173 *ret = *params; /* structure copy */
174 return ret;
175 }
176
177 static void decode_params(game_params *params, char const *string)
178 {
179 char const *p = string;
180
181 params->w = atoi(p);
182 while (*p && isdigit((unsigned char)*p)) p++;
183
184 if (*p == 'd') {
185 int i;
186 p++;
187 params->diff = DIFFCOUNT+1; /* ...which is invalid */
188 if (*p) {
189 for (i = 0; i < DIFFCOUNT; i++) {
190 if (*p == towers_diffchars[i])
191 params->diff = i;
192 }
193 p++;
194 }
195 }
196 }
197
198 static char *encode_params(game_params *params, int full)
199 {
200 char ret[80];
201
202 sprintf(ret, "%d", params->w);
203 if (full)
204 sprintf(ret + strlen(ret), "d%c", towers_diffchars[params->diff]);
205
206 return dupstr(ret);
207 }
208
209 static config_item *game_configure(game_params *params)
210 {
211 config_item *ret;
212 char buf[80];
213
214 ret = snewn(3, config_item);
215
216 ret[0].name = "Grid size";
217 ret[0].type = C_STRING;
218 sprintf(buf, "%d", params->w);
219 ret[0].sval = dupstr(buf);
220 ret[0].ival = 0;
221
222 ret[1].name = "Difficulty";
223 ret[1].type = C_CHOICES;
224 ret[1].sval = DIFFCONFIG;
225 ret[1].ival = params->diff;
226
227 ret[2].name = NULL;
228 ret[2].type = C_END;
229 ret[2].sval = NULL;
230 ret[2].ival = 0;
231
232 return ret;
233 }
234
235 static game_params *custom_params(config_item *cfg)
236 {
237 game_params *ret = snew(game_params);
238
239 ret->w = atoi(cfg[0].sval);
240 ret->diff = cfg[1].ival;
241
242 return ret;
243 }
244
245 static char *validate_params(game_params *params, int full)
246 {
247 if (params->w < 3 || params->w > 9)
248 return "Grid size must be between 3 and 9";
249 if (params->diff >= DIFFCOUNT)
250 return "Unknown difficulty rating";
251 return NULL;
252 }
253
254 /* ----------------------------------------------------------------------
255 * Solver.
256 */
257
258 struct solver_ctx {
259 int w, diff;
260 int started;
261 int *clues;
262 long *iscratch;
263 int *dscratch;
264 };
265
266 static int solver_easy(struct latin_solver *solver, void *vctx)
267 {
268 struct solver_ctx *ctx = (struct solver_ctx *)vctx;
269 int w = ctx->w;
270 int c, i, j, n, m, furthest;
271 int start, step, cstart, cstep, clue, pos, cpos;
272 int ret = 0;
273 #ifdef STANDALONE_SOLVER
274 char prefix[256];
275 #endif
276
277 if (!ctx->started) {
278 ctx->started = TRUE;
279 /*
280 * One-off loop to help get started: when a pair of facing
281 * clues sum to w+1, it must mean that the row consists of
282 * two increasing sequences back to back, so we can
283 * immediately place the highest digit by knowing the
284 * lengths of those two sequences.
285 */
286 for (c = 0; c < 3*w; c = (c == w-1 ? 2*w : c+1)) {
287 int c2 = c + w;
288
289 if (ctx->clues[c] && ctx->clues[c2] &&
290 ctx->clues[c] + ctx->clues[c2] == w+1) {
291 STARTSTEP(start, step, c, w);
292 CSTARTSTEP(cstart, cstep, c, w);
293 pos = start + (ctx->clues[c]-1)*step;
294 cpos = cstart + (ctx->clues[c]-1)*cstep;
295 if (solver->cube[cpos*w+w-1]) {
296 #ifdef STANDALONE_SOLVER
297 if (solver_show_working) {
298 printf("%*sfacing clues on %s %d are maximal:\n",
299 solver_recurse_depth*4, "",
300 c>=2*w ? "row" : "column", c % w + 1);
301 printf("%*s placing %d at (%d,%d)\n",
302 solver_recurse_depth*4, "",
303 w, pos%w+1, pos/w+1);
304 }
305 #endif
306 latin_solver_place(solver, pos%w, pos/w, w);
307 ret = 1;
308 } else {
309 ret = -1;
310 }
311 }
312 }
313
314 if (ret)
315 return ret;
316 }
317
318 /*
319 * Go over every clue doing reasonably simple heuristic
320 * deductions.
321 */
322 for (c = 0; c < 4*w; c++) {
323 clue = ctx->clues[c];
324 if (!clue)
325 continue;
326 STARTSTEP(start, step, c, w);
327 CSTARTSTEP(cstart, cstep, c, w);
328
329 /* Find the location of each number in the row. */
330 for (i = 0; i < w; i++)
331 ctx->dscratch[i] = w;
332 for (i = 0; i < w; i++)
333 if (solver->grid[start+i*step])
334 ctx->dscratch[solver->grid[start+i*step]-1] = i;
335
336 n = m = 0;
337 furthest = w;
338 for (i = w; i >= 1; i--) {
339 if (ctx->dscratch[i-1] == w) {
340 break;
341 } else if (ctx->dscratch[i-1] < furthest) {
342 furthest = ctx->dscratch[i-1];
343 m = i;
344 n++;
345 }
346 }
347 if (clue == n+1 && furthest > 1) {
348 #ifdef STANDALONE_SOLVER
349 if (solver_show_working)
350 sprintf(prefix, "%*sclue %s %d is nearly filled:\n",
351 solver_recurse_depth*4, "",
352 cluepos[c/w], c%w+1);
353 else
354 prefix[0] = '\0'; /* placate optimiser */
355 #endif
356 /*
357 * We can already see an increasing sequence of the very
358 * highest numbers, of length one less than that
359 * specified in the clue. All of those numbers _must_ be
360 * part of the clue sequence, so the number right next
361 * to the clue must be the final one - i.e. it must be
362 * bigger than any of the numbers between it and m. This
363 * allows us to rule out small numbers in that square.
364 *
365 * (This is a generalisation of the obvious deduction
366 * that when you see a clue saying 1, it must be right
367 * next to the largest possible number; and similarly,
368 * when you see a clue saying 2 opposite that, it must
369 * be right next to the second-largest.)
370 */
371 j = furthest-1; /* number of small numbers we can rule out */
372 for (i = 1; i <= w && j > 0; i++) {
373 if (ctx->dscratch[i-1] < w && ctx->dscratch[i-1] >= furthest)
374 continue; /* skip this number, it's elsewhere */
375 j--;
376 if (solver->cube[cstart*w+i-1]) {
377 #ifdef STANDALONE_SOLVER
378 if (solver_show_working) {
379 printf("%s%*s ruling out %d at (%d,%d)\n",
380 prefix, solver_recurse_depth*4, "",
381 i, start%w+1, start/w+1);
382 prefix[0] = '\0';
383 }
384 #endif
385 solver->cube[cstart*w+i-1] = 0;
386 ret = 1;
387 }
388 }
389 }
390
391 if (ret)
392 return ret;
393
394 #ifdef STANDALONE_SOLVER
395 if (solver_show_working)
396 sprintf(prefix, "%*slower bounds for clue %s %d:\n",
397 solver_recurse_depth*4, "",
398 cluepos[c/w], c%w+1);
399 else
400 prefix[0] = '\0'; /* placate optimiser */
401 #endif
402
403 i = 0;
404 for (n = w; n > 0; n--) {
405 /*
406 * The largest number cannot occur in the first (clue-1)
407 * squares of the row, or else there wouldn't be space
408 * for a sufficiently long increasing sequence which it
409 * terminated. The second-largest number (not counting
410 * any that are known to be on the far side of a larger
411 * number and hence excluded from this sequence) cannot
412 * occur in the first (clue-2) squares, similarly, and
413 * so on.
414 */
415
416 if (ctx->dscratch[n-1] < w) {
417 for (m = n+1; m < w; m++)
418 if (ctx->dscratch[m] < ctx->dscratch[n-1])
419 break;
420 if (m < w)
421 continue; /* this number doesn't count */
422 }
423
424 for (j = 0; j < clue - i - 1; j++)
425 if (solver->cube[(cstart + j*cstep)*w+n-1]) {
426 #ifdef STANDALONE_SOLVER
427 if (solver_show_working) {
428 int pos = start+j*step;
429 printf("%s%*s ruling out %d at (%d,%d)\n",
430 prefix, solver_recurse_depth*4, "",
431 n, pos%w+1, pos/w+1);
432 prefix[0] = '\0';
433 }
434 #endif
435 solver->cube[(cstart + j*cstep)*w+n-1] = 0;
436 ret = 1;
437 }
438 i++;
439 }
440 }
441
442 if (ret)
443 return ret;
444
445 return 0;
446 }
447
448 static int solver_hard(struct latin_solver *solver, void *vctx)
449 {
450 struct solver_ctx *ctx = (struct solver_ctx *)vctx;
451 int w = ctx->w;
452 int c, i, j, n, best, clue, start, step, ret;
453 long bitmap;
454 #ifdef STANDALONE_SOLVER
455 char prefix[256];
456 #endif
457
458 /*
459 * Go over every clue analysing all possibilities.
460 */
461 for (c = 0; c < 4*w; c++) {
462 clue = ctx->clues[c];
463 if (!clue)
464 continue;
465 CSTARTSTEP(start, step, c, w);
466
467 for (i = 0; i < w; i++)
468 ctx->iscratch[i] = 0;
469
470 /*
471 * Instead of a tedious physical recursion, I iterate in the
472 * scratch array through all possibilities. At any given
473 * moment, i indexes the element of the box that will next
474 * be incremented.
475 */
476 i = 0;
477 ctx->dscratch[i] = 0;
478 best = n = 0;
479 bitmap = 0;
480
481 while (1) {
482 if (i < w) {
483 /*
484 * Find the next valid value for cell i.
485 */
486 int limit = (n == clue ? best : w);
487 int pos = start + step * i;
488 for (j = ctx->dscratch[i] + 1; j <= limit; j++) {
489 if (bitmap & (1L << j))
490 continue; /* used this one already */
491 if (!solver->cube[pos*w+j-1])
492 continue; /* ruled out already */
493
494 /* Found one. */
495 break;
496 }
497
498 if (j > limit) {
499 /* No valid values left; drop back. */
500 i--;
501 if (i < 0)
502 break; /* overall iteration is finished */
503 bitmap &= ~(1L << ctx->dscratch[i]);
504 if (ctx->dscratch[i] == best) {
505 n--;
506 best = 0;
507 for (j = 0; j < i; j++)
508 if (best < ctx->dscratch[j])
509 best = ctx->dscratch[j];
510 }
511 } else {
512 /* Got a valid value; store it and move on. */
513 bitmap |= 1L << j;
514 ctx->dscratch[i++] = j;
515 if (j > best) {
516 best = j;
517 n++;
518 }
519 ctx->dscratch[i] = 0;
520 }
521 } else {
522 if (n == clue) {
523 for (j = 0; j < w; j++)
524 ctx->iscratch[j] |= 1L << ctx->dscratch[j];
525 }
526 i--;
527 bitmap &= ~(1L << ctx->dscratch[i]);
528 if (ctx->dscratch[i] == best) {
529 n--;
530 best = 0;
531 for (j = 0; j < i; j++)
532 if (best < ctx->dscratch[j])
533 best = ctx->dscratch[j];
534 }
535 }
536 }
537
538 #ifdef STANDALONE_SOLVER
539 if (solver_show_working)
540 sprintf(prefix, "%*sexhaustive analysis of clue %s %d:\n",
541 solver_recurse_depth*4, "",
542 cluepos[c/w], c%w+1);
543 else
544 prefix[0] = '\0'; /* placate optimiser */
545 #endif
546
547 ret = 0;
548
549 for (i = 0; i < w; i++) {
550 int pos = start + step * i;
551 for (j = 1; j <= w; j++) {
552 if (solver->cube[pos*w+j-1] &&
553 !(ctx->iscratch[i] & (1L << j))) {
554 #ifdef STANDALONE_SOLVER
555 if (solver_show_working) {
556 printf("%s%*s ruling out %d at (%d,%d)\n",
557 prefix, solver_recurse_depth*4, "",
558 j, pos/w+1, pos%w+1);
559 prefix[0] = '\0';
560 }
561 #endif
562 solver->cube[pos*w+j-1] = 0;
563 ret = 1;
564 }
565 }
566
567 /*
568 * Once we find one clue we can do something with in
569 * this way, revert to trying easier deductions, so as
570 * not to generate solver diagnostics that make the
571 * problem look harder than it is.
572 */
573 if (ret)
574 return ret;
575 }
576 }
577
578 return 0;
579 }
580
581 #define SOLVER(upper,title,func,lower) func,
582 static usersolver_t const towers_solvers[] = { DIFFLIST(SOLVER) };
583
584 static int solver(int w, int *clues, digit *soln, int maxdiff)
585 {
586 int ret;
587 struct solver_ctx ctx;
588
589 ctx.w = w;
590 ctx.diff = maxdiff;
591 ctx.clues = clues;
592 ctx.started = FALSE;
593 ctx.iscratch = snewn(w, long);
594 ctx.dscratch = snewn(w+1, int);
595
596 ret = latin_solver(soln, w, maxdiff,
597 DIFF_EASY, DIFF_HARD, DIFF_EXTREME,
598 DIFF_EXTREME, DIFF_UNREASONABLE,
599 towers_solvers, &ctx, NULL, NULL);
600
601 sfree(ctx.iscratch);
602 sfree(ctx.dscratch);
603
604 return ret;
605 }
606
607 /* ----------------------------------------------------------------------
608 * Grid generation.
609 */
610
611 static char *new_game_desc(game_params *params, random_state *rs,
612 char **aux, int interactive)
613 {
614 int w = params->w, a = w*w;
615 digit *grid, *soln, *soln2;
616 int *clues, *order;
617 int i, ret;
618 int diff = params->diff;
619 char *desc, *p;
620
621 /*
622 * Difficulty exceptions: some combinations of size and
623 * difficulty cannot be satisfied, because all puzzles of at
624 * most that difficulty are actually even easier.
625 *
626 * Remember to re-test this whenever a change is made to the
627 * solver logic!
628 *
629 * I tested it using the following shell command:
630
631 for d in e h x u; do
632 for i in {3..9}; do
633 echo -n "./towers --generate 1 ${i}d${d}: "
634 perl -e 'alarm 30; exec @ARGV' ./towers --generate 1 ${i}d${d} >/dev/null \
635 && echo ok
636 done
637 done
638
639 * Of course, it's better to do that after taking the exceptions
640 * _out_, so as to detect exceptions that should be removed as
641 * well as those which should be added.
642 */
643 if (diff > DIFF_HARD && w <= 3)
644 diff = DIFF_HARD;
645
646 grid = NULL;
647 clues = snewn(4*w, int);
648 soln = snewn(a, digit);
649 soln2 = snewn(a, digit);
650 order = snewn(max(4*w,a), int);
651
652 while (1) {
653 /*
654 * Construct a latin square to be the solution.
655 */
656 sfree(grid);
657 grid = latin_generate(w, rs);
658
659 /*
660 * Fill in the clues.
661 */
662 for (i = 0; i < 4*w; i++) {
663 int start, step, j, k, best;
664 STARTSTEP(start, step, i, w);
665 k = best = 0;
666 for (j = 0; j < w; j++) {
667 if (grid[start+j*step] > best) {
668 best = grid[start+j*step];
669 k++;
670 }
671 }
672 clues[i] = k;
673 }
674
675 /*
676 * Remove the grid numbers and then the clues, one by one,
677 * for as long as the game remains soluble at the given
678 * difficulty.
679 */
680 memcpy(soln, grid, a);
681
682 if (diff == DIFF_EASY && w <= 5) {
683 /*
684 * Special case: for Easy-mode grids that are small
685 * enough, it's nice to be able to find completely empty
686 * grids.
687 */
688 memset(soln2, 0, a);
689 ret = solver(w, clues, soln2, diff);
690 if (ret > diff)
691 continue;
692 }
693
694 for (i = 0; i < a; i++)
695 order[i] = i;
696 shuffle(order, a, sizeof(*order), rs);
697 for (i = 0; i < a; i++) {
698 int j = order[i];
699
700 memcpy(soln2, grid, a);
701 soln2[j] = 0;
702 ret = solver(w, clues, soln2, diff);
703 if (ret <= diff)
704 grid[j] = 0;
705 }
706
707 if (diff > DIFF_EASY) { /* leave all clues on Easy mode */
708 for (i = 0; i < 4*w; i++)
709 order[i] = i;
710 shuffle(order, 4*w, sizeof(*order), rs);
711 for (i = 0; i < 4*w; i++) {
712 int j = order[i];
713 int clue = clues[j];
714
715 memcpy(soln2, grid, a);
716 clues[j] = 0;
717 ret = solver(w, clues, soln2, diff);
718 if (ret > diff)
719 clues[j] = clue;
720 }
721 }
722
723 /*
724 * See if the game can be solved at the specified difficulty
725 * level, but not at the one below.
726 */
727 memcpy(soln2, grid, a);
728 ret = solver(w, clues, soln2, diff);
729 if (ret != diff)
730 continue; /* go round again */
731
732 /*
733 * We've got a usable puzzle!
734 */
735 break;
736 }
737
738 /*
739 * Encode the puzzle description.
740 */
741 desc = snewn(40*a, char);
742 p = desc;
743 for (i = 0; i < 4*w; i++) {
744 p += sprintf(p, "%s%.0d", i?"/":"", clues[i]);
745 }
746 for (i = 0; i < a; i++)
747 if (grid[i])
748 break;
749 if (i < a) {
750 int run = 0;
751
752 *p++ = ',';
753
754 for (i = 0; i <= a; i++) {
755 int n = (i < a ? grid[i] : -1);
756
757 if (!n)
758 run++;
759 else {
760 if (run) {
761 while (run > 0) {
762 int thisrun = min(run, 26);
763 *p++ = thisrun - 1 + 'a';
764 run -= thisrun;
765 }
766 } else {
767 /*
768 * If there's a number in the very top left or
769 * bottom right, there's no point putting an
770 * unnecessary _ before or after it.
771 */
772 if (i > 0 && n > 0)
773 *p++ = '_';
774 }
775 if (n > 0)
776 p += sprintf(p, "%d", n);
777 run = 0;
778 }
779 }
780 }
781 *p++ = '\0';
782 desc = sresize(desc, p - desc, char);
783
784 /*
785 * Encode the solution.
786 */
787 *aux = snewn(a+2, char);
788 (*aux)[0] = 'S';
789 for (i = 0; i < a; i++)
790 (*aux)[i+1] = '0' + soln[i];
791 (*aux)[a+1] = '\0';
792
793 sfree(grid);
794 sfree(clues);
795 sfree(soln);
796 sfree(soln2);
797 sfree(order);
798
799 return desc;
800 }
801
802 /* ----------------------------------------------------------------------
803 * Gameplay.
804 */
805
806 static char *validate_desc(game_params *params, char *desc)
807 {
808 int w = params->w, a = w*w;
809 const char *p = desc;
810 int i, clue;
811
812 /*
813 * Verify that the right number of clues are given, and that
814 * they're in range.
815 */
816 for (i = 0; i < 4*w; i++) {
817 if (!*p)
818 return "Too few clues for grid size";
819
820 if (i > 0) {
821 if (*p != '/')
822 return "Expected commas between clues";
823 p++;
824 }
825
826 if (isdigit((unsigned char)*p)) {
827 clue = atoi(p);
828 while (*p && isdigit((unsigned char)*p)) p++;
829
830 if (clue <= 0 || clue > w)
831 return "Clue number out of range";
832 }
833 }
834 if (*p == '/')
835 return "Too many clues for grid size";
836
837 if (*p == ',') {
838 /*
839 * Verify that the right amount of grid data is given, and
840 * that any grid elements provided are in range.
841 */
842 int squares = 0;
843
844 p++;
845 while (*p) {
846 int c = *p++;
847 if (c >= 'a' && c <= 'z') {
848 squares += c - 'a' + 1;
849 } else if (c == '_') {
850 /* do nothing */;
851 } else if (c > '0' && c <= '9') {
852 int val = atoi(p-1);
853 if (val < 1 || val > w)
854 return "Out-of-range number in grid description";
855 squares++;
856 while (*p && isdigit((unsigned char)*p)) p++;
857 } else
858 return "Invalid character in game description";
859 }
860
861 if (squares < a)
862 return "Not enough data to fill grid";
863
864 if (squares > a)
865 return "Too much data to fit in grid";
866 }
867
868 return NULL;
869 }
870
871 static game_state *new_game(midend *me, game_params *params, 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->pencil = snewn(a, int);
886
887 for (i = 0; i < a; i++) {
888 state->grid[i] = 0;
889 state->pencil[i] = 0;
890 }
891
892 memset(state->clues->immutable, 0, a);
893
894 for (i = 0; i < 4*w; i++) {
895 if (i > 0) {
896 assert(*p == '/');
897 p++;
898 }
899 if (*p && isdigit((unsigned char)*p)) {
900 state->clues->clues[i] = atoi(p);
901 while (*p && isdigit((unsigned char)*p)) p++;
902 } else
903 state->clues->clues[i] = 0;
904 }
905
906 if (*p == ',') {
907 int pos = 0;
908 p++;
909 while (*p) {
910 int c = *p++;
911 if (c >= 'a' && c <= 'z') {
912 pos += c - 'a' + 1;
913 } else if (c == '_') {
914 /* do nothing */;
915 } else if (c > '0' && c <= '9') {
916 int val = atoi(p-1);
917 assert(val >= 1 && val <= w);
918 assert(pos < a);
919 state->grid[pos] = state->clues->immutable[pos] = val;
920 pos++;
921 while (*p && isdigit((unsigned char)*p)) p++;
922 } else
923 assert(!"Corrupt game description");
924 }
925 assert(pos == a);
926 }
927 assert(!*p);
928
929 state->completed = state->cheated = FALSE;
930
931 return state;
932 }
933
934 static game_state *dup_game(game_state *state)
935 {
936 int w = state->par.w, a = w*w;
937 game_state *ret = snew(game_state);
938
939 ret->par = state->par; /* structure copy */
940
941 ret->clues = state->clues;
942 ret->clues->refcount++;
943
944 ret->grid = snewn(a, digit);
945 ret->pencil = snewn(a, int);
946 memcpy(ret->grid, state->grid, a*sizeof(digit));
947 memcpy(ret->pencil, state->pencil, a*sizeof(int));
948
949 ret->completed = state->completed;
950 ret->cheated = state->cheated;
951
952 return ret;
953 }
954
955 static void free_game(game_state *state)
956 {
957 sfree(state->grid);
958 sfree(state->pencil);
959 if (--state->clues->refcount <= 0) {
960 sfree(state->clues->immutable);
961 sfree(state->clues->clues);
962 sfree(state->clues);
963 }
964 sfree(state);
965 }
966
967 static char *solve_game(game_state *state, game_state *currstate,
968 char *aux, char **error)
969 {
970 int w = state->par.w, a = w*w;
971 int i, ret;
972 digit *soln;
973 char *out;
974
975 if (aux)
976 return dupstr(aux);
977
978 soln = snewn(a, digit);
979 memcpy(soln, state->clues->immutable, a);
980
981 ret = solver(w, state->clues->clues, soln, DIFFCOUNT-1);
982
983 if (ret == diff_impossible) {
984 *error = "No solution exists for this puzzle";
985 out = NULL;
986 } else if (ret == diff_ambiguous) {
987 *error = "Multiple solutions exist for this puzzle";
988 out = NULL;
989 } else {
990 out = snewn(a+2, char);
991 out[0] = 'S';
992 for (i = 0; i < a; i++)
993 out[i+1] = '0' + soln[i];
994 out[a+1] = '\0';
995 }
996
997 sfree(soln);
998 return out;
999 }
1000
1001 static int game_can_format_as_text_now(game_params *params)
1002 {
1003 return TRUE;
1004 }
1005
1006 static char *game_text_format(game_state *state)
1007 {
1008 int w = state->par.w /* , a = w*w */;
1009 char *ret;
1010 char *p;
1011 int x, y;
1012 int total;
1013
1014 /*
1015 * We have:
1016 * - a top clue row, consisting of three spaces, then w clue
1017 * digits with spaces between (total 2*w+3 chars including
1018 * newline)
1019 * - a blank line (one newline)
1020 * - w main rows, consisting of a left clue digit, two spaces,
1021 * w grid digits with spaces between, two spaces and a right
1022 * clue digit (total 2*w+6 chars each including newline)
1023 * - a blank line (one newline)
1024 * - a bottom clue row (same as top clue row)
1025 * - terminating NUL.
1026 *
1027 * Total size is therefore 2*(2*w+3) + 2 + w*(2*w+6) + 1
1028 * = 2w^2+10w+9.
1029 */
1030 total = 2*w*w + 10*w + 9;
1031 ret = snewn(total, char);
1032 p = ret;
1033
1034 /* Top clue row. */
1035 *p++ = ' '; *p++ = ' ';
1036 for (x = 0; x < w; x++) {
1037 *p++ = ' ';
1038 *p++ = (state->clues->clues[x] ? '0' + state->clues->clues[x] : ' ');
1039 }
1040 *p++ = '\n';
1041
1042 /* Blank line. */
1043 *p++ = '\n';
1044
1045 /* Main grid. */
1046 for (y = 0; y < w; y++) {
1047 *p++ = (state->clues->clues[y+2*w] ? '0' + state->clues->clues[y+2*w] :
1048 ' ');
1049 *p++ = ' ';
1050 for (x = 0; x < w; x++) {
1051 *p++ = ' ';
1052 *p++ = (state->grid[y*w+x] ? '0' + state->grid[y*w+x] : ' ');
1053 }
1054 *p++ = ' '; *p++ = ' ';
1055 *p++ = (state->clues->clues[y+3*w] ? '0' + state->clues->clues[y+3*w] :
1056 ' ');
1057 *p++ = '\n';
1058 }
1059
1060 /* Blank line. */
1061 *p++ = '\n';
1062
1063 /* Bottom clue row. */
1064 *p++ = ' '; *p++ = ' ';
1065 for (x = 0; x < w; x++) {
1066 *p++ = ' ';
1067 *p++ = (state->clues->clues[x+w] ? '0' + state->clues->clues[x+w] :
1068 ' ');
1069 }
1070 *p++ = '\n';
1071
1072 *p++ = '\0';
1073 assert(p == ret + total);
1074
1075 return ret;
1076 }
1077
1078 struct game_ui {
1079 /*
1080 * These are the coordinates of the currently highlighted
1081 * square on the grid, if hshow = 1.
1082 */
1083 int hx, hy;
1084 /*
1085 * This indicates whether the current highlight is a
1086 * pencil-mark one or a real one.
1087 */
1088 int hpencil;
1089 /*
1090 * This indicates whether or not we're showing the highlight
1091 * (used to be hx = hy = -1); important so that when we're
1092 * using the cursor keys it doesn't keep coming back at a
1093 * fixed position. When hshow = 1, pressing a valid number
1094 * or letter key or Space will enter that number or letter in the grid.
1095 */
1096 int hshow;
1097 /*
1098 * This indicates whether we're using the highlight as a cursor;
1099 * it means that it doesn't vanish on a keypress, and that it is
1100 * allowed on immutable squares.
1101 */
1102 int hcursor;
1103 };
1104
1105 static game_ui *new_ui(game_state *state)
1106 {
1107 game_ui *ui = snew(game_ui);
1108
1109 ui->hx = ui->hy = 0;
1110 ui->hpencil = ui->hshow = ui->hcursor = 0;
1111
1112 return ui;
1113 }
1114
1115 static void free_ui(game_ui *ui)
1116 {
1117 sfree(ui);
1118 }
1119
1120 static char *encode_ui(game_ui *ui)
1121 {
1122 return NULL;
1123 }
1124
1125 static void decode_ui(game_ui *ui, char *encoding)
1126 {
1127 }
1128
1129 static void game_changed_state(game_ui *ui, game_state *oldstate,
1130 game_state *newstate)
1131 {
1132 int w = newstate->par.w;
1133 /*
1134 * We prevent pencil-mode highlighting of a filled square, unless
1135 * we're using the cursor keys. So if the user has just filled in
1136 * a square which we had a pencil-mode highlight in (by Undo, or
1137 * by Redo, or by Solve), then we cancel the highlight.
1138 */
1139 if (ui->hshow && ui->hpencil && !ui->hcursor &&
1140 newstate->grid[ui->hy * w + ui->hx] != 0) {
1141 ui->hshow = 0;
1142 }
1143 }
1144
1145 #define PREFERRED_TILESIZE 48
1146 #define TILESIZE (ds->tilesize)
1147 #define BORDER (TILESIZE * 9 / 8)
1148 #define COORD(x) ((x)*TILESIZE + BORDER)
1149 #define FROMCOORD(x) (((x)+(TILESIZE-BORDER)) / TILESIZE - 1)
1150
1151 #define FLASH_TIME 0.4F
1152
1153 #define DF_PENCIL_SHIFT 16
1154 #define DF_ERROR 0x8000
1155 #define DF_HIGHLIGHT 0x4000
1156 #define DF_HIGHLIGHT_PENCIL 0x2000
1157 #define DF_IMMUTABLE 0x1000
1158 #define DF_PLAYAREA 0x0800
1159 #define DF_DIGIT_MASK 0x00FF
1160
1161 struct game_drawstate {
1162 int tilesize;
1163 int started;
1164 long *tiles;
1165 int *errtmp;
1166 };
1167
1168 static int check_errors(game_state *state, int *errors)
1169 {
1170 int w = state->par.w /*, a = w*w */;
1171 int W = w+2, A = W*W; /* the errors array is (w+2) square */
1172 int *clues = state->clues->clues;
1173 digit *grid = state->grid;
1174 int i, x, y, errs = FALSE;
1175 int tmp[32];
1176
1177 assert(w < lenof(tmp));
1178
1179 if (errors)
1180 for (i = 0; i < A; i++)
1181 errors[i] = 0;
1182
1183 for (y = 0; y < w; y++) {
1184 unsigned long mask = 0, errmask = 0;
1185 for (x = 0; x < w; x++) {
1186 unsigned long bit = 1UL << grid[y*w+x];
1187 errmask |= (mask & bit);
1188 mask |= bit;
1189 }
1190
1191 if (mask != (1L << (w+1)) - (1L << 1)) {
1192 errs = TRUE;
1193 errmask &= ~1UL;
1194 if (errors) {
1195 for (x = 0; x < w; x++)
1196 if (errmask & (1UL << grid[y*w+x]))
1197 errors[(y+1)*W+(x+1)] = TRUE;
1198 }
1199 }
1200 }
1201
1202 for (x = 0; x < w; x++) {
1203 unsigned long mask = 0, errmask = 0;
1204 for (y = 0; y < w; y++) {
1205 unsigned long bit = 1UL << grid[y*w+x];
1206 errmask |= (mask & bit);
1207 mask |= bit;
1208 }
1209
1210 if (mask != (1 << (w+1)) - (1 << 1)) {
1211 errs = TRUE;
1212 errmask &= ~1UL;
1213 if (errors) {
1214 for (y = 0; y < w; y++)
1215 if (errmask & (1UL << grid[y*w+x]))
1216 errors[(y+1)*W+(x+1)] = TRUE;
1217 }
1218 }
1219 }
1220
1221 for (i = 0; i < 4*w; i++) {
1222 int start, step, j, k, n, best;
1223 STARTSTEP(start, step, i, w);
1224
1225 if (!clues[i])
1226 continue;
1227
1228 best = n = 0;
1229 k = 0;
1230 for (j = 0; j < w; j++) {
1231 int number = grid[start+j*step];
1232 if (!number)
1233 break; /* can't tell what happens next */
1234 if (number > best) {
1235 best = number;
1236 n++;
1237 }
1238 }
1239
1240 if (n > clues[i] || (j == w && n < clues[i])) {
1241 if (errors) {
1242 int x, y;
1243 CLUEPOS(x, y, i, w);
1244 errors[(y+1)*W+(x+1)] = TRUE;
1245 }
1246 errs = TRUE;
1247 }
1248 }
1249
1250 return errs;
1251 }
1252
1253 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
1254 int x, int y, int button)
1255 {
1256 int w = state->par.w;
1257 int tx, ty;
1258 char buf[80];
1259
1260 button &= ~MOD_MASK;
1261
1262 tx = FROMCOORD(x);
1263 ty = FROMCOORD(y);
1264
1265 if (tx >= 0 && tx < w && ty >= 0 && ty < w) {
1266 if (button == LEFT_BUTTON) {
1267 if (tx == ui->hx && ty == ui->hy &&
1268 ui->hshow && ui->hpencil == 0) {
1269 ui->hshow = 0;
1270 } else {
1271 ui->hx = tx;
1272 ui->hy = ty;
1273 ui->hshow = !state->clues->immutable[ty*w+tx];
1274 ui->hpencil = 0;
1275 }
1276 ui->hcursor = 0;
1277 return ""; /* UI activity occurred */
1278 }
1279 if (button == RIGHT_BUTTON) {
1280 /*
1281 * Pencil-mode highlighting for non filled squares.
1282 */
1283 if (state->grid[ty*w+tx] == 0) {
1284 if (tx == ui->hx && ty == ui->hy &&
1285 ui->hshow && ui->hpencil) {
1286 ui->hshow = 0;
1287 } else {
1288 ui->hpencil = 1;
1289 ui->hx = tx;
1290 ui->hy = ty;
1291 ui->hshow = 1;
1292 }
1293 } else {
1294 ui->hshow = 0;
1295 }
1296 ui->hcursor = 0;
1297 return ""; /* UI activity occurred */
1298 }
1299 }
1300 if (IS_CURSOR_MOVE(button)) {
1301 move_cursor(button, &ui->hx, &ui->hy, w, w, 0);
1302 ui->hshow = ui->hcursor = 1;
1303 return "";
1304 }
1305 if (ui->hshow &&
1306 (button == CURSOR_SELECT)) {
1307 ui->hpencil = 1 - ui->hpencil;
1308 ui->hcursor = 1;
1309 return "";
1310 }
1311
1312 if (ui->hshow &&
1313 ((button >= '0' && button <= '9' && button - '0' <= w) ||
1314 button == CURSOR_SELECT2 || button == '\b')) {
1315 int n = button - '0';
1316 if (button == CURSOR_SELECT2 || button == '\b')
1317 n = 0;
1318
1319 /*
1320 * Can't make pencil marks in a filled square. This can only
1321 * become highlighted if we're using cursor keys.
1322 */
1323 if (ui->hpencil && state->grid[ui->hy*w+ui->hx])
1324 return NULL;
1325
1326 /*
1327 * Can't do anything to an immutable square.
1328 */
1329 if (state->clues->immutable[ui->hy*w+ui->hx])
1330 return NULL;
1331
1332 sprintf(buf, "%c%d,%d,%d",
1333 (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n);
1334
1335 if (!ui->hcursor) ui->hshow = 0;
1336
1337 return dupstr(buf);
1338 }
1339
1340 if (button == 'M' || button == 'm')
1341 return dupstr("M");
1342
1343 return NULL;
1344 }
1345
1346 static game_state *execute_move(game_state *from, char *move)
1347 {
1348 int w = from->par.w, a = w*w;
1349 game_state *ret;
1350 int x, y, i, n;
1351
1352 if (move[0] == 'S') {
1353 ret = dup_game(from);
1354 ret->completed = ret->cheated = TRUE;
1355
1356 for (i = 0; i < a; i++) {
1357 if (move[i+1] < '1' || move[i+1] > '0'+w) {
1358 free_game(ret);
1359 return NULL;
1360 }
1361 ret->grid[i] = move[i+1] - '0';
1362 ret->pencil[i] = 0;
1363 }
1364
1365 if (move[a+1] != '\0') {
1366 free_game(ret);
1367 return NULL;
1368 }
1369
1370 return ret;
1371 } else if ((move[0] == 'P' || move[0] == 'R') &&
1372 sscanf(move+1, "%d,%d,%d", &x, &y, &n) == 3 &&
1373 x >= 0 && x < w && y >= 0 && y < w && n >= 0 && n <= w) {
1374 if (from->clues->immutable[y*w+x])
1375 return NULL;
1376
1377 ret = dup_game(from);
1378 if (move[0] == 'P' && n > 0) {
1379 ret->pencil[y*w+x] ^= 1L << n;
1380 } else {
1381 ret->grid[y*w+x] = n;
1382 ret->pencil[y*w+x] = 0;
1383
1384 if (!ret->completed && !check_errors(ret, NULL))
1385 ret->completed = TRUE;
1386 }
1387 return ret;
1388 } else if (move[0] == 'M') {
1389 /*
1390 * Fill in absolutely all pencil marks everywhere. (I
1391 * wouldn't use this for actual play, but it's a handy
1392 * starting point when following through a set of
1393 * diagnostics output by the standalone solver.)
1394 */
1395 ret = dup_game(from);
1396 for (i = 0; i < a; i++) {
1397 if (!ret->grid[i])
1398 ret->pencil[i] = (1L << (w+1)) - (1L << 1);
1399 }
1400 return ret;
1401 } else
1402 return NULL; /* couldn't parse move string */
1403 }
1404
1405 /* ----------------------------------------------------------------------
1406 * Drawing routines.
1407 */
1408
1409 #define SIZE(w) ((w) * TILESIZE + 2*BORDER)
1410
1411 static void game_compute_size(game_params *params, int tilesize,
1412 int *x, int *y)
1413 {
1414 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1415 struct { int tilesize; } ads, *ds = &ads;
1416 ads.tilesize = tilesize;
1417
1418 *x = *y = SIZE(params->w);
1419 }
1420
1421 static void game_set_size(drawing *dr, game_drawstate *ds,
1422 game_params *params, int tilesize)
1423 {
1424 ds->tilesize = tilesize;
1425 }
1426
1427 static float *game_colours(frontend *fe, int *ncolours)
1428 {
1429 float *ret = snewn(3 * NCOLOURS, float);
1430
1431 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1432
1433 ret[COL_GRID * 3 + 0] = 0.0F;
1434 ret[COL_GRID * 3 + 1] = 0.0F;
1435 ret[COL_GRID * 3 + 2] = 0.0F;
1436
1437 ret[COL_USER * 3 + 0] = 0.0F;
1438 ret[COL_USER * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1];
1439 ret[COL_USER * 3 + 2] = 0.0F;
1440
1441 ret[COL_HIGHLIGHT * 3 + 0] = 0.78F * ret[COL_BACKGROUND * 3 + 0];
1442 ret[COL_HIGHLIGHT * 3 + 1] = 0.78F * ret[COL_BACKGROUND * 3 + 1];
1443 ret[COL_HIGHLIGHT * 3 + 2] = 0.78F * ret[COL_BACKGROUND * 3 + 2];
1444
1445 ret[COL_ERROR * 3 + 0] = 1.0F;
1446 ret[COL_ERROR * 3 + 1] = 0.0F;
1447 ret[COL_ERROR * 3 + 2] = 0.0F;
1448
1449 ret[COL_PENCIL * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1450 ret[COL_PENCIL * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1451 ret[COL_PENCIL * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
1452
1453 *ncolours = NCOLOURS;
1454 return ret;
1455 }
1456
1457 static const char *const minus_signs[] = { "\xE2\x88\x92", "-" };
1458 static const char *const times_signs[] = { "\xC3\x97", "*" };
1459 static const char *const divide_signs[] = { "\xC3\xB7", "/" };
1460
1461 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
1462 {
1463 int w = state->par.w /*, a = w*w */;
1464 struct game_drawstate *ds = snew(struct game_drawstate);
1465 int i;
1466
1467 ds->tilesize = 0;
1468 ds->started = FALSE;
1469 ds->tiles = snewn((w+2)*(w+2), long);
1470 for (i = 0; i < (w+2)*(w+2); i++)
1471 ds->tiles[i] = -1;
1472 ds->errtmp = snewn((w+2)*(w+2), int);
1473
1474 return ds;
1475 }
1476
1477 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1478 {
1479 sfree(ds->errtmp);
1480 sfree(ds->tiles);
1481 sfree(ds);
1482 }
1483
1484 static void draw_tile(drawing *dr, game_drawstate *ds, struct clues *clues,
1485 int x, int y, long tile)
1486 {
1487 int w = clues->w /* , a = w*w */;
1488 int tx, ty, tw, th;
1489 int cx, cy, cw, ch;
1490 char str[64];
1491
1492 tx = BORDER + x * TILESIZE + 1;
1493 ty = BORDER + y * TILESIZE + 1;
1494
1495 cx = tx;
1496 cy = ty;
1497 cw = tw = TILESIZE-1;
1498 ch = th = TILESIZE-1;
1499
1500 clip(dr, cx, cy, cw, ch);
1501
1502 /* background needs erasing */
1503 draw_rect(dr, cx, cy, cw, ch,
1504 (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND);
1505
1506 /* pencil-mode highlight */
1507 if (tile & DF_HIGHLIGHT_PENCIL) {
1508 int coords[6];
1509 coords[0] = cx;
1510 coords[1] = cy;
1511 coords[2] = cx+cw/2;
1512 coords[3] = cy;
1513 coords[4] = cx;
1514 coords[5] = cy+ch/2;
1515 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
1516 }
1517
1518 /* new number needs drawing? */
1519 if (tile & DF_DIGIT_MASK) {
1520 str[1] = '\0';
1521 str[0] = (tile & DF_DIGIT_MASK) + '0';
1522 draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2, FONT_VARIABLE,
1523 (tile & DF_PLAYAREA ? TILESIZE/2 : TILESIZE*2/5),
1524 ALIGN_VCENTRE | ALIGN_HCENTRE,
1525 (tile & DF_ERROR) ? COL_ERROR :
1526 (x < 0 || y < 0 || x >= w || y >= w) ? COL_GRID :
1527 (tile & DF_IMMUTABLE) ? COL_GRID : COL_USER, str);
1528 } else {
1529 int i, j, npencil;
1530 int pl, pr, pt, pb;
1531 float bestsize;
1532 int pw, ph, minph, pbest, fontsize;
1533
1534 /* Count the pencil marks required. */
1535 for (i = 1, npencil = 0; i <= w; i++)
1536 if (tile & (1L << (i + DF_PENCIL_SHIFT)))
1537 npencil++;
1538 if (npencil) {
1539
1540 minph = 2;
1541
1542 /*
1543 * Determine the bounding rectangle within which we're going
1544 * to put the pencil marks.
1545 */
1546 /* Start with the whole square */
1547 pl = tx;
1548 pr = pl + TILESIZE;
1549 pt = ty;
1550 pb = pt + TILESIZE;
1551
1552 /*
1553 * We arrange our pencil marks in a grid layout, with
1554 * the number of rows and columns adjusted to allow the
1555 * maximum font size.
1556 *
1557 * So now we work out what the grid size ought to be.
1558 */
1559 bestsize = 0.0;
1560 pbest = 0;
1561 /* Minimum */
1562 for (pw = 3; pw < max(npencil,4); pw++) {
1563 float fw, fh, fs;
1564
1565 ph = (npencil + pw - 1) / pw;
1566 ph = max(ph, minph);
1567 fw = (pr - pl) / (float)pw;
1568 fh = (pb - pt) / (float)ph;
1569 fs = min(fw, fh);
1570 if (fs > bestsize) {
1571 bestsize = fs;
1572 pbest = pw;
1573 }
1574 }
1575 assert(pbest > 0);
1576 pw = pbest;
1577 ph = (npencil + pw - 1) / pw;
1578 ph = max(ph, minph);
1579
1580 /*
1581 * Now we've got our grid dimensions, work out the pixel
1582 * size of a grid element, and round it to the nearest
1583 * pixel. (We don't want rounding errors to make the
1584 * grid look uneven at low pixel sizes.)
1585 */
1586 fontsize = min((pr - pl) / pw, (pb - pt) / ph);
1587
1588 /*
1589 * Centre the resulting figure in the square.
1590 */
1591 pl = tx + (TILESIZE - fontsize * pw) / 2;
1592 pt = ty + (TILESIZE - fontsize * ph) / 2;
1593
1594 /*
1595 * Now actually draw the pencil marks.
1596 */
1597 for (i = 1, j = 0; i <= w; i++)
1598 if (tile & (1L << (i + DF_PENCIL_SHIFT))) {
1599 int dx = j % pw, dy = j / pw;
1600
1601 str[1] = '\0';
1602 str[0] = i + '0';
1603 draw_text(dr, pl + fontsize * (2*dx+1) / 2,
1604 pt + fontsize * (2*dy+1) / 2,
1605 FONT_VARIABLE, fontsize,
1606 ALIGN_VCENTRE | ALIGN_HCENTRE, COL_PENCIL, str);
1607 j++;
1608 }
1609 }
1610 }
1611
1612 unclip(dr);
1613
1614 draw_update(dr, cx, cy, cw, ch);
1615 }
1616
1617 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
1618 game_state *state, int dir, game_ui *ui,
1619 float animtime, float flashtime)
1620 {
1621 int w = state->par.w /*, a = w*w */;
1622 int i, x, y;
1623
1624 if (!ds->started) {
1625 /*
1626 * The initial contents of the window are not guaranteed and
1627 * can vary with front ends. To be on the safe side, all
1628 * games should start by drawing a big background-colour
1629 * rectangle covering the whole window.
1630 */
1631 draw_rect(dr, 0, 0, SIZE(w), SIZE(w), COL_BACKGROUND);
1632
1633 /*
1634 * Big containing rectangle.
1635 */
1636 draw_rect(dr, COORD(0), COORD(0),
1637 w*TILESIZE+1, w*TILESIZE+1,
1638 COL_GRID);
1639
1640 draw_update(dr, 0, 0, SIZE(w), SIZE(w));
1641
1642 ds->started = TRUE;
1643 }
1644
1645 check_errors(state, ds->errtmp);
1646
1647 /*
1648 * Draw the clues.
1649 */
1650 for (i = 0; i < 4*w; i++) {
1651 long tile = state->clues->clues[i];
1652
1653 if (!tile)
1654 continue;
1655
1656 CLUEPOS(x, y, i, w);
1657
1658 if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1659 tile |= DF_ERROR;
1660
1661 if (ds->tiles[(y+1)*(w+2)+(x+1)] != tile) {
1662 ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1663 draw_tile(dr, ds, state->clues, x, y, tile);
1664 }
1665 }
1666
1667 /*
1668 * Draw the main grid.
1669 */
1670 for (y = 0; y < w; y++) {
1671 for (x = 0; x < w; x++) {
1672 long tile = DF_PLAYAREA;
1673
1674 if (state->grid[y*w+x])
1675 tile |= state->grid[y*w+x];
1676 else
1677 tile |= (long)state->pencil[y*w+x] << DF_PENCIL_SHIFT;
1678
1679 if (ui->hshow && ui->hx == x && ui->hy == y)
1680 tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT);
1681
1682 if (state->clues->immutable[y*w+x])
1683 tile |= DF_IMMUTABLE;
1684
1685 if (flashtime > 0 &&
1686 (flashtime <= FLASH_TIME/3 ||
1687 flashtime >= FLASH_TIME*2/3))
1688 tile |= DF_HIGHLIGHT; /* completion flash */
1689
1690 if (ds->errtmp[(y+1)*(w+2)+(x+1)])
1691 tile |= DF_ERROR;
1692
1693 if (ds->tiles[(y+1)*(w+2)+(x+1)] != tile) {
1694 ds->tiles[(y+1)*(w+2)+(x+1)] = tile;
1695 draw_tile(dr, ds, state->clues, x, y, tile);
1696 }
1697 }
1698 }
1699 }
1700
1701 static float game_anim_length(game_state *oldstate, game_state *newstate,
1702 int dir, game_ui *ui)
1703 {
1704 return 0.0F;
1705 }
1706
1707 static float game_flash_length(game_state *oldstate, game_state *newstate,
1708 int dir, game_ui *ui)
1709 {
1710 if (!oldstate->completed && newstate->completed &&
1711 !oldstate->cheated && !newstate->cheated)
1712 return FLASH_TIME;
1713 return 0.0F;
1714 }
1715
1716 static int game_timing_state(game_state *state, game_ui *ui)
1717 {
1718 if (state->completed)
1719 return FALSE;
1720 return TRUE;
1721 }
1722
1723 static void game_print_size(game_params *params, float *x, float *y)
1724 {
1725 int pw, ph;
1726
1727 /*
1728 * We use 9mm squares by default, like Solo.
1729 */
1730 game_compute_size(params, 900, &pw, &ph);
1731 *x = pw / 100.0F;
1732 *y = ph / 100.0F;
1733 }
1734
1735 static void game_print(drawing *dr, game_state *state, int tilesize)
1736 {
1737 int w = state->par.w;
1738 int ink = print_mono_colour(dr, 0);
1739 int i, x, y;
1740
1741 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1742 game_drawstate ads, *ds = &ads;
1743 game_set_size(dr, ds, NULL, tilesize);
1744
1745 /*
1746 * Border.
1747 */
1748 print_line_width(dr, 3 * TILESIZE / 40);
1749 draw_rect_outline(dr, BORDER, BORDER, w*TILESIZE, w*TILESIZE, ink);
1750
1751 /*
1752 * Main grid.
1753 */
1754 for (x = 1; x < w; x++) {
1755 print_line_width(dr, TILESIZE / 40);
1756 draw_line(dr, BORDER+x*TILESIZE, BORDER,
1757 BORDER+x*TILESIZE, BORDER+w*TILESIZE, ink);
1758 }
1759 for (y = 1; y < w; y++) {
1760 print_line_width(dr, TILESIZE / 40);
1761 draw_line(dr, BORDER, BORDER+y*TILESIZE,
1762 BORDER+w*TILESIZE, BORDER+y*TILESIZE, ink);
1763 }
1764
1765 /*
1766 * Clues.
1767 */
1768 for (i = 0; i < 4*w; i++) {
1769 char str[128];
1770
1771 if (!state->clues->clues[i])
1772 continue;
1773
1774 CLUEPOS(x, y, i, w);
1775
1776 sprintf (str, "%d", state->clues->clues[i]);
1777
1778 draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
1779 BORDER + y*TILESIZE + TILESIZE/2,
1780 FONT_VARIABLE, TILESIZE/2,
1781 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
1782 }
1783
1784 /*
1785 * Numbers for the solution, if any.
1786 */
1787 for (y = 0; y < w; y++)
1788 for (x = 0; x < w; x++)
1789 if (state->grid[y*w+x]) {
1790 char str[2];
1791 str[1] = '\0';
1792 str[0] = state->grid[y*w+x] + '0';
1793 draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
1794 BORDER + y*TILESIZE + TILESIZE/2,
1795 FONT_VARIABLE, TILESIZE/2,
1796 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
1797 }
1798 }
1799
1800 #ifdef COMBINED
1801 #define thegame towers
1802 #endif
1803
1804 const struct game thegame = {
1805 "Towers", "games.towers", "towers",
1806 default_params,
1807 game_fetch_preset,
1808 decode_params,
1809 encode_params,
1810 free_params,
1811 dup_params,
1812 TRUE, game_configure, custom_params,
1813 validate_params,
1814 new_game_desc,
1815 validate_desc,
1816 new_game,
1817 dup_game,
1818 free_game,
1819 TRUE, solve_game,
1820 TRUE, game_can_format_as_text_now, game_text_format,
1821 new_ui,
1822 free_ui,
1823 encode_ui,
1824 decode_ui,
1825 game_changed_state,
1826 interpret_move,
1827 execute_move,
1828 PREFERRED_TILESIZE, game_compute_size, game_set_size,
1829 game_colours,
1830 game_new_drawstate,
1831 game_free_drawstate,
1832 game_redraw,
1833 game_anim_length,
1834 game_flash_length,
1835 TRUE, FALSE, game_print_size, game_print,
1836 FALSE, /* wants_statusbar */
1837 FALSE, game_timing_state,
1838 REQUIRE_RBUTTON | REQUIRE_NUMPAD, /* flags */
1839 };
1840
1841 #ifdef STANDALONE_SOLVER
1842
1843 #include <stdarg.h>
1844
1845 int main(int argc, char **argv)
1846 {
1847 game_params *p;
1848 game_state *s;
1849 char *id = NULL, *desc, *err;
1850 int grade = FALSE;
1851 int ret, diff, really_show_working = FALSE;
1852
1853 while (--argc > 0) {
1854 char *p = *++argv;
1855 if (!strcmp(p, "-v")) {
1856 really_show_working = TRUE;
1857 } else if (!strcmp(p, "-g")) {
1858 grade = TRUE;
1859 } else if (*p == '-') {
1860 fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
1861 return 1;
1862 } else {
1863 id = p;
1864 }
1865 }
1866
1867 if (!id) {
1868 fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]);
1869 return 1;
1870 }
1871
1872 desc = strchr(id, ':');
1873 if (!desc) {
1874 fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
1875 return 1;
1876 }
1877 *desc++ = '\0';
1878
1879 p = default_params();
1880 decode_params(p, id);
1881 err = validate_desc(p, desc);
1882 if (err) {
1883 fprintf(stderr, "%s: %s\n", argv[0], err);
1884 return 1;
1885 }
1886 s = new_game(NULL, p, desc);
1887
1888 /*
1889 * When solving an Easy puzzle, we don't want to bother the
1890 * user with Hard-level deductions. For this reason, we grade
1891 * the puzzle internally before doing anything else.
1892 */
1893 ret = -1; /* placate optimiser */
1894 solver_show_working = FALSE;
1895 for (diff = 0; diff < DIFFCOUNT; diff++) {
1896 memcpy(s->grid, s->clues->immutable, p->w * p->w);
1897 ret = solver(p->w, s->clues->clues, s->grid, diff);
1898 if (ret <= diff)
1899 break;
1900 }
1901
1902 if (diff == DIFFCOUNT) {
1903 if (grade)
1904 printf("Difficulty rating: ambiguous\n");
1905 else
1906 printf("Unable to find a unique solution\n");
1907 } else {
1908 if (grade) {
1909 if (ret == diff_impossible)
1910 printf("Difficulty rating: impossible (no solution exists)\n");
1911 else
1912 printf("Difficulty rating: %s\n", towers_diffnames[ret]);
1913 } else {
1914 solver_show_working = really_show_working;
1915 memcpy(s->grid, s->clues->immutable, p->w * p->w);
1916 ret = solver(p->w, s->clues->clues, s->grid, diff);
1917 if (ret != diff)
1918 printf("Puzzle is inconsistent\n");
1919 else
1920 fputs(game_text_format(s), stdout);
1921 }
1922 }
1923
1924 return 0;
1925 }
1926
1927 #endif
1928
1929 /* vim: set shiftwidth=4 tabstop=8: */