2 * group.c: a Latin-square puzzle, but played with groups' Cayley
3 * tables. That is, you are given a Cayley table of a group with
4 * most elements blank and a few clues, and you must fill it in
5 * so as to preserve the group axioms.
7 * This is a perfectly playable and fully working puzzle, but I'm
8 * leaving it for the moment in the 'unfinished' directory because
9 * it's just too esoteric (not to mention _hard_) for me to be
10 * comfortable presenting it to the general public as something they
11 * might (implicitly) actually want to play.
15 * - more solver techniques?
16 * * Inverses: once we know that gh = e, we can immediately
17 * deduce hg = e as well; then for any gx=y we can deduce
18 * hy=x, and for any xg=y we have yh=x.
19 * * Hard-mode associativity: we currently deduce based on
20 * definite numbers in the grid, but we could also winnow
21 * based on _possible_ numbers.
22 * * My overambitious original thoughts included wondering if we
23 * could infer that there must be elements of certain orders
24 * (e.g. a group of order divisible by 5 must contain an
25 * element of order 5), but I think in fact this is probably
28 * - a mode which shuffles the identity element into the mix
29 * instead of keeping it clearly shown for you?
30 * * shuffle more fully during table generation
31 * * start clue removal by clearing the identity row and column
32 * completely, or else it'll be totally obvious where it is
33 * * have to print the group elements outside the grid
34 * * new_ui should start the cursor at 0,0 not 1,1, and cursor
35 * should not be constrained to x,y >= 1
36 * * get rid of the COL_IDENTITY highlights
37 * * will we need more checks in check_errors?
51 * Difficulty levels. I do some macro ickery here to ensure that my
52 * enum and the various forms of my name list always match up.
55 A(TRIVIAL,Trivial,NULL,t) \
56 A(NORMAL,Normal,solver_normal,n) \
58 A(EXTREME,Extreme,NULL,x) \
59 A(UNREASONABLE,Unreasonable,NULL,u)
60 #define ENUM(upper,title,func,lower) DIFF_ ## upper,
61 #define TITLE(upper,title,func,lower) #title,
62 #define ENCODE(upper,title,func,lower) #lower
63 #define CONFIG(upper,title,func,lower) ":" #title
64 enum { DIFFLIST(ENUM
) DIFFCOUNT
};
65 static char const *const group_diffnames
[] = { DIFFLIST(TITLE
) };
66 static char const group_diffchars
[] = DIFFLIST(ENCODE
);
67 #define DIFFCONFIG DIFFLIST(CONFIG)
80 #define FROMCHAR(c) ((c)>='0'&&(c)<='9' ? (c)-'0' : \
81 (c)>='A'&&(c)<='Z' ? (c)-'A'+10 : (c)-'a'+10)
82 #define ISCHAR(c) (((c)>='0'&&(c)<='9') || \
83 ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
84 #define TOCHAR(c) ((c)>=10 ? (c)-10+'a' : (c)+'0')
93 unsigned char *immutable
;
94 int *pencil
; /* bitmaps using bits 1<<1..1<<n */
95 int completed
, cheated
;
98 static game_params
*default_params(void)
100 game_params
*ret
= snew(game_params
);
103 ret
->diff
= DIFF_NORMAL
;
108 const static struct game_params group_presets
[] = {
113 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
118 if (i
< 0 || i
>= lenof(group_presets
))
121 ret
= snew(game_params
);
122 *ret
= group_presets
[i
]; /* structure copy */
124 sprintf(buf
, "%dx%d %s", ret
->w
, ret
->w
, group_diffnames
[ret
->diff
]);
131 static void free_params(game_params
*params
)
136 static game_params
*dup_params(game_params
*params
)
138 game_params
*ret
= snew(game_params
);
139 *ret
= *params
; /* structure copy */
143 static void decode_params(game_params
*params
, char const *string
)
145 char const *p
= string
;
148 while (*p
&& isdigit((unsigned char)*p
)) p
++;
153 params
->diff
= DIFFCOUNT
+1; /* ...which is invalid */
155 for (i
= 0; i
< DIFFCOUNT
; i
++) {
156 if (*p
== group_diffchars
[i
])
164 static char *encode_params(game_params
*params
, int full
)
168 sprintf(ret
, "%d", params
->w
);
170 sprintf(ret
+ strlen(ret
), "d%c", group_diffchars
[params
->diff
]);
175 static config_item
*game_configure(game_params
*params
)
180 ret
= snewn(3, config_item
);
182 ret
[0].name
= "Grid size";
183 ret
[0].type
= C_STRING
;
184 sprintf(buf
, "%d", params
->w
);
185 ret
[0].sval
= dupstr(buf
);
188 ret
[1].name
= "Difficulty";
189 ret
[1].type
= C_CHOICES
;
190 ret
[1].sval
= DIFFCONFIG
;
191 ret
[1].ival
= params
->diff
;
201 static game_params
*custom_params(config_item
*cfg
)
203 game_params
*ret
= snew(game_params
);
205 ret
->w
= atoi(cfg
[0].sval
);
206 ret
->diff
= cfg
[1].ival
;
211 static char *validate_params(game_params
*params
, int full
)
213 if (params
->w
< 3 || params
->w
> 31)
214 return "Grid size must be between 3 and 31";
215 if (params
->diff
>= DIFFCOUNT
)
216 return "Unknown difficulty rating";
220 /* ----------------------------------------------------------------------
224 static int solver_normal(struct latin_solver
*solver
, void *vctx
)
227 digit
*grid
= solver
->grid
;
231 * Deduce using associativity: (ab)c = a(bc).
233 * So we pick any a,b,c we like; then if we know ab, bc, and
234 * (ab)c we can fill in a(bc).
236 for (i
= 1; i
< w
; i
++)
237 for (j
= 1; j
< w
; j
++)
238 for (k
= 1; k
< w
; k
++) {
239 if (!grid
[i
*w
+j
] || !grid
[j
*w
+k
])
241 if (grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
242 !grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
243 int x
= grid
[j
*w
+k
]-1, y
= i
;
244 int n
= grid
[(grid
[i
*w
+j
]-1)*w
+k
];
245 #ifdef STANDALONE_SOLVER
246 if (solver_show_working
) {
247 printf("%*sassociativity on %d,%d,%d: %d*%d = %d*%d\n",
248 solver_recurse_depth
*4, "",
250 grid
[i
*w
+j
], k
+1, i
+1, grid
[j
*w
+k
]);
251 printf("%*s placing %d at (%d,%d)\n",
252 solver_recurse_depth
*4, "",
256 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
257 latin_solver_place(solver
, x
, y
, n
);
260 #ifdef STANDALONE_SOLVER
261 if (solver_show_working
)
262 printf("%*s contradiction!\n",
263 solver_recurse_depth
*4, "");
268 if (!grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
269 grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
270 int x
= k
, y
= grid
[i
*w
+j
]-1;
271 int n
= grid
[i
*w
+(grid
[j
*w
+k
]-1)];
272 #ifdef STANDALONE_SOLVER
273 if (solver_show_working
) {
274 printf("%*sassociativity on %d,%d,%d: %d*%d = %d*%d\n",
275 solver_recurse_depth
*4, "",
277 grid
[i
*w
+j
], k
+1, i
+1, grid
[j
*w
+k
]);
278 printf("%*s placing %d at (%d,%d)\n",
279 solver_recurse_depth
*4, "",
283 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
284 latin_solver_place(solver
, x
, y
, n
);
287 #ifdef STANDALONE_SOLVER
288 if (solver_show_working
)
289 printf("%*s contradiction!\n",
290 solver_recurse_depth
*4, "");
300 #define SOLVER(upper,title,func,lower) func,
301 static usersolver_t
const group_solvers
[] = { DIFFLIST(SOLVER
) };
303 static int solver(int w
, digit
*grid
, int maxdiff
)
307 ret
= latin_solver(grid
, w
, maxdiff
,
308 DIFF_TRIVIAL
, DIFF_HARD
, DIFF_EXTREME
,
309 DIFF_EXTREME
, DIFF_UNREASONABLE
,
310 group_solvers
, NULL
, NULL
, NULL
);
315 /* ----------------------------------------------------------------------
319 static char *encode_grid(char *desc
, digit
*grid
, int area
)
325 for (i
= 0; i
<= area
; i
++) {
326 int n
= (i
< area ? grid
[i
] : -1);
333 int c
= 'a' - 1 + run
;
337 run
-= c
- ('a' - 1);
341 * If there's a number in the very top left or
342 * bottom right, there's no point putting an
343 * unnecessary _ before or after it.
345 if (p
> desc
&& n
> 0)
349 p
+= sprintf(p
, "%d", n
);
356 /* ----- data generated by group.gap begins ----- */
359 unsigned long autosize
;
365 const struct group
*groups
;
368 static const struct group groupdata
[] = {
375 {6L, 4, 2, "2143" "3412"},
379 {6L, 6, 2, "365214" "214365"},
380 {2L, 6, 1, "436521"},
382 {6L, 7, 1, "2345671"},
384 {4L, 8, 1, "23564781"},
385 {8L, 8, 2, "24567183" "57284361"},
386 {8L, 8, 2, "57284361" "21563487"},
387 {24L, 8, 2, "24567183" "38472516"},
388 {168L, 8, 3, "21563487" "35172846" "46718235"},
390 {6L, 9, 1, "245378691"},
391 {48L, 9, 2, "245178396" "356781924"},
393 {20L, 10, 2, "3A52749618" "21436587A9"},
394 {4L, 10, 1, "436587A921"},
396 {10L, 11, 1, "23456789AB1"},
398 {12L, 12, 2, "7C4BA5832916" "2356179A4BC8"},
399 {4L, 12, 1, "589AB32C4761"},
400 {24L, 12, 2, "256719AB34C8" "6A2B8C574391"},
401 {12L, 12, 2, "7C4BA5832916" "2156349A78CB"},
402 {12L, 12, 2, "649A78C2B153" "794B6C83A512"},
404 {12L, 13, 1, "23456789ABCD1"},
406 {42L, 14, 2, "5C7E92B4D6183A" "21436587A9CBED"},
407 {6L, 14, 1, "6587A9CBED2143"},
409 {8L, 15, 1, "5783AB6DE9F2C41"},
411 {8L, 16, 1, "DBEG6F1427C3958A"},
412 {96L, 16, 2, "9CB3FE6G54A87D12" "2467891BCDE3F5GA"},
413 {32L, 16, 2, "D98G643FE2C1BA75" "25678AB1CDEF34G9"},
414 {32L, 16, 2, "9613F7CD45A2EGB8" "25678AB1CDEF34G9"},
415 {16L, 16, 2, "DF8G6B39E2C14A75" "2467895BCDEAF1G3"},
416 {16L, 16, 2, "D98G64AFE2C5B371" "2467895BCDEAF1G3"},
417 {32L, 16, 2, "DF8G6439E2C5BA71" "21678345CDE9ABGF"},
418 {16L, 16, 2, "D98G6BAFE2C14375" "74G8EF5B6C2391DA"},
419 {32L, 16, 2, "D92G64AF78C5B3E1" "3C59A7DGB1F8E642"},
421 "D38G619AE2C45F7B" "25678AB1CDEF34G9" "7BC2EF546G8A91D3"},
422 {64L, 16, 3, "D38G619AE2C45F7B" "CF76GBA92ED54381" "3D19A8G645FE2CB7"},
424 "9GB3F7DC54A2E618" "25678AB1CDEF34G9" "3D59A2G6B1F78C4E"},
425 {48L, 16, 3, "9G4AFE6C5B327D18" "6A2CD5F378GB19E4" "4795BC8EAF1DG236"},
427 "58AB1DE2F34G679C" "21678345CDE9ABGF" "3619A2CD45F78GBE"
430 {16L, 17, 1, "56789ABCDEFGH1234"},
432 {54L, 18, 2, "DB9HFGE17CI5342A86" "215634ABC789FGDEIH"},
433 {6L, 18, 1, "53AB786FG4DECI9H21"},
434 {12L, 18, 2, "53AB782FG1DE6I4HC9" "BEFGH36I5978CA1D24"},
436 "96E1BCH34FG278I5DA" "EFH36I978BCA1DG245" "215634ABC789FGDEIH"},
437 {48L, 18, 2, "53AB782FG1DE6I4HC9" "64BC89FG2DE1I5H3A7"},
439 {18L, 19, 1, "56789ABCDEFGHIJ1234"},
441 {40L, 20, 2, "7K4BI58F29CJ6DG3AH1E" "5129346D78AHBCEKFGIJ"},
442 {8L, 20, 1, "589AC3DEG7HIJB2K4F61"},
443 {20L, 20, 2, "4AJ8HE3CKI7G52B196FD" "5129346D78AHBCEKFGIJ"},
444 {40L, 20, 2, "7K4BI58F29CJ6DG3AH1E" "5329176D4BAH8FEKCJIG"},
445 {24L, 20, 2, "976D4BAH8FEKCJI5G321" "649A78DEBCHIFGK2J153"},
447 {42L, 21, 2, "9KCJ2FL5I4817B3AE6DHG" "5A8CDBFGEIJH1LK342679"},
448 {12L, 21, 1, "5783AB6DE9GHCJKFL2I41"},
450 {110L, 22, 2, "5K7M92B4D6F8HAJCLE1G3I" "21436587A9CBEDGFIHKJML"},
451 {10L, 22, 1, "6587A9CBEDGFIHKJML2143"},
453 {22L, 23, 1, "56789ABCDEFGHIJKLMN1234"},
455 {24L, 24, 2, "HO5ANGLDBCI9M26KJ1378E4F" "8IEFGJN3KLM2C49AO671BHD5"},
456 {8L, 24, 1, "DH2KL4IN678OA5C9EFGBJ1M3"},
457 {24L, 24, 2, "9FHI25LM6N78BC1ODEGJ34KA" "EAOFM74BJDK69GH5C3LI2N18"},
458 {48L, 24, 2, "HL5ANMO6BCI9G7DEJ132FK48" "8JEFGNC4KLM2I91BO673H5DA"},
459 {24L, 24, 2, "HO5ANGLDBCI9M26KJ1378E4F" "KN8EOCI9FGLDJ13HM2645A7B"},
460 {48L, 24, 2, "HL5ANMO6BCI9G7DEJ132FK48" "21678345DEFG9ABCKLMHIJON"},
462 "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F"
463 "8JEFGNC4KLM2I91BO673H5DA"},
465 "HLBANGO65JI9M2DEC4378K1F" "AO5HIMLDBCN3G76KJ192FE48"
466 "KIFEOCN38MLDJ19AG7645H2B"},
467 {16L, 24, 2, "DI7KLCN9FG6OJ4AH2ME5B381" "MBO8FH1JEKG23N45L679ACDI"},
468 {16L, 24, 2, "DI7KLCN9FG6OJ4AH2ME5B381" "IDCN97KLJ4AHFG6O5B32ME18"},
469 {48L, 24, 2, "9LCHI7ODJ43NFGEK5BA2M618" "7CDFGIJ4KL2MN95B6O8AH1E3"},
470 {24L, 24, 2, "LAGODI3JE87KCN9B6M254HF1" "EIL6MCN9GODFA54H87K3J12B"},
471 {24L, 24, 2, "D92KL1HI678O345NEFGABCMJ" "FBOM6NJ37LKE4IHA2GD1C985"},
473 "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F"
474 "21678345DEFG9ABCKLMHIJON"},
476 "HKBANFEO5JI98MLDC43G7612" "AE5HI8KLBCN3FGO6J19M2D47"
477 "85EFGABCKLM2HIJ1O67N34D9"},
479 {20L, 25, 1, "589CDEGHIJ6KLM2ANO4FP71B3"},
480 {480L, 25, 2, "589CDEGHIJ3KLM26NO4AP7FB1" "245789BCDE1GHIJ3KLM6NOAPF"},
483 "5O7Q92B4D6F8HAJCLENGPI1K3M" "21436587A9CBEDGFIHKJMLONQP"},
484 {12L, 26, 1, "6587A9CBEDGFIHKJMLONQP2143"},
486 {18L, 27, 1, "53BC689IJKDE4GHOP7LMANRFQ12"},
488 "54BC79AIJKEFGH1OPLM2N3RQ68D" "DI2LG5O67Q4NBCREF9A8JKMH1P3"},
490 "51BC234IJK6789AOPDEFGHRLMNQ" "3E89PLM1GHRB7Q64NKIJFDA5O2C"},
492 "54BC79AIJKEFGH1OPLM2N3RQ68D" "DR2LNKI67QA8P5OEFH1GBCM34J9"},
494 "51BC234IJK6789AOPDEFGHRLMNQ" "3689BDE1GHIJ2LM4N5OP7QACRFK"
495 "479ACEFGH1JKLM2N3OP5Q68RBDI"},
498 "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5129346D78AHBCELFGIPJKMSNOQR"},
499 {12L, 28, 1, "589AC3DEG7HIKBLMOFPQRJ2S4N61"},
501 "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5329176D4BAH8FELCJIPGNMSKRQO"},
503 "976D4BAH8FELCJIPGNMSKRQ5O321" "649A78DEBCHIFGLMJKPQNOS2R153"},
505 {28L, 29, 1, "56789ABCDEFGHIJKLMNOPQRST1234"},
508 "LHQ7NOTDERSA9JK6UGF1PBCM34I285" "BFHIL3NO5Q78RSATDE6UG9JKCM1P24"},
510 "DU4JOA89PS2GEFT56MKL7BCRQ1HI3N" "BQGHT36MNL78CRS1DEIU54JKOA9P2F"},
512 "DS4JU589POABEFT2GHKL76MNQ1CR3I" "215634ABC789GHIDEFMNOJKLRSPQUT"},
513 {8L, 30, 1, "HEMNJKCRS9PQIU5FT3OABL782G1D64"},
515 {30L, 31, 1, "56789ABCDEFGHIJKLMNOPQRSTUV1234"},
518 static const struct groups groups
[] = {
519 {0, NULL
}, /* trivial case: 0 */
520 {0, NULL
}, /* trivial case: 1 */
521 {1, groupdata
+ 0}, /* 2 */
522 {1, groupdata
+ 1}, /* 3 */
523 {2, groupdata
+ 2}, /* 4 */
524 {1, groupdata
+ 4}, /* 5 */
525 {2, groupdata
+ 5}, /* 6 */
526 {1, groupdata
+ 7}, /* 7 */
527 {5, groupdata
+ 8}, /* 8 */
528 {2, groupdata
+ 13}, /* 9 */
529 {2, groupdata
+ 15}, /* 10 */
530 {1, groupdata
+ 17}, /* 11 */
531 {5, groupdata
+ 18}, /* 12 */
532 {1, groupdata
+ 23}, /* 13 */
533 {2, groupdata
+ 24}, /* 14 */
534 {1, groupdata
+ 26}, /* 15 */
535 {14, groupdata
+ 27}, /* 16 */
536 {1, groupdata
+ 41}, /* 17 */
537 {5, groupdata
+ 42}, /* 18 */
538 {1, groupdata
+ 47}, /* 19 */
539 {5, groupdata
+ 48}, /* 20 */
540 {2, groupdata
+ 53}, /* 21 */
541 {2, groupdata
+ 55}, /* 22 */
542 {1, groupdata
+ 57}, /* 23 */
543 {15, groupdata
+ 58}, /* 24 */
544 {2, groupdata
+ 73}, /* 25 */
545 {2, groupdata
+ 75}, /* 26 */
546 {5, groupdata
+ 77}, /* 27 */
547 {4, groupdata
+ 82}, /* 28 */
548 {1, groupdata
+ 86}, /* 29 */
549 {4, groupdata
+ 87}, /* 30 */
550 {1, groupdata
+ 91}, /* 31 */
553 /* ----- data generated by group.gap ends ----- */
555 static char *new_game_desc(game_params
*params
, random_state
*rs
,
556 char **aux
, int interactive
)
558 int w
= params
->w
, a
= w
*w
;
559 digit
*grid
, *soln
, *soln2
;
562 int diff
= params
->diff
;
563 const struct group
*group
;
567 * Difficulty exceptions: some combinations of size and
568 * difficulty cannot be satisfied, because all puzzles of at
569 * most that difficulty are actually even easier.
571 * Remember to re-test this whenever a change is made to the
574 * I tested it using the following shell command:
576 for d in t n h x u; do
578 echo ./group --generate 1 ${i}d${d}
579 perl -e 'alarm 30; exec @ARGV' ./group --generate 5 ${i}d${d} >/dev/null \
584 * Of course, it's better to do that after taking the exceptions
585 * _out_, so as to detect exceptions that should be removed as
586 * well as those which should be added.
588 if (w
<= 9 && diff
== DIFF_EXTREME
)
590 if (w
<= 6 && diff
== DIFF_HARD
)
592 if (w
<= 4 && diff
> DIFF_TRIVIAL
)
595 grid
= snewn(a
, digit
);
596 soln
= snewn(a
, digit
);
597 soln2
= snewn(a
, digit
);
598 indices
= snewn(a
, int);
602 * Construct a valid group table, by picking a group from
603 * the above data table, decompressing it into a full
604 * representation by BFS, and then randomly permuting its
605 * non-identity elements.
607 * We build the canonical table in 'soln' (and use 'grid' as
608 * our BFS queue), then transfer the table into 'grid'
609 * having shuffled the rows.
612 assert(w
< lenof(groups
));
613 group
= groups
[w
].groups
+ random_upto(rs
, groups
[w
].ngroups
);
614 assert(group
->order
== w
);
616 for (i
= 0; i
< w
; i
++)
624 row
= soln
+ (i
-1)*w
;
626 for (j
= 0; j
< group
->ngens
; j
++) {
628 const char *gen
= group
->gens
+ j
*w
;
631 * Apply each group generator to row, constructing a
634 nri
= FROMCHAR(gen
[row
[0]-1]); /* which row is it? */
635 newrow
= soln
+ (nri
-1)*w
;
636 if (!newrow
[0]) { /* not done yet */
637 for (k
= 0; k
< w
; k
++)
638 newrow
[k
] = FROMCHAR(gen
[row
[k
]-1]);
643 /* That's got the canonical table. Now shuffle it. */
644 for (i
= 0; i
< w
; i
++)
646 shuffle(grid
+1, w
-1, sizeof(*grid
), rs
);
647 for (i
= 1; i
< w
; i
++)
648 for (j
= 0; j
< w
; j
++)
649 grid
[(grid
[i
]-1)*w
+(grid
[j
]-1)] = grid
[soln
[i
*w
+j
]-1];
650 for (i
= 0; i
< w
; i
++)
654 * Remove entries one by one while the puzzle is still
655 * soluble at the appropriate difficulty level.
657 memcpy(soln
, grid
, a
);
660 for (i
= 1; i
< w
; i
++)
661 for (j
= 1; j
< w
; j
++)
662 indices
[k
++] = i
*w
+j
;
663 shuffle(indices
, k
, sizeof(*indices
), rs
);
665 for (i
= 0; i
< k
; i
++) {
666 memcpy(soln2
, grid
, a
);
667 soln2
[indices
[i
]] = 0;
668 if (solver(w
, soln2
, diff
) <= diff
)
669 grid
[indices
[i
]] = 0;
673 * Make sure the puzzle isn't too easy.
676 memcpy(soln2
, grid
, a
);
677 if (solver(w
, soln2
, diff
-1) < diff
)
678 continue; /* go round and try again */
688 * Encode the puzzle description.
690 desc
= snewn(a
*20, char);
691 p
= encode_grid(desc
, grid
, a
);
693 desc
= sresize(desc
, p
- desc
, char);
696 * Encode the solution.
698 *aux
= snewn(a
+2, char);
700 for (i
= 0; i
< a
; i
++)
701 (*aux
)[i
+1] = TOCHAR(soln
[i
]);
712 /* ----------------------------------------------------------------------
716 static char *validate_grid_desc(const char **pdesc
, int range
, int area
)
718 const char *desc
= *pdesc
;
720 while (*desc
&& *desc
!= ',') {
722 if (n
>= 'a' && n
<= 'z') {
723 squares
+= n
- 'a' + 1;
724 } else if (n
== '_') {
726 } else if (n
> '0' && n
<= '9') {
727 int val
= atoi(desc
-1);
728 if (val
< 1 || val
> range
)
729 return "Out-of-range number in game description";
731 while (*desc
>= '0' && *desc
<= '9')
734 return "Invalid character in game description";
738 return "Not enough data to fill grid";
741 return "Too much data to fit in grid";
746 static char *validate_desc(game_params
*params
, char *desc
)
748 int w
= params
->w
, a
= w
*w
;
749 const char *p
= desc
;
751 return validate_grid_desc(&p
, w
, a
);
754 static char *spec_to_grid(char *desc
, digit
*grid
, int area
)
757 while (*desc
&& *desc
!= ',') {
759 if (n
>= 'a' && n
<= 'z') {
760 int run
= n
- 'a' + 1;
761 assert(i
+ run
<= area
);
764 } else if (n
== '_') {
766 } else if (n
> '0' && n
<= '9') {
768 grid
[i
++] = atoi(desc
-1);
769 while (*desc
>= '0' && *desc
<= '9')
772 assert(!"We can't get here");
779 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
781 int w
= params
->w
, a
= w
*w
;
782 game_state
*state
= snew(game_state
);
785 state
->par
= *params
; /* structure copy */
786 state
->grid
= snewn(a
, digit
);
787 state
->immutable
= snewn(a
, unsigned char);
788 state
->pencil
= snewn(a
, int);
789 for (i
= 0; i
< a
; i
++) {
791 state
->immutable
[i
] = 0;
792 state
->pencil
[i
] = 0;
795 desc
= spec_to_grid(desc
, state
->grid
, a
);
796 for (i
= 0; i
< a
; i
++)
797 if (state
->grid
[i
] != 0)
798 state
->immutable
[i
] = TRUE
;
800 state
->completed
= state
->cheated
= FALSE
;
805 static game_state
*dup_game(game_state
*state
)
807 int w
= state
->par
.w
, a
= w
*w
;
808 game_state
*ret
= snew(game_state
);
810 ret
->par
= state
->par
; /* structure copy */
812 ret
->grid
= snewn(a
, digit
);
813 ret
->immutable
= snewn(a
, unsigned char);
814 ret
->pencil
= snewn(a
, int);
815 memcpy(ret
->grid
, state
->grid
, a
*sizeof(digit
));
816 memcpy(ret
->immutable
, state
->immutable
, a
*sizeof(unsigned char));
817 memcpy(ret
->pencil
, state
->pencil
, a
*sizeof(int));
819 ret
->completed
= state
->completed
;
820 ret
->cheated
= state
->cheated
;
825 static void free_game(game_state
*state
)
828 sfree(state
->immutable
);
829 sfree(state
->pencil
);
833 static char *solve_game(game_state
*state
, game_state
*currstate
,
834 char *aux
, char **error
)
836 int w
= state
->par
.w
, a
= w
*w
;
844 soln
= snewn(a
, digit
);
845 memcpy(soln
, state
->grid
, a
*sizeof(digit
));
847 ret
= solver(w
, soln
, DIFFCOUNT
-1);
849 if (ret
== diff_impossible
) {
850 *error
= "No solution exists for this puzzle";
852 } else if (ret
== diff_ambiguous
) {
853 *error
= "Multiple solutions exist for this puzzle";
856 out
= snewn(a
+2, char);
858 for (i
= 0; i
< a
; i
++)
859 out
[i
+1] = TOCHAR(soln
[i
]);
867 static int game_can_format_as_text_now(game_params
*params
)
872 static char *game_text_format(game_state
*state
)
874 int w
= state
->par
.w
;
878 ret
= snewn(2*w
*w
+1, char); /* leave room for terminating NUL */
881 for (y
= 0; y
< w
; y
++) {
882 for (x
= 0; x
< w
; x
++) {
883 digit d
= state
->grid
[y
*w
+x
];
900 assert(p
- ret
== 2*w
*w
);
907 * These are the coordinates of the currently highlighted
908 * square on the grid, if hshow = 1.
912 * This indicates whether the current highlight is a
913 * pencil-mark one or a real one.
917 * This indicates whether or not we're showing the highlight
918 * (used to be hx = hy = -1); important so that when we're
919 * using the cursor keys it doesn't keep coming back at a
920 * fixed position. When hshow = 1, pressing a valid number
921 * or letter key or Space will enter that number or letter in the grid.
925 * This indicates whether we're using the highlight as a cursor;
926 * it means that it doesn't vanish on a keypress, and that it is
927 * allowed on immutable squares.
932 static game_ui
*new_ui(game_state
*state
)
934 game_ui
*ui
= snew(game_ui
);
937 ui
->hpencil
= ui
->hshow
= ui
->hcursor
= 0;
942 static void free_ui(game_ui
*ui
)
947 static char *encode_ui(game_ui
*ui
)
952 static void decode_ui(game_ui
*ui
, char *encoding
)
956 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
957 game_state
*newstate
)
959 int w
= newstate
->par
.w
;
961 * We prevent pencil-mode highlighting of a filled square, unless
962 * we're using the cursor keys. So if the user has just filled in
963 * a square which we had a pencil-mode highlight in (by Undo, or
964 * by Redo, or by Solve), then we cancel the highlight.
966 if (ui
->hshow
&& ui
->hpencil
&& !ui
->hcursor
&&
967 newstate
->grid
[ui
->hy
* w
+ ui
->hx
] != 0) {
972 #define PREFERRED_TILESIZE 48
973 #define TILESIZE (ds->tilesize)
974 #define BORDER (TILESIZE / 2)
975 #define GRIDEXTRA max((TILESIZE / 32),1)
976 #define COORD(x) ((x)*TILESIZE + BORDER)
977 #define FROMCOORD(x) (((x)+(TILESIZE-BORDER)) / TILESIZE - 1)
979 #define FLASH_TIME 0.4F
981 #define DF_HIGHLIGHT 0x0400
982 #define DF_HIGHLIGHT_PENCIL 0x0200
983 #define DF_IMMUTABLE 0x0100
984 #define DF_DIGIT_MASK 0x001F
986 #define EF_DIGIT_SHIFT 5
987 #define EF_DIGIT_MASK ((1 << EF_DIGIT_SHIFT) - 1)
988 #define EF_LEFT_SHIFT 0
989 #define EF_RIGHT_SHIFT (3*EF_DIGIT_SHIFT)
990 #define EF_LEFT_MASK ((1UL << (3*EF_DIGIT_SHIFT)) - 1UL)
991 #define EF_RIGHT_MASK (EF_LEFT_MASK << EF_RIGHT_SHIFT)
992 #define EF_LATIN (1UL << (6*EF_DIGIT_SHIFT))
994 struct game_drawstate
{
997 long *tiles
, *pencil
, *errors
;
1001 static int check_errors(game_state
*state
, long *errors
)
1003 int w
= state
->par
.w
, a
= w
*w
;
1004 digit
*grid
= state
->grid
;
1005 int i
, j
, k
, x
, y
, errs
= FALSE
;
1008 for (i
= 0; i
< a
; i
++)
1011 for (y
= 0; y
< w
; y
++) {
1012 unsigned long mask
= 0, errmask
= 0;
1013 for (x
= 0; x
< w
; x
++) {
1014 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1015 errmask
|= (mask
& bit
);
1019 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1023 for (x
= 0; x
< w
; x
++)
1024 if (errmask
& (1UL << grid
[y
*w
+x
]))
1025 errors
[y
*w
+x
] |= EF_LATIN
;
1030 for (x
= 0; x
< w
; x
++) {
1031 unsigned long mask
= 0, errmask
= 0;
1032 for (y
= 0; y
< w
; y
++) {
1033 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1034 errmask
|= (mask
& bit
);
1038 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1042 for (y
= 0; y
< w
; y
++)
1043 if (errmask
& (1UL << grid
[y
*w
+x
]))
1044 errors
[y
*w
+x
] |= EF_LATIN
;
1049 for (i
= 1; i
< w
; i
++)
1050 for (j
= 1; j
< w
; j
++)
1051 for (k
= 1; k
< w
; k
++)
1052 if (grid
[i
*w
+j
] && grid
[j
*w
+k
] &&
1053 grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
1054 grid
[i
*w
+(grid
[j
*w
+k
]-1)] &&
1055 grid
[(grid
[i
*w
+j
]-1)*w
+k
] != grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
1057 int a
= i
+1, b
= j
+1, c
= k
+1;
1058 int ab
= grid
[i
*w
+j
], bc
= grid
[j
*w
+k
];
1059 int left
= (ab
-1)*w
+(c
-1), right
= (a
-1)*w
+(bc
-1);
1061 * If the appropriate error slot is already
1062 * used for one of the squares, we don't
1063 * fill either of them.
1065 if (!(errors
[left
] & EF_LEFT_MASK
) &&
1066 !(errors
[right
] & EF_RIGHT_MASK
)) {
1069 err
= (err
<< EF_DIGIT_SHIFT
) | b
;
1070 err
= (err
<< EF_DIGIT_SHIFT
) | c
;
1071 errors
[left
] |= err
<< EF_LEFT_SHIFT
;
1072 errors
[right
] |= err
<< EF_RIGHT_SHIFT
;
1081 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1082 int x
, int y
, int button
)
1084 int w
= state
->par
.w
;
1088 button
&= ~MOD_MASK
;
1093 if (tx
> 0 && tx
< w
&& ty
> 0 && ty
< w
) {
1094 if (button
== LEFT_BUTTON
) {
1095 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1096 ui
->hshow
&& ui
->hpencil
== 0) {
1101 ui
->hshow
= !state
->immutable
[ty
*w
+tx
];
1105 return ""; /* UI activity occurred */
1107 if (button
== RIGHT_BUTTON
) {
1109 * Pencil-mode highlighting for non filled squares.
1111 if (state
->grid
[ty
*w
+tx
] == 0) {
1112 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1113 ui
->hshow
&& ui
->hpencil
) {
1125 return ""; /* UI activity occurred */
1128 if (IS_CURSOR_MOVE(button
)) {
1130 move_cursor(button
, &ui
->hx
, &ui
->hy
, w
-1, w
-1, 0);
1132 ui
->hshow
= ui
->hcursor
= 1;
1136 (button
== CURSOR_SELECT
)) {
1137 ui
->hpencil
= 1 - ui
->hpencil
;
1143 ((ISCHAR(button
) && FROMCHAR(button
) <= w
) ||
1144 button
== CURSOR_SELECT2
|| button
== '\b')) {
1145 int n
= FROMCHAR(button
);
1146 if (button
== CURSOR_SELECT2
|| button
== '\b')
1150 * Can't make pencil marks in a filled square. This can only
1151 * become highlighted if we're using cursor keys.
1153 if (ui
->hpencil
&& state
->grid
[ui
->hy
*w
+ui
->hx
])
1157 * Can't do anything to an immutable square.
1159 if (state
->immutable
[ui
->hy
*w
+ui
->hx
])
1162 sprintf(buf
, "%c%d,%d,%d",
1163 (char)(ui
->hpencil
&& n
> 0 ?
'P' : 'R'), ui
->hx
, ui
->hy
, n
);
1165 if (!ui
->hcursor
) ui
->hshow
= 0;
1170 if (button
== 'M' || button
== 'm')
1176 static game_state
*execute_move(game_state
*from
, char *move
)
1178 int w
= from
->par
.w
, a
= w
*w
;
1182 if (move
[0] == 'S') {
1183 ret
= dup_game(from
);
1184 ret
->completed
= ret
->cheated
= TRUE
;
1186 for (i
= 0; i
< a
; i
++) {
1187 if (!ISCHAR(move
[i
+1]) || FROMCHAR(move
[i
+1]) > w
) {
1191 ret
->grid
[i
] = FROMCHAR(move
[i
+1]);
1195 if (move
[a
+1] != '\0') {
1201 } else if ((move
[0] == 'P' || move
[0] == 'R') &&
1202 sscanf(move
+1, "%d,%d,%d", &x
, &y
, &n
) == 3 &&
1203 x
>= 0 && x
< w
&& y
>= 0 && y
< w
&& n
>= 0 && n
<= w
) {
1204 if (from
->immutable
[y
*w
+x
])
1207 ret
= dup_game(from
);
1208 if (move
[0] == 'P' && n
> 0) {
1209 ret
->pencil
[y
*w
+x
] ^= 1 << n
;
1211 ret
->grid
[y
*w
+x
] = n
;
1212 ret
->pencil
[y
*w
+x
] = 0;
1214 if (!ret
->completed
&& !check_errors(ret
, NULL
))
1215 ret
->completed
= TRUE
;
1218 } else if (move
[0] == 'M') {
1220 * Fill in absolutely all pencil marks everywhere. (I
1221 * wouldn't use this for actual play, but it's a handy
1222 * starting point when following through a set of
1223 * diagnostics output by the standalone solver.)
1225 ret
= dup_game(from
);
1226 for (i
= 0; i
< a
; i
++) {
1228 ret
->pencil
[i
] = (1 << (w
+1)) - (1 << 1);
1232 return NULL
; /* couldn't parse move string */
1235 /* ----------------------------------------------------------------------
1239 #define SIZE(w) ((w) * TILESIZE + 2*BORDER)
1241 static void game_compute_size(game_params
*params
, int tilesize
,
1244 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1245 struct { int tilesize
; } ads
, *ds
= &ads
;
1246 ads
.tilesize
= tilesize
;
1248 *x
= *y
= SIZE(params
->w
);
1251 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1252 game_params
*params
, int tilesize
)
1254 ds
->tilesize
= tilesize
;
1257 static float *game_colours(frontend
*fe
, int *ncolours
)
1259 float *ret
= snewn(3 * NCOLOURS
, float);
1261 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1263 ret
[COL_GRID
* 3 + 0] = 0.0F
;
1264 ret
[COL_GRID
* 3 + 1] = 0.0F
;
1265 ret
[COL_GRID
* 3 + 2] = 0.0F
;
1267 ret
[COL_IDENTITY
* 3 + 0] = 0.89F
* ret
[COL_BACKGROUND
* 3 + 0];
1268 ret
[COL_IDENTITY
* 3 + 1] = 0.89F
* ret
[COL_BACKGROUND
* 3 + 1];
1269 ret
[COL_IDENTITY
* 3 + 2] = 0.89F
* ret
[COL_BACKGROUND
* 3 + 2];
1271 ret
[COL_USER
* 3 + 0] = 0.0F
;
1272 ret
[COL_USER
* 3 + 1] = 0.6F
* ret
[COL_BACKGROUND
* 3 + 1];
1273 ret
[COL_USER
* 3 + 2] = 0.0F
;
1275 ret
[COL_HIGHLIGHT
* 3 + 0] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 0];
1276 ret
[COL_HIGHLIGHT
* 3 + 1] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 1];
1277 ret
[COL_HIGHLIGHT
* 3 + 2] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 2];
1279 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
1280 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
1281 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
1283 ret
[COL_PENCIL
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1284 ret
[COL_PENCIL
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1285 ret
[COL_PENCIL
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
1287 *ncolours
= NCOLOURS
;
1291 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1293 int w
= state
->par
.w
, a
= w
*w
;
1294 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1299 ds
->started
= FALSE
;
1300 ds
->tiles
= snewn(a
, long);
1301 ds
->pencil
= snewn(a
, long);
1302 ds
->errors
= snewn(a
, long);
1303 for (i
= 0; i
< a
; i
++)
1304 ds
->tiles
[i
] = ds
->pencil
[i
] = -1;
1305 ds
->errtmp
= snewn(a
, long);
1310 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1319 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, int x
, int y
, long tile
,
1320 long pencil
, long error
)
1322 int w
= ds
->w
/* , a = w*w */;
1327 tx
= BORDER
+ x
* TILESIZE
+ 1;
1328 ty
= BORDER
+ y
* TILESIZE
+ 1;
1332 cw
= tw
= TILESIZE
-1;
1333 ch
= th
= TILESIZE
-1;
1335 clip(dr
, cx
, cy
, cw
, ch
);
1337 /* background needs erasing */
1338 draw_rect(dr
, cx
, cy
, cw
, ch
,
1339 (tile
& DF_HIGHLIGHT
) ? COL_HIGHLIGHT
:
1340 (x
== 0 || y
== 0) ? COL_IDENTITY
: COL_BACKGROUND
);
1342 /* pencil-mode highlight */
1343 if (tile
& DF_HIGHLIGHT_PENCIL
) {
1347 coords
[2] = cx
+cw
/2;
1350 coords
[5] = cy
+ch
/2;
1351 draw_polygon(dr
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
1354 /* new number needs drawing? */
1355 if (tile
& DF_DIGIT_MASK
) {
1357 str
[0] = TOCHAR(tile
& DF_DIGIT_MASK
);
1358 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/2,
1359 FONT_VARIABLE
, TILESIZE
/2, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1360 (error
& EF_LATIN
) ? COL_ERROR
:
1361 (tile
& DF_IMMUTABLE
) ? COL_GRID
: COL_USER
, str
);
1363 if (error
& EF_LEFT_MASK
) {
1364 int a
= (error
>> (EF_LEFT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1365 int b
= (error
>> (EF_LEFT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1366 int c
= (error
>> (EF_LEFT_SHIFT
))&EF_DIGIT_MASK
;
1368 sprintf(buf
, "(%c%c)%c", TOCHAR(a
), TOCHAR(b
), TOCHAR(c
));
1369 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/6,
1370 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1373 if (error
& EF_RIGHT_MASK
) {
1374 int a
= (error
>> (EF_RIGHT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1375 int b
= (error
>> (EF_RIGHT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1376 int c
= (error
>> (EF_RIGHT_SHIFT
))&EF_DIGIT_MASK
;
1378 sprintf(buf
, "%c(%c%c)", TOCHAR(a
), TOCHAR(b
), TOCHAR(c
));
1379 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
- TILESIZE
/6,
1380 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1387 int pw
, ph
, minph
, pbest
, fontsize
;
1389 /* Count the pencil marks required. */
1390 for (i
= 1, npencil
= 0; i
<= w
; i
++)
1391 if (pencil
& (1 << i
))
1398 * Determine the bounding rectangle within which we're going
1399 * to put the pencil marks.
1401 /* Start with the whole square */
1402 pl
= tx
+ GRIDEXTRA
;
1403 pr
= pl
+ TILESIZE
- GRIDEXTRA
;
1404 pt
= ty
+ GRIDEXTRA
;
1405 pb
= pt
+ TILESIZE
- GRIDEXTRA
;
1408 * We arrange our pencil marks in a grid layout, with
1409 * the number of rows and columns adjusted to allow the
1410 * maximum font size.
1412 * So now we work out what the grid size ought to be.
1417 for (pw
= 3; pw
< max(npencil
,4); pw
++) {
1420 ph
= (npencil
+ pw
- 1) / pw
;
1421 ph
= max(ph
, minph
);
1422 fw
= (pr
- pl
) / (float)pw
;
1423 fh
= (pb
- pt
) / (float)ph
;
1425 if (fs
> bestsize
) {
1432 ph
= (npencil
+ pw
- 1) / pw
;
1433 ph
= max(ph
, minph
);
1436 * Now we've got our grid dimensions, work out the pixel
1437 * size of a grid element, and round it to the nearest
1438 * pixel. (We don't want rounding errors to make the
1439 * grid look uneven at low pixel sizes.)
1441 fontsize
= min((pr
- pl
) / pw
, (pb
- pt
) / ph
);
1444 * Centre the resulting figure in the square.
1446 pl
= tx
+ (TILESIZE
- fontsize
* pw
) / 2;
1447 pt
= ty
+ (TILESIZE
- fontsize
* ph
) / 2;
1450 * Now actually draw the pencil marks.
1452 for (i
= 1, j
= 0; i
<= w
; i
++)
1453 if (pencil
& (1 << i
)) {
1454 int dx
= j
% pw
, dy
= j
/ pw
;
1458 draw_text(dr
, pl
+ fontsize
* (2*dx
+1) / 2,
1459 pt
+ fontsize
* (2*dy
+1) / 2,
1460 FONT_VARIABLE
, fontsize
,
1461 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_PENCIL
, str
);
1469 draw_update(dr
, cx
, cy
, cw
, ch
);
1472 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1473 game_state
*state
, int dir
, game_ui
*ui
,
1474 float animtime
, float flashtime
)
1476 int w
= state
->par
.w
/*, a = w*w */;
1481 * The initial contents of the window are not guaranteed and
1482 * can vary with front ends. To be on the safe side, all
1483 * games should start by drawing a big background-colour
1484 * rectangle covering the whole window.
1486 draw_rect(dr
, 0, 0, SIZE(w
), SIZE(w
), COL_BACKGROUND
);
1489 * Big containing rectangle.
1491 draw_rect(dr
, COORD(0) - GRIDEXTRA
, COORD(0) - GRIDEXTRA
,
1492 w
*TILESIZE
+1+GRIDEXTRA
*2, w
*TILESIZE
+1+GRIDEXTRA
*2,
1495 draw_update(dr
, 0, 0, SIZE(w
), SIZE(w
));
1500 check_errors(state
, ds
->errtmp
);
1502 for (y
= 0; y
< w
; y
++) {
1503 for (x
= 0; x
< w
; x
++) {
1504 long tile
= 0L, pencil
= 0L, error
;
1506 if (state
->grid
[y
*w
+x
])
1507 tile
= state
->grid
[y
*w
+x
];
1509 pencil
= (long)state
->pencil
[y
*w
+x
];
1511 if (state
->immutable
[y
*w
+x
])
1512 tile
|= DF_IMMUTABLE
;
1514 if (ui
->hshow
&& ui
->hx
== x
&& ui
->hy
== y
)
1515 tile
|= (ui
->hpencil ? DF_HIGHLIGHT_PENCIL
: DF_HIGHLIGHT
);
1517 if (flashtime
> 0 &&
1518 (flashtime
<= FLASH_TIME
/3 ||
1519 flashtime
>= FLASH_TIME
*2/3))
1520 tile
|= DF_HIGHLIGHT
; /* completion flash */
1522 error
= ds
->errtmp
[y
*w
+x
];
1524 if (ds
->tiles
[y
*w
+x
] != tile
||
1525 ds
->pencil
[y
*w
+x
] != pencil
||
1526 ds
->errors
[y
*w
+x
] != error
) {
1527 ds
->tiles
[y
*w
+x
] = tile
;
1528 ds
->pencil
[y
*w
+x
] = pencil
;
1529 ds
->errors
[y
*w
+x
] = error
;
1530 draw_tile(dr
, ds
, x
, y
, tile
, pencil
, error
);
1536 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1537 int dir
, game_ui
*ui
)
1542 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1543 int dir
, game_ui
*ui
)
1545 if (!oldstate
->completed
&& newstate
->completed
&&
1546 !oldstate
->cheated
&& !newstate
->cheated
)
1551 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1553 if (state
->completed
)
1558 static void game_print_size(game_params
*params
, float *x
, float *y
)
1563 * We use 9mm squares by default, like Solo.
1565 game_compute_size(params
, 900, &pw
, &ph
);
1570 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1572 int w
= state
->par
.w
;
1573 int ink
= print_mono_colour(dr
, 0);
1574 int ehighlight
= print_grey_colour(dr
, 0.90F
);
1577 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1578 game_drawstate ads
, *ds
= &ads
;
1579 game_set_size(dr
, ds
, NULL
, tilesize
);
1582 * Highlight the identity row and column.
1584 for (x
= 1; x
< w
; x
++)
1585 draw_rect(dr
, BORDER
+ x
*TILESIZE
, BORDER
,
1586 TILESIZE
, TILESIZE
, ehighlight
);
1587 for (y
= 0; y
< w
; y
++)
1588 draw_rect(dr
, BORDER
, BORDER
+ y
*TILESIZE
,
1589 TILESIZE
, TILESIZE
, ehighlight
);
1594 print_line_width(dr
, 3 * TILESIZE
/ 40);
1595 draw_rect_outline(dr
, BORDER
, BORDER
, w
*TILESIZE
, w
*TILESIZE
, ink
);
1600 for (x
= 1; x
< w
; x
++) {
1601 print_line_width(dr
, TILESIZE
/ 40);
1602 draw_line(dr
, BORDER
+x
*TILESIZE
, BORDER
,
1603 BORDER
+x
*TILESIZE
, BORDER
+w
*TILESIZE
, ink
);
1605 for (y
= 1; y
< w
; y
++) {
1606 print_line_width(dr
, TILESIZE
/ 40);
1607 draw_line(dr
, BORDER
, BORDER
+y
*TILESIZE
,
1608 BORDER
+w
*TILESIZE
, BORDER
+y
*TILESIZE
, ink
);
1614 for (y
= 0; y
< w
; y
++)
1615 for (x
= 0; x
< w
; x
++)
1616 if (state
->grid
[y
*w
+x
]) {
1619 str
[0] = TOCHAR(state
->grid
[y
*w
+x
]);
1620 draw_text(dr
, BORDER
+ x
*TILESIZE
+ TILESIZE
/2,
1621 BORDER
+ y
*TILESIZE
+ TILESIZE
/2,
1622 FONT_VARIABLE
, TILESIZE
/2,
1623 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1628 #define thegame group
1631 const struct game thegame
= {
1632 "Group", NULL
, NULL
,
1639 TRUE
, game_configure
, custom_params
,
1647 TRUE
, game_can_format_as_text_now
, game_text_format
,
1655 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
1658 game_free_drawstate
,
1662 TRUE
, FALSE
, game_print_size
, game_print
,
1663 FALSE
, /* wants_statusbar */
1664 FALSE
, game_timing_state
,
1665 REQUIRE_RBUTTON
| REQUIRE_NUMPAD
, /* flags */
1668 #ifdef STANDALONE_SOLVER
1672 int main(int argc
, char **argv
)
1676 char *id
= NULL
, *desc
, *err
;
1679 int ret
, diff
, really_show_working
= FALSE
;
1681 while (--argc
> 0) {
1683 if (!strcmp(p
, "-v")) {
1684 really_show_working
= TRUE
;
1685 } else if (!strcmp(p
, "-g")) {
1687 } else if (*p
== '-') {
1688 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
1696 fprintf(stderr
, "usage: %s [-g | -v] <game_id>\n", argv
[0]);
1700 desc
= strchr(id
, ':');
1702 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
1707 p
= default_params();
1708 decode_params(p
, id
);
1709 err
= validate_desc(p
, desc
);
1711 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
1714 s
= new_game(NULL
, p
, desc
);
1716 grid
= snewn(p
->w
* p
->w
, digit
);
1719 * When solving a Normal puzzle, we don't want to bother the
1720 * user with Hard-level deductions. For this reason, we grade
1721 * the puzzle internally before doing anything else.
1723 ret
= -1; /* placate optimiser */
1724 solver_show_working
= FALSE
;
1725 for (diff
= 0; diff
< DIFFCOUNT
; diff
++) {
1726 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1727 ret
= solver(p
->w
, grid
, diff
);
1732 if (diff
== DIFFCOUNT
) {
1734 printf("Difficulty rating: ambiguous\n");
1736 printf("Unable to find a unique solution\n");
1739 if (ret
== diff_impossible
)
1740 printf("Difficulty rating: impossible (no solution exists)\n");
1742 printf("Difficulty rating: %s\n", group_diffnames
[ret
]);
1744 solver_show_working
= really_show_working
;
1745 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1746 ret
= solver(p
->w
, grid
, diff
);
1748 printf("Puzzle is inconsistent\n");
1750 memcpy(s
->grid
, grid
, p
->w
* p
->w
);
1751 fputs(game_text_format(s
), stdout
);
1761 /* vim: set shiftwidth=4 tabstop=8: */