d8fa223c |
1 | /* |
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. |
6 | * |
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. |
12 | * |
13 | * TODO: |
14 | * |
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 |
26 | * silly. |
d8fa223c |
27 | */ |
28 | |
29 | #include <stdio.h> |
30 | #include <stdlib.h> |
31 | #include <string.h> |
32 | #include <assert.h> |
33 | #include <ctype.h> |
34 | #include <math.h> |
35 | |
36 | #include "puzzles.h" |
37 | #include "latin.h" |
38 | |
39 | /* |
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. |
42 | */ |
43 | #define DIFFLIST(A) \ |
44 | A(TRIVIAL,Trivial,NULL,t) \ |
45 | A(NORMAL,Normal,solver_normal,n) \ |
46 | A(HARD,Hard,NULL,h) \ |
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) |
57 | |
58 | enum { |
59 | COL_BACKGROUND, |
d8fa223c |
60 | COL_GRID, |
61 | COL_USER, |
62 | COL_HIGHLIGHT, |
63 | COL_ERROR, |
64 | COL_PENCIL, |
65 | NCOLOURS |
66 | }; |
67 | |
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') |
73 | |
74 | struct game_params { |
b84b31a8 |
75 | int w, diff, id; |
d8fa223c |
76 | }; |
77 | |
78 | struct game_state { |
79 | game_params par; |
80 | digit *grid; |
81 | unsigned char *immutable; |
82 | int *pencil; /* bitmaps using bits 1<<1..1<<n */ |
83 | int completed, cheated; |
84 | }; |
85 | |
86 | static game_params *default_params(void) |
87 | { |
88 | game_params *ret = snew(game_params); |
89 | |
90 | ret->w = 6; |
91 | ret->diff = DIFF_NORMAL; |
b84b31a8 |
92 | ret->id = TRUE; |
d8fa223c |
93 | |
94 | return ret; |
95 | } |
96 | |
97 | const static struct game_params group_presets[] = { |
b84b31a8 |
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 }, |
d8fa223c |
105 | }; |
106 | |
107 | static int game_fetch_preset(int i, char **name, game_params **params) |
108 | { |
109 | game_params *ret; |
110 | char buf[80]; |
111 | |
112 | if (i < 0 || i >= lenof(group_presets)) |
113 | return FALSE; |
114 | |
115 | ret = snew(game_params); |
116 | *ret = group_presets[i]; /* structure copy */ |
117 | |
b84b31a8 |
118 | sprintf(buf, "%dx%d %s%s", ret->w, ret->w, group_diffnames[ret->diff], |
119 | ret->id ? "" : ", identity hidden"); |
d8fa223c |
120 | |
121 | *name = dupstr(buf); |
122 | *params = ret; |
123 | return TRUE; |
124 | } |
125 | |
126 | static void free_params(game_params *params) |
127 | { |
128 | sfree(params); |
129 | } |
130 | |
131 | static game_params *dup_params(game_params *params) |
132 | { |
133 | game_params *ret = snew(game_params); |
134 | *ret = *params; /* structure copy */ |
135 | return ret; |
136 | } |
137 | |
138 | static void decode_params(game_params *params, char const *string) |
139 | { |
140 | char const *p = string; |
141 | |
142 | params->w = atoi(p); |
143 | while (*p && isdigit((unsigned char)*p)) p++; |
b84b31a8 |
144 | params->diff = DIFF_NORMAL; |
145 | params->id = TRUE; |
146 | |
147 | while (*p) { |
148 | if (*p == 'd') { |
149 | int i; |
150 | p++; |
151 | params->diff = DIFFCOUNT+1; /* ...which is invalid */ |
152 | if (*p) { |
153 | for (i = 0; i < DIFFCOUNT; i++) { |
154 | if (*p == group_diffchars[i]) |
155 | params->diff = i; |
156 | } |
157 | p++; |
158 | } |
159 | } else if (*p == 'i') { |
160 | params->id = FALSE; |
161 | } |
d8fa223c |
162 | } |
163 | } |
164 | |
165 | static char *encode_params(game_params *params, int full) |
166 | { |
167 | char ret[80]; |
168 | |
169 | sprintf(ret, "%d", params->w); |
170 | if (full) |
171 | sprintf(ret + strlen(ret), "d%c", group_diffchars[params->diff]); |
b84b31a8 |
172 | if (!params->id) |
173 | sprintf(ret + strlen(ret), "i"); |
d8fa223c |
174 | |
175 | return dupstr(ret); |
176 | } |
177 | |
178 | static config_item *game_configure(game_params *params) |
179 | { |
180 | config_item *ret; |
181 | char buf[80]; |
182 | |
b84b31a8 |
183 | ret = snewn(4, config_item); |
d8fa223c |
184 | |
185 | ret[0].name = "Grid size"; |
186 | ret[0].type = C_STRING; |
187 | sprintf(buf, "%d", params->w); |
188 | ret[0].sval = dupstr(buf); |
189 | ret[0].ival = 0; |
190 | |
191 | ret[1].name = "Difficulty"; |
192 | ret[1].type = C_CHOICES; |
193 | ret[1].sval = DIFFCONFIG; |
194 | ret[1].ival = params->diff; |
195 | |
b84b31a8 |
196 | ret[2].name = "Show identity"; |
197 | ret[2].type = C_BOOLEAN; |
d8fa223c |
198 | ret[2].sval = NULL; |
b84b31a8 |
199 | ret[2].ival = params->id; |
200 | |
201 | ret[3].name = NULL; |
202 | ret[3].type = C_END; |
203 | ret[3].sval = NULL; |
204 | ret[3].ival = 0; |
d8fa223c |
205 | |
206 | return ret; |
207 | } |
208 | |
209 | static game_params *custom_params(config_item *cfg) |
210 | { |
211 | game_params *ret = snew(game_params); |
212 | |
213 | ret->w = atoi(cfg[0].sval); |
214 | ret->diff = cfg[1].ival; |
b84b31a8 |
215 | ret->id = cfg[2].ival; |
d8fa223c |
216 | |
217 | return ret; |
218 | } |
219 | |
220 | static char *validate_params(game_params *params, int full) |
221 | { |
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"; |
226 | return NULL; |
227 | } |
228 | |
229 | /* ---------------------------------------------------------------------- |
230 | * Solver. |
231 | */ |
232 | |
233 | static int solver_normal(struct latin_solver *solver, void *vctx) |
234 | { |
235 | int w = solver->o; |
236 | digit *grid = solver->grid; |
237 | int i, j, k; |
238 | |
239 | /* |
240 | * Deduce using associativity: (ab)c = a(bc). |
241 | * |
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). |
244 | */ |
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]) |
249 | continue; |
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, "", |
258 | i+1, j+1, k+1, |
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, "", |
262 | n, x+1, y+1); |
263 | } |
264 | #endif |
265 | if (solver->cube[(x*w+y)*w+n-1]) { |
266 | latin_solver_place(solver, x, y, n); |
267 | return 1; |
268 | } else { |
269 | #ifdef STANDALONE_SOLVER |
270 | if (solver_show_working) |
271 | printf("%*s contradiction!\n", |
272 | solver_recurse_depth*4, ""); |
273 | return -1; |
274 | #endif |
275 | } |
276 | } |
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, "", |
285 | i+1, j+1, k+1, |
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, "", |
289 | n, x+1, y+1); |
290 | } |
291 | #endif |
292 | if (solver->cube[(x*w+y)*w+n-1]) { |
293 | latin_solver_place(solver, x, y, n); |
294 | return 1; |
295 | } else { |
296 | #ifdef STANDALONE_SOLVER |
297 | if (solver_show_working) |
298 | printf("%*s contradiction!\n", |
299 | solver_recurse_depth*4, ""); |
300 | return -1; |
301 | #endif |
302 | } |
303 | } |
304 | } |
305 | |
306 | return 0; |
307 | } |
308 | |
309 | #define SOLVER(upper,title,func,lower) func, |
310 | static usersolver_t const group_solvers[] = { DIFFLIST(SOLVER) }; |
311 | |
312 | static int solver(int w, digit *grid, int maxdiff) |
313 | { |
314 | int ret; |
315 | |
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); |
320 | |
321 | return ret; |
322 | } |
323 | |
324 | /* ---------------------------------------------------------------------- |
325 | * Grid generation. |
326 | */ |
327 | |
328 | static char *encode_grid(char *desc, digit *grid, int area) |
329 | { |
330 | int run, i; |
331 | char *p = desc; |
332 | |
333 | run = 0; |
334 | for (i = 0; i <= area; i++) { |
335 | int n = (i < area ? grid[i] : -1); |
336 | |
337 | if (!n) |
338 | run++; |
339 | else { |
340 | if (run) { |
341 | while (run > 0) { |
342 | int c = 'a' - 1 + run; |
343 | if (run > 26) |
344 | c = 'z'; |
345 | *p++ = c; |
346 | run -= c - ('a' - 1); |
347 | } |
348 | } else { |
349 | /* |
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. |
353 | */ |
354 | if (p > desc && n > 0) |
355 | *p++ = '_'; |
356 | } |
357 | if (n > 0) |
358 | p += sprintf(p, "%d", n); |
359 | run = 0; |
360 | } |
361 | } |
362 | return p; |
363 | } |
364 | |
365 | /* ----- data generated by group.gap begins ----- */ |
366 | |
367 | struct group { |
368 | unsigned long autosize; |
369 | int order, ngens; |
370 | const char *gens; |
371 | }; |
372 | struct groups { |
373 | int ngroups; |
374 | const struct group *groups; |
375 | }; |
376 | |
377 | static const struct group groupdata[] = { |
378 | /* order 2 */ |
379 | {1L, 2, 1, "21"}, |
380 | /* order 3 */ |
381 | {2L, 3, 1, "231"}, |
382 | /* order 4 */ |
383 | {2L, 4, 1, "2341"}, |
384 | {6L, 4, 2, "2143" "3412"}, |
385 | /* order 5 */ |
386 | {4L, 5, 1, "23451"}, |
387 | /* order 6 */ |
388 | {6L, 6, 2, "365214" "214365"}, |
389 | {2L, 6, 1, "436521"}, |
390 | /* order 7 */ |
391 | {6L, 7, 1, "2345671"}, |
392 | /* order 8 */ |
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"}, |
398 | /* order 9 */ |
399 | {6L, 9, 1, "245378691"}, |
400 | {48L, 9, 2, "245178396" "356781924"}, |
401 | /* order 10 */ |
402 | {20L, 10, 2, "3A52749618" "21436587A9"}, |
403 | {4L, 10, 1, "436587A921"}, |
404 | /* order 11 */ |
405 | {10L, 11, 1, "23456789AB1"}, |
406 | /* order 12 */ |
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"}, |
412 | /* order 13 */ |
413 | {12L, 13, 1, "23456789ABCD1"}, |
414 | /* order 14 */ |
415 | {42L, 14, 2, "5C7E92B4D6183A" "21436587A9CBED"}, |
416 | {6L, 14, 1, "6587A9CBED2143"}, |
417 | /* order 15 */ |
418 | {8L, 15, 1, "5783AB6DE9F2C41"}, |
419 | /* order 16 */ |
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"}, |
429 | {192L, 16, 3, |
430 | "D38G619AE2C45F7B" "25678AB1CDEF34G9" "7BC2EF546G8A91D3"}, |
431 | {64L, 16, 3, "D38G619AE2C45F7B" "CF76GBA92ED54381" "3D19A8G645FE2CB7"}, |
432 | {192L, 16, 3, |
433 | "9GB3F7DC54A2E618" "25678AB1CDEF34G9" "3D59A2G6B1F78C4E"}, |
434 | {48L, 16, 3, "9G4AFE6C5B327D18" "6A2CD5F378GB19E4" "4795BC8EAF1DG236"}, |
435 | {20160L, 16, 4, |
436 | "58AB1DE2F34G679C" "21678345CDE9ABGF" "3619A2CD45F78GBE" |
437 | "4791BC2E3F56G8AD"}, |
438 | /* order 17 */ |
439 | {16L, 17, 1, "56789ABCDEFGH1234"}, |
440 | /* order 18 */ |
441 | {54L, 18, 2, "DB9HFGE17CI5342A86" "215634ABC789FGDEIH"}, |
442 | {6L, 18, 1, "53AB786FG4DECI9H21"}, |
443 | {12L, 18, 2, "53AB782FG1DE6I4HC9" "BEFGH36I5978CA1D24"}, |
444 | {432L, 18, 3, |
445 | "96E1BCH34FG278I5DA" "EFH36I978BCA1DG245" "215634ABC789FGDEIH"}, |
446 | {48L, 18, 2, "53AB782FG1DE6I4HC9" "64BC89FG2DE1I5H3A7"}, |
447 | /* order 19 */ |
448 | {18L, 19, 1, "56789ABCDEFGHIJ1234"}, |
449 | /* order 20 */ |
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"}, |
455 | /* order 21 */ |
456 | {42L, 21, 2, "9KCJ2FL5I4817B3AE6DHG" "5A8CDBFGEIJH1LK342679"}, |
457 | {12L, 21, 1, "5783AB6DE9GHCJKFL2I41"}, |
458 | /* order 22 */ |
459 | {110L, 22, 2, "5K7M92B4D6F8HAJCLE1G3I" "21436587A9CBEDGFIHKJML"}, |
460 | {10L, 22, 1, "6587A9CBEDGFIHKJML2143"}, |
461 | /* order 23 */ |
462 | {22L, 23, 1, "56789ABCDEFGHIJKLMN1234"}, |
463 | /* order 24 */ |
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"}, |
470 | {48L, 24, 3, |
471 | "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F" |
472 | "8JEFGNC4KLM2I91BO673H5DA"}, |
473 | {24L, 24, 3, |
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"}, |
481 | {144L, 24, 3, |
482 | "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F" |
483 | "21678345DEFG9ABCKLMHIJON"}, |
484 | {336L, 24, 3, |
485 | "HKBANFEO5JI98MLDC43G7612" "AE5HI8KLBCN3FGO6J19M2D47" |
486 | "85EFGABCKLM2HIJ1O67N34D9"}, |
487 | /* order 25 */ |
488 | {20L, 25, 1, "589CDEGHIJ6KLM2ANO4FP71B3"}, |
489 | {480L, 25, 2, "589CDEGHIJ3KLM26NO4AP7FB1" "245789BCDE1GHIJ3KLM6NOAPF"}, |
490 | /* order 26 */ |
491 | {156L, 26, 2, |
492 | "5O7Q92B4D6F8HAJCLENGPI1K3M" "21436587A9CBEDGFIHKJMLONQP"}, |
493 | {12L, 26, 1, "6587A9CBEDGFIHKJMLONQP2143"}, |
494 | /* order 27 */ |
495 | {18L, 27, 1, "53BC689IJKDE4GHOP7LMANRFQ12"}, |
496 | {108L, 27, 2, |
497 | "54BC79AIJKEFGH1OPLM2N3RQ68D" "DI2LG5O67Q4NBCREF9A8JKMH1P3"}, |
498 | {432L, 27, 2, |
499 | "51BC234IJK6789AOPDEFGHRLMNQ" "3E89PLM1GHRB7Q64NKIJFDA5O2C"}, |
500 | {54L, 27, 2, |
501 | "54BC79AIJKEFGH1OPLM2N3RQ68D" "DR2LNKI67QA8P5OEFH1GBCM34J9"}, |
502 | {11232L, 27, 3, |
503 | "51BC234IJK6789AOPDEFGHRLMNQ" "3689BDE1GHIJ2LM4N5OP7QACRFK" |
504 | "479ACEFGH1JKLM2N3OP5Q68RBDI"}, |
505 | /* order 28 */ |
506 | {84L, 28, 2, |
507 | "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5129346D78AHBCELFGIPJKMSNOQR"}, |
508 | {12L, 28, 1, "589AC3DEG7HIKBLMOFPQRJ2S4N61"}, |
509 | {84L, 28, 2, |
510 | "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5329176D4BAH8FELCJIPGNMSKRQO"}, |
511 | {36L, 28, 2, |
512 | "976D4BAH8FELCJIPGNMSKRQ5O321" "649A78DEBCHIFGLMJKPQNOS2R153"}, |
513 | /* order 29 */ |
514 | {28L, 29, 1, "56789ABCDEFGHIJKLMNOPQRST1234"}, |
515 | /* order 30 */ |
516 | {24L, 30, 2, |
517 | "LHQ7NOTDERSA9JK6UGF1PBCM34I285" "BFHIL3NO5Q78RSATDE6UG9JKCM1P24"}, |
518 | {40L, 30, 2, |
519 | "DU4JOA89PS2GEFT56MKL7BCRQ1HI3N" "BQGHT36MNL78CRS1DEIU54JKOA9P2F"}, |
520 | {120L, 30, 2, |
521 | "DS4JU589POABEFT2GHKL76MNQ1CR3I" "215634ABC789GHIDEFMNOJKLRSPQUT"}, |
522 | {8L, 30, 1, "HEMNJKCRS9PQIU5FT3OABL782G1D64"}, |
523 | /* order 31 */ |
524 | {30L, 31, 1, "56789ABCDEFGHIJKLMNOPQRSTUV1234"}, |
525 | }; |
526 | |
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 */ |
560 | }; |
561 | |
562 | /* ----- data generated by group.gap ends ----- */ |
563 | |
564 | static char *new_game_desc(game_params *params, random_state *rs, |
565 | char **aux, int interactive) |
566 | { |
567 | int w = params->w, a = w*w; |
568 | digit *grid, *soln, *soln2; |
569 | int *indices; |
570 | int i, j, k, qh, qt; |
571 | int diff = params->diff; |
572 | const struct group *group; |
573 | char *desc, *p; |
574 | |
575 | /* |
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. |
579 | * |
580 | * Remember to re-test this whenever a change is made to the |
581 | * solver logic! |
582 | * |
583 | * I tested it using the following shell command: |
584 | |
585 | for d in t n h x u; do |
586 | for i in {3..9}; do |
587 | echo ./group --generate 1 ${i}d${d} |
588 | perl -e 'alarm 30; exec @ARGV' ./group --generate 5 ${i}d${d} >/dev/null \ |
589 | || echo broken |
590 | done |
591 | done |
592 | |
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. |
596 | */ |
597 | if (w <= 9 && diff == DIFF_EXTREME) |
598 | diff--; |
599 | if (w <= 6 && diff == DIFF_HARD) |
600 | diff--; |
601 | if (w <= 4 && diff > DIFF_TRIVIAL) |
602 | diff = DIFF_TRIVIAL; |
603 | |
604 | grid = snewn(a, digit); |
605 | soln = snewn(a, digit); |
606 | soln2 = snewn(a, digit); |
607 | indices = snewn(a, int); |
608 | |
609 | while (1) { |
610 | /* |
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. |
615 | * |
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. |
619 | */ |
620 | assert(w >= 2); |
621 | assert(w < lenof(groups)); |
622 | group = groups[w].groups + random_upto(rs, groups[w].ngroups); |
623 | assert(group->order == w); |
624 | memset(soln, 0, a); |
625 | for (i = 0; i < w; i++) |
626 | soln[i] = i+1; |
627 | qh = qt = 0; |
628 | grid[qt++] = 1; |
629 | while (qh < qt) { |
630 | digit *row, *newrow; |
631 | |
632 | i = grid[qh++]; |
633 | row = soln + (i-1)*w; |
634 | |
635 | for (j = 0; j < group->ngens; j++) { |
636 | int nri; |
637 | const char *gen = group->gens + j*w; |
638 | |
639 | /* |
640 | * Apply each group generator to row, constructing a |
641 | * new row. |
642 | */ |
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]); |
648 | grid[qt++] = nri; |
649 | } |
650 | } |
651 | } |
652 | /* That's got the canonical table. Now shuffle it. */ |
653 | for (i = 0; i < w; i++) |
b84b31a8 |
654 | soln2[i] = i; |
655 | if (params->id) /* do we shuffle in the identity? */ |
656 | shuffle(soln2+1, w-1, sizeof(*soln2), rs); |
657 | else |
658 | shuffle(soln2, w, sizeof(*soln2), rs); |
d8fa223c |
659 | for (i = 0; i < w; i++) |
b84b31a8 |
660 | for (j = 0; j < w; j++) |
661 | grid[(soln2[i])*w+(soln2[j])] = soln2[soln[i*w+j]-1]+1; |
d8fa223c |
662 | |
663 | /* |
664 | * Remove entries one by one while the puzzle is still |
665 | * soluble at the appropriate difficulty level. |
666 | */ |
667 | memcpy(soln, grid, a); |
b84b31a8 |
668 | if (!params->id) { |
669 | /* |
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 |
673 | * identity. |
674 | */ |
675 | |
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; |
680 | } |
681 | |
682 | memcpy(soln2, grid, a); |
683 | if (solver(w, soln2, diff) > diff) |
684 | continue; /* go round again if that didn't work */ |
685 | } |
d8fa223c |
686 | |
687 | k = 0; |
b84b31a8 |
688 | for (i = (params->id ? 1 : 0); i < w; i++) |
689 | for (j = (params->id ? 1 : 0); j < w; j++) |
690 | if (grid[i*w+j]) |
691 | indices[k++] = i*w+j; |
7a8dfea2 |
692 | shuffle(indices, k, sizeof(*indices), rs); |
d8fa223c |
693 | |
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; |
699 | } |
700 | |
701 | /* |
702 | * Make sure the puzzle isn't too easy. |
703 | */ |
704 | if (diff > 0) { |
705 | memcpy(soln2, grid, a); |
706 | if (solver(w, soln2, diff-1) < diff) |
707 | continue; /* go round and try again */ |
708 | } |
709 | |
710 | /* |
711 | * Done. |
712 | */ |
713 | break; |
714 | } |
715 | |
716 | /* |
717 | * Encode the puzzle description. |
718 | */ |
719 | desc = snewn(a*20, char); |
720 | p = encode_grid(desc, grid, a); |
721 | *p++ = '\0'; |
722 | desc = sresize(desc, p - desc, char); |
723 | |
724 | /* |
725 | * Encode the solution. |
726 | */ |
727 | *aux = snewn(a+2, char); |
728 | (*aux)[0] = 'S'; |
729 | for (i = 0; i < a; i++) |
730 | (*aux)[i+1] = TOCHAR(soln[i]); |
731 | (*aux)[a+1] = '\0'; |
732 | |
733 | sfree(grid); |
734 | sfree(soln); |
735 | sfree(soln2); |
736 | sfree(indices); |
737 | |
738 | return desc; |
739 | } |
740 | |
741 | /* ---------------------------------------------------------------------- |
742 | * Gameplay. |
743 | */ |
744 | |
745 | static char *validate_grid_desc(const char **pdesc, int range, int area) |
746 | { |
747 | const char *desc = *pdesc; |
748 | int squares = 0; |
749 | while (*desc && *desc != ',') { |
750 | int n = *desc++; |
751 | if (n >= 'a' && n <= 'z') { |
752 | squares += n - 'a' + 1; |
753 | } else if (n == '_') { |
754 | /* do nothing */; |
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"; |
759 | squares++; |
760 | while (*desc >= '0' && *desc <= '9') |
761 | desc++; |
762 | } else |
763 | return "Invalid character in game description"; |
764 | } |
765 | |
766 | if (squares < area) |
767 | return "Not enough data to fill grid"; |
768 | |
769 | if (squares > area) |
770 | return "Too much data to fit in grid"; |
771 | *pdesc = desc; |
772 | return NULL; |
773 | } |
774 | |
775 | static char *validate_desc(game_params *params, char *desc) |
776 | { |
777 | int w = params->w, a = w*w; |
778 | const char *p = desc; |
779 | |
780 | return validate_grid_desc(&p, w, a); |
781 | } |
782 | |
783 | static char *spec_to_grid(char *desc, digit *grid, int area) |
784 | { |
785 | int i = 0; |
786 | while (*desc && *desc != ',') { |
787 | int n = *desc++; |
788 | if (n >= 'a' && n <= 'z') { |
789 | int run = n - 'a' + 1; |
790 | assert(i + run <= area); |
791 | while (run-- > 0) |
792 | grid[i++] = 0; |
793 | } else if (n == '_') { |
794 | /* do nothing */; |
795 | } else if (n > '0' && n <= '9') { |
796 | assert(i < area); |
797 | grid[i++] = atoi(desc-1); |
798 | while (*desc >= '0' && *desc <= '9') |
799 | desc++; |
800 | } else { |
801 | assert(!"We can't get here"); |
802 | } |
803 | } |
804 | assert(i == area); |
805 | return desc; |
806 | } |
807 | |
808 | static game_state *new_game(midend *me, game_params *params, char *desc) |
809 | { |
810 | int w = params->w, a = w*w; |
811 | game_state *state = snew(game_state); |
812 | int i; |
813 | |
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++) { |
819 | state->grid[i] = 0; |
820 | state->immutable[i] = 0; |
821 | state->pencil[i] = 0; |
822 | } |
823 | |
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; |
828 | |
829 | state->completed = state->cheated = FALSE; |
830 | |
831 | return state; |
832 | } |
833 | |
834 | static game_state *dup_game(game_state *state) |
835 | { |
836 | int w = state->par.w, a = w*w; |
837 | game_state *ret = snew(game_state); |
838 | |
839 | ret->par = state->par; /* structure copy */ |
840 | |
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)); |
847 | |
848 | ret->completed = state->completed; |
849 | ret->cheated = state->cheated; |
850 | |
851 | return ret; |
852 | } |
853 | |
854 | static void free_game(game_state *state) |
855 | { |
856 | sfree(state->grid); |
857 | sfree(state->immutable); |
858 | sfree(state->pencil); |
859 | sfree(state); |
860 | } |
861 | |
862 | static char *solve_game(game_state *state, game_state *currstate, |
863 | char *aux, char **error) |
864 | { |
865 | int w = state->par.w, a = w*w; |
866 | int i, ret; |
867 | digit *soln; |
868 | char *out; |
869 | |
870 | if (aux) |
871 | return dupstr(aux); |
872 | |
873 | soln = snewn(a, digit); |
874 | memcpy(soln, state->grid, a*sizeof(digit)); |
875 | |
876 | ret = solver(w, soln, DIFFCOUNT-1); |
877 | |
878 | if (ret == diff_impossible) { |
879 | *error = "No solution exists for this puzzle"; |
880 | out = NULL; |
881 | } else if (ret == diff_ambiguous) { |
882 | *error = "Multiple solutions exist for this puzzle"; |
883 | out = NULL; |
884 | } else { |
885 | out = snewn(a+2, char); |
886 | out[0] = 'S'; |
887 | for (i = 0; i < a; i++) |
888 | out[i+1] = TOCHAR(soln[i]); |
889 | out[a+1] = '\0'; |
890 | } |
891 | |
892 | sfree(soln); |
893 | return out; |
894 | } |
895 | |
896 | static int game_can_format_as_text_now(game_params *params) |
897 | { |
898 | return TRUE; |
899 | } |
900 | |
901 | static char *game_text_format(game_state *state) |
902 | { |
903 | int w = state->par.w; |
904 | int x, y; |
905 | char *ret, *p, ch; |
906 | |
907 | ret = snewn(2*w*w+1, char); /* leave room for terminating NUL */ |
908 | |
909 | p = ret; |
910 | for (y = 0; y < w; y++) { |
911 | for (x = 0; x < w; x++) { |
912 | digit d = state->grid[y*w+x]; |
913 | |
914 | if (d == 0) { |
915 | ch = '.'; |
916 | } else { |
917 | ch = TOCHAR(d); |
918 | } |
919 | |
920 | *p++ = ch; |
921 | if (x == w-1) { |
922 | *p++ = '\n'; |
923 | } else { |
924 | *p++ = ' '; |
925 | } |
926 | } |
927 | } |
928 | |
929 | assert(p - ret == 2*w*w); |
930 | *p = '\0'; |
931 | return ret; |
932 | } |
933 | |
934 | struct game_ui { |
935 | /* |
936 | * These are the coordinates of the currently highlighted |
937 | * square on the grid, if hshow = 1. |
938 | */ |
939 | int hx, hy; |
940 | /* |
941 | * This indicates whether the current highlight is a |
942 | * pencil-mark one or a real one. |
943 | */ |
944 | int hpencil; |
945 | /* |
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. |
951 | */ |
952 | int hshow; |
953 | /* |
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. |
957 | */ |
958 | int hcursor; |
959 | }; |
960 | |
961 | static game_ui *new_ui(game_state *state) |
962 | { |
963 | game_ui *ui = snew(game_ui); |
964 | |
b84b31a8 |
965 | ui->hx = ui->hy = 0; |
d8fa223c |
966 | ui->hpencil = ui->hshow = ui->hcursor = 0; |
967 | |
968 | return ui; |
969 | } |
970 | |
971 | static void free_ui(game_ui *ui) |
972 | { |
973 | sfree(ui); |
974 | } |
975 | |
976 | static char *encode_ui(game_ui *ui) |
977 | { |
978 | return NULL; |
979 | } |
980 | |
981 | static void decode_ui(game_ui *ui, char *encoding) |
982 | { |
983 | } |
984 | |
985 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
986 | game_state *newstate) |
987 | { |
988 | int w = newstate->par.w; |
989 | /* |
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. |
994 | */ |
995 | if (ui->hshow && ui->hpencil && !ui->hcursor && |
996 | newstate->grid[ui->hy * w + ui->hx] != 0) { |
997 | ui->hshow = 0; |
998 | } |
999 | } |
1000 | |
1001 | #define PREFERRED_TILESIZE 48 |
1002 | #define TILESIZE (ds->tilesize) |
1003 | #define BORDER (TILESIZE / 2) |
b84b31a8 |
1004 | #define LEGEND (TILESIZE) |
d8fa223c |
1005 | #define GRIDEXTRA max((TILESIZE / 32),1) |
b84b31a8 |
1006 | #define COORD(x) ((x)*TILESIZE + BORDER + LEGEND) |
1007 | #define FROMCOORD(x) (((x)+(TILESIZE-BORDER-LEGEND)) / TILESIZE - 1) |
d8fa223c |
1008 | |
1009 | #define FLASH_TIME 0.4F |
1010 | |
1011 | #define DF_HIGHLIGHT 0x0400 |
1012 | #define DF_HIGHLIGHT_PENCIL 0x0200 |
1013 | #define DF_IMMUTABLE 0x0100 |
1014 | #define DF_DIGIT_MASK 0x001F |
1015 | |
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)) |
1023 | |
1024 | struct game_drawstate { |
1025 | int w, tilesize; |
1026 | int started; |
1027 | long *tiles, *pencil, *errors; |
1028 | long *errtmp; |
1029 | }; |
1030 | |
1031 | static int check_errors(game_state *state, long *errors) |
1032 | { |
1033 | int w = state->par.w, a = w*w; |
1034 | digit *grid = state->grid; |
1035 | int i, j, k, x, y, errs = FALSE; |
1036 | |
1037 | if (errors) |
1038 | for (i = 0; i < a; i++) |
1039 | errors[i] = 0; |
1040 | |
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); |
1046 | mask |= bit; |
1047 | } |
1048 | |
1049 | if (mask != (1 << (w+1)) - (1 << 1)) { |
1050 | errs = TRUE; |
1051 | errmask &= ~1UL; |
1052 | if (errors) { |
1053 | for (x = 0; x < w; x++) |
1054 | if (errmask & (1UL << grid[y*w+x])) |
1055 | errors[y*w+x] |= EF_LATIN; |
1056 | } |
1057 | } |
1058 | } |
1059 | |
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); |
1065 | mask |= bit; |
1066 | } |
1067 | |
1068 | if (mask != (1 << (w+1)) - (1 << 1)) { |
1069 | errs = TRUE; |
1070 | errmask &= ~1UL; |
1071 | if (errors) { |
1072 | for (y = 0; y < w; y++) |
1073 | if (errmask & (1UL << grid[y*w+x])) |
1074 | errors[y*w+x] |= EF_LATIN; |
1075 | } |
1076 | } |
1077 | } |
1078 | |
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)]) { |
1086 | if (errors) { |
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); |
1090 | /* |
1091 | * If the appropriate error slot is already |
1092 | * used for one of the squares, we don't |
1093 | * fill either of them. |
1094 | */ |
1095 | if (!(errors[left] & EF_LEFT_MASK) && |
1096 | !(errors[right] & EF_RIGHT_MASK)) { |
1097 | long err; |
1098 | err = a; |
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; |
1103 | } |
1104 | } |
1105 | errs = TRUE; |
1106 | } |
1107 | |
1108 | return errs; |
1109 | } |
1110 | |
1111 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
1112 | int x, int y, int button) |
1113 | { |
1114 | int w = state->par.w; |
1115 | int tx, ty; |
1116 | char buf[80]; |
1117 | |
1118 | button &= ~MOD_MASK; |
1119 | |
1120 | tx = FROMCOORD(x); |
1121 | ty = FROMCOORD(y); |
1122 | |
b84b31a8 |
1123 | if (tx >= 0 && tx < w && ty >= 0 && ty < w) { |
d8fa223c |
1124 | if (button == LEFT_BUTTON) { |
1125 | if (tx == ui->hx && ty == ui->hy && |
1126 | ui->hshow && ui->hpencil == 0) { |
1127 | ui->hshow = 0; |
1128 | } else { |
1129 | ui->hx = tx; |
1130 | ui->hy = ty; |
1131 | ui->hshow = !state->immutable[ty*w+tx]; |
1132 | ui->hpencil = 0; |
1133 | } |
1134 | ui->hcursor = 0; |
1135 | return ""; /* UI activity occurred */ |
1136 | } |
1137 | if (button == RIGHT_BUTTON) { |
1138 | /* |
1139 | * Pencil-mode highlighting for non filled squares. |
1140 | */ |
1141 | if (state->grid[ty*w+tx] == 0) { |
1142 | if (tx == ui->hx && ty == ui->hy && |
1143 | ui->hshow && ui->hpencil) { |
1144 | ui->hshow = 0; |
1145 | } else { |
1146 | ui->hpencil = 1; |
1147 | ui->hx = tx; |
1148 | ui->hy = ty; |
1149 | ui->hshow = 1; |
1150 | } |
1151 | } else { |
1152 | ui->hshow = 0; |
1153 | } |
1154 | ui->hcursor = 0; |
1155 | return ""; /* UI activity occurred */ |
1156 | } |
1157 | } |
1158 | if (IS_CURSOR_MOVE(button)) { |
b84b31a8 |
1159 | move_cursor(button, &ui->hx, &ui->hy, w, w, 0); |
d8fa223c |
1160 | ui->hshow = ui->hcursor = 1; |
1161 | return ""; |
1162 | } |
1163 | if (ui->hshow && |
1164 | (button == CURSOR_SELECT)) { |
1165 | ui->hpencil = 1 - ui->hpencil; |
1166 | ui->hcursor = 1; |
1167 | return ""; |
1168 | } |
1169 | |
1170 | if (ui->hshow && |
1171 | ((ISCHAR(button) && FROMCHAR(button) <= w) || |
1172 | button == CURSOR_SELECT2 || button == '\b')) { |
1173 | int n = FROMCHAR(button); |
1174 | if (button == CURSOR_SELECT2 || button == '\b') |
1175 | n = 0; |
1176 | |
1177 | /* |
1178 | * Can't make pencil marks in a filled square. This can only |
1179 | * become highlighted if we're using cursor keys. |
1180 | */ |
1181 | if (ui->hpencil && state->grid[ui->hy*w+ui->hx]) |
1182 | return NULL; |
1183 | |
1184 | /* |
1185 | * Can't do anything to an immutable square. |
1186 | */ |
1187 | if (state->immutable[ui->hy*w+ui->hx]) |
1188 | return NULL; |
1189 | |
1190 | sprintf(buf, "%c%d,%d,%d", |
1191 | (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n); |
1192 | |
1193 | if (!ui->hcursor) ui->hshow = 0; |
1194 | |
1195 | return dupstr(buf); |
1196 | } |
1197 | |
1198 | if (button == 'M' || button == 'm') |
1199 | return dupstr("M"); |
1200 | |
1201 | return NULL; |
1202 | } |
1203 | |
1204 | static game_state *execute_move(game_state *from, char *move) |
1205 | { |
1206 | int w = from->par.w, a = w*w; |
1207 | game_state *ret; |
1208 | int x, y, i, n; |
1209 | |
1210 | if (move[0] == 'S') { |
1211 | ret = dup_game(from); |
1212 | ret->completed = ret->cheated = TRUE; |
1213 | |
1214 | for (i = 0; i < a; i++) { |
1215 | if (!ISCHAR(move[i+1]) || FROMCHAR(move[i+1]) > w) { |
1216 | free_game(ret); |
1217 | return NULL; |
1218 | } |
1219 | ret->grid[i] = FROMCHAR(move[i+1]); |
1220 | ret->pencil[i] = 0; |
1221 | } |
1222 | |
1223 | if (move[a+1] != '\0') { |
1224 | free_game(ret); |
1225 | return NULL; |
1226 | } |
1227 | |
1228 | return ret; |
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]) |
1233 | return NULL; |
1234 | |
1235 | ret = dup_game(from); |
1236 | if (move[0] == 'P' && n > 0) { |
1237 | ret->pencil[y*w+x] ^= 1 << n; |
1238 | } else { |
1239 | ret->grid[y*w+x] = n; |
1240 | ret->pencil[y*w+x] = 0; |
1241 | |
1242 | if (!ret->completed && !check_errors(ret, NULL)) |
1243 | ret->completed = TRUE; |
1244 | } |
1245 | return ret; |
1246 | } else if (move[0] == 'M') { |
1247 | /* |
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.) |
1252 | */ |
1253 | ret = dup_game(from); |
1254 | for (i = 0; i < a; i++) { |
1255 | if (!ret->grid[i]) |
1256 | ret->pencil[i] = (1 << (w+1)) - (1 << 1); |
1257 | } |
1258 | return ret; |
1259 | } else |
1260 | return NULL; /* couldn't parse move string */ |
1261 | } |
1262 | |
1263 | /* ---------------------------------------------------------------------- |
1264 | * Drawing routines. |
1265 | */ |
1266 | |
b84b31a8 |
1267 | #define SIZE(w) ((w) * TILESIZE + 2*BORDER + LEGEND) |
d8fa223c |
1268 | |
1269 | static void game_compute_size(game_params *params, int tilesize, |
1270 | int *x, int *y) |
1271 | { |
1272 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1273 | struct { int tilesize; } ads, *ds = &ads; |
1274 | ads.tilesize = tilesize; |
1275 | |
1276 | *x = *y = SIZE(params->w); |
1277 | } |
1278 | |
1279 | static void game_set_size(drawing *dr, game_drawstate *ds, |
1280 | game_params *params, int tilesize) |
1281 | { |
1282 | ds->tilesize = tilesize; |
1283 | } |
1284 | |
1285 | static float *game_colours(frontend *fe, int *ncolours) |
1286 | { |
1287 | float *ret = snewn(3 * NCOLOURS, float); |
1288 | |
1289 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
1290 | |
1291 | ret[COL_GRID * 3 + 0] = 0.0F; |
1292 | ret[COL_GRID * 3 + 1] = 0.0F; |
1293 | ret[COL_GRID * 3 + 2] = 0.0F; |
1294 | |
d8fa223c |
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; |
1298 | |
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]; |
1302 | |
1303 | ret[COL_ERROR * 3 + 0] = 1.0F; |
1304 | ret[COL_ERROR * 3 + 1] = 0.0F; |
1305 | ret[COL_ERROR * 3 + 2] = 0.0F; |
1306 | |
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]; |
1310 | |
1311 | *ncolours = NCOLOURS; |
1312 | return ret; |
1313 | } |
1314 | |
1315 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
1316 | { |
1317 | int w = state->par.w, a = w*w; |
1318 | struct game_drawstate *ds = snew(struct game_drawstate); |
1319 | int i; |
1320 | |
1321 | ds->w = w; |
1322 | ds->tilesize = 0; |
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); |
1330 | |
1331 | return ds; |
1332 | } |
1333 | |
1334 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
1335 | { |
1336 | sfree(ds->tiles); |
1337 | sfree(ds->pencil); |
1338 | sfree(ds->errors); |
1339 | sfree(ds->errtmp); |
1340 | sfree(ds); |
1341 | } |
1342 | |
d2cfd12c |
1343 | static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile, |
1344 | long pencil, long error) |
d8fa223c |
1345 | { |
1346 | int w = ds->w /* , a = w*w */; |
1347 | int tx, ty, tw, th; |
1348 | int cx, cy, cw, ch; |
1349 | char str[64]; |
1350 | |
b84b31a8 |
1351 | tx = BORDER + LEGEND + x * TILESIZE + 1; |
1352 | ty = BORDER + LEGEND + y * TILESIZE + 1; |
d8fa223c |
1353 | |
1354 | cx = tx; |
1355 | cy = ty; |
1356 | cw = tw = TILESIZE-1; |
1357 | ch = th = TILESIZE-1; |
1358 | |
1359 | clip(dr, cx, cy, cw, ch); |
1360 | |
1361 | /* background needs erasing */ |
1362 | draw_rect(dr, cx, cy, cw, ch, |
b84b31a8 |
1363 | (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND); |
d8fa223c |
1364 | |
1365 | /* pencil-mode highlight */ |
1366 | if (tile & DF_HIGHLIGHT_PENCIL) { |
1367 | int coords[6]; |
1368 | coords[0] = cx; |
1369 | coords[1] = cy; |
1370 | coords[2] = cx+cw/2; |
1371 | coords[3] = cy; |
1372 | coords[4] = cx; |
1373 | coords[5] = cy+ch/2; |
1374 | draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); |
1375 | } |
1376 | |
1377 | /* new number needs drawing? */ |
1378 | if (tile & DF_DIGIT_MASK) { |
1379 | str[1] = '\0'; |
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); |
1385 | |
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; |
1390 | char buf[10]; |
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, |
1394 | COL_ERROR, buf); |
1395 | } |
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; |
1400 | char buf[10]; |
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, |
1404 | COL_ERROR, buf); |
1405 | } |
1406 | } else { |
1407 | int i, j, npencil; |
1408 | int pl, pr, pt, pb; |
1409 | float bestsize; |
1410 | int pw, ph, minph, pbest, fontsize; |
1411 | |
1412 | /* Count the pencil marks required. */ |
1413 | for (i = 1, npencil = 0; i <= w; i++) |
1414 | if (pencil & (1 << i)) |
1415 | npencil++; |
1416 | if (npencil) { |
1417 | |
1418 | minph = 2; |
1419 | |
1420 | /* |
1421 | * Determine the bounding rectangle within which we're going |
1422 | * to put the pencil marks. |
1423 | */ |
1424 | /* Start with the whole square */ |
1425 | pl = tx + GRIDEXTRA; |
1426 | pr = pl + TILESIZE - GRIDEXTRA; |
1427 | pt = ty + GRIDEXTRA; |
1428 | pb = pt + TILESIZE - GRIDEXTRA; |
1429 | |
1430 | /* |
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. |
1434 | * |
1435 | * So now we work out what the grid size ought to be. |
1436 | */ |
1437 | bestsize = 0.0; |
1438 | pbest = 0; |
1439 | /* Minimum */ |
1440 | for (pw = 3; pw < max(npencil,4); pw++) { |
1441 | float fw, fh, fs; |
1442 | |
1443 | ph = (npencil + pw - 1) / pw; |
1444 | ph = max(ph, minph); |
1445 | fw = (pr - pl) / (float)pw; |
1446 | fh = (pb - pt) / (float)ph; |
1447 | fs = min(fw, fh); |
1448 | if (fs > bestsize) { |
1449 | bestsize = fs; |
1450 | pbest = pw; |
1451 | } |
1452 | } |
1453 | assert(pbest > 0); |
1454 | pw = pbest; |
1455 | ph = (npencil + pw - 1) / pw; |
1456 | ph = max(ph, minph); |
1457 | |
1458 | /* |
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.) |
1463 | */ |
1464 | fontsize = min((pr - pl) / pw, (pb - pt) / ph); |
1465 | |
1466 | /* |
1467 | * Centre the resulting figure in the square. |
1468 | */ |
1469 | pl = tx + (TILESIZE - fontsize * pw) / 2; |
1470 | pt = ty + (TILESIZE - fontsize * ph) / 2; |
1471 | |
1472 | /* |
1473 | * Now actually draw the pencil marks. |
1474 | */ |
1475 | for (i = 1, j = 0; i <= w; i++) |
1476 | if (pencil & (1 << i)) { |
1477 | int dx = j % pw, dy = j / pw; |
1478 | |
1479 | str[1] = '\0'; |
1480 | str[0] = TOCHAR(i); |
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); |
1485 | j++; |
1486 | } |
1487 | } |
1488 | } |
1489 | |
1490 | unclip(dr); |
1491 | |
1492 | draw_update(dr, cx, cy, cw, ch); |
1493 | } |
1494 | |
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) |
1498 | { |
1499 | int w = state->par.w /*, a = w*w */; |
1500 | int x, y; |
1501 | |
1502 | if (!ds->started) { |
1503 | /* |
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. |
1508 | */ |
1509 | draw_rect(dr, 0, 0, SIZE(w), SIZE(w), COL_BACKGROUND); |
1510 | |
1511 | /* |
1512 | * Big containing rectangle. |
1513 | */ |
1514 | draw_rect(dr, COORD(0) - GRIDEXTRA, COORD(0) - GRIDEXTRA, |
1515 | w*TILESIZE+1+GRIDEXTRA*2, w*TILESIZE+1+GRIDEXTRA*2, |
1516 | COL_GRID); |
1517 | |
b84b31a8 |
1518 | /* |
1519 | * Table legend. |
1520 | */ |
1521 | for (x = 0; x < w; x++) { |
1522 | char str[2]; |
1523 | str[1] = '\0'; |
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); |
1531 | } |
1532 | |
d8fa223c |
1533 | draw_update(dr, 0, 0, SIZE(w), SIZE(w)); |
1534 | |
1535 | ds->started = TRUE; |
1536 | } |
1537 | |
1538 | check_errors(state, ds->errtmp); |
1539 | |
1540 | for (y = 0; y < w; y++) { |
1541 | for (x = 0; x < w; x++) { |
1542 | long tile = 0L, pencil = 0L, error; |
1543 | |
1544 | if (state->grid[y*w+x]) |
1545 | tile = state->grid[y*w+x]; |
1546 | else |
1547 | pencil = (long)state->pencil[y*w+x]; |
1548 | |
1549 | if (state->immutable[y*w+x]) |
1550 | tile |= DF_IMMUTABLE; |
1551 | |
1552 | if (ui->hshow && ui->hx == x && ui->hy == y) |
1553 | tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT); |
1554 | |
1555 | if (flashtime > 0 && |
1556 | (flashtime <= FLASH_TIME/3 || |
1557 | flashtime >= FLASH_TIME*2/3)) |
1558 | tile |= DF_HIGHLIGHT; /* completion flash */ |
1559 | |
1560 | error = ds->errtmp[y*w+x]; |
1561 | |
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); |
1569 | } |
1570 | } |
1571 | } |
1572 | } |
1573 | |
1574 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
1575 | int dir, game_ui *ui) |
1576 | { |
1577 | return 0.0F; |
1578 | } |
1579 | |
1580 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
1581 | int dir, game_ui *ui) |
1582 | { |
1583 | if (!oldstate->completed && newstate->completed && |
1584 | !oldstate->cheated && !newstate->cheated) |
1585 | return FLASH_TIME; |
1586 | return 0.0F; |
1587 | } |
1588 | |
1589 | static int game_timing_state(game_state *state, game_ui *ui) |
1590 | { |
1591 | if (state->completed) |
1592 | return FALSE; |
1593 | return TRUE; |
1594 | } |
1595 | |
1596 | static void game_print_size(game_params *params, float *x, float *y) |
1597 | { |
1598 | int pw, ph; |
1599 | |
1600 | /* |
1601 | * We use 9mm squares by default, like Solo. |
1602 | */ |
1603 | game_compute_size(params, 900, &pw, &ph); |
1604 | *x = pw / 100.0F; |
1605 | *y = ph / 100.0F; |
1606 | } |
1607 | |
1608 | static void game_print(drawing *dr, game_state *state, int tilesize) |
1609 | { |
1610 | int w = state->par.w; |
1611 | int ink = print_mono_colour(dr, 0); |
d8fa223c |
1612 | int x, y; |
1613 | |
1614 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1615 | game_drawstate ads, *ds = &ads; |
1616 | game_set_size(dr, ds, NULL, tilesize); |
1617 | |
1618 | /* |
b84b31a8 |
1619 | * Border. |
d8fa223c |
1620 | */ |
b84b31a8 |
1621 | print_line_width(dr, 3 * TILESIZE / 40); |
1622 | draw_rect_outline(dr, BORDER + LEGEND, BORDER + LEGEND, |
1623 | w*TILESIZE, w*TILESIZE, ink); |
d8fa223c |
1624 | |
1625 | /* |
b84b31a8 |
1626 | * Legend on table. |
d8fa223c |
1627 | */ |
b84b31a8 |
1628 | for (x = 0; x < w; x++) { |
1629 | char str[2]; |
1630 | str[1] = '\0'; |
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); |
1640 | } |
d8fa223c |
1641 | |
1642 | /* |
1643 | * Main grid. |
1644 | */ |
1645 | for (x = 1; x < w; x++) { |
1646 | print_line_width(dr, TILESIZE / 40); |
b84b31a8 |
1647 | draw_line(dr, BORDER+LEGEND+x*TILESIZE, BORDER+LEGEND, |
1648 | BORDER+LEGEND+x*TILESIZE, BORDER+LEGEND+w*TILESIZE, ink); |
d8fa223c |
1649 | } |
1650 | for (y = 1; y < w; y++) { |
1651 | print_line_width(dr, TILESIZE / 40); |
b84b31a8 |
1652 | draw_line(dr, BORDER+LEGEND, BORDER+LEGEND+y*TILESIZE, |
1653 | BORDER+LEGEND+w*TILESIZE, BORDER+LEGEND+y*TILESIZE, ink); |
d8fa223c |
1654 | } |
1655 | |
1656 | /* |
1657 | * Numbers. |
1658 | */ |
1659 | for (y = 0; y < w; y++) |
1660 | for (x = 0; x < w; x++) |
1661 | if (state->grid[y*w+x]) { |
1662 | char str[2]; |
1663 | str[1] = '\0'; |
1664 | str[0] = TOCHAR(state->grid[y*w+x]); |
b84b31a8 |
1665 | draw_text(dr, BORDER+LEGEND + x*TILESIZE + TILESIZE/2, |
1666 | BORDER+LEGEND + y*TILESIZE + TILESIZE/2, |
d8fa223c |
1667 | FONT_VARIABLE, TILESIZE/2, |
1668 | ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str); |
1669 | } |
1670 | } |
1671 | |
1672 | #ifdef COMBINED |
1673 | #define thegame group |
1674 | #endif |
1675 | |
1676 | const struct game thegame = { |
1677 | "Group", NULL, NULL, |
1678 | default_params, |
1679 | game_fetch_preset, |
1680 | decode_params, |
1681 | encode_params, |
1682 | free_params, |
1683 | dup_params, |
1684 | TRUE, game_configure, custom_params, |
1685 | validate_params, |
1686 | new_game_desc, |
1687 | validate_desc, |
1688 | new_game, |
1689 | dup_game, |
1690 | free_game, |
1691 | TRUE, solve_game, |
1692 | TRUE, game_can_format_as_text_now, game_text_format, |
1693 | new_ui, |
1694 | free_ui, |
1695 | encode_ui, |
1696 | decode_ui, |
1697 | game_changed_state, |
1698 | interpret_move, |
1699 | execute_move, |
1700 | PREFERRED_TILESIZE, game_compute_size, game_set_size, |
1701 | game_colours, |
1702 | game_new_drawstate, |
1703 | game_free_drawstate, |
1704 | game_redraw, |
1705 | game_anim_length, |
1706 | game_flash_length, |
1707 | TRUE, FALSE, game_print_size, game_print, |
1708 | FALSE, /* wants_statusbar */ |
1709 | FALSE, game_timing_state, |
1710 | REQUIRE_RBUTTON | REQUIRE_NUMPAD, /* flags */ |
1711 | }; |
1712 | |
1713 | #ifdef STANDALONE_SOLVER |
1714 | |
1715 | #include <stdarg.h> |
1716 | |
1717 | int main(int argc, char **argv) |
1718 | { |
1719 | game_params *p; |
1720 | game_state *s; |
1721 | char *id = NULL, *desc, *err; |
1722 | digit *grid; |
1723 | int grade = FALSE; |
1724 | int ret, diff, really_show_working = FALSE; |
1725 | |
1726 | while (--argc > 0) { |
1727 | char *p = *++argv; |
1728 | if (!strcmp(p, "-v")) { |
1729 | really_show_working = TRUE; |
1730 | } else if (!strcmp(p, "-g")) { |
1731 | grade = TRUE; |
1732 | } else if (*p == '-') { |
1733 | fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); |
1734 | return 1; |
1735 | } else { |
1736 | id = p; |
1737 | } |
1738 | } |
1739 | |
1740 | if (!id) { |
1741 | fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]); |
1742 | return 1; |
1743 | } |
1744 | |
1745 | desc = strchr(id, ':'); |
1746 | if (!desc) { |
1747 | fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); |
1748 | return 1; |
1749 | } |
1750 | *desc++ = '\0'; |
1751 | |
1752 | p = default_params(); |
1753 | decode_params(p, id); |
1754 | err = validate_desc(p, desc); |
1755 | if (err) { |
1756 | fprintf(stderr, "%s: %s\n", argv[0], err); |
1757 | return 1; |
1758 | } |
1759 | s = new_game(NULL, p, desc); |
1760 | |
1761 | grid = snewn(p->w * p->w, digit); |
1762 | |
1763 | /* |
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. |
1767 | */ |
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); |
1773 | if (ret <= diff) |
1774 | break; |
1775 | } |
1776 | |
1777 | if (diff == DIFFCOUNT) { |
1778 | if (grade) |
1779 | printf("Difficulty rating: ambiguous\n"); |
1780 | else |
1781 | printf("Unable to find a unique solution\n"); |
1782 | } else { |
1783 | if (grade) { |
1784 | if (ret == diff_impossible) |
1785 | printf("Difficulty rating: impossible (no solution exists)\n"); |
1786 | else |
1787 | printf("Difficulty rating: %s\n", group_diffnames[ret]); |
1788 | } else { |
1789 | solver_show_working = really_show_working; |
1790 | memcpy(grid, s->grid, p->w * p->w); |
1791 | ret = solver(p->w, grid, diff); |
1792 | if (ret != diff) |
1793 | printf("Puzzle is inconsistent\n"); |
1794 | else { |
1795 | memcpy(s->grid, grid, p->w * p->w); |
1796 | fputs(game_text_format(s), stdout); |
1797 | } |
1798 | } |
1799 | } |
1800 | |
1801 | return 0; |
1802 | } |
1803 | |
1804 | #endif |
1805 | |
1806 | /* vim: set shiftwidth=4 tabstop=8: */ |