2 * group.c: a Latin-square puzzle, but played with groups' Cayley
3 * tables. That is, you are given a Cayley table of a group with
4 * most elements blank and a few clues, and you must fill it in
5 * so as to preserve the group axioms.
7 * This is a perfectly playable and fully working puzzle, but I'm
8 * leaving it for the moment in the 'unfinished' directory because
9 * it's just too esoteric (not to mention _hard_) for me to be
10 * comfortable presenting it to the general public as something they
11 * might (implicitly) actually want to play.
15 * - more solver techniques?
16 * * Inverses: once we know that gh = e, we can immediately
17 * deduce hg = e as well; then for any gx=y we can deduce
18 * hy=x, and for any xg=y we have yh=x.
19 * * Hard-mode associativity: we currently deduce based on
20 * definite numbers in the grid, but we could also winnow
21 * based on _possible_ numbers.
22 * * My overambitious original thoughts included wondering if we
23 * could infer that there must be elements of certain orders
24 * (e.g. a group of order divisible by 5 must contain an
25 * element of order 5), but I think in fact this is probably
40 * Difficulty levels. I do some macro ickery here to ensure that my
41 * enum and the various forms of my name list always match up.
44 A(TRIVIAL,Trivial,NULL,t) \
45 A(NORMAL,Normal,solver_normal,n) \
47 A(EXTREME,Extreme,NULL,x) \
48 A(UNREASONABLE,Unreasonable,NULL,u)
49 #define ENUM(upper,title,func,lower) DIFF_ ## upper,
50 #define TITLE(upper,title,func,lower) #title,
51 #define ENCODE(upper,title,func,lower) #lower
52 #define CONFIG(upper,title,func,lower) ":" #title
53 enum { DIFFLIST(ENUM
) DIFFCOUNT
};
54 static char const *const group_diffnames
[] = { DIFFLIST(TITLE
) };
55 static char const group_diffchars
[] = DIFFLIST(ENCODE
);
56 #define DIFFCONFIG DIFFLIST(CONFIG)
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.
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) )
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))
86 unsigned char *immutable
;
87 int *pencil
; /* bitmaps using bits 1<<1..1<<n */
88 int completed
, cheated
;
89 digit
*sequence
; /* sequence of group elements shown */
92 static game_params
*default_params(void)
94 game_params
*ret
= snew(game_params
);
97 ret
->diff
= DIFF_NORMAL
;
103 const static struct game_params group_presets
[] = {
104 { 6, DIFF_NORMAL
, TRUE
},
105 { 6, DIFF_NORMAL
, FALSE
},
106 { 8, DIFF_NORMAL
, TRUE
},
107 { 8, DIFF_NORMAL
, FALSE
},
108 { 8, DIFF_HARD
, TRUE
},
109 { 8, DIFF_HARD
, FALSE
},
110 { 12, DIFF_NORMAL
, TRUE
},
113 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
118 if (i
< 0 || i
>= lenof(group_presets
))
121 ret
= snew(game_params
);
122 *ret
= group_presets
[i
]; /* structure copy */
124 sprintf(buf
, "%dx%d %s%s", ret
->w
, ret
->w
, group_diffnames
[ret
->diff
],
125 ret
->id ?
"" : ", identity hidden");
132 static void free_params(game_params
*params
)
137 static game_params
*dup_params(game_params
*params
)
139 game_params
*ret
= snew(game_params
);
140 *ret
= *params
; /* structure copy */
144 static void decode_params(game_params
*params
, char const *string
)
146 char const *p
= string
;
149 while (*p
&& isdigit((unsigned char)*p
)) p
++;
150 params
->diff
= DIFF_NORMAL
;
157 params
->diff
= DIFFCOUNT
+1; /* ...which is invalid */
159 for (i
= 0; i
< DIFFCOUNT
; i
++) {
160 if (*p
== group_diffchars
[i
])
165 } else if (*p
== 'i') {
169 /* unrecognised character */
175 static char *encode_params(game_params
*params
, int full
)
179 sprintf(ret
, "%d", params
->w
);
181 sprintf(ret
+ strlen(ret
), "d%c", group_diffchars
[params
->diff
]);
183 sprintf(ret
+ strlen(ret
), "i");
188 static config_item
*game_configure(game_params
*params
)
193 ret
= snewn(4, config_item
);
195 ret
[0].name
= "Grid size";
196 ret
[0].type
= C_STRING
;
197 sprintf(buf
, "%d", params
->w
);
198 ret
[0].sval
= dupstr(buf
);
201 ret
[1].name
= "Difficulty";
202 ret
[1].type
= C_CHOICES
;
203 ret
[1].sval
= DIFFCONFIG
;
204 ret
[1].ival
= params
->diff
;
206 ret
[2].name
= "Show identity";
207 ret
[2].type
= C_BOOLEAN
;
209 ret
[2].ival
= params
->id
;
219 static game_params
*custom_params(config_item
*cfg
)
221 game_params
*ret
= snew(game_params
);
223 ret
->w
= atoi(cfg
[0].sval
);
224 ret
->diff
= cfg
[1].ival
;
225 ret
->id
= cfg
[2].ival
;
230 static char *validate_params(game_params
*params
, int full
)
232 if (params
->w
< 3 || params
->w
> 26)
233 return "Grid size must be between 3 and 26";
234 if (params
->diff
>= DIFFCOUNT
)
235 return "Unknown difficulty rating";
236 if (!params
->id
&& params
->diff
== DIFF_TRIVIAL
) {
238 * We can't have a Trivial-difficulty puzzle (i.e. latin
239 * square deductions only) without a clear identity, because
240 * identityless puzzles always have two rows and two columns
241 * entirely blank, and no latin-square deduction permits the
242 * distinguishing of two such rows.
244 return "Trivial puzzles must have an identity";
246 if (!params
->id
&& params
->w
== 3) {
248 * We can't have a 3x3 puzzle without an identity either,
249 * because 3x3 puzzles can't ever be harder than Trivial
250 * (there are no 3x3 latin squares which aren't also valid
251 * group tables, so enabling group-based deductions doesn't
252 * rule out any possible solutions) and - as above - Trivial
253 * puzzles can't not have an identity.
255 return "3x3 puzzles must have an identity";
260 /* ----------------------------------------------------------------------
264 static int solver_normal(struct latin_solver
*solver
, void *vctx
)
267 #ifdef STANDALONE_SOLVER
268 char **names
= solver
->names
;
270 digit
*grid
= solver
->grid
;
274 * Deduce using associativity: (ab)c = a(bc).
276 * So we pick any a,b,c we like; then if we know ab, bc, and
277 * (ab)c we can fill in a(bc).
279 for (i
= 1; i
< w
; i
++)
280 for (j
= 1; j
< w
; j
++)
281 for (k
= 1; k
< w
; k
++) {
282 if (!grid
[i
*w
+j
] || !grid
[j
*w
+k
])
284 if (grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
285 !grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
286 int x
= grid
[j
*w
+k
]-1, y
= i
;
287 int n
= grid
[(grid
[i
*w
+j
]-1)*w
+k
];
288 #ifdef STANDALONE_SOLVER
289 if (solver_show_working
) {
290 printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n",
291 solver_recurse_depth
*4, "",
292 names
[i
], names
[j
], names
[k
],
293 names
[grid
[i
*w
+j
]-1], names
[k
],
294 names
[i
], names
[grid
[j
*w
+k
]-1]);
295 printf("%*s placing %s at (%d,%d)\n",
296 solver_recurse_depth
*4, "",
297 names
[n
-1], x
+1, y
+1);
300 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
301 latin_solver_place(solver
, x
, y
, n
);
304 #ifdef STANDALONE_SOLVER
305 if (solver_show_working
)
306 printf("%*s contradiction!\n",
307 solver_recurse_depth
*4, "");
312 if (!grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
313 grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
314 int x
= k
, y
= grid
[i
*w
+j
]-1;
315 int n
= grid
[i
*w
+(grid
[j
*w
+k
]-1)];
316 #ifdef STANDALONE_SOLVER
317 if (solver_show_working
) {
318 printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n",
319 solver_recurse_depth
*4, "",
320 names
[i
], names
[j
], names
[k
],
321 names
[grid
[i
*w
+j
]-1], names
[k
],
322 names
[i
], names
[grid
[j
*w
+k
]-1]);
323 printf("%*s placing %s at (%d,%d)\n",
324 solver_recurse_depth
*4, "",
325 names
[n
-1], x
+1, y
+1);
328 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
329 latin_solver_place(solver
, x
, y
, n
);
332 #ifdef STANDALONE_SOLVER
333 if (solver_show_working
)
334 printf("%*s contradiction!\n",
335 solver_recurse_depth
*4, "");
345 #define SOLVER(upper,title,func,lower) func,
346 static usersolver_t
const group_solvers
[] = { DIFFLIST(SOLVER
) };
348 static int solver(game_params
*params
, digit
*grid
, int maxdiff
)
352 struct latin_solver solver
;
353 #ifdef STANDALONE_SOLVER
354 char *p
, text
[100], *names
[50];
358 latin_solver_alloc(&solver
, grid
, w
);
359 #ifdef STANDALONE_SOLVER
360 for (i
= 0, p
= text
; i
< w
; i
++) {
362 *p
++ = TOCHAR(i
+1, params
->id
);
365 solver
.names
= names
;
368 ret
= latin_solver_main(&solver
, maxdiff
,
369 DIFF_TRIVIAL
, DIFF_HARD
, DIFF_EXTREME
,
370 DIFF_EXTREME
, DIFF_UNREASONABLE
,
371 group_solvers
, NULL
, NULL
, NULL
);
373 latin_solver_free(&solver
);
378 /* ----------------------------------------------------------------------
382 static char *encode_grid(char *desc
, digit
*grid
, int area
)
388 for (i
= 0; i
<= area
; i
++) {
389 int n
= (i
< area ? grid
[i
] : -1);
396 int c
= 'a' - 1 + run
;
400 run
-= c
- ('a' - 1);
404 * If there's a number in the very top left or
405 * bottom right, there's no point putting an
406 * unnecessary _ before or after it.
408 if (p
> desc
&& n
> 0)
412 p
+= sprintf(p
, "%d", n
);
419 /* ----- data generated by group.gap begins ----- */
422 unsigned long autosize
;
428 const struct group
*groups
;
431 static const struct group groupdata
[] = {
438 {6L, 4, 2, "BADC" "CDAB"},
442 {6L, 6, 2, "CFEBAD" "BADCFE"},
443 {2L, 6, 1, "DCFEBA"},
445 {6L, 7, 1, "BCDEFGA"},
447 {4L, 8, 1, "BCEFDGHA"},
448 {8L, 8, 2, "BDEFGAHC" "EGBHDCFA"},
449 {8L, 8, 2, "EGBHDCFA" "BAEFCDHG"},
450 {24L, 8, 2, "BDEFGAHC" "CHDGBEAF"},
451 {168L, 8, 3, "BAEFCDHG" "CEAGBHDF" "DFGAHBCE"},
453 {6L, 9, 1, "BDECGHFIA"},
454 {48L, 9, 2, "BDEAGHCIF" "CEFGHAIBD"},
456 {20L, 10, 2, "CJEBGDIFAH" "BADCFEHGJI"},
457 {4L, 10, 1, "DCFEHGJIBA"},
459 {10L, 11, 1, "BCDEFGHIJKA"},
461 {12L, 12, 2, "GLDKJEHCBIAF" "BCEFAGIJDKLH"},
462 {4L, 12, 1, "EHIJKCBLDGFA"},
463 {24L, 12, 2, "BEFGAIJKCDLH" "FJBKHLEGDCIA"},
464 {12L, 12, 2, "GLDKJEHCBIAF" "BAEFCDIJGHLK"},
465 {12L, 12, 2, "FDIJGHLBKAEC" "GIDKFLHCJEAB"},
467 {12L, 13, 1, "BCDEFGHIJKLMA"},
469 {42L, 14, 2, "ELGNIBKDMFAHCJ" "BADCFEHGJILKNM"},
470 {6L, 14, 1, "FEHGJILKNMBADC"},
472 {8L, 15, 1, "EGHCJKFMNIOBLDA"},
474 {8L, 16, 1, "MKNPFOADBGLCIEHJ"},
475 {96L, 16, 2, "ILKCONFPEDJHGMAB" "BDFGHIAKLMNCOEPJ"},
476 {32L, 16, 2, "MIHPFDCONBLAKJGE" "BEFGHJKALMNOCDPI"},
477 {32L, 16, 2, "IFACOGLMDEJBNPKH" "BEFGHJKALMNOCDPI"},
478 {16L, 16, 2, "MOHPFKCINBLADJGE" "BDFGHIEKLMNJOAPC"},
479 {16L, 16, 2, "MIHPFDJONBLEKCGA" "BDFGHIEKLMNJOAPC"},
480 {32L, 16, 2, "MOHPFDCINBLEKJGA" "BAFGHCDELMNIJKPO"},
481 {16L, 16, 2, "MIHPFKJONBLADCGE" "GDPHNOEKFLBCIAMJ"},
482 {32L, 16, 2, "MIBPFDJOGHLEKCNA" "CLEIJGMPKAOHNFDB"},
484 "MCHPFAIJNBLDEOGK" "BEFGHJKALMNOCDPI" "GKLBNOEDFPHJIAMC"},
485 {64L, 16, 3, "MCHPFAIJNBLDEOGK" "LOGFPKJIBNMEDCHA" "CMAIJHPFDEONBLKG"},
487 "IPKCOGMLEDJBNFAH" "BEFGHJKALMNOCDPI" "CMEIJBPFKAOGHLDN"},
488 {48L, 16, 3, "IPDJONFLEKCBGMAH" "FJBLMEOCGHPKAIND" "DGIEKLHNJOAMPBCF"},
490 "EHJKAMNBOCDPFGIL" "BAFGHCDELMNIJKPO" "CFAIJBLMDEOGHPKN"
493 {16L, 17, 1, "EFGHIJKLMNOPQABCD"},
495 {54L, 18, 2, "MKIQOPNAGLRECDBJHF" "BAEFCDJKLGHIOPMNRQ"},
496 {6L, 18, 1, "ECJKGHFOPDMNLRIQBA"},
497 {12L, 18, 2, "ECJKGHBOPAMNFRDQLI" "KNOPQCFREIGHLJAMBD"},
499 "IFNAKLQCDOPBGHREMJ" "NOQCFRIGHKLJAMPBDE" "BAEFCDJKLGHIOPMNRQ"},
500 {48L, 18, 2, "ECJKGHBOPAMNFRDQLI" "FDKLHIOPBMNAREQCJG"},
502 {18L, 19, 1, "EFGHIJKLMNOPQRSABCD"},
504 {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "EABICDFMGHJQKLNTOPRS"},
505 {8L, 20, 1, "EHIJLCMNPGQRSKBTDOFA"},
506 {20L, 20, 2, "DJSHQNCLTRGPEBKAIFOM" "EABICDFMGHJQKLNTOPRS"},
507 {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "ECBIAGFMDKJQHONTLSRP"},
508 {24L, 20, 2, "IGFMDKJQHONTLSREPCBA" "FDIJGHMNKLQROPTBSAEC"},
510 {42L, 21, 2, "ITLSBOUERDHAGKCJNFMQP" "EJHLMKOPNRSQAUTCDBFGI"},
511 {12L, 21, 1, "EGHCJKFMNIPQLSTOUBRDA"},
513 {110L, 22, 2, "ETGVIBKDMFOHQJSLUNAPCR" "BADCFEHGJILKNMPORQTSVU"},
514 {10L, 22, 1, "FEHGJILKNMPORQTSVUBADC"},
516 {22L, 23, 1, "EFGHIJKLMNOPQRSTUVWABCD"},
518 {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "HRNOPSWCTUVBLDIJXFGAKQME"},
519 {8L, 24, 1, "MQBTUDRWFGHXJELINOPKSAVC"},
520 {24L, 24, 2, "IOQRBEUVFWGHKLAXMNPSCDTJ" "NJXOVGDKSMTFIPQELCURBWAH"},
521 {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "HSNOPWLDTUVBRIAKXFGCQEMJ"},
522 {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "TWHNXLRIOPUMSACQVBFDEJGK"},
523 {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "BAFGHCDEMNOPIJKLTUVQRSXW"},
525 "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO"
526 "HSNOPWLDTUVBRIAKXFGCQEMJ"},
528 "QUKJWPXFESRIVBMNLDCGHTAO" "JXEQRVUMKLWCPGFTSAIBONDH"
529 "TRONXLWCHVUMSAIJPGFDEQBK"},
530 {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "VKXHOQASNTPBCWDEUFGIJLMR"},
531 {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "RMLWIGTUSDJQOPFXEKCBVNAH"},
532 {48L, 24, 2, "IULQRGXMSDCWOPNTEKJBVFAH" "GLMOPRSDTUBVWIEKFXHJQANC"},
533 {24L, 24, 2, "UJPXMRCSNHGTLWIKFVBEDQOA" "NRUFVLWIPXMOJEDQHGTCSABK"},
534 {24L, 24, 2, "MIBTUAQRFGHXCDEWNOPJKLVS" "OKXVFWSCGUTNDRQJBPMALIHE"},
536 "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO"
537 "BAFGHCDEMNOPIJKLTUVQRSXW"},
539 "QTKJWONXESRIHVUMLDCPGFAB" "JNEQRHTUKLWCOPXFSAIVBMDG"
540 "HENOPJKLTUVBQRSAXFGWCDMI"},
542 {20L, 25, 1, "EHILMNPQRSFTUVBJWXDOYGAKC"},
543 {480L, 25, 2, "EHILMNPQRSCTUVBFWXDJYGOKA" "BDEGHIKLMNAPQRSCTUVFWXJYO"},
546 "EXGZIBKDMFOHQJSLUNWPYRATCV" "BADCFEHGJILKNMPORQTSVUXWZY"},
547 {12L, 26, 1, "FEHGJILKNMPORQTSVUXWZYBADC"},
550 static const struct groups groups
[] = {
551 {0, NULL
}, /* trivial case: 0 */
552 {0, NULL
}, /* trivial case: 1 */
553 {1, groupdata
+ 0}, /* 2 */
554 {1, groupdata
+ 1}, /* 3 */
555 {2, groupdata
+ 2}, /* 4 */
556 {1, groupdata
+ 4}, /* 5 */
557 {2, groupdata
+ 5}, /* 6 */
558 {1, groupdata
+ 7}, /* 7 */
559 {5, groupdata
+ 8}, /* 8 */
560 {2, groupdata
+ 13}, /* 9 */
561 {2, groupdata
+ 15}, /* 10 */
562 {1, groupdata
+ 17}, /* 11 */
563 {5, groupdata
+ 18}, /* 12 */
564 {1, groupdata
+ 23}, /* 13 */
565 {2, groupdata
+ 24}, /* 14 */
566 {1, groupdata
+ 26}, /* 15 */
567 {14, groupdata
+ 27}, /* 16 */
568 {1, groupdata
+ 41}, /* 17 */
569 {5, groupdata
+ 42}, /* 18 */
570 {1, groupdata
+ 47}, /* 19 */
571 {5, groupdata
+ 48}, /* 20 */
572 {2, groupdata
+ 53}, /* 21 */
573 {2, groupdata
+ 55}, /* 22 */
574 {1, groupdata
+ 57}, /* 23 */
575 {15, groupdata
+ 58}, /* 24 */
576 {2, groupdata
+ 73}, /* 25 */
577 {2, groupdata
+ 75}, /* 26 */
580 /* ----- data generated by group.gap ends ----- */
582 static char *new_game_desc(game_params
*params
, random_state
*rs
,
583 char **aux
, int interactive
)
585 int w
= params
->w
, a
= w
*w
;
586 digit
*grid
, *soln
, *soln2
;
589 int diff
= params
->diff
;
590 const struct group
*group
;
594 * Difficulty exceptions: some combinations of size and
595 * difficulty cannot be satisfied, because all puzzles of at
596 * most that difficulty are actually even easier.
598 * Remember to re-test this whenever a change is made to the
601 * I tested it using the following shell command:
603 for d in t n h x u; do
606 echo -n "./group --generate 1 ${i}d${d}${id}: "
607 perl -e 'alarm 30; exec @ARGV' \
608 ./group --generate 1 ${i}d${d}${id} >/dev/null && echo ok
613 * Of course, it's better to do that after taking the exceptions
614 * _out_, so as to detect exceptions that should be removed as
615 * well as those which should be added.
617 if (w
< 5 && diff
== DIFF_UNREASONABLE
)
619 if ((w
< 5 || ((w
== 6 || w
== 8) && params
->id
)) && diff
== DIFF_EXTREME
)
621 if ((w
< 6 || (w
== 6 && params
->id
)) && diff
== DIFF_HARD
)
623 if ((w
< 4 || (w
== 4 && params
->id
)) && diff
== DIFF_NORMAL
)
626 grid
= snewn(a
, digit
);
627 soln
= snewn(a
, digit
);
628 soln2
= snewn(a
, digit
);
629 indices
= snewn(a
, int);
633 * Construct a valid group table, by picking a group from
634 * the above data table, decompressing it into a full
635 * representation by BFS, and then randomly permuting its
636 * non-identity elements.
638 * We build the canonical table in 'soln' (and use 'grid' as
639 * our BFS queue), then transfer the table into 'grid'
640 * having shuffled the rows.
643 assert(w
< lenof(groups
));
644 group
= groups
[w
].groups
+ random_upto(rs
, groups
[w
].ngroups
);
645 assert(group
->order
== w
);
647 for (i
= 0; i
< w
; i
++)
655 row
= soln
+ (i
-1)*w
;
657 for (j
= 0; j
< group
->ngens
; j
++) {
659 const char *gen
= group
->gens
+ j
*w
;
662 * Apply each group generator to row, constructing a
665 nri
= gen
[row
[0]-1] - 'A' + 1; /* which row is it? */
666 newrow
= soln
+ (nri
-1)*w
;
667 if (!newrow
[0]) { /* not done yet */
668 for (k
= 0; k
< w
; k
++)
669 newrow
[k
] = gen
[row
[k
]-1] - 'A' + 1;
674 /* That's got the canonical table. Now shuffle it. */
675 for (i
= 0; i
< w
; i
++)
677 if (params
->id
) /* do we shuffle in the identity? */
678 shuffle(soln2
+1, w
-1, sizeof(*soln2
), rs
);
680 shuffle(soln2
, w
, sizeof(*soln2
), rs
);
681 for (i
= 0; i
< w
; i
++)
682 for (j
= 0; j
< w
; j
++)
683 grid
[(soln2
[i
])*w
+(soln2
[j
])] = soln2
[soln
[i
*w
+j
]-1]+1;
686 * Remove entries one by one while the puzzle is still
687 * soluble at the appropriate difficulty level.
689 memcpy(soln
, grid
, a
);
692 * Start by blanking the entire identity row and column,
693 * and also another row and column so that the player
694 * can't trivially determine which element is the
698 j
= 1 + random_upto(rs
, w
-1); /* pick a second row/col to blank */
699 for (i
= 0; i
< w
; i
++) {
700 grid
[(soln2
[0])*w
+i
] = grid
[i
*w
+(soln2
[0])] = 0;
701 grid
[(soln2
[j
])*w
+i
] = grid
[i
*w
+(soln2
[j
])] = 0;
704 memcpy(soln2
, grid
, a
);
705 if (solver(params
, soln2
, diff
) > diff
)
706 continue; /* go round again if that didn't work */
710 for (i
= (params
->id ?
1 : 0); i
< w
; i
++)
711 for (j
= (params
->id ?
1 : 0); j
< w
; j
++)
713 indices
[k
++] = i
*w
+j
;
714 shuffle(indices
, k
, sizeof(*indices
), rs
);
716 for (i
= 0; i
< k
; i
++) {
717 memcpy(soln2
, grid
, a
);
718 soln2
[indices
[i
]] = 0;
719 if (solver(params
, soln2
, diff
) <= diff
)
720 grid
[indices
[i
]] = 0;
724 * Make sure the puzzle isn't too easy.
727 memcpy(soln2
, grid
, a
);
728 if (solver(params
, soln2
, diff
-1) < diff
)
729 continue; /* go round and try again */
739 * Encode the puzzle description.
741 desc
= snewn(a
*20, char);
742 p
= encode_grid(desc
, grid
, a
);
744 desc
= sresize(desc
, p
- desc
, char);
747 * Encode the solution.
749 *aux
= snewn(a
+2, char);
751 for (i
= 0; i
< a
; i
++)
752 (*aux
)[i
+1] = TOCHAR(soln
[i
], params
->id
);
763 /* ----------------------------------------------------------------------
767 static char *validate_grid_desc(const char **pdesc
, int range
, int area
)
769 const char *desc
= *pdesc
;
771 while (*desc
&& *desc
!= ',') {
773 if (n
>= 'a' && n
<= 'z') {
774 squares
+= n
- 'a' + 1;
775 } else if (n
== '_') {
777 } else if (n
> '0' && n
<= '9') {
778 int val
= atoi(desc
-1);
779 if (val
< 1 || val
> range
)
780 return "Out-of-range number in game description";
782 while (*desc
>= '0' && *desc
<= '9')
785 return "Invalid character in game description";
789 return "Not enough data to fill grid";
792 return "Too much data to fit in grid";
797 static char *validate_desc(game_params
*params
, char *desc
)
799 int w
= params
->w
, a
= w
*w
;
800 const char *p
= desc
;
802 return validate_grid_desc(&p
, w
, a
);
805 static char *spec_to_grid(char *desc
, digit
*grid
, int area
)
808 while (*desc
&& *desc
!= ',') {
810 if (n
>= 'a' && n
<= 'z') {
811 int run
= n
- 'a' + 1;
812 assert(i
+ run
<= area
);
815 } else if (n
== '_') {
817 } else if (n
> '0' && n
<= '9') {
819 grid
[i
++] = atoi(desc
-1);
820 while (*desc
>= '0' && *desc
<= '9')
823 assert(!"We can't get here");
830 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
832 int w
= params
->w
, a
= w
*w
;
833 game_state
*state
= snew(game_state
);
836 state
->par
= *params
; /* structure copy */
837 state
->grid
= snewn(a
, digit
);
838 state
->immutable
= snewn(a
, unsigned char);
839 state
->pencil
= snewn(a
, int);
840 for (i
= 0; i
< a
; i
++) {
842 state
->immutable
[i
] = 0;
843 state
->pencil
[i
] = 0;
845 state
->sequence
= snewn(w
, digit
);
846 for (i
= 0; i
< w
; i
++) {
847 state
->sequence
[i
] = i
;
850 desc
= spec_to_grid(desc
, state
->grid
, a
);
851 for (i
= 0; i
< a
; i
++)
852 if (state
->grid
[i
] != 0)
853 state
->immutable
[i
] = TRUE
;
855 state
->completed
= state
->cheated
= FALSE
;
860 static game_state
*dup_game(game_state
*state
)
862 int w
= state
->par
.w
, a
= w
*w
;
863 game_state
*ret
= snew(game_state
);
865 ret
->par
= state
->par
; /* structure copy */
867 ret
->grid
= snewn(a
, digit
);
868 ret
->immutable
= snewn(a
, unsigned char);
869 ret
->pencil
= snewn(a
, int);
870 ret
->sequence
= snewn(w
, digit
);
871 memcpy(ret
->grid
, state
->grid
, a
*sizeof(digit
));
872 memcpy(ret
->immutable
, state
->immutable
, a
*sizeof(unsigned char));
873 memcpy(ret
->pencil
, state
->pencil
, a
*sizeof(int));
874 memcpy(ret
->sequence
, state
->sequence
, w
*sizeof(digit
));
876 ret
->completed
= state
->completed
;
877 ret
->cheated
= state
->cheated
;
882 static void free_game(game_state
*state
)
885 sfree(state
->immutable
);
886 sfree(state
->pencil
);
887 sfree(state
->sequence
);
891 static char *solve_game(game_state
*state
, game_state
*currstate
,
892 char *aux
, char **error
)
894 int w
= state
->par
.w
, a
= w
*w
;
902 soln
= snewn(a
, digit
);
903 memcpy(soln
, state
->grid
, a
*sizeof(digit
));
905 ret
= solver(&state
->par
, soln
, DIFFCOUNT
-1);
907 if (ret
== diff_impossible
) {
908 *error
= "No solution exists for this puzzle";
910 } else if (ret
== diff_ambiguous
) {
911 *error
= "Multiple solutions exist for this puzzle";
914 out
= snewn(a
+2, char);
916 for (i
= 0; i
< a
; i
++)
917 out
[i
+1] = TOCHAR(soln
[i
], state
->par
.id
);
925 static int game_can_format_as_text_now(game_params
*params
)
930 static char *game_text_format(game_state
*state
)
932 int w
= state
->par
.w
;
936 ret
= snewn(2*w
*w
+1, char); /* leave room for terminating NUL */
939 for (y
= 0; y
< w
; y
++) {
940 for (x
= 0; x
< w
; x
++) {
941 digit d
= state
->grid
[y
*w
+x
];
946 ch
= TOCHAR(d
, state
->par
.id
);
958 assert(p
- ret
== 2*w
*w
);
965 * These are the coordinates of the currently highlighted
966 * square on the grid, if hshow = 1.
970 * This indicates whether the current highlight is a
971 * pencil-mark one or a real one.
975 * This indicates whether or not we're showing the highlight
976 * (used to be hx = hy = -1); important so that when we're
977 * using the cursor keys it doesn't keep coming back at a
978 * fixed position. When hshow = 1, pressing a valid number
979 * or letter key or Space will enter that number or letter in the grid.
983 * This indicates whether we're using the highlight as a cursor;
984 * it means that it doesn't vanish on a keypress, and that it is
985 * allowed on immutable squares.
989 * This indicates whether we're dragging a table header to
990 * reposition an entire row or column.
992 int drag
; /* 0=none 1=row 2=col */
993 int dragnum
; /* element being dragged */
994 int dragpos
; /* its current position */
997 static game_ui
*new_ui(game_state
*state
)
999 game_ui
*ui
= snew(game_ui
);
1001 ui
->hx
= ui
->hy
= 0;
1002 ui
->hpencil
= ui
->hshow
= ui
->hcursor
= 0;
1008 static void free_ui(game_ui
*ui
)
1013 static char *encode_ui(game_ui
*ui
)
1018 static void decode_ui(game_ui
*ui
, char *encoding
)
1022 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1023 game_state
*newstate
)
1025 int w
= newstate
->par
.w
;
1027 * We prevent pencil-mode highlighting of a filled square, unless
1028 * we're using the cursor keys. So if the user has just filled in
1029 * a square which we had a pencil-mode highlight in (by Undo, or
1030 * by Redo, or by Solve), then we cancel the highlight.
1032 if (ui
->hshow
&& ui
->hpencil
&& !ui
->hcursor
&&
1033 newstate
->grid
[ui
->hy
* w
+ ui
->hx
] != 0) {
1038 #define PREFERRED_TILESIZE 48
1039 #define TILESIZE (ds->tilesize)
1040 #define BORDER (TILESIZE / 2)
1041 #define LEGEND (TILESIZE)
1042 #define GRIDEXTRA max((TILESIZE / 32),1)
1043 #define COORD(x) ((x)*TILESIZE + BORDER + LEGEND)
1044 #define FROMCOORD(x) (((x)+(TILESIZE-BORDER-LEGEND)) / TILESIZE - 1)
1046 #define FLASH_TIME 0.4F
1048 #define DF_HIGHLIGHT 0x0400
1049 #define DF_HIGHLIGHT_PENCIL 0x0200
1050 #define DF_IMMUTABLE 0x0100
1051 #define DF_LEGEND 0x0080
1052 #define DF_DIGIT_MASK 0x001F
1054 #define EF_DIGIT_SHIFT 5
1055 #define EF_DIGIT_MASK ((1 << EF_DIGIT_SHIFT) - 1)
1056 #define EF_LEFT_SHIFT 0
1057 #define EF_RIGHT_SHIFT (3*EF_DIGIT_SHIFT)
1058 #define EF_LEFT_MASK ((1UL << (3*EF_DIGIT_SHIFT)) - 1UL)
1059 #define EF_RIGHT_MASK (EF_LEFT_MASK << EF_RIGHT_SHIFT)
1060 #define EF_LATIN (1UL << (6*EF_DIGIT_SHIFT))
1062 struct game_drawstate
{
1066 long *tiles
, *legend
, *pencil
, *errors
;
1071 static int check_errors(game_state
*state
, long *errors
)
1073 int w
= state
->par
.w
, a
= w
*w
;
1074 digit
*grid
= state
->grid
;
1075 int i
, j
, k
, x
, y
, errs
= FALSE
;
1078 * To verify that we have a valid group table, it suffices to
1079 * test latin-square-hood and associativity only. All the other
1080 * group axioms follow from those two.
1084 * Associativity is given; closure is obvious from latin-
1085 * square-hood. We need to show that an identity exists and that
1086 * every element has an inverse.
1088 * Identity: take any element a. There will be some element e
1089 * such that ea=a (in a latin square, every element occurs in
1090 * every row and column, so a must occur somewhere in the a
1091 * column, say on row e). For any other element b, there must
1092 * exist x such that ax=b (same argument from latin-square-hood
1093 * again), and then associativity gives us eb = e(ax) = (ea)x =
1094 * ax = b. Hence eb=b for all b, i.e. e is a left-identity. A
1095 * similar argument tells us that there must be some f which is
1096 * a right-identity, and then we show they are the same element
1097 * by observing that ef must simultaneously equal e and equal f.
1099 * Inverses: given any a, by the latin-square argument again,
1100 * there must exist p and q such that pa=e and aq=e (i.e. left-
1101 * and right-inverses). We can show these are equal by
1102 * associativity: p = pe = p(aq) = (pa)q = eq = q. []
1106 for (i
= 0; i
< a
; i
++)
1109 for (y
= 0; y
< w
; y
++) {
1110 unsigned long mask
= 0, errmask
= 0;
1111 for (x
= 0; x
< w
; x
++) {
1112 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1113 errmask
|= (mask
& bit
);
1117 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1121 for (x
= 0; x
< w
; x
++)
1122 if (errmask
& (1UL << grid
[y
*w
+x
]))
1123 errors
[y
*w
+x
] |= EF_LATIN
;
1128 for (x
= 0; x
< w
; x
++) {
1129 unsigned long mask
= 0, errmask
= 0;
1130 for (y
= 0; y
< w
; y
++) {
1131 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1132 errmask
|= (mask
& bit
);
1136 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1140 for (y
= 0; y
< w
; y
++)
1141 if (errmask
& (1UL << grid
[y
*w
+x
]))
1142 errors
[y
*w
+x
] |= EF_LATIN
;
1147 for (i
= 1; i
< w
; i
++)
1148 for (j
= 1; j
< w
; j
++)
1149 for (k
= 1; k
< w
; k
++)
1150 if (grid
[i
*w
+j
] && grid
[j
*w
+k
] &&
1151 grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
1152 grid
[i
*w
+(grid
[j
*w
+k
]-1)] &&
1153 grid
[(grid
[i
*w
+j
]-1)*w
+k
] != grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
1155 int a
= i
+1, b
= j
+1, c
= k
+1;
1156 int ab
= grid
[i
*w
+j
], bc
= grid
[j
*w
+k
];
1157 int left
= (ab
-1)*w
+(c
-1), right
= (a
-1)*w
+(bc
-1);
1159 * If the appropriate error slot is already
1160 * used for one of the squares, we don't
1161 * fill either of them.
1163 if (!(errors
[left
] & EF_LEFT_MASK
) &&
1164 !(errors
[right
] & EF_RIGHT_MASK
)) {
1167 err
= (err
<< EF_DIGIT_SHIFT
) | b
;
1168 err
= (err
<< EF_DIGIT_SHIFT
) | c
;
1169 errors
[left
] |= err
<< EF_LEFT_SHIFT
;
1170 errors
[right
] |= err
<< EF_RIGHT_SHIFT
;
1179 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1180 int x
, int y
, int button
)
1182 int w
= state
->par
.w
;
1186 button
&= ~MOD_MASK
;
1192 if (IS_MOUSE_DRAG(button
)) {
1193 int tcoord
= (ui
->drag
== 1 ? ty
: tx
);
1194 if (tcoord
>= 0 && tcoord
< w
) {
1195 ui
->dragpos
= tcoord
;
1198 } else if (IS_MOUSE_RELEASE(button
)) {
1199 ui
->drag
= 0; /* end drag */
1200 if (state
->sequence
[ui
->dragpos
] == ui
->dragnum
)
1201 return ""; /* drag was a no-op overall */
1202 sprintf(buf
, "D%d,%d", ui
->dragnum
, ui
->dragpos
);
1205 } else if (IS_MOUSE_DOWN(button
)) {
1206 if (tx
>= 0 && tx
< w
&& ty
>= 0 && ty
< w
) {
1207 tx
= state
->sequence
[tx
];
1208 ty
= state
->sequence
[ty
];
1209 if (button
== LEFT_BUTTON
) {
1210 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1211 ui
->hshow
&& ui
->hpencil
== 0) {
1216 ui
->hshow
= !state
->immutable
[ty
*w
+tx
];
1220 return ""; /* UI activity occurred */
1222 if (button
== RIGHT_BUTTON
) {
1224 * Pencil-mode highlighting for non filled squares.
1226 if (state
->grid
[ty
*w
+tx
] == 0) {
1227 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1228 ui
->hshow
&& ui
->hpencil
) {
1240 return ""; /* UI activity occurred */
1242 } else if (tx
>= 0 && tx
< w
&& ty
== -1) {
1244 ui
->dragnum
= state
->sequence
[tx
];
1247 } else if (ty
>= 0 && ty
< w
&& tx
== -1) {
1249 ui
->dragnum
= state
->sequence
[ty
];
1255 if (IS_CURSOR_MOVE(button
)) {
1256 move_cursor(button
, &ui
->hx
, &ui
->hy
, w
, w
, 0);
1257 ui
->hshow
= ui
->hcursor
= 1;
1261 (button
== CURSOR_SELECT
)) {
1262 ui
->hpencil
= 1 - ui
->hpencil
;
1268 ((ISCHAR(button
) && FROMCHAR(button
, state
->par
.id
) <= w
) ||
1269 button
== CURSOR_SELECT2
|| button
== '\b')) {
1270 int n
= FROMCHAR(button
, state
->par
.id
);
1271 if (button
== CURSOR_SELECT2
|| button
== '\b')
1275 * Can't make pencil marks in a filled square. This can only
1276 * become highlighted if we're using cursor keys.
1278 if (ui
->hpencil
&& state
->grid
[ui
->hy
*w
+ui
->hx
])
1282 * Can't do anything to an immutable square.
1284 if (state
->immutable
[ui
->hy
*w
+ui
->hx
])
1287 sprintf(buf
, "%c%d,%d,%d",
1288 (char)(ui
->hpencil
&& n
> 0 ?
'P' : 'R'), ui
->hx
, ui
->hy
, n
);
1290 if (!ui
->hcursor
) ui
->hshow
= 0;
1295 if (button
== 'M' || button
== 'm')
1301 static game_state
*execute_move(game_state
*from
, char *move
)
1303 int w
= from
->par
.w
, a
= w
*w
;
1307 if (move
[0] == 'S') {
1308 ret
= dup_game(from
);
1309 ret
->completed
= ret
->cheated
= TRUE
;
1311 for (i
= 0; i
< a
; i
++) {
1312 if (!ISCHAR(move
[i
+1]) || FROMCHAR(move
[i
+1], from
->par
.id
) > w
) {
1316 ret
->grid
[i
] = FROMCHAR(move
[i
+1], from
->par
.id
);
1320 if (move
[a
+1] != '\0') {
1326 } else if ((move
[0] == 'P' || move
[0] == 'R') &&
1327 sscanf(move
+1, "%d,%d,%d", &x
, &y
, &n
) == 3 &&
1328 x
>= 0 && x
< w
&& y
>= 0 && y
< w
&& n
>= 0 && n
<= w
) {
1329 if (from
->immutable
[y
*w
+x
])
1332 ret
= dup_game(from
);
1333 if (move
[0] == 'P' && n
> 0) {
1334 ret
->pencil
[y
*w
+x
] ^= 1 << n
;
1336 ret
->grid
[y
*w
+x
] = n
;
1337 ret
->pencil
[y
*w
+x
] = 0;
1339 if (!ret
->completed
&& !check_errors(ret
, NULL
))
1340 ret
->completed
= TRUE
;
1343 } else if (move
[0] == 'M') {
1345 * Fill in absolutely all pencil marks everywhere. (I
1346 * wouldn't use this for actual play, but it's a handy
1347 * starting point when following through a set of
1348 * diagnostics output by the standalone solver.)
1350 ret
= dup_game(from
);
1351 for (i
= 0; i
< a
; i
++) {
1353 ret
->pencil
[i
] = (1 << (w
+1)) - (1 << 1);
1356 } else if (move
[0] == 'D' &&
1357 sscanf(move
+1, "%d,%d", &x
, &y
) == 2) {
1359 * Reorder the rows and columns so that digit x is in position
1362 ret
= dup_game(from
);
1363 for (i
= j
= 0; i
< w
; i
++) {
1365 ret
->sequence
[i
] = x
;
1367 if (from
->sequence
[j
] == x
)
1369 ret
->sequence
[i
] = from
->sequence
[j
++];
1374 return NULL
; /* couldn't parse move string */
1377 /* ----------------------------------------------------------------------
1381 #define SIZE(w) ((w) * TILESIZE + 2*BORDER + LEGEND)
1383 static void game_compute_size(game_params
*params
, int tilesize
,
1386 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1387 struct { int tilesize
; } ads
, *ds
= &ads
;
1388 ads
.tilesize
= tilesize
;
1390 *x
= *y
= SIZE(params
->w
);
1393 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1394 game_params
*params
, int tilesize
)
1396 ds
->tilesize
= tilesize
;
1399 static float *game_colours(frontend
*fe
, int *ncolours
)
1401 float *ret
= snewn(3 * NCOLOURS
, float);
1403 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1405 ret
[COL_GRID
* 3 + 0] = 0.0F
;
1406 ret
[COL_GRID
* 3 + 1] = 0.0F
;
1407 ret
[COL_GRID
* 3 + 2] = 0.0F
;
1409 ret
[COL_USER
* 3 + 0] = 0.0F
;
1410 ret
[COL_USER
* 3 + 1] = 0.6F
* ret
[COL_BACKGROUND
* 3 + 1];
1411 ret
[COL_USER
* 3 + 2] = 0.0F
;
1413 ret
[COL_HIGHLIGHT
* 3 + 0] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 0];
1414 ret
[COL_HIGHLIGHT
* 3 + 1] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 1];
1415 ret
[COL_HIGHLIGHT
* 3 + 2] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 2];
1417 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
1418 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
1419 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
1421 ret
[COL_PENCIL
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1422 ret
[COL_PENCIL
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1423 ret
[COL_PENCIL
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
1425 *ncolours
= NCOLOURS
;
1429 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1431 int w
= state
->par
.w
, a
= w
*w
;
1432 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1436 ds
->par
= state
->par
; /* structure copy */
1438 ds
->started
= FALSE
;
1439 ds
->tiles
= snewn(a
, long);
1440 ds
->legend
= snewn(w
, long);
1441 ds
->pencil
= snewn(a
, long);
1442 ds
->errors
= snewn(a
, long);
1443 ds
->sequence
= snewn(a
, digit
);
1444 for (i
= 0; i
< a
; i
++)
1445 ds
->tiles
[i
] = ds
->pencil
[i
] = -1;
1446 for (i
= 0; i
< w
; i
++)
1448 ds
->errtmp
= snewn(a
, long);
1453 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1459 sfree(ds
->sequence
);
1463 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, int x
, int y
, long tile
,
1464 long pencil
, long error
)
1466 int w
= ds
->w
/* , a = w*w */;
1471 tx
= BORDER
+ LEGEND
+ x
* TILESIZE
+ 1;
1472 ty
= BORDER
+ LEGEND
+ y
* TILESIZE
+ 1;
1476 cw
= tw
= TILESIZE
-1;
1477 ch
= th
= TILESIZE
-1;
1479 if (tile
& DF_LEGEND
) {
1484 tile
|= DF_IMMUTABLE
;
1487 clip(dr
, cx
, cy
, cw
, ch
);
1489 /* background needs erasing */
1490 draw_rect(dr
, cx
, cy
, cw
, ch
,
1491 (tile
& DF_HIGHLIGHT
) ? COL_HIGHLIGHT
: COL_BACKGROUND
);
1493 /* pencil-mode highlight */
1494 if (tile
& DF_HIGHLIGHT_PENCIL
) {
1498 coords
[2] = cx
+cw
/2;
1501 coords
[5] = cy
+ch
/2;
1502 draw_polygon(dr
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
1505 /* new number needs drawing? */
1506 if (tile
& DF_DIGIT_MASK
) {
1508 str
[0] = TOCHAR(tile
& DF_DIGIT_MASK
, ds
->par
.id
);
1509 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/2,
1510 FONT_VARIABLE
, TILESIZE
/2, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1511 (error
& EF_LATIN
) ? COL_ERROR
:
1512 (tile
& DF_IMMUTABLE
) ? COL_GRID
: COL_USER
, str
);
1514 if (error
& EF_LEFT_MASK
) {
1515 int a
= (error
>> (EF_LEFT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1516 int b
= (error
>> (EF_LEFT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1517 int c
= (error
>> (EF_LEFT_SHIFT
))&EF_DIGIT_MASK
;
1519 sprintf(buf
, "(%c%c)%c", TOCHAR(a
, ds
->par
.id
),
1520 TOCHAR(b
, ds
->par
.id
), TOCHAR(c
, ds
->par
.id
));
1521 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/6,
1522 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1525 if (error
& EF_RIGHT_MASK
) {
1526 int a
= (error
>> (EF_RIGHT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1527 int b
= (error
>> (EF_RIGHT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1528 int c
= (error
>> (EF_RIGHT_SHIFT
))&EF_DIGIT_MASK
;
1530 sprintf(buf
, "%c(%c%c)", TOCHAR(a
, ds
->par
.id
),
1531 TOCHAR(b
, ds
->par
.id
), TOCHAR(c
, ds
->par
.id
));
1532 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
- TILESIZE
/6,
1533 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1540 int pw
, ph
, minph
, pbest
, fontsize
;
1542 /* Count the pencil marks required. */
1543 for (i
= 1, npencil
= 0; i
<= w
; i
++)
1544 if (pencil
& (1 << i
))
1551 * Determine the bounding rectangle within which we're going
1552 * to put the pencil marks.
1554 /* Start with the whole square */
1555 pl
= tx
+ GRIDEXTRA
;
1556 pr
= pl
+ TILESIZE
- GRIDEXTRA
;
1557 pt
= ty
+ GRIDEXTRA
;
1558 pb
= pt
+ TILESIZE
- GRIDEXTRA
;
1561 * We arrange our pencil marks in a grid layout, with
1562 * the number of rows and columns adjusted to allow the
1563 * maximum font size.
1565 * So now we work out what the grid size ought to be.
1570 for (pw
= 3; pw
< max(npencil
,4); pw
++) {
1573 ph
= (npencil
+ pw
- 1) / pw
;
1574 ph
= max(ph
, minph
);
1575 fw
= (pr
- pl
) / (float)pw
;
1576 fh
= (pb
- pt
) / (float)ph
;
1578 if (fs
> bestsize
) {
1585 ph
= (npencil
+ pw
- 1) / pw
;
1586 ph
= max(ph
, minph
);
1589 * Now we've got our grid dimensions, work out the pixel
1590 * size of a grid element, and round it to the nearest
1591 * pixel. (We don't want rounding errors to make the
1592 * grid look uneven at low pixel sizes.)
1594 fontsize
= min((pr
- pl
) / pw
, (pb
- pt
) / ph
);
1597 * Centre the resulting figure in the square.
1599 pl
= tx
+ (TILESIZE
- fontsize
* pw
) / 2;
1600 pt
= ty
+ (TILESIZE
- fontsize
* ph
) / 2;
1603 * Now actually draw the pencil marks.
1605 for (i
= 1, j
= 0; i
<= w
; i
++)
1606 if (pencil
& (1 << i
)) {
1607 int dx
= j
% pw
, dy
= j
/ pw
;
1610 str
[0] = TOCHAR(i
, ds
->par
.id
);
1611 draw_text(dr
, pl
+ fontsize
* (2*dx
+1) / 2,
1612 pt
+ fontsize
* (2*dy
+1) / 2,
1613 FONT_VARIABLE
, fontsize
,
1614 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_PENCIL
, str
);
1622 draw_update(dr
, cx
, cy
, cw
, ch
);
1625 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1626 game_state
*state
, int dir
, game_ui
*ui
,
1627 float animtime
, float flashtime
)
1629 int w
= state
->par
.w
/*, a = w*w */;
1634 * The initial contents of the window are not guaranteed and
1635 * can vary with front ends. To be on the safe side, all
1636 * games should start by drawing a big background-colour
1637 * rectangle covering the whole window.
1639 draw_rect(dr
, 0, 0, SIZE(w
), SIZE(w
), COL_BACKGROUND
);
1642 * Big containing rectangle.
1644 draw_rect(dr
, COORD(0) - GRIDEXTRA
, COORD(0) - GRIDEXTRA
,
1645 w
*TILESIZE
+1+GRIDEXTRA
*2, w
*TILESIZE
+1+GRIDEXTRA
*2,
1648 draw_update(dr
, 0, 0, SIZE(w
), SIZE(w
));
1653 check_errors(state
, ds
->errtmp
);
1656 * Construct a modified version of state->sequence which takes
1657 * into account an unfinished drag operation.
1665 for (i
= j
= 0; i
< w
; i
++) {
1667 ds
->sequence
[i
] = x
;
1669 if (state
->sequence
[j
] == x
)
1671 ds
->sequence
[i
] = state
->sequence
[j
++];
1676 * Draw the table legend.
1678 for (x
= 0; x
< w
; x
++) {
1679 int sx
= ds
->sequence
[x
];
1680 long tile
= (sx
+1) | DF_LEGEND
;
1681 if (ds
->legend
[x
] != tile
) {
1682 ds
->legend
[x
] = tile
;
1683 draw_tile(dr
, ds
, -1, x
, tile
, 0, 0);
1684 draw_tile(dr
, ds
, x
, -1, tile
, 0, 0);
1688 for (y
= 0; y
< w
; y
++) {
1689 int sy
= ds
->sequence
[y
];
1690 for (x
= 0; x
< w
; x
++) {
1691 long tile
= 0L, pencil
= 0L, error
;
1692 int sx
= ds
->sequence
[x
];
1694 if (state
->grid
[sy
*w
+sx
])
1695 tile
= state
->grid
[sy
*w
+sx
];
1697 pencil
= (long)state
->pencil
[sy
*w
+sx
];
1699 if (state
->immutable
[sy
*w
+sx
])
1700 tile
|= DF_IMMUTABLE
;
1702 if ((ui
->drag
== 1 && ui
->dragnum
== sy
) ||
1703 (ui
->drag
== 2 && ui
->dragnum
== sx
))
1704 tile
|= DF_HIGHLIGHT
;
1705 else if (ui
->hshow
&& ui
->hx
== sx
&& ui
->hy
== sy
)
1706 tile
|= (ui
->hpencil ? DF_HIGHLIGHT_PENCIL
: DF_HIGHLIGHT
);
1708 if (flashtime
> 0 &&
1709 (flashtime
<= FLASH_TIME
/3 ||
1710 flashtime
>= FLASH_TIME
*2/3))
1711 tile
|= DF_HIGHLIGHT
; /* completion flash */
1713 error
= ds
->errtmp
[sy
*w
+sx
];
1715 if (ds
->tiles
[y
*w
+x
] != tile
||
1716 ds
->pencil
[y
*w
+x
] != pencil
||
1717 ds
->errors
[y
*w
+x
] != error
) {
1718 ds
->tiles
[y
*w
+x
] = tile
;
1719 ds
->pencil
[y
*w
+x
] = pencil
;
1720 ds
->errors
[y
*w
+x
] = error
;
1721 draw_tile(dr
, ds
, x
, y
, tile
, pencil
, error
);
1727 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1728 int dir
, game_ui
*ui
)
1733 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1734 int dir
, game_ui
*ui
)
1736 if (!oldstate
->completed
&& newstate
->completed
&&
1737 !oldstate
->cheated
&& !newstate
->cheated
)
1742 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1744 if (state
->completed
)
1749 static void game_print_size(game_params
*params
, float *x
, float *y
)
1754 * We use 9mm squares by default, like Solo.
1756 game_compute_size(params
, 900, &pw
, &ph
);
1761 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1763 int w
= state
->par
.w
;
1764 int ink
= print_mono_colour(dr
, 0);
1767 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1768 game_drawstate ads
, *ds
= &ads
;
1769 game_set_size(dr
, ds
, NULL
, tilesize
);
1774 print_line_width(dr
, 3 * TILESIZE
/ 40);
1775 draw_rect_outline(dr
, BORDER
+ LEGEND
, BORDER
+ LEGEND
,
1776 w
*TILESIZE
, w
*TILESIZE
, ink
);
1781 for (x
= 0; x
< w
; x
++) {
1784 str
[0] = TOCHAR(x
+1, state
->par
.id
);
1785 draw_text(dr
, BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1786 BORDER
+ TILESIZE
/2,
1787 FONT_VARIABLE
, TILESIZE
/2,
1788 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1789 draw_text(dr
, BORDER
+ TILESIZE
/2,
1790 BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1791 FONT_VARIABLE
, TILESIZE
/2,
1792 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1798 for (x
= 1; x
< w
; x
++) {
1799 print_line_width(dr
, TILESIZE
/ 40);
1800 draw_line(dr
, BORDER
+LEGEND
+x
*TILESIZE
, BORDER
+LEGEND
,
1801 BORDER
+LEGEND
+x
*TILESIZE
, BORDER
+LEGEND
+w
*TILESIZE
, ink
);
1803 for (y
= 1; y
< w
; y
++) {
1804 print_line_width(dr
, TILESIZE
/ 40);
1805 draw_line(dr
, BORDER
+LEGEND
, BORDER
+LEGEND
+y
*TILESIZE
,
1806 BORDER
+LEGEND
+w
*TILESIZE
, BORDER
+LEGEND
+y
*TILESIZE
, ink
);
1812 for (y
= 0; y
< w
; y
++)
1813 for (x
= 0; x
< w
; x
++)
1814 if (state
->grid
[y
*w
+x
]) {
1817 str
[0] = TOCHAR(state
->grid
[y
*w
+x
], state
->par
.id
);
1818 draw_text(dr
, BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1819 BORDER
+LEGEND
+ y
*TILESIZE
+ TILESIZE
/2,
1820 FONT_VARIABLE
, TILESIZE
/2,
1821 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1826 #define thegame group
1829 const struct game thegame
= {
1830 "Group", NULL
, NULL
,
1837 TRUE
, game_configure
, custom_params
,
1845 TRUE
, game_can_format_as_text_now
, game_text_format
,
1853 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
1856 game_free_drawstate
,
1860 TRUE
, FALSE
, game_print_size
, game_print
,
1861 FALSE
, /* wants_statusbar */
1862 FALSE
, game_timing_state
,
1863 REQUIRE_RBUTTON
| REQUIRE_NUMPAD
, /* flags */
1866 #ifdef STANDALONE_SOLVER
1870 int main(int argc
, char **argv
)
1874 char *id
= NULL
, *desc
, *err
;
1877 int ret
, diff
, really_show_working
= FALSE
;
1879 while (--argc
> 0) {
1881 if (!strcmp(p
, "-v")) {
1882 really_show_working
= TRUE
;
1883 } else if (!strcmp(p
, "-g")) {
1885 } else if (*p
== '-') {
1886 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
1894 fprintf(stderr
, "usage: %s [-g | -v] <game_id>\n", argv
[0]);
1898 desc
= strchr(id
, ':');
1900 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
1905 p
= default_params();
1906 decode_params(p
, id
);
1907 err
= validate_desc(p
, desc
);
1909 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
1912 s
= new_game(NULL
, p
, desc
);
1914 grid
= snewn(p
->w
* p
->w
, digit
);
1917 * When solving a Normal puzzle, we don't want to bother the
1918 * user with Hard-level deductions. For this reason, we grade
1919 * the puzzle internally before doing anything else.
1921 ret
= -1; /* placate optimiser */
1922 solver_show_working
= FALSE
;
1923 for (diff
= 0; diff
< DIFFCOUNT
; diff
++) {
1924 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1925 ret
= solver(&s
->par
, grid
, diff
);
1930 if (diff
== DIFFCOUNT
) {
1932 printf("Difficulty rating: ambiguous\n");
1934 printf("Unable to find a unique solution\n");
1937 if (ret
== diff_impossible
)
1938 printf("Difficulty rating: impossible (no solution exists)\n");
1940 printf("Difficulty rating: %s\n", group_diffnames
[ret
]);
1942 solver_show_working
= really_show_working
;
1943 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1944 ret
= solver(&s
->par
, grid
, diff
);
1946 printf("Puzzle is inconsistent\n");
1948 memcpy(s
->grid
, grid
, p
->w
* p
->w
);
1949 fputs(game_text_format(s
), stdout
);
1959 /* vim: set shiftwidth=4 tabstop=8: */