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
40 * Difficulty levels. I do some macro ickery here to ensure that my
41 * enum and the various forms of my name list always match up.
44 A(TRIVIAL,Trivial,NULL,t) \
45 A(NORMAL,Normal,solver_normal,n) \
47 A(EXTREME,Extreme,NULL,x) \
48 A(UNREASONABLE,Unreasonable,NULL,u)
49 #define ENUM(upper,title,func,lower) DIFF_ ## upper,
50 #define TITLE(upper,title,func,lower) #title,
51 #define ENCODE(upper,title,func,lower) #lower
52 #define CONFIG(upper,title,func,lower) ":" #title
53 enum { DIFFLIST(ENUM
) DIFFCOUNT
};
54 static char const *const group_diffnames
[] = { DIFFLIST(TITLE
) };
55 static char const group_diffchars
[] = DIFFLIST(ENCODE
);
56 #define DIFFCONFIG DIFFLIST(CONFIG)
68 #define FROMCHAR(c) ((c)>='0'&&(c)<='9' ? (c)-'0' : \
69 (c)>='A'&&(c)<='Z' ? (c)-'A'+10 : (c)-'a'+10)
70 #define ISCHAR(c) (((c)>='0'&&(c)<='9') || \
71 ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
72 #define TOCHAR(c) ((c)>=10 ? (c)-10+'a' : (c)+'0')
81 unsigned char *immutable
;
82 int *pencil
; /* bitmaps using bits 1<<1..1<<n */
83 int completed
, cheated
;
86 static game_params
*default_params(void)
88 game_params
*ret
= snew(game_params
);
91 ret
->diff
= DIFF_NORMAL
;
97 const static struct game_params group_presets
[] = {
98 { 6, DIFF_NORMAL
, TRUE
},
99 { 6, DIFF_NORMAL
, FALSE
},
100 { 6, DIFF_HARD
, TRUE
},
101 { 6, DIFF_HARD
, FALSE
},
102 { 8, DIFF_NORMAL
, TRUE
},
103 { 8, DIFF_NORMAL
, FALSE
},
104 { 12, DIFF_NORMAL
, TRUE
},
107 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
112 if (i
< 0 || i
>= lenof(group_presets
))
115 ret
= snew(game_params
);
116 *ret
= group_presets
[i
]; /* structure copy */
118 sprintf(buf
, "%dx%d %s%s", ret
->w
, ret
->w
, group_diffnames
[ret
->diff
],
119 ret
->id ?
"" : ", identity hidden");
126 static void free_params(game_params
*params
)
131 static game_params
*dup_params(game_params
*params
)
133 game_params
*ret
= snew(game_params
);
134 *ret
= *params
; /* structure copy */
138 static void decode_params(game_params
*params
, char const *string
)
140 char const *p
= string
;
143 while (*p
&& isdigit((unsigned char)*p
)) p
++;
144 params
->diff
= DIFF_NORMAL
;
151 params
->diff
= DIFFCOUNT
+1; /* ...which is invalid */
153 for (i
= 0; i
< DIFFCOUNT
; i
++) {
154 if (*p
== group_diffchars
[i
])
159 } else if (*p
== 'i') {
165 static char *encode_params(game_params
*params
, int full
)
169 sprintf(ret
, "%d", params
->w
);
171 sprintf(ret
+ strlen(ret
), "d%c", group_diffchars
[params
->diff
]);
173 sprintf(ret
+ strlen(ret
), "i");
178 static config_item
*game_configure(game_params
*params
)
183 ret
= snewn(4, config_item
);
185 ret
[0].name
= "Grid size";
186 ret
[0].type
= C_STRING
;
187 sprintf(buf
, "%d", params
->w
);
188 ret
[0].sval
= dupstr(buf
);
191 ret
[1].name
= "Difficulty";
192 ret
[1].type
= C_CHOICES
;
193 ret
[1].sval
= DIFFCONFIG
;
194 ret
[1].ival
= params
->diff
;
196 ret
[2].name
= "Show identity";
197 ret
[2].type
= C_BOOLEAN
;
199 ret
[2].ival
= params
->id
;
209 static game_params
*custom_params(config_item
*cfg
)
211 game_params
*ret
= snew(game_params
);
213 ret
->w
= atoi(cfg
[0].sval
);
214 ret
->diff
= cfg
[1].ival
;
215 ret
->id
= cfg
[2].ival
;
220 static char *validate_params(game_params
*params
, int full
)
222 if (params
->w
< 3 || params
->w
> 31)
223 return "Grid size must be between 3 and 31";
224 if (params
->diff
>= DIFFCOUNT
)
225 return "Unknown difficulty rating";
229 /* ----------------------------------------------------------------------
233 static int solver_normal(struct latin_solver
*solver
, void *vctx
)
236 digit
*grid
= solver
->grid
;
240 * Deduce using associativity: (ab)c = a(bc).
242 * So we pick any a,b,c we like; then if we know ab, bc, and
243 * (ab)c we can fill in a(bc).
245 for (i
= 1; i
< w
; i
++)
246 for (j
= 1; j
< w
; j
++)
247 for (k
= 1; k
< w
; k
++) {
248 if (!grid
[i
*w
+j
] || !grid
[j
*w
+k
])
250 if (grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
251 !grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
252 int x
= grid
[j
*w
+k
]-1, y
= i
;
253 int n
= grid
[(grid
[i
*w
+j
]-1)*w
+k
];
254 #ifdef STANDALONE_SOLVER
255 if (solver_show_working
) {
256 printf("%*sassociativity on %d,%d,%d: %d*%d = %d*%d\n",
257 solver_recurse_depth
*4, "",
259 grid
[i
*w
+j
], k
+1, i
+1, grid
[j
*w
+k
]);
260 printf("%*s placing %d at (%d,%d)\n",
261 solver_recurse_depth
*4, "",
265 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
266 latin_solver_place(solver
, x
, y
, n
);
269 #ifdef STANDALONE_SOLVER
270 if (solver_show_working
)
271 printf("%*s contradiction!\n",
272 solver_recurse_depth
*4, "");
277 if (!grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
278 grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
279 int x
= k
, y
= grid
[i
*w
+j
]-1;
280 int n
= grid
[i
*w
+(grid
[j
*w
+k
]-1)];
281 #ifdef STANDALONE_SOLVER
282 if (solver_show_working
) {
283 printf("%*sassociativity on %d,%d,%d: %d*%d = %d*%d\n",
284 solver_recurse_depth
*4, "",
286 grid
[i
*w
+j
], k
+1, i
+1, grid
[j
*w
+k
]);
287 printf("%*s placing %d at (%d,%d)\n",
288 solver_recurse_depth
*4, "",
292 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
293 latin_solver_place(solver
, x
, y
, n
);
296 #ifdef STANDALONE_SOLVER
297 if (solver_show_working
)
298 printf("%*s contradiction!\n",
299 solver_recurse_depth
*4, "");
309 #define SOLVER(upper,title,func,lower) func,
310 static usersolver_t
const group_solvers
[] = { DIFFLIST(SOLVER
) };
312 static int solver(int w
, digit
*grid
, int maxdiff
)
316 ret
= latin_solver(grid
, w
, maxdiff
,
317 DIFF_TRIVIAL
, DIFF_HARD
, DIFF_EXTREME
,
318 DIFF_EXTREME
, DIFF_UNREASONABLE
,
319 group_solvers
, NULL
, NULL
, NULL
);
324 /* ----------------------------------------------------------------------
328 static char *encode_grid(char *desc
, digit
*grid
, int area
)
334 for (i
= 0; i
<= area
; i
++) {
335 int n
= (i
< area ? grid
[i
] : -1);
342 int c
= 'a' - 1 + run
;
346 run
-= c
- ('a' - 1);
350 * If there's a number in the very top left or
351 * bottom right, there's no point putting an
352 * unnecessary _ before or after it.
354 if (p
> desc
&& n
> 0)
358 p
+= sprintf(p
, "%d", n
);
365 /* ----- data generated by group.gap begins ----- */
368 unsigned long autosize
;
374 const struct group
*groups
;
377 static const struct group groupdata
[] = {
384 {6L, 4, 2, "2143" "3412"},
388 {6L, 6, 2, "365214" "214365"},
389 {2L, 6, 1, "436521"},
391 {6L, 7, 1, "2345671"},
393 {4L, 8, 1, "23564781"},
394 {8L, 8, 2, "24567183" "57284361"},
395 {8L, 8, 2, "57284361" "21563487"},
396 {24L, 8, 2, "24567183" "38472516"},
397 {168L, 8, 3, "21563487" "35172846" "46718235"},
399 {6L, 9, 1, "245378691"},
400 {48L, 9, 2, "245178396" "356781924"},
402 {20L, 10, 2, "3A52749618" "21436587A9"},
403 {4L, 10, 1, "436587A921"},
405 {10L, 11, 1, "23456789AB1"},
407 {12L, 12, 2, "7C4BA5832916" "2356179A4BC8"},
408 {4L, 12, 1, "589AB32C4761"},
409 {24L, 12, 2, "256719AB34C8" "6A2B8C574391"},
410 {12L, 12, 2, "7C4BA5832916" "2156349A78CB"},
411 {12L, 12, 2, "649A78C2B153" "794B6C83A512"},
413 {12L, 13, 1, "23456789ABCD1"},
415 {42L, 14, 2, "5C7E92B4D6183A" "21436587A9CBED"},
416 {6L, 14, 1, "6587A9CBED2143"},
418 {8L, 15, 1, "5783AB6DE9F2C41"},
420 {8L, 16, 1, "DBEG6F1427C3958A"},
421 {96L, 16, 2, "9CB3FE6G54A87D12" "2467891BCDE3F5GA"},
422 {32L, 16, 2, "D98G643FE2C1BA75" "25678AB1CDEF34G9"},
423 {32L, 16, 2, "9613F7CD45A2EGB8" "25678AB1CDEF34G9"},
424 {16L, 16, 2, "DF8G6B39E2C14A75" "2467895BCDEAF1G3"},
425 {16L, 16, 2, "D98G64AFE2C5B371" "2467895BCDEAF1G3"},
426 {32L, 16, 2, "DF8G6439E2C5BA71" "21678345CDE9ABGF"},
427 {16L, 16, 2, "D98G6BAFE2C14375" "74G8EF5B6C2391DA"},
428 {32L, 16, 2, "D92G64AF78C5B3E1" "3C59A7DGB1F8E642"},
430 "D38G619AE2C45F7B" "25678AB1CDEF34G9" "7BC2EF546G8A91D3"},
431 {64L, 16, 3, "D38G619AE2C45F7B" "CF76GBA92ED54381" "3D19A8G645FE2CB7"},
433 "9GB3F7DC54A2E618" "25678AB1CDEF34G9" "3D59A2G6B1F78C4E"},
434 {48L, 16, 3, "9G4AFE6C5B327D18" "6A2CD5F378GB19E4" "4795BC8EAF1DG236"},
436 "58AB1DE2F34G679C" "21678345CDE9ABGF" "3619A2CD45F78GBE"
439 {16L, 17, 1, "56789ABCDEFGH1234"},
441 {54L, 18, 2, "DB9HFGE17CI5342A86" "215634ABC789FGDEIH"},
442 {6L, 18, 1, "53AB786FG4DECI9H21"},
443 {12L, 18, 2, "53AB782FG1DE6I4HC9" "BEFGH36I5978CA1D24"},
445 "96E1BCH34FG278I5DA" "EFH36I978BCA1DG245" "215634ABC789FGDEIH"},
446 {48L, 18, 2, "53AB782FG1DE6I4HC9" "64BC89FG2DE1I5H3A7"},
448 {18L, 19, 1, "56789ABCDEFGHIJ1234"},
450 {40L, 20, 2, "7K4BI58F29CJ6DG3AH1E" "5129346D78AHBCEKFGIJ"},
451 {8L, 20, 1, "589AC3DEG7HIJB2K4F61"},
452 {20L, 20, 2, "4AJ8HE3CKI7G52B196FD" "5129346D78AHBCEKFGIJ"},
453 {40L, 20, 2, "7K4BI58F29CJ6DG3AH1E" "5329176D4BAH8FEKCJIG"},
454 {24L, 20, 2, "976D4BAH8FEKCJI5G321" "649A78DEBCHIFGK2J153"},
456 {42L, 21, 2, "9KCJ2FL5I4817B3AE6DHG" "5A8CDBFGEIJH1LK342679"},
457 {12L, 21, 1, "5783AB6DE9GHCJKFL2I41"},
459 {110L, 22, 2, "5K7M92B4D6F8HAJCLE1G3I" "21436587A9CBEDGFIHKJML"},
460 {10L, 22, 1, "6587A9CBEDGFIHKJML2143"},
462 {22L, 23, 1, "56789ABCDEFGHIJKLMN1234"},
464 {24L, 24, 2, "HO5ANGLDBCI9M26KJ1378E4F" "8IEFGJN3KLM2C49AO671BHD5"},
465 {8L, 24, 1, "DH2KL4IN678OA5C9EFGBJ1M3"},
466 {24L, 24, 2, "9FHI25LM6N78BC1ODEGJ34KA" "EAOFM74BJDK69GH5C3LI2N18"},
467 {48L, 24, 2, "HL5ANMO6BCI9G7DEJ132FK48" "8JEFGNC4KLM2I91BO673H5DA"},
468 {24L, 24, 2, "HO5ANGLDBCI9M26KJ1378E4F" "KN8EOCI9FGLDJ13HM2645A7B"},
469 {48L, 24, 2, "HL5ANMO6BCI9G7DEJ132FK48" "21678345DEFG9ABCKLMHIJON"},
471 "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F"
472 "8JEFGNC4KLM2I91BO673H5DA"},
474 "HLBANGO65JI9M2DEC4378K1F" "AO5HIMLDBCN3G76KJ192FE48"
475 "KIFEOCN38MLDJ19AG7645H2B"},
476 {16L, 24, 2, "DI7KLCN9FG6OJ4AH2ME5B381" "MBO8FH1JEKG23N45L679ACDI"},
477 {16L, 24, 2, "DI7KLCN9FG6OJ4AH2ME5B381" "IDCN97KLJ4AHFG6O5B32ME18"},
478 {48L, 24, 2, "9LCHI7ODJ43NFGEK5BA2M618" "7CDFGIJ4KL2MN95B6O8AH1E3"},
479 {24L, 24, 2, "LAGODI3JE87KCN9B6M254HF1" "EIL6MCN9GODFA54H87K3J12B"},
480 {24L, 24, 2, "D92KL1HI678O345NEFGABCMJ" "FBOM6NJ37LKE4IHA2GD1C985"},
482 "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F"
483 "21678345DEFG9ABCKLMHIJON"},
485 "HKBANFEO5JI98MLDC43G7612" "AE5HI8KLBCN3FGO6J19M2D47"
486 "85EFGABCKLM2HIJ1O67N34D9"},
488 {20L, 25, 1, "589CDEGHIJ6KLM2ANO4FP71B3"},
489 {480L, 25, 2, "589CDEGHIJ3KLM26NO4AP7FB1" "245789BCDE1GHIJ3KLM6NOAPF"},
492 "5O7Q92B4D6F8HAJCLENGPI1K3M" "21436587A9CBEDGFIHKJMLONQP"},
493 {12L, 26, 1, "6587A9CBEDGFIHKJMLONQP2143"},
495 {18L, 27, 1, "53BC689IJKDE4GHOP7LMANRFQ12"},
497 "54BC79AIJKEFGH1OPLM2N3RQ68D" "DI2LG5O67Q4NBCREF9A8JKMH1P3"},
499 "51BC234IJK6789AOPDEFGHRLMNQ" "3E89PLM1GHRB7Q64NKIJFDA5O2C"},
501 "54BC79AIJKEFGH1OPLM2N3RQ68D" "DR2LNKI67QA8P5OEFH1GBCM34J9"},
503 "51BC234IJK6789AOPDEFGHRLMNQ" "3689BDE1GHIJ2LM4N5OP7QACRFK"
504 "479ACEFGH1JKLM2N3OP5Q68RBDI"},
507 "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5129346D78AHBCELFGIPJKMSNOQR"},
508 {12L, 28, 1, "589AC3DEG7HIKBLMOFPQRJ2S4N61"},
510 "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5329176D4BAH8FELCJIPGNMSKRQO"},
512 "976D4BAH8FELCJIPGNMSKRQ5O321" "649A78DEBCHIFGLMJKPQNOS2R153"},
514 {28L, 29, 1, "56789ABCDEFGHIJKLMNOPQRST1234"},
517 "LHQ7NOTDERSA9JK6UGF1PBCM34I285" "BFHIL3NO5Q78RSATDE6UG9JKCM1P24"},
519 "DU4JOA89PS2GEFT56MKL7BCRQ1HI3N" "BQGHT36MNL78CRS1DEIU54JKOA9P2F"},
521 "DS4JU589POABEFT2GHKL76MNQ1CR3I" "215634ABC789GHIDEFMNOJKLRSPQUT"},
522 {8L, 30, 1, "HEMNJKCRS9PQIU5FT3OABL782G1D64"},
524 {30L, 31, 1, "56789ABCDEFGHIJKLMNOPQRSTUV1234"},
527 static const struct groups groups
[] = {
528 {0, NULL
}, /* trivial case: 0 */
529 {0, NULL
}, /* trivial case: 1 */
530 {1, groupdata
+ 0}, /* 2 */
531 {1, groupdata
+ 1}, /* 3 */
532 {2, groupdata
+ 2}, /* 4 */
533 {1, groupdata
+ 4}, /* 5 */
534 {2, groupdata
+ 5}, /* 6 */
535 {1, groupdata
+ 7}, /* 7 */
536 {5, groupdata
+ 8}, /* 8 */
537 {2, groupdata
+ 13}, /* 9 */
538 {2, groupdata
+ 15}, /* 10 */
539 {1, groupdata
+ 17}, /* 11 */
540 {5, groupdata
+ 18}, /* 12 */
541 {1, groupdata
+ 23}, /* 13 */
542 {2, groupdata
+ 24}, /* 14 */
543 {1, groupdata
+ 26}, /* 15 */
544 {14, groupdata
+ 27}, /* 16 */
545 {1, groupdata
+ 41}, /* 17 */
546 {5, groupdata
+ 42}, /* 18 */
547 {1, groupdata
+ 47}, /* 19 */
548 {5, groupdata
+ 48}, /* 20 */
549 {2, groupdata
+ 53}, /* 21 */
550 {2, groupdata
+ 55}, /* 22 */
551 {1, groupdata
+ 57}, /* 23 */
552 {15, groupdata
+ 58}, /* 24 */
553 {2, groupdata
+ 73}, /* 25 */
554 {2, groupdata
+ 75}, /* 26 */
555 {5, groupdata
+ 77}, /* 27 */
556 {4, groupdata
+ 82}, /* 28 */
557 {1, groupdata
+ 86}, /* 29 */
558 {4, groupdata
+ 87}, /* 30 */
559 {1, groupdata
+ 91}, /* 31 */
562 /* ----- data generated by group.gap ends ----- */
564 static char *new_game_desc(game_params
*params
, random_state
*rs
,
565 char **aux
, int interactive
)
567 int w
= params
->w
, a
= w
*w
;
568 digit
*grid
, *soln
, *soln2
;
571 int diff
= params
->diff
;
572 const struct group
*group
;
576 * Difficulty exceptions: some combinations of size and
577 * difficulty cannot be satisfied, because all puzzles of at
578 * most that difficulty are actually even easier.
580 * Remember to re-test this whenever a change is made to the
583 * I tested it using the following shell command:
585 for d in t n h x u; do
587 echo ./group --generate 1 ${i}d${d}
588 perl -e 'alarm 30; exec @ARGV' ./group --generate 5 ${i}d${d} >/dev/null \
593 * Of course, it's better to do that after taking the exceptions
594 * _out_, so as to detect exceptions that should be removed as
595 * well as those which should be added.
597 if (w
<= 9 && diff
== DIFF_EXTREME
)
599 if (w
<= 6 && diff
== DIFF_HARD
)
601 if (w
<= 4 && diff
> DIFF_TRIVIAL
)
604 grid
= snewn(a
, digit
);
605 soln
= snewn(a
, digit
);
606 soln2
= snewn(a
, digit
);
607 indices
= snewn(a
, int);
611 * Construct a valid group table, by picking a group from
612 * the above data table, decompressing it into a full
613 * representation by BFS, and then randomly permuting its
614 * non-identity elements.
616 * We build the canonical table in 'soln' (and use 'grid' as
617 * our BFS queue), then transfer the table into 'grid'
618 * having shuffled the rows.
621 assert(w
< lenof(groups
));
622 group
= groups
[w
].groups
+ random_upto(rs
, groups
[w
].ngroups
);
623 assert(group
->order
== w
);
625 for (i
= 0; i
< w
; i
++)
633 row
= soln
+ (i
-1)*w
;
635 for (j
= 0; j
< group
->ngens
; j
++) {
637 const char *gen
= group
->gens
+ j
*w
;
640 * Apply each group generator to row, constructing a
643 nri
= FROMCHAR(gen
[row
[0]-1]); /* which row is it? */
644 newrow
= soln
+ (nri
-1)*w
;
645 if (!newrow
[0]) { /* not done yet */
646 for (k
= 0; k
< w
; k
++)
647 newrow
[k
] = FROMCHAR(gen
[row
[k
]-1]);
652 /* That's got the canonical table. Now shuffle it. */
653 for (i
= 0; i
< w
; i
++)
655 if (params
->id
) /* do we shuffle in the identity? */
656 shuffle(soln2
+1, w
-1, sizeof(*soln2
), rs
);
658 shuffle(soln2
, w
, sizeof(*soln2
), rs
);
659 for (i
= 0; i
< w
; i
++)
660 for (j
= 0; j
< w
; j
++)
661 grid
[(soln2
[i
])*w
+(soln2
[j
])] = soln2
[soln
[i
*w
+j
]-1]+1;
664 * Remove entries one by one while the puzzle is still
665 * soluble at the appropriate difficulty level.
667 memcpy(soln
, grid
, a
);
670 * Start by blanking the entire identity row and column,
671 * and also another row and column so that the player
672 * can't trivially determine which element is the
676 j
= 1 + random_upto(rs
, w
-1); /* pick a second row/col to blank */
677 for (i
= 0; i
< w
; i
++) {
678 grid
[(soln2
[0])*w
+i
] = grid
[i
*w
+(soln2
[0])] = 0;
679 grid
[(soln2
[j
])*w
+i
] = grid
[i
*w
+(soln2
[j
])] = 0;
682 memcpy(soln2
, grid
, a
);
683 if (solver(w
, soln2
, diff
) > diff
)
684 continue; /* go round again if that didn't work */
688 for (i
= (params
->id ?
1 : 0); i
< w
; i
++)
689 for (j
= (params
->id ?
1 : 0); j
< w
; j
++)
691 indices
[k
++] = i
*w
+j
;
692 shuffle(indices
, k
, sizeof(*indices
), rs
);
694 for (i
= 0; i
< k
; i
++) {
695 memcpy(soln2
, grid
, a
);
696 soln2
[indices
[i
]] = 0;
697 if (solver(w
, soln2
, diff
) <= diff
)
698 grid
[indices
[i
]] = 0;
702 * Make sure the puzzle isn't too easy.
705 memcpy(soln2
, grid
, a
);
706 if (solver(w
, soln2
, diff
-1) < diff
)
707 continue; /* go round and try again */
717 * Encode the puzzle description.
719 desc
= snewn(a
*20, char);
720 p
= encode_grid(desc
, grid
, a
);
722 desc
= sresize(desc
, p
- desc
, char);
725 * Encode the solution.
727 *aux
= snewn(a
+2, char);
729 for (i
= 0; i
< a
; i
++)
730 (*aux
)[i
+1] = TOCHAR(soln
[i
]);
741 /* ----------------------------------------------------------------------
745 static char *validate_grid_desc(const char **pdesc
, int range
, int area
)
747 const char *desc
= *pdesc
;
749 while (*desc
&& *desc
!= ',') {
751 if (n
>= 'a' && n
<= 'z') {
752 squares
+= n
- 'a' + 1;
753 } else if (n
== '_') {
755 } else if (n
> '0' && n
<= '9') {
756 int val
= atoi(desc
-1);
757 if (val
< 1 || val
> range
)
758 return "Out-of-range number in game description";
760 while (*desc
>= '0' && *desc
<= '9')
763 return "Invalid character in game description";
767 return "Not enough data to fill grid";
770 return "Too much data to fit in grid";
775 static char *validate_desc(game_params
*params
, char *desc
)
777 int w
= params
->w
, a
= w
*w
;
778 const char *p
= desc
;
780 return validate_grid_desc(&p
, w
, a
);
783 static char *spec_to_grid(char *desc
, digit
*grid
, int area
)
786 while (*desc
&& *desc
!= ',') {
788 if (n
>= 'a' && n
<= 'z') {
789 int run
= n
- 'a' + 1;
790 assert(i
+ run
<= area
);
793 } else if (n
== '_') {
795 } else if (n
> '0' && n
<= '9') {
797 grid
[i
++] = atoi(desc
-1);
798 while (*desc
>= '0' && *desc
<= '9')
801 assert(!"We can't get here");
808 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
810 int w
= params
->w
, a
= w
*w
;
811 game_state
*state
= snew(game_state
);
814 state
->par
= *params
; /* structure copy */
815 state
->grid
= snewn(a
, digit
);
816 state
->immutable
= snewn(a
, unsigned char);
817 state
->pencil
= snewn(a
, int);
818 for (i
= 0; i
< a
; i
++) {
820 state
->immutable
[i
] = 0;
821 state
->pencil
[i
] = 0;
824 desc
= spec_to_grid(desc
, state
->grid
, a
);
825 for (i
= 0; i
< a
; i
++)
826 if (state
->grid
[i
] != 0)
827 state
->immutable
[i
] = TRUE
;
829 state
->completed
= state
->cheated
= FALSE
;
834 static game_state
*dup_game(game_state
*state
)
836 int w
= state
->par
.w
, a
= w
*w
;
837 game_state
*ret
= snew(game_state
);
839 ret
->par
= state
->par
; /* structure copy */
841 ret
->grid
= snewn(a
, digit
);
842 ret
->immutable
= snewn(a
, unsigned char);
843 ret
->pencil
= snewn(a
, int);
844 memcpy(ret
->grid
, state
->grid
, a
*sizeof(digit
));
845 memcpy(ret
->immutable
, state
->immutable
, a
*sizeof(unsigned char));
846 memcpy(ret
->pencil
, state
->pencil
, a
*sizeof(int));
848 ret
->completed
= state
->completed
;
849 ret
->cheated
= state
->cheated
;
854 static void free_game(game_state
*state
)
857 sfree(state
->immutable
);
858 sfree(state
->pencil
);
862 static char *solve_game(game_state
*state
, game_state
*currstate
,
863 char *aux
, char **error
)
865 int w
= state
->par
.w
, a
= w
*w
;
873 soln
= snewn(a
, digit
);
874 memcpy(soln
, state
->grid
, a
*sizeof(digit
));
876 ret
= solver(w
, soln
, DIFFCOUNT
-1);
878 if (ret
== diff_impossible
) {
879 *error
= "No solution exists for this puzzle";
881 } else if (ret
== diff_ambiguous
) {
882 *error
= "Multiple solutions exist for this puzzle";
885 out
= snewn(a
+2, char);
887 for (i
= 0; i
< a
; i
++)
888 out
[i
+1] = TOCHAR(soln
[i
]);
896 static int game_can_format_as_text_now(game_params
*params
)
901 static char *game_text_format(game_state
*state
)
903 int w
= state
->par
.w
;
907 ret
= snewn(2*w
*w
+1, char); /* leave room for terminating NUL */
910 for (y
= 0; y
< w
; y
++) {
911 for (x
= 0; x
< w
; x
++) {
912 digit d
= state
->grid
[y
*w
+x
];
929 assert(p
- ret
== 2*w
*w
);
936 * These are the coordinates of the currently highlighted
937 * square on the grid, if hshow = 1.
941 * This indicates whether the current highlight is a
942 * pencil-mark one or a real one.
946 * This indicates whether or not we're showing the highlight
947 * (used to be hx = hy = -1); important so that when we're
948 * using the cursor keys it doesn't keep coming back at a
949 * fixed position. When hshow = 1, pressing a valid number
950 * or letter key or Space will enter that number or letter in the grid.
954 * This indicates whether we're using the highlight as a cursor;
955 * it means that it doesn't vanish on a keypress, and that it is
956 * allowed on immutable squares.
961 static game_ui
*new_ui(game_state
*state
)
963 game_ui
*ui
= snew(game_ui
);
966 ui
->hpencil
= ui
->hshow
= ui
->hcursor
= 0;
971 static void free_ui(game_ui
*ui
)
976 static char *encode_ui(game_ui
*ui
)
981 static void decode_ui(game_ui
*ui
, char *encoding
)
985 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
986 game_state
*newstate
)
988 int w
= newstate
->par
.w
;
990 * We prevent pencil-mode highlighting of a filled square, unless
991 * we're using the cursor keys. So if the user has just filled in
992 * a square which we had a pencil-mode highlight in (by Undo, or
993 * by Redo, or by Solve), then we cancel the highlight.
995 if (ui
->hshow
&& ui
->hpencil
&& !ui
->hcursor
&&
996 newstate
->grid
[ui
->hy
* w
+ ui
->hx
] != 0) {
1001 #define PREFERRED_TILESIZE 48
1002 #define TILESIZE (ds->tilesize)
1003 #define BORDER (TILESIZE / 2)
1004 #define LEGEND (TILESIZE)
1005 #define GRIDEXTRA max((TILESIZE / 32),1)
1006 #define COORD(x) ((x)*TILESIZE + BORDER + LEGEND)
1007 #define FROMCOORD(x) (((x)+(TILESIZE-BORDER-LEGEND)) / TILESIZE - 1)
1009 #define FLASH_TIME 0.4F
1011 #define DF_HIGHLIGHT 0x0400
1012 #define DF_HIGHLIGHT_PENCIL 0x0200
1013 #define DF_IMMUTABLE 0x0100
1014 #define DF_DIGIT_MASK 0x001F
1016 #define EF_DIGIT_SHIFT 5
1017 #define EF_DIGIT_MASK ((1 << EF_DIGIT_SHIFT) - 1)
1018 #define EF_LEFT_SHIFT 0
1019 #define EF_RIGHT_SHIFT (3*EF_DIGIT_SHIFT)
1020 #define EF_LEFT_MASK ((1UL << (3*EF_DIGIT_SHIFT)) - 1UL)
1021 #define EF_RIGHT_MASK (EF_LEFT_MASK << EF_RIGHT_SHIFT)
1022 #define EF_LATIN (1UL << (6*EF_DIGIT_SHIFT))
1024 struct game_drawstate
{
1027 long *tiles
, *pencil
, *errors
;
1031 static int check_errors(game_state
*state
, long *errors
)
1033 int w
= state
->par
.w
, a
= w
*w
;
1034 digit
*grid
= state
->grid
;
1035 int i
, j
, k
, x
, y
, errs
= FALSE
;
1038 for (i
= 0; i
< a
; i
++)
1041 for (y
= 0; y
< w
; y
++) {
1042 unsigned long mask
= 0, errmask
= 0;
1043 for (x
= 0; x
< w
; x
++) {
1044 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1045 errmask
|= (mask
& bit
);
1049 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1053 for (x
= 0; x
< w
; x
++)
1054 if (errmask
& (1UL << grid
[y
*w
+x
]))
1055 errors
[y
*w
+x
] |= EF_LATIN
;
1060 for (x
= 0; x
< w
; x
++) {
1061 unsigned long mask
= 0, errmask
= 0;
1062 for (y
= 0; y
< w
; y
++) {
1063 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1064 errmask
|= (mask
& bit
);
1068 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1072 for (y
= 0; y
< w
; y
++)
1073 if (errmask
& (1UL << grid
[y
*w
+x
]))
1074 errors
[y
*w
+x
] |= EF_LATIN
;
1079 for (i
= 1; i
< w
; i
++)
1080 for (j
= 1; j
< w
; j
++)
1081 for (k
= 1; k
< w
; k
++)
1082 if (grid
[i
*w
+j
] && grid
[j
*w
+k
] &&
1083 grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
1084 grid
[i
*w
+(grid
[j
*w
+k
]-1)] &&
1085 grid
[(grid
[i
*w
+j
]-1)*w
+k
] != grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
1087 int a
= i
+1, b
= j
+1, c
= k
+1;
1088 int ab
= grid
[i
*w
+j
], bc
= grid
[j
*w
+k
];
1089 int left
= (ab
-1)*w
+(c
-1), right
= (a
-1)*w
+(bc
-1);
1091 * If the appropriate error slot is already
1092 * used for one of the squares, we don't
1093 * fill either of them.
1095 if (!(errors
[left
] & EF_LEFT_MASK
) &&
1096 !(errors
[right
] & EF_RIGHT_MASK
)) {
1099 err
= (err
<< EF_DIGIT_SHIFT
) | b
;
1100 err
= (err
<< EF_DIGIT_SHIFT
) | c
;
1101 errors
[left
] |= err
<< EF_LEFT_SHIFT
;
1102 errors
[right
] |= err
<< EF_RIGHT_SHIFT
;
1111 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1112 int x
, int y
, int button
)
1114 int w
= state
->par
.w
;
1118 button
&= ~MOD_MASK
;
1123 if (tx
>= 0 && tx
< w
&& ty
>= 0 && ty
< w
) {
1124 if (button
== LEFT_BUTTON
) {
1125 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1126 ui
->hshow
&& ui
->hpencil
== 0) {
1131 ui
->hshow
= !state
->immutable
[ty
*w
+tx
];
1135 return ""; /* UI activity occurred */
1137 if (button
== RIGHT_BUTTON
) {
1139 * Pencil-mode highlighting for non filled squares.
1141 if (state
->grid
[ty
*w
+tx
] == 0) {
1142 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1143 ui
->hshow
&& ui
->hpencil
) {
1155 return ""; /* UI activity occurred */
1158 if (IS_CURSOR_MOVE(button
)) {
1159 move_cursor(button
, &ui
->hx
, &ui
->hy
, w
, w
, 0);
1160 ui
->hshow
= ui
->hcursor
= 1;
1164 (button
== CURSOR_SELECT
)) {
1165 ui
->hpencil
= 1 - ui
->hpencil
;
1171 ((ISCHAR(button
) && FROMCHAR(button
) <= w
) ||
1172 button
== CURSOR_SELECT2
|| button
== '\b')) {
1173 int n
= FROMCHAR(button
);
1174 if (button
== CURSOR_SELECT2
|| button
== '\b')
1178 * Can't make pencil marks in a filled square. This can only
1179 * become highlighted if we're using cursor keys.
1181 if (ui
->hpencil
&& state
->grid
[ui
->hy
*w
+ui
->hx
])
1185 * Can't do anything to an immutable square.
1187 if (state
->immutable
[ui
->hy
*w
+ui
->hx
])
1190 sprintf(buf
, "%c%d,%d,%d",
1191 (char)(ui
->hpencil
&& n
> 0 ?
'P' : 'R'), ui
->hx
, ui
->hy
, n
);
1193 if (!ui
->hcursor
) ui
->hshow
= 0;
1198 if (button
== 'M' || button
== 'm')
1204 static game_state
*execute_move(game_state
*from
, char *move
)
1206 int w
= from
->par
.w
, a
= w
*w
;
1210 if (move
[0] == 'S') {
1211 ret
= dup_game(from
);
1212 ret
->completed
= ret
->cheated
= TRUE
;
1214 for (i
= 0; i
< a
; i
++) {
1215 if (!ISCHAR(move
[i
+1]) || FROMCHAR(move
[i
+1]) > w
) {
1219 ret
->grid
[i
] = FROMCHAR(move
[i
+1]);
1223 if (move
[a
+1] != '\0') {
1229 } else if ((move
[0] == 'P' || move
[0] == 'R') &&
1230 sscanf(move
+1, "%d,%d,%d", &x
, &y
, &n
) == 3 &&
1231 x
>= 0 && x
< w
&& y
>= 0 && y
< w
&& n
>= 0 && n
<= w
) {
1232 if (from
->immutable
[y
*w
+x
])
1235 ret
= dup_game(from
);
1236 if (move
[0] == 'P' && n
> 0) {
1237 ret
->pencil
[y
*w
+x
] ^= 1 << n
;
1239 ret
->grid
[y
*w
+x
] = n
;
1240 ret
->pencil
[y
*w
+x
] = 0;
1242 if (!ret
->completed
&& !check_errors(ret
, NULL
))
1243 ret
->completed
= TRUE
;
1246 } else if (move
[0] == 'M') {
1248 * Fill in absolutely all pencil marks everywhere. (I
1249 * wouldn't use this for actual play, but it's a handy
1250 * starting point when following through a set of
1251 * diagnostics output by the standalone solver.)
1253 ret
= dup_game(from
);
1254 for (i
= 0; i
< a
; i
++) {
1256 ret
->pencil
[i
] = (1 << (w
+1)) - (1 << 1);
1260 return NULL
; /* couldn't parse move string */
1263 /* ----------------------------------------------------------------------
1267 #define SIZE(w) ((w) * TILESIZE + 2*BORDER + LEGEND)
1269 static void game_compute_size(game_params
*params
, int tilesize
,
1272 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1273 struct { int tilesize
; } ads
, *ds
= &ads
;
1274 ads
.tilesize
= tilesize
;
1276 *x
= *y
= SIZE(params
->w
);
1279 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1280 game_params
*params
, int tilesize
)
1282 ds
->tilesize
= tilesize
;
1285 static float *game_colours(frontend
*fe
, int *ncolours
)
1287 float *ret
= snewn(3 * NCOLOURS
, float);
1289 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1291 ret
[COL_GRID
* 3 + 0] = 0.0F
;
1292 ret
[COL_GRID
* 3 + 1] = 0.0F
;
1293 ret
[COL_GRID
* 3 + 2] = 0.0F
;
1295 ret
[COL_USER
* 3 + 0] = 0.0F
;
1296 ret
[COL_USER
* 3 + 1] = 0.6F
* ret
[COL_BACKGROUND
* 3 + 1];
1297 ret
[COL_USER
* 3 + 2] = 0.0F
;
1299 ret
[COL_HIGHLIGHT
* 3 + 0] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 0];
1300 ret
[COL_HIGHLIGHT
* 3 + 1] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 1];
1301 ret
[COL_HIGHLIGHT
* 3 + 2] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 2];
1303 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
1304 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
1305 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
1307 ret
[COL_PENCIL
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1308 ret
[COL_PENCIL
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1309 ret
[COL_PENCIL
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
1311 *ncolours
= NCOLOURS
;
1315 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1317 int w
= state
->par
.w
, a
= w
*w
;
1318 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1323 ds
->started
= FALSE
;
1324 ds
->tiles
= snewn(a
, long);
1325 ds
->pencil
= snewn(a
, long);
1326 ds
->errors
= snewn(a
, long);
1327 for (i
= 0; i
< a
; i
++)
1328 ds
->tiles
[i
] = ds
->pencil
[i
] = -1;
1329 ds
->errtmp
= snewn(a
, long);
1334 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1343 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, int x
, int y
, long tile
,
1344 long pencil
, long error
)
1346 int w
= ds
->w
/* , a = w*w */;
1351 tx
= BORDER
+ LEGEND
+ x
* TILESIZE
+ 1;
1352 ty
= BORDER
+ LEGEND
+ y
* TILESIZE
+ 1;
1356 cw
= tw
= TILESIZE
-1;
1357 ch
= th
= TILESIZE
-1;
1359 clip(dr
, cx
, cy
, cw
, ch
);
1361 /* background needs erasing */
1362 draw_rect(dr
, cx
, cy
, cw
, ch
,
1363 (tile
& DF_HIGHLIGHT
) ? COL_HIGHLIGHT
: COL_BACKGROUND
);
1365 /* pencil-mode highlight */
1366 if (tile
& DF_HIGHLIGHT_PENCIL
) {
1370 coords
[2] = cx
+cw
/2;
1373 coords
[5] = cy
+ch
/2;
1374 draw_polygon(dr
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
1377 /* new number needs drawing? */
1378 if (tile
& DF_DIGIT_MASK
) {
1380 str
[0] = TOCHAR(tile
& DF_DIGIT_MASK
);
1381 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/2,
1382 FONT_VARIABLE
, TILESIZE
/2, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1383 (error
& EF_LATIN
) ? COL_ERROR
:
1384 (tile
& DF_IMMUTABLE
) ? COL_GRID
: COL_USER
, str
);
1386 if (error
& EF_LEFT_MASK
) {
1387 int a
= (error
>> (EF_LEFT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1388 int b
= (error
>> (EF_LEFT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1389 int c
= (error
>> (EF_LEFT_SHIFT
))&EF_DIGIT_MASK
;
1391 sprintf(buf
, "(%c%c)%c", TOCHAR(a
), TOCHAR(b
), TOCHAR(c
));
1392 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/6,
1393 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1396 if (error
& EF_RIGHT_MASK
) {
1397 int a
= (error
>> (EF_RIGHT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1398 int b
= (error
>> (EF_RIGHT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1399 int c
= (error
>> (EF_RIGHT_SHIFT
))&EF_DIGIT_MASK
;
1401 sprintf(buf
, "%c(%c%c)", TOCHAR(a
), TOCHAR(b
), TOCHAR(c
));
1402 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
- TILESIZE
/6,
1403 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1410 int pw
, ph
, minph
, pbest
, fontsize
;
1412 /* Count the pencil marks required. */
1413 for (i
= 1, npencil
= 0; i
<= w
; i
++)
1414 if (pencil
& (1 << i
))
1421 * Determine the bounding rectangle within which we're going
1422 * to put the pencil marks.
1424 /* Start with the whole square */
1425 pl
= tx
+ GRIDEXTRA
;
1426 pr
= pl
+ TILESIZE
- GRIDEXTRA
;
1427 pt
= ty
+ GRIDEXTRA
;
1428 pb
= pt
+ TILESIZE
- GRIDEXTRA
;
1431 * We arrange our pencil marks in a grid layout, with
1432 * the number of rows and columns adjusted to allow the
1433 * maximum font size.
1435 * So now we work out what the grid size ought to be.
1440 for (pw
= 3; pw
< max(npencil
,4); pw
++) {
1443 ph
= (npencil
+ pw
- 1) / pw
;
1444 ph
= max(ph
, minph
);
1445 fw
= (pr
- pl
) / (float)pw
;
1446 fh
= (pb
- pt
) / (float)ph
;
1448 if (fs
> bestsize
) {
1455 ph
= (npencil
+ pw
- 1) / pw
;
1456 ph
= max(ph
, minph
);
1459 * Now we've got our grid dimensions, work out the pixel
1460 * size of a grid element, and round it to the nearest
1461 * pixel. (We don't want rounding errors to make the
1462 * grid look uneven at low pixel sizes.)
1464 fontsize
= min((pr
- pl
) / pw
, (pb
- pt
) / ph
);
1467 * Centre the resulting figure in the square.
1469 pl
= tx
+ (TILESIZE
- fontsize
* pw
) / 2;
1470 pt
= ty
+ (TILESIZE
- fontsize
* ph
) / 2;
1473 * Now actually draw the pencil marks.
1475 for (i
= 1, j
= 0; i
<= w
; i
++)
1476 if (pencil
& (1 << i
)) {
1477 int dx
= j
% pw
, dy
= j
/ pw
;
1481 draw_text(dr
, pl
+ fontsize
* (2*dx
+1) / 2,
1482 pt
+ fontsize
* (2*dy
+1) / 2,
1483 FONT_VARIABLE
, fontsize
,
1484 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_PENCIL
, str
);
1492 draw_update(dr
, cx
, cy
, cw
, ch
);
1495 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1496 game_state
*state
, int dir
, game_ui
*ui
,
1497 float animtime
, float flashtime
)
1499 int w
= state
->par
.w
/*, a = w*w */;
1504 * The initial contents of the window are not guaranteed and
1505 * can vary with front ends. To be on the safe side, all
1506 * games should start by drawing a big background-colour
1507 * rectangle covering the whole window.
1509 draw_rect(dr
, 0, 0, SIZE(w
), SIZE(w
), COL_BACKGROUND
);
1512 * Big containing rectangle.
1514 draw_rect(dr
, COORD(0) - GRIDEXTRA
, COORD(0) - GRIDEXTRA
,
1515 w
*TILESIZE
+1+GRIDEXTRA
*2, w
*TILESIZE
+1+GRIDEXTRA
*2,
1521 for (x
= 0; x
< w
; x
++) {
1524 str
[0] = TOCHAR(x
+1);
1525 draw_text(dr
, COORD(x
) + TILESIZE
/2, BORDER
+ TILESIZE
/2,
1526 FONT_VARIABLE
, TILESIZE
/2,
1527 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_GRID
, str
);
1528 draw_text(dr
, BORDER
+ TILESIZE
/2, COORD(x
) + TILESIZE
/2,
1529 FONT_VARIABLE
, TILESIZE
/2,
1530 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_GRID
, str
);
1533 draw_update(dr
, 0, 0, SIZE(w
), SIZE(w
));
1538 check_errors(state
, ds
->errtmp
);
1540 for (y
= 0; y
< w
; y
++) {
1541 for (x
= 0; x
< w
; x
++) {
1542 long tile
= 0L, pencil
= 0L, error
;
1544 if (state
->grid
[y
*w
+x
])
1545 tile
= state
->grid
[y
*w
+x
];
1547 pencil
= (long)state
->pencil
[y
*w
+x
];
1549 if (state
->immutable
[y
*w
+x
])
1550 tile
|= DF_IMMUTABLE
;
1552 if (ui
->hshow
&& ui
->hx
== x
&& ui
->hy
== y
)
1553 tile
|= (ui
->hpencil ? DF_HIGHLIGHT_PENCIL
: DF_HIGHLIGHT
);
1555 if (flashtime
> 0 &&
1556 (flashtime
<= FLASH_TIME
/3 ||
1557 flashtime
>= FLASH_TIME
*2/3))
1558 tile
|= DF_HIGHLIGHT
; /* completion flash */
1560 error
= ds
->errtmp
[y
*w
+x
];
1562 if (ds
->tiles
[y
*w
+x
] != tile
||
1563 ds
->pencil
[y
*w
+x
] != pencil
||
1564 ds
->errors
[y
*w
+x
] != error
) {
1565 ds
->tiles
[y
*w
+x
] = tile
;
1566 ds
->pencil
[y
*w
+x
] = pencil
;
1567 ds
->errors
[y
*w
+x
] = error
;
1568 draw_tile(dr
, ds
, x
, y
, tile
, pencil
, error
);
1574 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1575 int dir
, game_ui
*ui
)
1580 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1581 int dir
, game_ui
*ui
)
1583 if (!oldstate
->completed
&& newstate
->completed
&&
1584 !oldstate
->cheated
&& !newstate
->cheated
)
1589 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1591 if (state
->completed
)
1596 static void game_print_size(game_params
*params
, float *x
, float *y
)
1601 * We use 9mm squares by default, like Solo.
1603 game_compute_size(params
, 900, &pw
, &ph
);
1608 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1610 int w
= state
->par
.w
;
1611 int ink
= print_mono_colour(dr
, 0);
1614 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1615 game_drawstate ads
, *ds
= &ads
;
1616 game_set_size(dr
, ds
, NULL
, tilesize
);
1621 print_line_width(dr
, 3 * TILESIZE
/ 40);
1622 draw_rect_outline(dr
, BORDER
+ LEGEND
, BORDER
+ LEGEND
,
1623 w
*TILESIZE
, w
*TILESIZE
, ink
);
1628 for (x
= 0; x
< w
; x
++) {
1631 str
[0] = TOCHAR(x
+1);
1632 draw_text(dr
, BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1633 BORDER
+ TILESIZE
/2,
1634 FONT_VARIABLE
, TILESIZE
/2,
1635 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1636 draw_text(dr
, BORDER
+ TILESIZE
/2,
1637 BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1638 FONT_VARIABLE
, TILESIZE
/2,
1639 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1645 for (x
= 1; x
< w
; x
++) {
1646 print_line_width(dr
, TILESIZE
/ 40);
1647 draw_line(dr
, BORDER
+LEGEND
+x
*TILESIZE
, BORDER
+LEGEND
,
1648 BORDER
+LEGEND
+x
*TILESIZE
, BORDER
+LEGEND
+w
*TILESIZE
, ink
);
1650 for (y
= 1; y
< w
; y
++) {
1651 print_line_width(dr
, TILESIZE
/ 40);
1652 draw_line(dr
, BORDER
+LEGEND
, BORDER
+LEGEND
+y
*TILESIZE
,
1653 BORDER
+LEGEND
+w
*TILESIZE
, BORDER
+LEGEND
+y
*TILESIZE
, ink
);
1659 for (y
= 0; y
< w
; y
++)
1660 for (x
= 0; x
< w
; x
++)
1661 if (state
->grid
[y
*w
+x
]) {
1664 str
[0] = TOCHAR(state
->grid
[y
*w
+x
]);
1665 draw_text(dr
, BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1666 BORDER
+LEGEND
+ y
*TILESIZE
+ TILESIZE
/2,
1667 FONT_VARIABLE
, TILESIZE
/2,
1668 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1673 #define thegame group
1676 const struct game thegame
= {
1677 "Group", NULL
, NULL
,
1684 TRUE
, game_configure
, custom_params
,
1692 TRUE
, game_can_format_as_text_now
, game_text_format
,
1700 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
1703 game_free_drawstate
,
1707 TRUE
, FALSE
, game_print_size
, game_print
,
1708 FALSE
, /* wants_statusbar */
1709 FALSE
, game_timing_state
,
1710 REQUIRE_RBUTTON
| REQUIRE_NUMPAD
, /* flags */
1713 #ifdef STANDALONE_SOLVER
1717 int main(int argc
, char **argv
)
1721 char *id
= NULL
, *desc
, *err
;
1724 int ret
, diff
, really_show_working
= FALSE
;
1726 while (--argc
> 0) {
1728 if (!strcmp(p
, "-v")) {
1729 really_show_working
= TRUE
;
1730 } else if (!strcmp(p
, "-g")) {
1732 } else if (*p
== '-') {
1733 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
1741 fprintf(stderr
, "usage: %s [-g | -v] <game_id>\n", argv
[0]);
1745 desc
= strchr(id
, ':');
1747 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
1752 p
= default_params();
1753 decode_params(p
, id
);
1754 err
= validate_desc(p
, desc
);
1756 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
1759 s
= new_game(NULL
, p
, desc
);
1761 grid
= snewn(p
->w
* p
->w
, digit
);
1764 * When solving a Normal puzzle, we don't want to bother the
1765 * user with Hard-level deductions. For this reason, we grade
1766 * the puzzle internally before doing anything else.
1768 ret
= -1; /* placate optimiser */
1769 solver_show_working
= FALSE
;
1770 for (diff
= 0; diff
< DIFFCOUNT
; diff
++) {
1771 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1772 ret
= solver(p
->w
, grid
, diff
);
1777 if (diff
== DIFFCOUNT
) {
1779 printf("Difficulty rating: ambiguous\n");
1781 printf("Unable to find a unique solution\n");
1784 if (ret
== diff_impossible
)
1785 printf("Difficulty rating: impossible (no solution exists)\n");
1787 printf("Difficulty rating: %s\n", group_diffnames
[ret
]);
1789 solver_show_working
= really_show_working
;
1790 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1791 ret
= solver(p
->w
, grid
, diff
);
1793 printf("Puzzle is inconsistent\n");
1795 memcpy(s
->grid
, grid
, p
->w
* p
->w
);
1796 fputs(game_text_format(s
), stdout
);
1806 /* vim: set shiftwidth=4 tabstop=8: */