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 | |
5523645e |
68 | /* |
69 | * In identity mode, we number the elements e,a,b,c,d,f,g,h,... |
70 | * Otherwise, they're a,b,c,d,e,f,g,h,... in the obvious way. |
71 | */ |
72 | #define E_TO_FRONT(c,id) ( (id) && (c)<=5 ? (c) % 5 + 1 : (c) ) |
73 | #define E_FROM_FRONT(c,id) ( (id) && (c)<=5 ? ((c) + 3) % 5 + 1 : (c) ) |
74 | |
75 | #define FROMCHAR(c,id) E_TO_FRONT((((c)-('A'-1)) & ~0x20), id) |
76 | #define ISCHAR(c) (((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z')) |
77 | #define TOCHAR(c,id) (E_FROM_FRONT(c,id) + ('a'-1)) |
d8fa223c |
78 | |
79 | struct game_params { |
b84b31a8 |
80 | int w, diff, id; |
d8fa223c |
81 | }; |
82 | |
83 | struct game_state { |
84 | game_params par; |
85 | digit *grid; |
86 | unsigned char *immutable; |
87 | int *pencil; /* bitmaps using bits 1<<1..1<<n */ |
88 | int completed, cheated; |
f536db21 |
89 | digit *sequence; /* sequence of group elements shown */ |
a346afcd |
90 | |
91 | /* |
92 | * This array indicates thick lines separating rows and columns |
93 | * placed and unplaced manually by the user as a visual aid, e.g. |
94 | * to delineate a subgroup and its cosets. |
95 | * |
96 | * When a line is placed, it's deemed to be between the two |
97 | * particular group elements that are on either side of it at the |
98 | * time; dragging those two away from each other automatically |
99 | * gets rid of the line. Hence, for a given element i, dividers[i] |
100 | * is either -1 (indicating no divider to the right of i), or some |
101 | * other element (indicating a divider to the right of i iff that |
102 | * element is the one right of it). These are eagerly cleared |
103 | * during drags. |
104 | */ |
105 | int *dividers; /* thick lines between rows/cols */ |
d8fa223c |
106 | }; |
107 | |
108 | static game_params *default_params(void) |
109 | { |
110 | game_params *ret = snew(game_params); |
111 | |
112 | ret->w = 6; |
113 | ret->diff = DIFF_NORMAL; |
b84b31a8 |
114 | ret->id = TRUE; |
d8fa223c |
115 | |
116 | return ret; |
117 | } |
118 | |
119 | const static struct game_params group_presets[] = { |
b84b31a8 |
120 | { 6, DIFF_NORMAL, TRUE }, |
121 | { 6, DIFF_NORMAL, FALSE }, |
b84b31a8 |
122 | { 8, DIFF_NORMAL, TRUE }, |
123 | { 8, DIFF_NORMAL, FALSE }, |
5523645e |
124 | { 8, DIFF_HARD, TRUE }, |
125 | { 8, DIFF_HARD, FALSE }, |
b84b31a8 |
126 | { 12, DIFF_NORMAL, TRUE }, |
d8fa223c |
127 | }; |
128 | |
129 | static int game_fetch_preset(int i, char **name, game_params **params) |
130 | { |
131 | game_params *ret; |
132 | char buf[80]; |
133 | |
134 | if (i < 0 || i >= lenof(group_presets)) |
135 | return FALSE; |
136 | |
137 | ret = snew(game_params); |
138 | *ret = group_presets[i]; /* structure copy */ |
139 | |
b84b31a8 |
140 | sprintf(buf, "%dx%d %s%s", ret->w, ret->w, group_diffnames[ret->diff], |
141 | ret->id ? "" : ", identity hidden"); |
d8fa223c |
142 | |
143 | *name = dupstr(buf); |
144 | *params = ret; |
145 | return TRUE; |
146 | } |
147 | |
148 | static void free_params(game_params *params) |
149 | { |
150 | sfree(params); |
151 | } |
152 | |
153 | static game_params *dup_params(game_params *params) |
154 | { |
155 | game_params *ret = snew(game_params); |
156 | *ret = *params; /* structure copy */ |
157 | return ret; |
158 | } |
159 | |
160 | static void decode_params(game_params *params, char const *string) |
161 | { |
162 | char const *p = string; |
163 | |
164 | params->w = atoi(p); |
165 | while (*p && isdigit((unsigned char)*p)) p++; |
b84b31a8 |
166 | params->diff = DIFF_NORMAL; |
167 | params->id = TRUE; |
168 | |
169 | while (*p) { |
170 | if (*p == 'd') { |
171 | int i; |
172 | p++; |
173 | params->diff = DIFFCOUNT+1; /* ...which is invalid */ |
174 | if (*p) { |
175 | for (i = 0; i < DIFFCOUNT; i++) { |
176 | if (*p == group_diffchars[i]) |
177 | params->diff = i; |
178 | } |
179 | p++; |
180 | } |
181 | } else if (*p == 'i') { |
182 | params->id = FALSE; |
5523645e |
183 | p++; |
184 | } else { |
185 | /* unrecognised character */ |
186 | p++; |
b84b31a8 |
187 | } |
d8fa223c |
188 | } |
189 | } |
190 | |
191 | static char *encode_params(game_params *params, int full) |
192 | { |
193 | char ret[80]; |
194 | |
195 | sprintf(ret, "%d", params->w); |
196 | if (full) |
197 | sprintf(ret + strlen(ret), "d%c", group_diffchars[params->diff]); |
b84b31a8 |
198 | if (!params->id) |
199 | sprintf(ret + strlen(ret), "i"); |
d8fa223c |
200 | |
201 | return dupstr(ret); |
202 | } |
203 | |
204 | static config_item *game_configure(game_params *params) |
205 | { |
206 | config_item *ret; |
207 | char buf[80]; |
208 | |
b84b31a8 |
209 | ret = snewn(4, config_item); |
d8fa223c |
210 | |
211 | ret[0].name = "Grid size"; |
212 | ret[0].type = C_STRING; |
213 | sprintf(buf, "%d", params->w); |
214 | ret[0].sval = dupstr(buf); |
215 | ret[0].ival = 0; |
216 | |
217 | ret[1].name = "Difficulty"; |
218 | ret[1].type = C_CHOICES; |
219 | ret[1].sval = DIFFCONFIG; |
220 | ret[1].ival = params->diff; |
221 | |
b84b31a8 |
222 | ret[2].name = "Show identity"; |
223 | ret[2].type = C_BOOLEAN; |
d8fa223c |
224 | ret[2].sval = NULL; |
b84b31a8 |
225 | ret[2].ival = params->id; |
226 | |
227 | ret[3].name = NULL; |
228 | ret[3].type = C_END; |
229 | ret[3].sval = NULL; |
230 | ret[3].ival = 0; |
d8fa223c |
231 | |
232 | return ret; |
233 | } |
234 | |
235 | static game_params *custom_params(config_item *cfg) |
236 | { |
237 | game_params *ret = snew(game_params); |
238 | |
239 | ret->w = atoi(cfg[0].sval); |
240 | ret->diff = cfg[1].ival; |
b84b31a8 |
241 | ret->id = cfg[2].ival; |
d8fa223c |
242 | |
243 | return ret; |
244 | } |
245 | |
246 | static char *validate_params(game_params *params, int full) |
247 | { |
5523645e |
248 | if (params->w < 3 || params->w > 26) |
249 | return "Grid size must be between 3 and 26"; |
d8fa223c |
250 | if (params->diff >= DIFFCOUNT) |
251 | return "Unknown difficulty rating"; |
5523645e |
252 | if (!params->id && params->diff == DIFF_TRIVIAL) { |
253 | /* |
254 | * We can't have a Trivial-difficulty puzzle (i.e. latin |
255 | * square deductions only) without a clear identity, because |
256 | * identityless puzzles always have two rows and two columns |
257 | * entirely blank, and no latin-square deduction permits the |
258 | * distinguishing of two such rows. |
259 | */ |
260 | return "Trivial puzzles must have an identity"; |
261 | } |
08d8cd00 |
262 | if (!params->id && params->w == 3) { |
263 | /* |
264 | * We can't have a 3x3 puzzle without an identity either, |
265 | * because 3x3 puzzles can't ever be harder than Trivial |
266 | * (there are no 3x3 latin squares which aren't also valid |
267 | * group tables, so enabling group-based deductions doesn't |
268 | * rule out any possible solutions) and - as above - Trivial |
269 | * puzzles can't not have an identity. |
270 | */ |
271 | return "3x3 puzzles must have an identity"; |
272 | } |
d8fa223c |
273 | return NULL; |
274 | } |
275 | |
276 | /* ---------------------------------------------------------------------- |
277 | * Solver. |
278 | */ |
279 | |
280 | static int solver_normal(struct latin_solver *solver, void *vctx) |
281 | { |
282 | int w = solver->o; |
5523645e |
283 | #ifdef STANDALONE_SOLVER |
284 | char **names = solver->names; |
285 | #endif |
d8fa223c |
286 | digit *grid = solver->grid; |
287 | int i, j, k; |
288 | |
289 | /* |
290 | * Deduce using associativity: (ab)c = a(bc). |
291 | * |
292 | * So we pick any a,b,c we like; then if we know ab, bc, and |
293 | * (ab)c we can fill in a(bc). |
294 | */ |
295 | for (i = 1; i < w; i++) |
296 | for (j = 1; j < w; j++) |
297 | for (k = 1; k < w; k++) { |
298 | if (!grid[i*w+j] || !grid[j*w+k]) |
299 | continue; |
300 | if (grid[(grid[i*w+j]-1)*w+k] && |
301 | !grid[i*w+(grid[j*w+k]-1)]) { |
302 | int x = grid[j*w+k]-1, y = i; |
303 | int n = grid[(grid[i*w+j]-1)*w+k]; |
304 | #ifdef STANDALONE_SOLVER |
305 | if (solver_show_working) { |
5523645e |
306 | printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n", |
d8fa223c |
307 | solver_recurse_depth*4, "", |
5523645e |
308 | names[i], names[j], names[k], |
309 | names[grid[i*w+j]-1], names[k], |
310 | names[i], names[grid[j*w+k]-1]); |
311 | printf("%*s placing %s at (%d,%d)\n", |
d8fa223c |
312 | solver_recurse_depth*4, "", |
5523645e |
313 | names[n-1], x+1, y+1); |
d8fa223c |
314 | } |
315 | #endif |
316 | if (solver->cube[(x*w+y)*w+n-1]) { |
317 | latin_solver_place(solver, x, y, n); |
318 | return 1; |
319 | } else { |
320 | #ifdef STANDALONE_SOLVER |
321 | if (solver_show_working) |
322 | printf("%*s contradiction!\n", |
323 | solver_recurse_depth*4, ""); |
324 | return -1; |
325 | #endif |
326 | } |
327 | } |
328 | if (!grid[(grid[i*w+j]-1)*w+k] && |
329 | grid[i*w+(grid[j*w+k]-1)]) { |
330 | int x = k, y = grid[i*w+j]-1; |
331 | int n = grid[i*w+(grid[j*w+k]-1)]; |
332 | #ifdef STANDALONE_SOLVER |
333 | if (solver_show_working) { |
5523645e |
334 | printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n", |
d8fa223c |
335 | solver_recurse_depth*4, "", |
5523645e |
336 | names[i], names[j], names[k], |
337 | names[grid[i*w+j]-1], names[k], |
338 | names[i], names[grid[j*w+k]-1]); |
339 | printf("%*s placing %s at (%d,%d)\n", |
d8fa223c |
340 | solver_recurse_depth*4, "", |
5523645e |
341 | names[n-1], x+1, y+1); |
d8fa223c |
342 | } |
343 | #endif |
344 | if (solver->cube[(x*w+y)*w+n-1]) { |
345 | latin_solver_place(solver, x, y, n); |
346 | return 1; |
347 | } else { |
348 | #ifdef STANDALONE_SOLVER |
349 | if (solver_show_working) |
350 | printf("%*s contradiction!\n", |
351 | solver_recurse_depth*4, ""); |
352 | return -1; |
353 | #endif |
354 | } |
355 | } |
356 | } |
357 | |
358 | return 0; |
359 | } |
360 | |
361 | #define SOLVER(upper,title,func,lower) func, |
362 | static usersolver_t const group_solvers[] = { DIFFLIST(SOLVER) }; |
363 | |
5523645e |
364 | static int solver(game_params *params, digit *grid, int maxdiff) |
d8fa223c |
365 | { |
5523645e |
366 | int w = params->w; |
d8fa223c |
367 | int ret; |
5523645e |
368 | struct latin_solver solver; |
369 | #ifdef STANDALONE_SOLVER |
370 | char *p, text[100], *names[50]; |
371 | int i; |
372 | #endif |
373 | |
374 | latin_solver_alloc(&solver, grid, w); |
375 | #ifdef STANDALONE_SOLVER |
376 | for (i = 0, p = text; i < w; i++) { |
377 | names[i] = p; |
378 | *p++ = TOCHAR(i+1, params->id); |
379 | *p++ = '\0'; |
380 | } |
381 | solver.names = names; |
382 | #endif |
383 | |
384 | ret = latin_solver_main(&solver, maxdiff, |
385 | DIFF_TRIVIAL, DIFF_HARD, DIFF_EXTREME, |
386 | DIFF_EXTREME, DIFF_UNREASONABLE, |
387 | group_solvers, NULL, NULL, NULL); |
388 | |
389 | latin_solver_free(&solver); |
d8fa223c |
390 | |
391 | return ret; |
392 | } |
393 | |
394 | /* ---------------------------------------------------------------------- |
395 | * Grid generation. |
396 | */ |
397 | |
398 | static char *encode_grid(char *desc, digit *grid, int area) |
399 | { |
400 | int run, i; |
401 | char *p = desc; |
402 | |
403 | run = 0; |
404 | for (i = 0; i <= area; i++) { |
405 | int n = (i < area ? grid[i] : -1); |
406 | |
407 | if (!n) |
408 | run++; |
409 | else { |
410 | if (run) { |
411 | while (run > 0) { |
412 | int c = 'a' - 1 + run; |
413 | if (run > 26) |
414 | c = 'z'; |
415 | *p++ = c; |
416 | run -= c - ('a' - 1); |
417 | } |
418 | } else { |
419 | /* |
420 | * If there's a number in the very top left or |
421 | * bottom right, there's no point putting an |
422 | * unnecessary _ before or after it. |
423 | */ |
424 | if (p > desc && n > 0) |
425 | *p++ = '_'; |
426 | } |
427 | if (n > 0) |
428 | p += sprintf(p, "%d", n); |
429 | run = 0; |
430 | } |
431 | } |
432 | return p; |
433 | } |
434 | |
435 | /* ----- data generated by group.gap begins ----- */ |
436 | |
437 | struct group { |
438 | unsigned long autosize; |
439 | int order, ngens; |
440 | const char *gens; |
441 | }; |
442 | struct groups { |
443 | int ngroups; |
444 | const struct group *groups; |
445 | }; |
446 | |
447 | static const struct group groupdata[] = { |
448 | /* order 2 */ |
5523645e |
449 | {1L, 2, 1, "BA"}, |
d8fa223c |
450 | /* order 3 */ |
5523645e |
451 | {2L, 3, 1, "BCA"}, |
d8fa223c |
452 | /* order 4 */ |
5523645e |
453 | {2L, 4, 1, "BCDA"}, |
454 | {6L, 4, 2, "BADC" "CDAB"}, |
d8fa223c |
455 | /* order 5 */ |
5523645e |
456 | {4L, 5, 1, "BCDEA"}, |
d8fa223c |
457 | /* order 6 */ |
5523645e |
458 | {6L, 6, 2, "CFEBAD" "BADCFE"}, |
459 | {2L, 6, 1, "DCFEBA"}, |
d8fa223c |
460 | /* order 7 */ |
5523645e |
461 | {6L, 7, 1, "BCDEFGA"}, |
d8fa223c |
462 | /* order 8 */ |
5523645e |
463 | {4L, 8, 1, "BCEFDGHA"}, |
464 | {8L, 8, 2, "BDEFGAHC" "EGBHDCFA"}, |
465 | {8L, 8, 2, "EGBHDCFA" "BAEFCDHG"}, |
466 | {24L, 8, 2, "BDEFGAHC" "CHDGBEAF"}, |
467 | {168L, 8, 3, "BAEFCDHG" "CEAGBHDF" "DFGAHBCE"}, |
d8fa223c |
468 | /* order 9 */ |
5523645e |
469 | {6L, 9, 1, "BDECGHFIA"}, |
470 | {48L, 9, 2, "BDEAGHCIF" "CEFGHAIBD"}, |
d8fa223c |
471 | /* order 10 */ |
5523645e |
472 | {20L, 10, 2, "CJEBGDIFAH" "BADCFEHGJI"}, |
473 | {4L, 10, 1, "DCFEHGJIBA"}, |
d8fa223c |
474 | /* order 11 */ |
5523645e |
475 | {10L, 11, 1, "BCDEFGHIJKA"}, |
d8fa223c |
476 | /* order 12 */ |
5523645e |
477 | {12L, 12, 2, "GLDKJEHCBIAF" "BCEFAGIJDKLH"}, |
478 | {4L, 12, 1, "EHIJKCBLDGFA"}, |
479 | {24L, 12, 2, "BEFGAIJKCDLH" "FJBKHLEGDCIA"}, |
480 | {12L, 12, 2, "GLDKJEHCBIAF" "BAEFCDIJGHLK"}, |
481 | {12L, 12, 2, "FDIJGHLBKAEC" "GIDKFLHCJEAB"}, |
d8fa223c |
482 | /* order 13 */ |
5523645e |
483 | {12L, 13, 1, "BCDEFGHIJKLMA"}, |
d8fa223c |
484 | /* order 14 */ |
5523645e |
485 | {42L, 14, 2, "ELGNIBKDMFAHCJ" "BADCFEHGJILKNM"}, |
486 | {6L, 14, 1, "FEHGJILKNMBADC"}, |
d8fa223c |
487 | /* order 15 */ |
5523645e |
488 | {8L, 15, 1, "EGHCJKFMNIOBLDA"}, |
d8fa223c |
489 | /* order 16 */ |
5523645e |
490 | {8L, 16, 1, "MKNPFOADBGLCIEHJ"}, |
491 | {96L, 16, 2, "ILKCONFPEDJHGMAB" "BDFGHIAKLMNCOEPJ"}, |
492 | {32L, 16, 2, "MIHPFDCONBLAKJGE" "BEFGHJKALMNOCDPI"}, |
493 | {32L, 16, 2, "IFACOGLMDEJBNPKH" "BEFGHJKALMNOCDPI"}, |
494 | {16L, 16, 2, "MOHPFKCINBLADJGE" "BDFGHIEKLMNJOAPC"}, |
495 | {16L, 16, 2, "MIHPFDJONBLEKCGA" "BDFGHIEKLMNJOAPC"}, |
496 | {32L, 16, 2, "MOHPFDCINBLEKJGA" "BAFGHCDELMNIJKPO"}, |
497 | {16L, 16, 2, "MIHPFKJONBLADCGE" "GDPHNOEKFLBCIAMJ"}, |
498 | {32L, 16, 2, "MIBPFDJOGHLEKCNA" "CLEIJGMPKAOHNFDB"}, |
d8fa223c |
499 | {192L, 16, 3, |
5523645e |
500 | "MCHPFAIJNBLDEOGK" "BEFGHJKALMNOCDPI" "GKLBNOEDFPHJIAMC"}, |
501 | {64L, 16, 3, "MCHPFAIJNBLDEOGK" "LOGFPKJIBNMEDCHA" "CMAIJHPFDEONBLKG"}, |
d8fa223c |
502 | {192L, 16, 3, |
5523645e |
503 | "IPKCOGMLEDJBNFAH" "BEFGHJKALMNOCDPI" "CMEIJBPFKAOGHLDN"}, |
504 | {48L, 16, 3, "IPDJONFLEKCBGMAH" "FJBLMEOCGHPKAIND" "DGIEKLHNJOAMPBCF"}, |
d8fa223c |
505 | {20160L, 16, 4, |
5523645e |
506 | "EHJKAMNBOCDPFGIL" "BAFGHCDELMNIJKPO" "CFAIJBLMDEOGHPKN" |
507 | "DGIAKLBNCOEFPHJM"}, |
d8fa223c |
508 | /* order 17 */ |
5523645e |
509 | {16L, 17, 1, "EFGHIJKLMNOPQABCD"}, |
d8fa223c |
510 | /* order 18 */ |
5523645e |
511 | {54L, 18, 2, "MKIQOPNAGLRECDBJHF" "BAEFCDJKLGHIOPMNRQ"}, |
512 | {6L, 18, 1, "ECJKGHFOPDMNLRIQBA"}, |
513 | {12L, 18, 2, "ECJKGHBOPAMNFRDQLI" "KNOPQCFREIGHLJAMBD"}, |
d8fa223c |
514 | {432L, 18, 3, |
5523645e |
515 | "IFNAKLQCDOPBGHREMJ" "NOQCFRIGHKLJAMPBDE" "BAEFCDJKLGHIOPMNRQ"}, |
516 | {48L, 18, 2, "ECJKGHBOPAMNFRDQLI" "FDKLHIOPBMNAREQCJG"}, |
d8fa223c |
517 | /* order 19 */ |
5523645e |
518 | {18L, 19, 1, "EFGHIJKLMNOPQRSABCD"}, |
d8fa223c |
519 | /* order 20 */ |
5523645e |
520 | {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "EABICDFMGHJQKLNTOPRS"}, |
521 | {8L, 20, 1, "EHIJLCMNPGQRSKBTDOFA"}, |
522 | {20L, 20, 2, "DJSHQNCLTRGPEBKAIFOM" "EABICDFMGHJQKLNTOPRS"}, |
523 | {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "ECBIAGFMDKJQHONTLSRP"}, |
524 | {24L, 20, 2, "IGFMDKJQHONTLSREPCBA" "FDIJGHMNKLQROPTBSAEC"}, |
d8fa223c |
525 | /* order 21 */ |
5523645e |
526 | {42L, 21, 2, "ITLSBOUERDHAGKCJNFMQP" "EJHLMKOPNRSQAUTCDBFGI"}, |
527 | {12L, 21, 1, "EGHCJKFMNIPQLSTOUBRDA"}, |
d8fa223c |
528 | /* order 22 */ |
5523645e |
529 | {110L, 22, 2, "ETGVIBKDMFOHQJSLUNAPCR" "BADCFEHGJILKNMPORQTSVU"}, |
530 | {10L, 22, 1, "FEHGJILKNMPORQTSVUBADC"}, |
d8fa223c |
531 | /* order 23 */ |
5523645e |
532 | {22L, 23, 1, "EFGHIJKLMNOPQRSTUVWABCD"}, |
d8fa223c |
533 | /* order 24 */ |
5523645e |
534 | {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "HRNOPSWCTUVBLDIJXFGAKQME"}, |
535 | {8L, 24, 1, "MQBTUDRWFGHXJELINOPKSAVC"}, |
536 | {24L, 24, 2, "IOQRBEUVFWGHKLAXMNPSCDTJ" "NJXOVGDKSMTFIPQELCURBWAH"}, |
537 | {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "HSNOPWLDTUVBRIAKXFGCQEMJ"}, |
538 | {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "TWHNXLRIOPUMSACQVBFDEJGK"}, |
539 | {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "BAFGHCDEMNOPIJKLTUVQRSXW"}, |
d8fa223c |
540 | {48L, 24, 3, |
5523645e |
541 | "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO" |
542 | "HSNOPWLDTUVBRIAKXFGCQEMJ"}, |
d8fa223c |
543 | {24L, 24, 3, |
5523645e |
544 | "QUKJWPXFESRIVBMNLDCGHTAO" "JXEQRVUMKLWCPGFTSAIBONDH" |
545 | "TRONXLWCHVUMSAIJPGFDEQBK"}, |
546 | {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "VKXHOQASNTPBCWDEUFGIJLMR"}, |
547 | {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "RMLWIGTUSDJQOPFXEKCBVNAH"}, |
548 | {48L, 24, 2, "IULQRGXMSDCWOPNTEKJBVFAH" "GLMOPRSDTUBVWIEKFXHJQANC"}, |
549 | {24L, 24, 2, "UJPXMRCSNHGTLWIKFVBEDQOA" "NRUFVLWIPXMOJEDQHGTCSABK"}, |
550 | {24L, 24, 2, "MIBTUAQRFGHXCDEWNOPJKLVS" "OKXVFWSCGUTNDRQJBPMALIHE"}, |
d8fa223c |
551 | {144L, 24, 3, |
5523645e |
552 | "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO" |
553 | "BAFGHCDEMNOPIJKLTUVQRSXW"}, |
d8fa223c |
554 | {336L, 24, 3, |
5523645e |
555 | "QTKJWONXESRIHVUMLDCPGFAB" "JNEQRHTUKLWCOPXFSAIVBMDG" |
556 | "HENOPJKLTUVBQRSAXFGWCDMI"}, |
d8fa223c |
557 | /* order 25 */ |
5523645e |
558 | {20L, 25, 1, "EHILMNPQRSFTUVBJWXDOYGAKC"}, |
559 | {480L, 25, 2, "EHILMNPQRSCTUVBFWXDJYGOKA" "BDEGHIKLMNAPQRSCTUVFWXJYO"}, |
d8fa223c |
560 | /* order 26 */ |
561 | {156L, 26, 2, |
5523645e |
562 | "EXGZIBKDMFOHQJSLUNWPYRATCV" "BADCFEHGJILKNMPORQTSVUXWZY"}, |
563 | {12L, 26, 1, "FEHGJILKNMPORQTSVUXWZYBADC"}, |
d8fa223c |
564 | }; |
565 | |
566 | static const struct groups groups[] = { |
5523645e |
567 | {0, NULL}, /* trivial case: 0 */ |
568 | {0, NULL}, /* trivial case: 1 */ |
569 | {1, groupdata + 0}, /* 2 */ |
570 | {1, groupdata + 1}, /* 3 */ |
571 | {2, groupdata + 2}, /* 4 */ |
572 | {1, groupdata + 4}, /* 5 */ |
573 | {2, groupdata + 5}, /* 6 */ |
574 | {1, groupdata + 7}, /* 7 */ |
575 | {5, groupdata + 8}, /* 8 */ |
576 | {2, groupdata + 13}, /* 9 */ |
577 | {2, groupdata + 15}, /* 10 */ |
578 | {1, groupdata + 17}, /* 11 */ |
579 | {5, groupdata + 18}, /* 12 */ |
580 | {1, groupdata + 23}, /* 13 */ |
581 | {2, groupdata + 24}, /* 14 */ |
582 | {1, groupdata + 26}, /* 15 */ |
583 | {14, groupdata + 27}, /* 16 */ |
584 | {1, groupdata + 41}, /* 17 */ |
585 | {5, groupdata + 42}, /* 18 */ |
586 | {1, groupdata + 47}, /* 19 */ |
587 | {5, groupdata + 48}, /* 20 */ |
588 | {2, groupdata + 53}, /* 21 */ |
589 | {2, groupdata + 55}, /* 22 */ |
590 | {1, groupdata + 57}, /* 23 */ |
591 | {15, groupdata + 58}, /* 24 */ |
592 | {2, groupdata + 73}, /* 25 */ |
593 | {2, groupdata + 75}, /* 26 */ |
d8fa223c |
594 | }; |
595 | |
596 | /* ----- data generated by group.gap ends ----- */ |
597 | |
598 | static char *new_game_desc(game_params *params, random_state *rs, |
599 | char **aux, int interactive) |
600 | { |
601 | int w = params->w, a = w*w; |
602 | digit *grid, *soln, *soln2; |
603 | int *indices; |
604 | int i, j, k, qh, qt; |
605 | int diff = params->diff; |
606 | const struct group *group; |
607 | char *desc, *p; |
608 | |
609 | /* |
610 | * Difficulty exceptions: some combinations of size and |
611 | * difficulty cannot be satisfied, because all puzzles of at |
612 | * most that difficulty are actually even easier. |
613 | * |
614 | * Remember to re-test this whenever a change is made to the |
615 | * solver logic! |
616 | * |
617 | * I tested it using the following shell command: |
618 | |
619 | for d in t n h x u; do |
5523645e |
620 | for id in '' i; do |
621 | for i in {3..9}; do |
622 | echo -n "./group --generate 1 ${i}d${d}${id}: " |
623 | perl -e 'alarm 30; exec @ARGV' \ |
624 | ./group --generate 1 ${i}d${d}${id} >/dev/null && echo ok |
625 | done |
d8fa223c |
626 | done |
627 | done |
628 | |
629 | * Of course, it's better to do that after taking the exceptions |
630 | * _out_, so as to detect exceptions that should be removed as |
631 | * well as those which should be added. |
632 | */ |
5523645e |
633 | if (w < 5 && diff == DIFF_UNREASONABLE) |
634 | diff--; |
635 | if ((w < 5 || ((w == 6 || w == 8) && params->id)) && diff == DIFF_EXTREME) |
d8fa223c |
636 | diff--; |
5523645e |
637 | if ((w < 6 || (w == 6 && params->id)) && diff == DIFF_HARD) |
d8fa223c |
638 | diff--; |
5523645e |
639 | if ((w < 4 || (w == 4 && params->id)) && diff == DIFF_NORMAL) |
640 | diff--; |
d8fa223c |
641 | |
642 | grid = snewn(a, digit); |
643 | soln = snewn(a, digit); |
644 | soln2 = snewn(a, digit); |
645 | indices = snewn(a, int); |
646 | |
647 | while (1) { |
648 | /* |
649 | * Construct a valid group table, by picking a group from |
650 | * the above data table, decompressing it into a full |
651 | * representation by BFS, and then randomly permuting its |
652 | * non-identity elements. |
653 | * |
654 | * We build the canonical table in 'soln' (and use 'grid' as |
655 | * our BFS queue), then transfer the table into 'grid' |
656 | * having shuffled the rows. |
657 | */ |
658 | assert(w >= 2); |
659 | assert(w < lenof(groups)); |
660 | group = groups[w].groups + random_upto(rs, groups[w].ngroups); |
661 | assert(group->order == w); |
662 | memset(soln, 0, a); |
663 | for (i = 0; i < w; i++) |
664 | soln[i] = i+1; |
665 | qh = qt = 0; |
666 | grid[qt++] = 1; |
667 | while (qh < qt) { |
668 | digit *row, *newrow; |
669 | |
670 | i = grid[qh++]; |
671 | row = soln + (i-1)*w; |
672 | |
673 | for (j = 0; j < group->ngens; j++) { |
674 | int nri; |
675 | const char *gen = group->gens + j*w; |
676 | |
677 | /* |
678 | * Apply each group generator to row, constructing a |
679 | * new row. |
680 | */ |
5523645e |
681 | nri = gen[row[0]-1] - 'A' + 1; /* which row is it? */ |
d8fa223c |
682 | newrow = soln + (nri-1)*w; |
683 | if (!newrow[0]) { /* not done yet */ |
684 | for (k = 0; k < w; k++) |
5523645e |
685 | newrow[k] = gen[row[k]-1] - 'A' + 1; |
d8fa223c |
686 | grid[qt++] = nri; |
687 | } |
688 | } |
689 | } |
690 | /* That's got the canonical table. Now shuffle it. */ |
691 | for (i = 0; i < w; i++) |
b84b31a8 |
692 | soln2[i] = i; |
693 | if (params->id) /* do we shuffle in the identity? */ |
694 | shuffle(soln2+1, w-1, sizeof(*soln2), rs); |
695 | else |
696 | shuffle(soln2, w, sizeof(*soln2), rs); |
d8fa223c |
697 | for (i = 0; i < w; i++) |
b84b31a8 |
698 | for (j = 0; j < w; j++) |
699 | grid[(soln2[i])*w+(soln2[j])] = soln2[soln[i*w+j]-1]+1; |
d8fa223c |
700 | |
701 | /* |
702 | * Remove entries one by one while the puzzle is still |
703 | * soluble at the appropriate difficulty level. |
704 | */ |
705 | memcpy(soln, grid, a); |
b84b31a8 |
706 | if (!params->id) { |
707 | /* |
708 | * Start by blanking the entire identity row and column, |
709 | * and also another row and column so that the player |
710 | * can't trivially determine which element is the |
711 | * identity. |
712 | */ |
713 | |
714 | j = 1 + random_upto(rs, w-1); /* pick a second row/col to blank */ |
715 | for (i = 0; i < w; i++) { |
716 | grid[(soln2[0])*w+i] = grid[i*w+(soln2[0])] = 0; |
717 | grid[(soln2[j])*w+i] = grid[i*w+(soln2[j])] = 0; |
718 | } |
719 | |
720 | memcpy(soln2, grid, a); |
5523645e |
721 | if (solver(params, soln2, diff) > diff) |
b84b31a8 |
722 | continue; /* go round again if that didn't work */ |
723 | } |
d8fa223c |
724 | |
725 | k = 0; |
b84b31a8 |
726 | for (i = (params->id ? 1 : 0); i < w; i++) |
727 | for (j = (params->id ? 1 : 0); j < w; j++) |
728 | if (grid[i*w+j]) |
729 | indices[k++] = i*w+j; |
7a8dfea2 |
730 | shuffle(indices, k, sizeof(*indices), rs); |
d8fa223c |
731 | |
732 | for (i = 0; i < k; i++) { |
733 | memcpy(soln2, grid, a); |
734 | soln2[indices[i]] = 0; |
5523645e |
735 | if (solver(params, soln2, diff) <= diff) |
d8fa223c |
736 | grid[indices[i]] = 0; |
737 | } |
738 | |
739 | /* |
740 | * Make sure the puzzle isn't too easy. |
741 | */ |
742 | if (diff > 0) { |
743 | memcpy(soln2, grid, a); |
5523645e |
744 | if (solver(params, soln2, diff-1) < diff) |
d8fa223c |
745 | continue; /* go round and try again */ |
746 | } |
747 | |
748 | /* |
749 | * Done. |
750 | */ |
751 | break; |
752 | } |
753 | |
754 | /* |
755 | * Encode the puzzle description. |
756 | */ |
757 | desc = snewn(a*20, char); |
758 | p = encode_grid(desc, grid, a); |
759 | *p++ = '\0'; |
760 | desc = sresize(desc, p - desc, char); |
761 | |
762 | /* |
763 | * Encode the solution. |
764 | */ |
765 | *aux = snewn(a+2, char); |
766 | (*aux)[0] = 'S'; |
767 | for (i = 0; i < a; i++) |
5523645e |
768 | (*aux)[i+1] = TOCHAR(soln[i], params->id); |
d8fa223c |
769 | (*aux)[a+1] = '\0'; |
770 | |
771 | sfree(grid); |
772 | sfree(soln); |
773 | sfree(soln2); |
774 | sfree(indices); |
775 | |
776 | return desc; |
777 | } |
778 | |
779 | /* ---------------------------------------------------------------------- |
780 | * Gameplay. |
781 | */ |
782 | |
783 | static char *validate_grid_desc(const char **pdesc, int range, int area) |
784 | { |
785 | const char *desc = *pdesc; |
786 | int squares = 0; |
787 | while (*desc && *desc != ',') { |
788 | int n = *desc++; |
789 | if (n >= 'a' && n <= 'z') { |
790 | squares += n - 'a' + 1; |
791 | } else if (n == '_') { |
792 | /* do nothing */; |
793 | } else if (n > '0' && n <= '9') { |
794 | int val = atoi(desc-1); |
795 | if (val < 1 || val > range) |
796 | return "Out-of-range number in game description"; |
797 | squares++; |
798 | while (*desc >= '0' && *desc <= '9') |
799 | desc++; |
800 | } else |
801 | return "Invalid character in game description"; |
802 | } |
803 | |
804 | if (squares < area) |
805 | return "Not enough data to fill grid"; |
806 | |
807 | if (squares > area) |
808 | return "Too much data to fit in grid"; |
809 | *pdesc = desc; |
810 | return NULL; |
811 | } |
812 | |
813 | static char *validate_desc(game_params *params, char *desc) |
814 | { |
815 | int w = params->w, a = w*w; |
816 | const char *p = desc; |
817 | |
818 | return validate_grid_desc(&p, w, a); |
819 | } |
820 | |
821 | static char *spec_to_grid(char *desc, digit *grid, int area) |
822 | { |
823 | int i = 0; |
824 | while (*desc && *desc != ',') { |
825 | int n = *desc++; |
826 | if (n >= 'a' && n <= 'z') { |
827 | int run = n - 'a' + 1; |
828 | assert(i + run <= area); |
829 | while (run-- > 0) |
830 | grid[i++] = 0; |
831 | } else if (n == '_') { |
832 | /* do nothing */; |
833 | } else if (n > '0' && n <= '9') { |
834 | assert(i < area); |
835 | grid[i++] = atoi(desc-1); |
836 | while (*desc >= '0' && *desc <= '9') |
837 | desc++; |
838 | } else { |
839 | assert(!"We can't get here"); |
840 | } |
841 | } |
842 | assert(i == area); |
843 | return desc; |
844 | } |
845 | |
846 | static game_state *new_game(midend *me, game_params *params, char *desc) |
847 | { |
848 | int w = params->w, a = w*w; |
849 | game_state *state = snew(game_state); |
850 | int i; |
851 | |
852 | state->par = *params; /* structure copy */ |
853 | state->grid = snewn(a, digit); |
854 | state->immutable = snewn(a, unsigned char); |
855 | state->pencil = snewn(a, int); |
856 | for (i = 0; i < a; i++) { |
857 | state->grid[i] = 0; |
858 | state->immutable[i] = 0; |
859 | state->pencil[i] = 0; |
860 | } |
f536db21 |
861 | state->sequence = snewn(w, digit); |
a346afcd |
862 | state->dividers = snewn(w, int); |
f536db21 |
863 | for (i = 0; i < w; i++) { |
864 | state->sequence[i] = i; |
a346afcd |
865 | state->dividers[i] = -1; |
f536db21 |
866 | } |
d8fa223c |
867 | |
868 | desc = spec_to_grid(desc, state->grid, a); |
869 | for (i = 0; i < a; i++) |
870 | if (state->grid[i] != 0) |
871 | state->immutable[i] = TRUE; |
872 | |
873 | state->completed = state->cheated = FALSE; |
874 | |
875 | return state; |
876 | } |
877 | |
878 | static game_state *dup_game(game_state *state) |
879 | { |
880 | int w = state->par.w, a = w*w; |
881 | game_state *ret = snew(game_state); |
882 | |
883 | ret->par = state->par; /* structure copy */ |
884 | |
885 | ret->grid = snewn(a, digit); |
886 | ret->immutable = snewn(a, unsigned char); |
887 | ret->pencil = snewn(a, int); |
f536db21 |
888 | ret->sequence = snewn(w, digit); |
a346afcd |
889 | ret->dividers = snewn(w, int); |
d8fa223c |
890 | memcpy(ret->grid, state->grid, a*sizeof(digit)); |
891 | memcpy(ret->immutable, state->immutable, a*sizeof(unsigned char)); |
892 | memcpy(ret->pencil, state->pencil, a*sizeof(int)); |
f536db21 |
893 | memcpy(ret->sequence, state->sequence, w*sizeof(digit)); |
a346afcd |
894 | memcpy(ret->dividers, state->dividers, w*sizeof(int)); |
d8fa223c |
895 | |
896 | ret->completed = state->completed; |
897 | ret->cheated = state->cheated; |
898 | |
899 | return ret; |
900 | } |
901 | |
902 | static void free_game(game_state *state) |
903 | { |
904 | sfree(state->grid); |
905 | sfree(state->immutable); |
906 | sfree(state->pencil); |
f536db21 |
907 | sfree(state->sequence); |
d8fa223c |
908 | sfree(state); |
909 | } |
910 | |
911 | static char *solve_game(game_state *state, game_state *currstate, |
912 | char *aux, char **error) |
913 | { |
914 | int w = state->par.w, a = w*w; |
915 | int i, ret; |
916 | digit *soln; |
917 | char *out; |
918 | |
919 | if (aux) |
920 | return dupstr(aux); |
921 | |
922 | soln = snewn(a, digit); |
923 | memcpy(soln, state->grid, a*sizeof(digit)); |
924 | |
5523645e |
925 | ret = solver(&state->par, soln, DIFFCOUNT-1); |
d8fa223c |
926 | |
927 | if (ret == diff_impossible) { |
928 | *error = "No solution exists for this puzzle"; |
929 | out = NULL; |
930 | } else if (ret == diff_ambiguous) { |
931 | *error = "Multiple solutions exist for this puzzle"; |
932 | out = NULL; |
933 | } else { |
934 | out = snewn(a+2, char); |
935 | out[0] = 'S'; |
936 | for (i = 0; i < a; i++) |
5523645e |
937 | out[i+1] = TOCHAR(soln[i], state->par.id); |
d8fa223c |
938 | out[a+1] = '\0'; |
939 | } |
940 | |
941 | sfree(soln); |
942 | return out; |
943 | } |
944 | |
945 | static int game_can_format_as_text_now(game_params *params) |
946 | { |
947 | return TRUE; |
948 | } |
949 | |
950 | static char *game_text_format(game_state *state) |
951 | { |
952 | int w = state->par.w; |
953 | int x, y; |
954 | char *ret, *p, ch; |
955 | |
956 | ret = snewn(2*w*w+1, char); /* leave room for terminating NUL */ |
957 | |
958 | p = ret; |
959 | for (y = 0; y < w; y++) { |
960 | for (x = 0; x < w; x++) { |
961 | digit d = state->grid[y*w+x]; |
962 | |
963 | if (d == 0) { |
964 | ch = '.'; |
965 | } else { |
5523645e |
966 | ch = TOCHAR(d, state->par.id); |
d8fa223c |
967 | } |
968 | |
969 | *p++ = ch; |
970 | if (x == w-1) { |
971 | *p++ = '\n'; |
972 | } else { |
973 | *p++ = ' '; |
974 | } |
975 | } |
976 | } |
977 | |
978 | assert(p - ret == 2*w*w); |
979 | *p = '\0'; |
980 | return ret; |
981 | } |
982 | |
983 | struct game_ui { |
984 | /* |
985 | * These are the coordinates of the currently highlighted |
986 | * square on the grid, if hshow = 1. |
987 | */ |
988 | int hx, hy; |
989 | /* |
990 | * This indicates whether the current highlight is a |
991 | * pencil-mark one or a real one. |
992 | */ |
993 | int hpencil; |
994 | /* |
995 | * This indicates whether or not we're showing the highlight |
996 | * (used to be hx = hy = -1); important so that when we're |
997 | * using the cursor keys it doesn't keep coming back at a |
998 | * fixed position. When hshow = 1, pressing a valid number |
999 | * or letter key or Space will enter that number or letter in the grid. |
1000 | */ |
1001 | int hshow; |
1002 | /* |
1003 | * This indicates whether we're using the highlight as a cursor; |
1004 | * it means that it doesn't vanish on a keypress, and that it is |
1005 | * allowed on immutable squares. |
1006 | */ |
1007 | int hcursor; |
f536db21 |
1008 | /* |
1009 | * This indicates whether we're dragging a table header to |
1010 | * reposition an entire row or column. |
1011 | */ |
1012 | int drag; /* 0=none 1=row 2=col */ |
1013 | int dragnum; /* element being dragged */ |
1014 | int dragpos; /* its current position */ |
a346afcd |
1015 | int edgepos; |
d8fa223c |
1016 | }; |
1017 | |
1018 | static game_ui *new_ui(game_state *state) |
1019 | { |
1020 | game_ui *ui = snew(game_ui); |
1021 | |
b84b31a8 |
1022 | ui->hx = ui->hy = 0; |
d8fa223c |
1023 | ui->hpencil = ui->hshow = ui->hcursor = 0; |
f536db21 |
1024 | ui->drag = 0; |
d8fa223c |
1025 | |
1026 | return ui; |
1027 | } |
1028 | |
1029 | static void free_ui(game_ui *ui) |
1030 | { |
1031 | sfree(ui); |
1032 | } |
1033 | |
1034 | static char *encode_ui(game_ui *ui) |
1035 | { |
1036 | return NULL; |
1037 | } |
1038 | |
1039 | static void decode_ui(game_ui *ui, char *encoding) |
1040 | { |
1041 | } |
1042 | |
1043 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
1044 | game_state *newstate) |
1045 | { |
1046 | int w = newstate->par.w; |
1047 | /* |
1048 | * We prevent pencil-mode highlighting of a filled square, unless |
1049 | * we're using the cursor keys. So if the user has just filled in |
1050 | * a square which we had a pencil-mode highlight in (by Undo, or |
1051 | * by Redo, or by Solve), then we cancel the highlight. |
1052 | */ |
1053 | if (ui->hshow && ui->hpencil && !ui->hcursor && |
1054 | newstate->grid[ui->hy * w + ui->hx] != 0) { |
1055 | ui->hshow = 0; |
1056 | } |
1057 | } |
1058 | |
1059 | #define PREFERRED_TILESIZE 48 |
1060 | #define TILESIZE (ds->tilesize) |
1061 | #define BORDER (TILESIZE / 2) |
b84b31a8 |
1062 | #define LEGEND (TILESIZE) |
d8fa223c |
1063 | #define GRIDEXTRA max((TILESIZE / 32),1) |
b84b31a8 |
1064 | #define COORD(x) ((x)*TILESIZE + BORDER + LEGEND) |
1065 | #define FROMCOORD(x) (((x)+(TILESIZE-BORDER-LEGEND)) / TILESIZE - 1) |
d8fa223c |
1066 | |
1067 | #define FLASH_TIME 0.4F |
1068 | |
a346afcd |
1069 | #define DF_DIVIDER_TOP 0x1000 |
1070 | #define DF_DIVIDER_BOT 0x2000 |
1071 | #define DF_DIVIDER_LEFT 0x4000 |
1072 | #define DF_DIVIDER_RIGHT 0x8000 |
d8fa223c |
1073 | #define DF_HIGHLIGHT 0x0400 |
1074 | #define DF_HIGHLIGHT_PENCIL 0x0200 |
1075 | #define DF_IMMUTABLE 0x0100 |
f536db21 |
1076 | #define DF_LEGEND 0x0080 |
d8fa223c |
1077 | #define DF_DIGIT_MASK 0x001F |
1078 | |
1079 | #define EF_DIGIT_SHIFT 5 |
1080 | #define EF_DIGIT_MASK ((1 << EF_DIGIT_SHIFT) - 1) |
1081 | #define EF_LEFT_SHIFT 0 |
1082 | #define EF_RIGHT_SHIFT (3*EF_DIGIT_SHIFT) |
1083 | #define EF_LEFT_MASK ((1UL << (3*EF_DIGIT_SHIFT)) - 1UL) |
1084 | #define EF_RIGHT_MASK (EF_LEFT_MASK << EF_RIGHT_SHIFT) |
1085 | #define EF_LATIN (1UL << (6*EF_DIGIT_SHIFT)) |
1086 | |
1087 | struct game_drawstate { |
5523645e |
1088 | game_params par; |
d8fa223c |
1089 | int w, tilesize; |
1090 | int started; |
f536db21 |
1091 | long *tiles, *legend, *pencil, *errors; |
d8fa223c |
1092 | long *errtmp; |
f536db21 |
1093 | digit *sequence; |
d8fa223c |
1094 | }; |
1095 | |
1096 | static int check_errors(game_state *state, long *errors) |
1097 | { |
1098 | int w = state->par.w, a = w*w; |
1099 | digit *grid = state->grid; |
1100 | int i, j, k, x, y, errs = FALSE; |
1101 | |
05d6901a |
1102 | /* |
1103 | * To verify that we have a valid group table, it suffices to |
1104 | * test latin-square-hood and associativity only. All the other |
1105 | * group axioms follow from those two. |
1106 | * |
1107 | * Proof: |
1108 | * |
1109 | * Associativity is given; closure is obvious from latin- |
1110 | * square-hood. We need to show that an identity exists and that |
1111 | * every element has an inverse. |
1112 | * |
1113 | * Identity: take any element a. There will be some element e |
1114 | * such that ea=a (in a latin square, every element occurs in |
1115 | * every row and column, so a must occur somewhere in the a |
1116 | * column, say on row e). For any other element b, there must |
1117 | * exist x such that ax=b (same argument from latin-square-hood |
1118 | * again), and then associativity gives us eb = e(ax) = (ea)x = |
1119 | * ax = b. Hence eb=b for all b, i.e. e is a left-identity. A |
1120 | * similar argument tells us that there must be some f which is |
1121 | * a right-identity, and then we show they are the same element |
1122 | * by observing that ef must simultaneously equal e and equal f. |
1123 | * |
1124 | * Inverses: given any a, by the latin-square argument again, |
1125 | * there must exist p and q such that pa=e and aq=e (i.e. left- |
1126 | * and right-inverses). We can show these are equal by |
1127 | * associativity: p = pe = p(aq) = (pa)q = eq = q. [] |
1128 | */ |
1129 | |
d8fa223c |
1130 | if (errors) |
1131 | for (i = 0; i < a; i++) |
1132 | errors[i] = 0; |
1133 | |
1134 | for (y = 0; y < w; y++) { |
1135 | unsigned long mask = 0, errmask = 0; |
1136 | for (x = 0; x < w; x++) { |
1137 | unsigned long bit = 1UL << grid[y*w+x]; |
1138 | errmask |= (mask & bit); |
1139 | mask |= bit; |
1140 | } |
1141 | |
1142 | if (mask != (1 << (w+1)) - (1 << 1)) { |
1143 | errs = TRUE; |
1144 | errmask &= ~1UL; |
1145 | if (errors) { |
1146 | for (x = 0; x < w; x++) |
1147 | if (errmask & (1UL << grid[y*w+x])) |
1148 | errors[y*w+x] |= EF_LATIN; |
1149 | } |
1150 | } |
1151 | } |
1152 | |
1153 | for (x = 0; x < w; x++) { |
1154 | unsigned long mask = 0, errmask = 0; |
1155 | for (y = 0; y < w; y++) { |
1156 | unsigned long bit = 1UL << grid[y*w+x]; |
1157 | errmask |= (mask & bit); |
1158 | mask |= bit; |
1159 | } |
1160 | |
1161 | if (mask != (1 << (w+1)) - (1 << 1)) { |
1162 | errs = TRUE; |
1163 | errmask &= ~1UL; |
1164 | if (errors) { |
1165 | for (y = 0; y < w; y++) |
1166 | if (errmask & (1UL << grid[y*w+x])) |
1167 | errors[y*w+x] |= EF_LATIN; |
1168 | } |
1169 | } |
1170 | } |
1171 | |
1172 | for (i = 1; i < w; i++) |
1173 | for (j = 1; j < w; j++) |
1174 | for (k = 1; k < w; k++) |
1175 | if (grid[i*w+j] && grid[j*w+k] && |
1176 | grid[(grid[i*w+j]-1)*w+k] && |
1177 | grid[i*w+(grid[j*w+k]-1)] && |
1178 | grid[(grid[i*w+j]-1)*w+k] != grid[i*w+(grid[j*w+k]-1)]) { |
1179 | if (errors) { |
1180 | int a = i+1, b = j+1, c = k+1; |
1181 | int ab = grid[i*w+j], bc = grid[j*w+k]; |
1182 | int left = (ab-1)*w+(c-1), right = (a-1)*w+(bc-1); |
1183 | /* |
1184 | * If the appropriate error slot is already |
1185 | * used for one of the squares, we don't |
1186 | * fill either of them. |
1187 | */ |
1188 | if (!(errors[left] & EF_LEFT_MASK) && |
1189 | !(errors[right] & EF_RIGHT_MASK)) { |
1190 | long err; |
1191 | err = a; |
1192 | err = (err << EF_DIGIT_SHIFT) | b; |
1193 | err = (err << EF_DIGIT_SHIFT) | c; |
1194 | errors[left] |= err << EF_LEFT_SHIFT; |
1195 | errors[right] |= err << EF_RIGHT_SHIFT; |
1196 | } |
1197 | } |
1198 | errs = TRUE; |
1199 | } |
1200 | |
1201 | return errs; |
1202 | } |
1203 | |
ff5d35e1 |
1204 | static int find_in_sequence(digit *seq, int len, digit n) |
1205 | { |
1206 | int i; |
1207 | |
1208 | for (i = 0; i < len; i++) |
1209 | if (seq[i] == n) |
1210 | return i; |
1211 | |
1212 | assert(!"Should never get here"); |
1213 | return -1; |
1214 | } |
1215 | |
c71c970b |
1216 | static char *interpret_move(game_state *state, game_ui *ui, |
1217 | const game_drawstate *ds, int x, int y, int button) |
d8fa223c |
1218 | { |
1219 | int w = state->par.w; |
1220 | int tx, ty; |
1221 | char buf[80]; |
1222 | |
1223 | button &= ~MOD_MASK; |
1224 | |
1225 | tx = FROMCOORD(x); |
1226 | ty = FROMCOORD(y); |
1227 | |
f536db21 |
1228 | if (ui->drag) { |
1229 | if (IS_MOUSE_DRAG(button)) { |
a346afcd |
1230 | int tcoord = ((ui->drag &~ 4) == 1 ? ty : tx); |
1231 | ui->drag |= 4; /* some movement has happened */ |
f536db21 |
1232 | if (tcoord >= 0 && tcoord < w) { |
1233 | ui->dragpos = tcoord; |
1234 | return ""; |
d8fa223c |
1235 | } |
f536db21 |
1236 | } else if (IS_MOUSE_RELEASE(button)) { |
a346afcd |
1237 | if (ui->drag & 4) { |
1238 | ui->drag = 0; /* end drag */ |
1239 | if (state->sequence[ui->dragpos] == ui->dragnum) |
1240 | return ""; /* drag was a no-op overall */ |
1241 | sprintf(buf, "D%d,%d", ui->dragnum, ui->dragpos); |
1242 | return dupstr(buf); |
1243 | } else { |
1244 | ui->drag = 0; /* end 'drag' */ |
1245 | if (ui->edgepos > 0 && ui->edgepos < w) { |
1246 | sprintf(buf, "V%d,%d", |
1247 | state->sequence[ui->edgepos-1], |
1248 | state->sequence[ui->edgepos]); |
1249 | return dupstr(buf); |
1250 | } else |
1251 | return ""; /* no-op */ |
1252 | } |
d8fa223c |
1253 | } |
f536db21 |
1254 | } else if (IS_MOUSE_DOWN(button)) { |
1255 | if (tx >= 0 && tx < w && ty >= 0 && ty < w) { |
1256 | tx = state->sequence[tx]; |
1257 | ty = state->sequence[ty]; |
1258 | if (button == LEFT_BUTTON) { |
d8fa223c |
1259 | if (tx == ui->hx && ty == ui->hy && |
f536db21 |
1260 | ui->hshow && ui->hpencil == 0) { |
d8fa223c |
1261 | ui->hshow = 0; |
1262 | } else { |
d8fa223c |
1263 | ui->hx = tx; |
1264 | ui->hy = ty; |
f536db21 |
1265 | ui->hshow = !state->immutable[ty*w+tx]; |
1266 | ui->hpencil = 0; |
d8fa223c |
1267 | } |
f536db21 |
1268 | ui->hcursor = 0; |
1269 | return ""; /* UI activity occurred */ |
d8fa223c |
1270 | } |
f536db21 |
1271 | if (button == RIGHT_BUTTON) { |
1272 | /* |
1273 | * Pencil-mode highlighting for non filled squares. |
1274 | */ |
1275 | if (state->grid[ty*w+tx] == 0) { |
1276 | if (tx == ui->hx && ty == ui->hy && |
1277 | ui->hshow && ui->hpencil) { |
1278 | ui->hshow = 0; |
1279 | } else { |
1280 | ui->hpencil = 1; |
1281 | ui->hx = tx; |
1282 | ui->hy = ty; |
1283 | ui->hshow = 1; |
1284 | } |
1285 | } else { |
1286 | ui->hshow = 0; |
1287 | } |
1288 | ui->hcursor = 0; |
1289 | return ""; /* UI activity occurred */ |
1290 | } |
1291 | } else if (tx >= 0 && tx < w && ty == -1) { |
1292 | ui->drag = 2; |
1293 | ui->dragnum = state->sequence[tx]; |
1294 | ui->dragpos = tx; |
a346afcd |
1295 | ui->edgepos = FROMCOORD(x + TILESIZE/2); |
f536db21 |
1296 | return ""; |
1297 | } else if (ty >= 0 && ty < w && tx == -1) { |
1298 | ui->drag = 1; |
1299 | ui->dragnum = state->sequence[ty]; |
1300 | ui->dragpos = ty; |
a346afcd |
1301 | ui->edgepos = FROMCOORD(y + TILESIZE/2); |
f536db21 |
1302 | return ""; |
d8fa223c |
1303 | } |
1304 | } |
f536db21 |
1305 | |
d8fa223c |
1306 | if (IS_CURSOR_MOVE(button)) { |
ff5d35e1 |
1307 | int cx = find_in_sequence(state->sequence, w, ui->hx); |
1308 | int cy = find_in_sequence(state->sequence, w, ui->hy); |
1309 | move_cursor(button, &cx, &cy, w, w, 0); |
1310 | ui->hx = state->sequence[cx]; |
1311 | ui->hy = state->sequence[cy]; |
d8fa223c |
1312 | ui->hshow = ui->hcursor = 1; |
1313 | return ""; |
1314 | } |
1315 | if (ui->hshow && |
1316 | (button == CURSOR_SELECT)) { |
1317 | ui->hpencil = 1 - ui->hpencil; |
1318 | ui->hcursor = 1; |
1319 | return ""; |
1320 | } |
1321 | |
1322 | if (ui->hshow && |
5523645e |
1323 | ((ISCHAR(button) && FROMCHAR(button, state->par.id) <= w) || |
d8fa223c |
1324 | button == CURSOR_SELECT2 || button == '\b')) { |
5523645e |
1325 | int n = FROMCHAR(button, state->par.id); |
d8fa223c |
1326 | if (button == CURSOR_SELECT2 || button == '\b') |
1327 | n = 0; |
1328 | |
1329 | /* |
1330 | * Can't make pencil marks in a filled square. This can only |
1331 | * become highlighted if we're using cursor keys. |
1332 | */ |
1333 | if (ui->hpencil && state->grid[ui->hy*w+ui->hx]) |
1334 | return NULL; |
1335 | |
1336 | /* |
1337 | * Can't do anything to an immutable square. |
1338 | */ |
1339 | if (state->immutable[ui->hy*w+ui->hx]) |
1340 | return NULL; |
1341 | |
1342 | sprintf(buf, "%c%d,%d,%d", |
1343 | (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n); |
1344 | |
1345 | if (!ui->hcursor) ui->hshow = 0; |
1346 | |
1347 | return dupstr(buf); |
1348 | } |
1349 | |
1350 | if (button == 'M' || button == 'm') |
1351 | return dupstr("M"); |
1352 | |
1353 | return NULL; |
1354 | } |
1355 | |
1356 | static game_state *execute_move(game_state *from, char *move) |
1357 | { |
1358 | int w = from->par.w, a = w*w; |
1359 | game_state *ret; |
f536db21 |
1360 | int x, y, i, j, n; |
d8fa223c |
1361 | |
1362 | if (move[0] == 'S') { |
1363 | ret = dup_game(from); |
1364 | ret->completed = ret->cheated = TRUE; |
1365 | |
1366 | for (i = 0; i < a; i++) { |
5523645e |
1367 | if (!ISCHAR(move[i+1]) || FROMCHAR(move[i+1], from->par.id) > w) { |
d8fa223c |
1368 | free_game(ret); |
1369 | return NULL; |
1370 | } |
5523645e |
1371 | ret->grid[i] = FROMCHAR(move[i+1], from->par.id); |
d8fa223c |
1372 | ret->pencil[i] = 0; |
1373 | } |
1374 | |
1375 | if (move[a+1] != '\0') { |
1376 | free_game(ret); |
1377 | return NULL; |
1378 | } |
1379 | |
1380 | return ret; |
1381 | } else if ((move[0] == 'P' || move[0] == 'R') && |
1382 | sscanf(move+1, "%d,%d,%d", &x, &y, &n) == 3 && |
1383 | x >= 0 && x < w && y >= 0 && y < w && n >= 0 && n <= w) { |
1384 | if (from->immutable[y*w+x]) |
1385 | return NULL; |
1386 | |
1387 | ret = dup_game(from); |
1388 | if (move[0] == 'P' && n > 0) { |
1389 | ret->pencil[y*w+x] ^= 1 << n; |
1390 | } else { |
1391 | ret->grid[y*w+x] = n; |
1392 | ret->pencil[y*w+x] = 0; |
1393 | |
1394 | if (!ret->completed && !check_errors(ret, NULL)) |
1395 | ret->completed = TRUE; |
1396 | } |
1397 | return ret; |
1398 | } else if (move[0] == 'M') { |
1399 | /* |
1400 | * Fill in absolutely all pencil marks everywhere. (I |
1401 | * wouldn't use this for actual play, but it's a handy |
1402 | * starting point when following through a set of |
1403 | * diagnostics output by the standalone solver.) |
1404 | */ |
1405 | ret = dup_game(from); |
1406 | for (i = 0; i < a; i++) { |
1407 | if (!ret->grid[i]) |
1408 | ret->pencil[i] = (1 << (w+1)) - (1 << 1); |
1409 | } |
1410 | return ret; |
f536db21 |
1411 | } else if (move[0] == 'D' && |
1412 | sscanf(move+1, "%d,%d", &x, &y) == 2) { |
1413 | /* |
1414 | * Reorder the rows and columns so that digit x is in position |
1415 | * y. |
1416 | */ |
1417 | ret = dup_game(from); |
1418 | for (i = j = 0; i < w; i++) { |
1419 | if (i == y) { |
1420 | ret->sequence[i] = x; |
1421 | } else { |
1422 | if (from->sequence[j] == x) |
1423 | j++; |
1424 | ret->sequence[i] = from->sequence[j++]; |
1425 | } |
1426 | } |
a346afcd |
1427 | /* |
1428 | * Eliminate any obsoleted dividers. |
1429 | */ |
1430 | for (x = 0; x+1 < w; x++) { |
1431 | int i = ret->sequence[x], j = ret->sequence[x+1]; |
1432 | if (ret->dividers[i] != j) |
1433 | ret->dividers[i] = -1; |
1434 | } |
1435 | return ret; |
1436 | } else if (move[0] == 'V' && |
1437 | sscanf(move+1, "%d,%d", &i, &j) == 2) { |
1438 | ret = dup_game(from); |
1439 | if (ret->dividers[i] == j) |
1440 | ret->dividers[i] = -1; |
1441 | else |
1442 | ret->dividers[i] = j; |
f536db21 |
1443 | return ret; |
d8fa223c |
1444 | } else |
1445 | return NULL; /* couldn't parse move string */ |
1446 | } |
1447 | |
1448 | /* ---------------------------------------------------------------------- |
1449 | * Drawing routines. |
1450 | */ |
1451 | |
b84b31a8 |
1452 | #define SIZE(w) ((w) * TILESIZE + 2*BORDER + LEGEND) |
d8fa223c |
1453 | |
1454 | static void game_compute_size(game_params *params, int tilesize, |
1455 | int *x, int *y) |
1456 | { |
1457 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1458 | struct { int tilesize; } ads, *ds = &ads; |
1459 | ads.tilesize = tilesize; |
1460 | |
1461 | *x = *y = SIZE(params->w); |
1462 | } |
1463 | |
1464 | static void game_set_size(drawing *dr, game_drawstate *ds, |
1465 | game_params *params, int tilesize) |
1466 | { |
1467 | ds->tilesize = tilesize; |
1468 | } |
1469 | |
1470 | static float *game_colours(frontend *fe, int *ncolours) |
1471 | { |
1472 | float *ret = snewn(3 * NCOLOURS, float); |
1473 | |
1474 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
1475 | |
1476 | ret[COL_GRID * 3 + 0] = 0.0F; |
1477 | ret[COL_GRID * 3 + 1] = 0.0F; |
1478 | ret[COL_GRID * 3 + 2] = 0.0F; |
1479 | |
d8fa223c |
1480 | ret[COL_USER * 3 + 0] = 0.0F; |
1481 | ret[COL_USER * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1]; |
1482 | ret[COL_USER * 3 + 2] = 0.0F; |
1483 | |
1484 | ret[COL_HIGHLIGHT * 3 + 0] = 0.78F * ret[COL_BACKGROUND * 3 + 0]; |
1485 | ret[COL_HIGHLIGHT * 3 + 1] = 0.78F * ret[COL_BACKGROUND * 3 + 1]; |
1486 | ret[COL_HIGHLIGHT * 3 + 2] = 0.78F * ret[COL_BACKGROUND * 3 + 2]; |
1487 | |
1488 | ret[COL_ERROR * 3 + 0] = 1.0F; |
1489 | ret[COL_ERROR * 3 + 1] = 0.0F; |
1490 | ret[COL_ERROR * 3 + 2] = 0.0F; |
1491 | |
1492 | ret[COL_PENCIL * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0]; |
1493 | ret[COL_PENCIL * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1]; |
1494 | ret[COL_PENCIL * 3 + 2] = ret[COL_BACKGROUND * 3 + 2]; |
1495 | |
1496 | *ncolours = NCOLOURS; |
1497 | return ret; |
1498 | } |
1499 | |
1500 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
1501 | { |
1502 | int w = state->par.w, a = w*w; |
1503 | struct game_drawstate *ds = snew(struct game_drawstate); |
1504 | int i; |
1505 | |
1506 | ds->w = w; |
5523645e |
1507 | ds->par = state->par; /* structure copy */ |
d8fa223c |
1508 | ds->tilesize = 0; |
1509 | ds->started = FALSE; |
1510 | ds->tiles = snewn(a, long); |
f536db21 |
1511 | ds->legend = snewn(w, long); |
d8fa223c |
1512 | ds->pencil = snewn(a, long); |
1513 | ds->errors = snewn(a, long); |
f536db21 |
1514 | ds->sequence = snewn(a, digit); |
d8fa223c |
1515 | for (i = 0; i < a; i++) |
1516 | ds->tiles[i] = ds->pencil[i] = -1; |
f536db21 |
1517 | for (i = 0; i < w; i++) |
1518 | ds->legend[i] = -1; |
d8fa223c |
1519 | ds->errtmp = snewn(a, long); |
1520 | |
1521 | return ds; |
1522 | } |
1523 | |
1524 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
1525 | { |
1526 | sfree(ds->tiles); |
1527 | sfree(ds->pencil); |
1528 | sfree(ds->errors); |
1529 | sfree(ds->errtmp); |
f536db21 |
1530 | sfree(ds->sequence); |
d8fa223c |
1531 | sfree(ds); |
1532 | } |
1533 | |
d2cfd12c |
1534 | static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile, |
1535 | long pencil, long error) |
d8fa223c |
1536 | { |
1537 | int w = ds->w /* , a = w*w */; |
1538 | int tx, ty, tw, th; |
1539 | int cx, cy, cw, ch; |
1540 | char str[64]; |
1541 | |
b84b31a8 |
1542 | tx = BORDER + LEGEND + x * TILESIZE + 1; |
1543 | ty = BORDER + LEGEND + y * TILESIZE + 1; |
d8fa223c |
1544 | |
1545 | cx = tx; |
1546 | cy = ty; |
1547 | cw = tw = TILESIZE-1; |
1548 | ch = th = TILESIZE-1; |
1549 | |
f536db21 |
1550 | if (tile & DF_LEGEND) { |
1551 | cx += TILESIZE/10; |
1552 | cy += TILESIZE/10; |
1553 | cw -= TILESIZE/5; |
1554 | ch -= TILESIZE/5; |
1555 | tile |= DF_IMMUTABLE; |
1556 | } |
1557 | |
d8fa223c |
1558 | clip(dr, cx, cy, cw, ch); |
1559 | |
1560 | /* background needs erasing */ |
1561 | draw_rect(dr, cx, cy, cw, ch, |
b84b31a8 |
1562 | (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND); |
d8fa223c |
1563 | |
a346afcd |
1564 | /* dividers */ |
1565 | if (tile & DF_DIVIDER_TOP) |
1566 | draw_rect(dr, cx, cy, cw, 1, COL_GRID); |
1567 | if (tile & DF_DIVIDER_BOT) |
1568 | draw_rect(dr, cx, cy+ch-1, cw, 1, COL_GRID); |
1569 | if (tile & DF_DIVIDER_LEFT) |
1570 | draw_rect(dr, cx, cy, 1, ch, COL_GRID); |
1571 | if (tile & DF_DIVIDER_RIGHT) |
1572 | draw_rect(dr, cx+cw-1, cy, 1, ch, COL_GRID); |
1573 | |
d8fa223c |
1574 | /* pencil-mode highlight */ |
1575 | if (tile & DF_HIGHLIGHT_PENCIL) { |
1576 | int coords[6]; |
1577 | coords[0] = cx; |
1578 | coords[1] = cy; |
1579 | coords[2] = cx+cw/2; |
1580 | coords[3] = cy; |
1581 | coords[4] = cx; |
1582 | coords[5] = cy+ch/2; |
1583 | draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); |
1584 | } |
1585 | |
1586 | /* new number needs drawing? */ |
1587 | if (tile & DF_DIGIT_MASK) { |
1588 | str[1] = '\0'; |
5523645e |
1589 | str[0] = TOCHAR(tile & DF_DIGIT_MASK, ds->par.id); |
d8fa223c |
1590 | draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2, |
1591 | FONT_VARIABLE, TILESIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE, |
1592 | (error & EF_LATIN) ? COL_ERROR : |
1593 | (tile & DF_IMMUTABLE) ? COL_GRID : COL_USER, str); |
1594 | |
1595 | if (error & EF_LEFT_MASK) { |
1596 | int a = (error >> (EF_LEFT_SHIFT+2*EF_DIGIT_SHIFT))&EF_DIGIT_MASK; |
1597 | int b = (error >> (EF_LEFT_SHIFT+1*EF_DIGIT_SHIFT))&EF_DIGIT_MASK; |
1598 | int c = (error >> (EF_LEFT_SHIFT ))&EF_DIGIT_MASK; |
1599 | char buf[10]; |
5523645e |
1600 | sprintf(buf, "(%c%c)%c", TOCHAR(a, ds->par.id), |
1601 | TOCHAR(b, ds->par.id), TOCHAR(c, ds->par.id)); |
d8fa223c |
1602 | draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/6, |
1603 | FONT_VARIABLE, TILESIZE/6, ALIGN_VCENTRE | ALIGN_HCENTRE, |
1604 | COL_ERROR, buf); |
1605 | } |
1606 | if (error & EF_RIGHT_MASK) { |
1607 | int a = (error >> (EF_RIGHT_SHIFT+2*EF_DIGIT_SHIFT))&EF_DIGIT_MASK; |
1608 | int b = (error >> (EF_RIGHT_SHIFT+1*EF_DIGIT_SHIFT))&EF_DIGIT_MASK; |
1609 | int c = (error >> (EF_RIGHT_SHIFT ))&EF_DIGIT_MASK; |
1610 | char buf[10]; |
5523645e |
1611 | sprintf(buf, "%c(%c%c)", TOCHAR(a, ds->par.id), |
1612 | TOCHAR(b, ds->par.id), TOCHAR(c, ds->par.id)); |
d8fa223c |
1613 | draw_text(dr, tx + TILESIZE/2, ty + TILESIZE - TILESIZE/6, |
1614 | FONT_VARIABLE, TILESIZE/6, ALIGN_VCENTRE | ALIGN_HCENTRE, |
1615 | COL_ERROR, buf); |
1616 | } |
1617 | } else { |
1618 | int i, j, npencil; |
1619 | int pl, pr, pt, pb; |
1620 | float bestsize; |
1621 | int pw, ph, minph, pbest, fontsize; |
1622 | |
1623 | /* Count the pencil marks required. */ |
1624 | for (i = 1, npencil = 0; i <= w; i++) |
1625 | if (pencil & (1 << i)) |
1626 | npencil++; |
1627 | if (npencil) { |
1628 | |
1629 | minph = 2; |
1630 | |
1631 | /* |
1632 | * Determine the bounding rectangle within which we're going |
1633 | * to put the pencil marks. |
1634 | */ |
1635 | /* Start with the whole square */ |
1636 | pl = tx + GRIDEXTRA; |
1637 | pr = pl + TILESIZE - GRIDEXTRA; |
1638 | pt = ty + GRIDEXTRA; |
1639 | pb = pt + TILESIZE - GRIDEXTRA; |
1640 | |
1641 | /* |
1642 | * We arrange our pencil marks in a grid layout, with |
1643 | * the number of rows and columns adjusted to allow the |
1644 | * maximum font size. |
1645 | * |
1646 | * So now we work out what the grid size ought to be. |
1647 | */ |
1648 | bestsize = 0.0; |
1649 | pbest = 0; |
1650 | /* Minimum */ |
1651 | for (pw = 3; pw < max(npencil,4); pw++) { |
1652 | float fw, fh, fs; |
1653 | |
1654 | ph = (npencil + pw - 1) / pw; |
1655 | ph = max(ph, minph); |
1656 | fw = (pr - pl) / (float)pw; |
1657 | fh = (pb - pt) / (float)ph; |
1658 | fs = min(fw, fh); |
1659 | if (fs > bestsize) { |
1660 | bestsize = fs; |
1661 | pbest = pw; |
1662 | } |
1663 | } |
1664 | assert(pbest > 0); |
1665 | pw = pbest; |
1666 | ph = (npencil + pw - 1) / pw; |
1667 | ph = max(ph, minph); |
1668 | |
1669 | /* |
1670 | * Now we've got our grid dimensions, work out the pixel |
1671 | * size of a grid element, and round it to the nearest |
1672 | * pixel. (We don't want rounding errors to make the |
1673 | * grid look uneven at low pixel sizes.) |
1674 | */ |
1675 | fontsize = min((pr - pl) / pw, (pb - pt) / ph); |
1676 | |
1677 | /* |
1678 | * Centre the resulting figure in the square. |
1679 | */ |
1680 | pl = tx + (TILESIZE - fontsize * pw) / 2; |
1681 | pt = ty + (TILESIZE - fontsize * ph) / 2; |
1682 | |
1683 | /* |
1684 | * Now actually draw the pencil marks. |
1685 | */ |
1686 | for (i = 1, j = 0; i <= w; i++) |
1687 | if (pencil & (1 << i)) { |
1688 | int dx = j % pw, dy = j / pw; |
1689 | |
1690 | str[1] = '\0'; |
5523645e |
1691 | str[0] = TOCHAR(i, ds->par.id); |
d8fa223c |
1692 | draw_text(dr, pl + fontsize * (2*dx+1) / 2, |
1693 | pt + fontsize * (2*dy+1) / 2, |
1694 | FONT_VARIABLE, fontsize, |
1695 | ALIGN_VCENTRE | ALIGN_HCENTRE, COL_PENCIL, str); |
1696 | j++; |
1697 | } |
1698 | } |
1699 | } |
1700 | |
1701 | unclip(dr); |
1702 | |
1703 | draw_update(dr, cx, cy, cw, ch); |
1704 | } |
1705 | |
1706 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
1707 | game_state *state, int dir, game_ui *ui, |
1708 | float animtime, float flashtime) |
1709 | { |
1710 | int w = state->par.w /*, a = w*w */; |
f536db21 |
1711 | int x, y, i, j; |
d8fa223c |
1712 | |
1713 | if (!ds->started) { |
1714 | /* |
1715 | * The initial contents of the window are not guaranteed and |
1716 | * can vary with front ends. To be on the safe side, all |
1717 | * games should start by drawing a big background-colour |
1718 | * rectangle covering the whole window. |
1719 | */ |
1720 | draw_rect(dr, 0, 0, SIZE(w), SIZE(w), COL_BACKGROUND); |
1721 | |
1722 | /* |
1723 | * Big containing rectangle. |
1724 | */ |
1725 | draw_rect(dr, COORD(0) - GRIDEXTRA, COORD(0) - GRIDEXTRA, |
1726 | w*TILESIZE+1+GRIDEXTRA*2, w*TILESIZE+1+GRIDEXTRA*2, |
1727 | COL_GRID); |
1728 | |
1729 | draw_update(dr, 0, 0, SIZE(w), SIZE(w)); |
1730 | |
1731 | ds->started = TRUE; |
1732 | } |
1733 | |
1734 | check_errors(state, ds->errtmp); |
1735 | |
f536db21 |
1736 | /* |
1737 | * Construct a modified version of state->sequence which takes |
1738 | * into account an unfinished drag operation. |
1739 | */ |
1740 | if (ui->drag) { |
1741 | x = ui->dragnum; |
1742 | y = ui->dragpos; |
1743 | } else { |
1744 | x = y = -1; |
1745 | } |
1746 | for (i = j = 0; i < w; i++) { |
1747 | if (i == y) { |
1748 | ds->sequence[i] = x; |
1749 | } else { |
1750 | if (state->sequence[j] == x) |
1751 | j++; |
1752 | ds->sequence[i] = state->sequence[j++]; |
1753 | } |
1754 | } |
1755 | |
1756 | /* |
1757 | * Draw the table legend. |
1758 | */ |
1759 | for (x = 0; x < w; x++) { |
1760 | int sx = ds->sequence[x]; |
1761 | long tile = (sx+1) | DF_LEGEND; |
1762 | if (ds->legend[x] != tile) { |
1763 | ds->legend[x] = tile; |
1764 | draw_tile(dr, ds, -1, x, tile, 0, 0); |
1765 | draw_tile(dr, ds, x, -1, tile, 0, 0); |
1766 | } |
1767 | } |
1768 | |
d8fa223c |
1769 | for (y = 0; y < w; y++) { |
f536db21 |
1770 | int sy = ds->sequence[y]; |
d8fa223c |
1771 | for (x = 0; x < w; x++) { |
1772 | long tile = 0L, pencil = 0L, error; |
f536db21 |
1773 | int sx = ds->sequence[x]; |
d8fa223c |
1774 | |
f536db21 |
1775 | if (state->grid[sy*w+sx]) |
1776 | tile = state->grid[sy*w+sx]; |
d8fa223c |
1777 | else |
f536db21 |
1778 | pencil = (long)state->pencil[sy*w+sx]; |
d8fa223c |
1779 | |
f536db21 |
1780 | if (state->immutable[sy*w+sx]) |
d8fa223c |
1781 | tile |= DF_IMMUTABLE; |
1782 | |
a346afcd |
1783 | if ((ui->drag == 5 && ui->dragnum == sy) || |
1784 | (ui->drag == 6 && ui->dragnum == sx)) |
f536db21 |
1785 | tile |= DF_HIGHLIGHT; |
1786 | else if (ui->hshow && ui->hx == sx && ui->hy == sy) |
d8fa223c |
1787 | tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT); |
1788 | |
1789 | if (flashtime > 0 && |
1790 | (flashtime <= FLASH_TIME/3 || |
1791 | flashtime >= FLASH_TIME*2/3)) |
1792 | tile |= DF_HIGHLIGHT; /* completion flash */ |
1793 | |
a346afcd |
1794 | if (y <= 0 || state->dividers[ds->sequence[y-1]] == sy) |
1795 | tile |= DF_DIVIDER_TOP; |
1796 | if (y+1 >= w || state->dividers[sy] == ds->sequence[y+1]) |
1797 | tile |= DF_DIVIDER_BOT; |
1798 | if (x <= 0 || state->dividers[ds->sequence[x-1]] == sx) |
1799 | tile |= DF_DIVIDER_LEFT; |
1800 | if (x+1 >= w || state->dividers[sx] == ds->sequence[x+1]) |
1801 | tile |= DF_DIVIDER_RIGHT; |
1802 | |
b725220b |
1803 | error = ds->errtmp[sy*w+sx]; |
d8fa223c |
1804 | |
1805 | if (ds->tiles[y*w+x] != tile || |
1806 | ds->pencil[y*w+x] != pencil || |
1807 | ds->errors[y*w+x] != error) { |
1808 | ds->tiles[y*w+x] = tile; |
1809 | ds->pencil[y*w+x] = pencil; |
1810 | ds->errors[y*w+x] = error; |
1811 | draw_tile(dr, ds, x, y, tile, pencil, error); |
1812 | } |
1813 | } |
1814 | } |
1815 | } |
1816 | |
1817 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
1818 | int dir, game_ui *ui) |
1819 | { |
1820 | return 0.0F; |
1821 | } |
1822 | |
1823 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
1824 | int dir, game_ui *ui) |
1825 | { |
1826 | if (!oldstate->completed && newstate->completed && |
1827 | !oldstate->cheated && !newstate->cheated) |
1828 | return FLASH_TIME; |
1829 | return 0.0F; |
1830 | } |
1831 | |
1cea529f |
1832 | static int game_status(game_state *state) |
4496362f |
1833 | { |
1cea529f |
1834 | return state->completed ? +1 : 0; |
4496362f |
1835 | } |
1836 | |
d8fa223c |
1837 | static int game_timing_state(game_state *state, game_ui *ui) |
1838 | { |
1839 | if (state->completed) |
1840 | return FALSE; |
1841 | return TRUE; |
1842 | } |
1843 | |
1844 | static void game_print_size(game_params *params, float *x, float *y) |
1845 | { |
1846 | int pw, ph; |
1847 | |
1848 | /* |
1849 | * We use 9mm squares by default, like Solo. |
1850 | */ |
1851 | game_compute_size(params, 900, &pw, &ph); |
1852 | *x = pw / 100.0F; |
1853 | *y = ph / 100.0F; |
1854 | } |
1855 | |
1856 | static void game_print(drawing *dr, game_state *state, int tilesize) |
1857 | { |
1858 | int w = state->par.w; |
1859 | int ink = print_mono_colour(dr, 0); |
d8fa223c |
1860 | int x, y; |
1861 | |
1862 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1863 | game_drawstate ads, *ds = &ads; |
1864 | game_set_size(dr, ds, NULL, tilesize); |
1865 | |
1866 | /* |
b84b31a8 |
1867 | * Border. |
d8fa223c |
1868 | */ |
b84b31a8 |
1869 | print_line_width(dr, 3 * TILESIZE / 40); |
1870 | draw_rect_outline(dr, BORDER + LEGEND, BORDER + LEGEND, |
1871 | w*TILESIZE, w*TILESIZE, ink); |
d8fa223c |
1872 | |
1873 | /* |
b84b31a8 |
1874 | * Legend on table. |
d8fa223c |
1875 | */ |
b84b31a8 |
1876 | for (x = 0; x < w; x++) { |
1877 | char str[2]; |
1878 | str[1] = '\0'; |
5523645e |
1879 | str[0] = TOCHAR(x+1, state->par.id); |
b84b31a8 |
1880 | draw_text(dr, BORDER+LEGEND + x*TILESIZE + TILESIZE/2, |
1881 | BORDER + TILESIZE/2, |
1882 | FONT_VARIABLE, TILESIZE/2, |
1883 | ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str); |
1884 | draw_text(dr, BORDER + TILESIZE/2, |
1885 | BORDER+LEGEND + x*TILESIZE + TILESIZE/2, |
1886 | FONT_VARIABLE, TILESIZE/2, |
1887 | ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str); |
1888 | } |
d8fa223c |
1889 | |
1890 | /* |
1891 | * Main grid. |
1892 | */ |
1893 | for (x = 1; x < w; x++) { |
1894 | print_line_width(dr, TILESIZE / 40); |
b84b31a8 |
1895 | draw_line(dr, BORDER+LEGEND+x*TILESIZE, BORDER+LEGEND, |
1896 | BORDER+LEGEND+x*TILESIZE, BORDER+LEGEND+w*TILESIZE, ink); |
d8fa223c |
1897 | } |
1898 | for (y = 1; y < w; y++) { |
1899 | print_line_width(dr, TILESIZE / 40); |
b84b31a8 |
1900 | draw_line(dr, BORDER+LEGEND, BORDER+LEGEND+y*TILESIZE, |
1901 | BORDER+LEGEND+w*TILESIZE, BORDER+LEGEND+y*TILESIZE, ink); |
d8fa223c |
1902 | } |
1903 | |
1904 | /* |
1905 | * Numbers. |
1906 | */ |
1907 | for (y = 0; y < w; y++) |
1908 | for (x = 0; x < w; x++) |
1909 | if (state->grid[y*w+x]) { |
1910 | char str[2]; |
1911 | str[1] = '\0'; |
5523645e |
1912 | str[0] = TOCHAR(state->grid[y*w+x], state->par.id); |
b84b31a8 |
1913 | draw_text(dr, BORDER+LEGEND + x*TILESIZE + TILESIZE/2, |
1914 | BORDER+LEGEND + y*TILESIZE + TILESIZE/2, |
d8fa223c |
1915 | FONT_VARIABLE, TILESIZE/2, |
1916 | ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str); |
1917 | } |
1918 | } |
1919 | |
1920 | #ifdef COMBINED |
1921 | #define thegame group |
1922 | #endif |
1923 | |
1924 | const struct game thegame = { |
1925 | "Group", NULL, NULL, |
1926 | default_params, |
1927 | game_fetch_preset, |
1928 | decode_params, |
1929 | encode_params, |
1930 | free_params, |
1931 | dup_params, |
1932 | TRUE, game_configure, custom_params, |
1933 | validate_params, |
1934 | new_game_desc, |
1935 | validate_desc, |
1936 | new_game, |
1937 | dup_game, |
1938 | free_game, |
1939 | TRUE, solve_game, |
1940 | TRUE, game_can_format_as_text_now, game_text_format, |
1941 | new_ui, |
1942 | free_ui, |
1943 | encode_ui, |
1944 | decode_ui, |
1945 | game_changed_state, |
1946 | interpret_move, |
1947 | execute_move, |
1948 | PREFERRED_TILESIZE, game_compute_size, game_set_size, |
1949 | game_colours, |
1950 | game_new_drawstate, |
1951 | game_free_drawstate, |
1952 | game_redraw, |
1953 | game_anim_length, |
1954 | game_flash_length, |
1cea529f |
1955 | game_status, |
d8fa223c |
1956 | TRUE, FALSE, game_print_size, game_print, |
1957 | FALSE, /* wants_statusbar */ |
1958 | FALSE, game_timing_state, |
1959 | REQUIRE_RBUTTON | REQUIRE_NUMPAD, /* flags */ |
1960 | }; |
1961 | |
1962 | #ifdef STANDALONE_SOLVER |
1963 | |
1964 | #include <stdarg.h> |
1965 | |
1966 | int main(int argc, char **argv) |
1967 | { |
1968 | game_params *p; |
1969 | game_state *s; |
1970 | char *id = NULL, *desc, *err; |
1971 | digit *grid; |
1972 | int grade = FALSE; |
1973 | int ret, diff, really_show_working = FALSE; |
1974 | |
1975 | while (--argc > 0) { |
1976 | char *p = *++argv; |
1977 | if (!strcmp(p, "-v")) { |
1978 | really_show_working = TRUE; |
1979 | } else if (!strcmp(p, "-g")) { |
1980 | grade = TRUE; |
1981 | } else if (*p == '-') { |
1982 | fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p); |
1983 | return 1; |
1984 | } else { |
1985 | id = p; |
1986 | } |
1987 | } |
1988 | |
1989 | if (!id) { |
1990 | fprintf(stderr, "usage: %s [-g | -v] <game_id>\n", argv[0]); |
1991 | return 1; |
1992 | } |
1993 | |
1994 | desc = strchr(id, ':'); |
1995 | if (!desc) { |
1996 | fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]); |
1997 | return 1; |
1998 | } |
1999 | *desc++ = '\0'; |
2000 | |
2001 | p = default_params(); |
2002 | decode_params(p, id); |
2003 | err = validate_desc(p, desc); |
2004 | if (err) { |
2005 | fprintf(stderr, "%s: %s\n", argv[0], err); |
2006 | return 1; |
2007 | } |
2008 | s = new_game(NULL, p, desc); |
2009 | |
2010 | grid = snewn(p->w * p->w, digit); |
2011 | |
2012 | /* |
2013 | * When solving a Normal puzzle, we don't want to bother the |
2014 | * user with Hard-level deductions. For this reason, we grade |
2015 | * the puzzle internally before doing anything else. |
2016 | */ |
2017 | ret = -1; /* placate optimiser */ |
2018 | solver_show_working = FALSE; |
2019 | for (diff = 0; diff < DIFFCOUNT; diff++) { |
2020 | memcpy(grid, s->grid, p->w * p->w); |
5523645e |
2021 | ret = solver(&s->par, grid, diff); |
d8fa223c |
2022 | if (ret <= diff) |
2023 | break; |
2024 | } |
2025 | |
2026 | if (diff == DIFFCOUNT) { |
2027 | if (grade) |
2028 | printf("Difficulty rating: ambiguous\n"); |
2029 | else |
2030 | printf("Unable to find a unique solution\n"); |
2031 | } else { |
2032 | if (grade) { |
2033 | if (ret == diff_impossible) |
2034 | printf("Difficulty rating: impossible (no solution exists)\n"); |
2035 | else |
2036 | printf("Difficulty rating: %s\n", group_diffnames[ret]); |
2037 | } else { |
2038 | solver_show_working = really_show_working; |
2039 | memcpy(grid, s->grid, p->w * p->w); |
5523645e |
2040 | ret = solver(&s->par, grid, diff); |
d8fa223c |
2041 | if (ret != diff) |
2042 | printf("Puzzle is inconsistent\n"); |
2043 | else { |
2044 | memcpy(s->grid, grid, p->w * p->w); |
2045 | fputs(game_text_format(s), stdout); |
2046 | } |
2047 | } |
2048 | } |
2049 | |
2050 | return 0; |
2051 | } |
2052 | |
2053 | #endif |
2054 | |
2055 | /* vim: set shiftwidth=4 tabstop=8: */ |