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
;
91 static game_params
*default_params(void)
93 game_params
*ret
= snew(game_params
);
96 ret
->diff
= DIFF_NORMAL
;
102 const static struct game_params group_presets
[] = {
103 { 6, DIFF_NORMAL
, TRUE
},
104 { 6, DIFF_NORMAL
, FALSE
},
105 { 8, DIFF_NORMAL
, TRUE
},
106 { 8, DIFF_NORMAL
, FALSE
},
107 { 8, DIFF_HARD
, TRUE
},
108 { 8, DIFF_HARD
, FALSE
},
109 { 12, DIFF_NORMAL
, TRUE
},
112 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
117 if (i
< 0 || i
>= lenof(group_presets
))
120 ret
= snew(game_params
);
121 *ret
= group_presets
[i
]; /* structure copy */
123 sprintf(buf
, "%dx%d %s%s", ret
->w
, ret
->w
, group_diffnames
[ret
->diff
],
124 ret
->id ?
"" : ", identity hidden");
131 static void free_params(game_params
*params
)
136 static game_params
*dup_params(game_params
*params
)
138 game_params
*ret
= snew(game_params
);
139 *ret
= *params
; /* structure copy */
143 static void decode_params(game_params
*params
, char const *string
)
145 char const *p
= string
;
148 while (*p
&& isdigit((unsigned char)*p
)) p
++;
149 params
->diff
= DIFF_NORMAL
;
156 params
->diff
= DIFFCOUNT
+1; /* ...which is invalid */
158 for (i
= 0; i
< DIFFCOUNT
; i
++) {
159 if (*p
== group_diffchars
[i
])
164 } else if (*p
== 'i') {
168 /* unrecognised character */
174 static char *encode_params(game_params
*params
, int full
)
178 sprintf(ret
, "%d", params
->w
);
180 sprintf(ret
+ strlen(ret
), "d%c", group_diffchars
[params
->diff
]);
182 sprintf(ret
+ strlen(ret
), "i");
187 static config_item
*game_configure(game_params
*params
)
192 ret
= snewn(4, config_item
);
194 ret
[0].name
= "Grid size";
195 ret
[0].type
= C_STRING
;
196 sprintf(buf
, "%d", params
->w
);
197 ret
[0].sval
= dupstr(buf
);
200 ret
[1].name
= "Difficulty";
201 ret
[1].type
= C_CHOICES
;
202 ret
[1].sval
= DIFFCONFIG
;
203 ret
[1].ival
= params
->diff
;
205 ret
[2].name
= "Show identity";
206 ret
[2].type
= C_BOOLEAN
;
208 ret
[2].ival
= params
->id
;
218 static game_params
*custom_params(config_item
*cfg
)
220 game_params
*ret
= snew(game_params
);
222 ret
->w
= atoi(cfg
[0].sval
);
223 ret
->diff
= cfg
[1].ival
;
224 ret
->id
= cfg
[2].ival
;
229 static char *validate_params(game_params
*params
, int full
)
231 if (params
->w
< 3 || params
->w
> 26)
232 return "Grid size must be between 3 and 26";
233 if (params
->diff
>= DIFFCOUNT
)
234 return "Unknown difficulty rating";
235 if (!params
->id
&& params
->diff
== DIFF_TRIVIAL
) {
237 * We can't have a Trivial-difficulty puzzle (i.e. latin
238 * square deductions only) without a clear identity, because
239 * identityless puzzles always have two rows and two columns
240 * entirely blank, and no latin-square deduction permits the
241 * distinguishing of two such rows.
243 return "Trivial puzzles must have an identity";
248 /* ----------------------------------------------------------------------
252 static int solver_normal(struct latin_solver
*solver
, void *vctx
)
255 #ifdef STANDALONE_SOLVER
256 char **names
= solver
->names
;
258 digit
*grid
= solver
->grid
;
262 * Deduce using associativity: (ab)c = a(bc).
264 * So we pick any a,b,c we like; then if we know ab, bc, and
265 * (ab)c we can fill in a(bc).
267 for (i
= 1; i
< w
; i
++)
268 for (j
= 1; j
< w
; j
++)
269 for (k
= 1; k
< w
; k
++) {
270 if (!grid
[i
*w
+j
] || !grid
[j
*w
+k
])
272 if (grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
273 !grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
274 int x
= grid
[j
*w
+k
]-1, y
= i
;
275 int n
= grid
[(grid
[i
*w
+j
]-1)*w
+k
];
276 #ifdef STANDALONE_SOLVER
277 if (solver_show_working
) {
278 printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n",
279 solver_recurse_depth
*4, "",
280 names
[i
], names
[j
], names
[k
],
281 names
[grid
[i
*w
+j
]-1], names
[k
],
282 names
[i
], names
[grid
[j
*w
+k
]-1]);
283 printf("%*s placing %s at (%d,%d)\n",
284 solver_recurse_depth
*4, "",
285 names
[n
-1], x
+1, y
+1);
288 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
289 latin_solver_place(solver
, x
, y
, n
);
292 #ifdef STANDALONE_SOLVER
293 if (solver_show_working
)
294 printf("%*s contradiction!\n",
295 solver_recurse_depth
*4, "");
300 if (!grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
301 grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
302 int x
= k
, y
= grid
[i
*w
+j
]-1;
303 int n
= grid
[i
*w
+(grid
[j
*w
+k
]-1)];
304 #ifdef STANDALONE_SOLVER
305 if (solver_show_working
) {
306 printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n",
307 solver_recurse_depth
*4, "",
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",
312 solver_recurse_depth
*4, "",
313 names
[n
-1], x
+1, y
+1);
316 if (solver
->cube
[(x
*w
+y
)*w
+n
-1]) {
317 latin_solver_place(solver
, x
, y
, n
);
320 #ifdef STANDALONE_SOLVER
321 if (solver_show_working
)
322 printf("%*s contradiction!\n",
323 solver_recurse_depth
*4, "");
333 #define SOLVER(upper,title,func,lower) func,
334 static usersolver_t
const group_solvers
[] = { DIFFLIST(SOLVER
) };
336 static int solver(game_params
*params
, digit
*grid
, int maxdiff
)
340 struct latin_solver solver
;
341 #ifdef STANDALONE_SOLVER
342 char *p
, text
[100], *names
[50];
346 latin_solver_alloc(&solver
, grid
, w
);
347 #ifdef STANDALONE_SOLVER
348 for (i
= 0, p
= text
; i
< w
; i
++) {
350 *p
++ = TOCHAR(i
+1, params
->id
);
353 solver
.names
= names
;
356 ret
= latin_solver_main(&solver
, maxdiff
,
357 DIFF_TRIVIAL
, DIFF_HARD
, DIFF_EXTREME
,
358 DIFF_EXTREME
, DIFF_UNREASONABLE
,
359 group_solvers
, NULL
, NULL
, NULL
);
361 latin_solver_free(&solver
);
366 /* ----------------------------------------------------------------------
370 static char *encode_grid(char *desc
, digit
*grid
, int area
)
376 for (i
= 0; i
<= area
; i
++) {
377 int n
= (i
< area ? grid
[i
] : -1);
384 int c
= 'a' - 1 + run
;
388 run
-= c
- ('a' - 1);
392 * If there's a number in the very top left or
393 * bottom right, there's no point putting an
394 * unnecessary _ before or after it.
396 if (p
> desc
&& n
> 0)
400 p
+= sprintf(p
, "%d", n
);
407 /* ----- data generated by group.gap begins ----- */
410 unsigned long autosize
;
416 const struct group
*groups
;
419 static const struct group groupdata
[] = {
426 {6L, 4, 2, "BADC" "CDAB"},
430 {6L, 6, 2, "CFEBAD" "BADCFE"},
431 {2L, 6, 1, "DCFEBA"},
433 {6L, 7, 1, "BCDEFGA"},
435 {4L, 8, 1, "BCEFDGHA"},
436 {8L, 8, 2, "BDEFGAHC" "EGBHDCFA"},
437 {8L, 8, 2, "EGBHDCFA" "BAEFCDHG"},
438 {24L, 8, 2, "BDEFGAHC" "CHDGBEAF"},
439 {168L, 8, 3, "BAEFCDHG" "CEAGBHDF" "DFGAHBCE"},
441 {6L, 9, 1, "BDECGHFIA"},
442 {48L, 9, 2, "BDEAGHCIF" "CEFGHAIBD"},
444 {20L, 10, 2, "CJEBGDIFAH" "BADCFEHGJI"},
445 {4L, 10, 1, "DCFEHGJIBA"},
447 {10L, 11, 1, "BCDEFGHIJKA"},
449 {12L, 12, 2, "GLDKJEHCBIAF" "BCEFAGIJDKLH"},
450 {4L, 12, 1, "EHIJKCBLDGFA"},
451 {24L, 12, 2, "BEFGAIJKCDLH" "FJBKHLEGDCIA"},
452 {12L, 12, 2, "GLDKJEHCBIAF" "BAEFCDIJGHLK"},
453 {12L, 12, 2, "FDIJGHLBKAEC" "GIDKFLHCJEAB"},
455 {12L, 13, 1, "BCDEFGHIJKLMA"},
457 {42L, 14, 2, "ELGNIBKDMFAHCJ" "BADCFEHGJILKNM"},
458 {6L, 14, 1, "FEHGJILKNMBADC"},
460 {8L, 15, 1, "EGHCJKFMNIOBLDA"},
462 {8L, 16, 1, "MKNPFOADBGLCIEHJ"},
463 {96L, 16, 2, "ILKCONFPEDJHGMAB" "BDFGHIAKLMNCOEPJ"},
464 {32L, 16, 2, "MIHPFDCONBLAKJGE" "BEFGHJKALMNOCDPI"},
465 {32L, 16, 2, "IFACOGLMDEJBNPKH" "BEFGHJKALMNOCDPI"},
466 {16L, 16, 2, "MOHPFKCINBLADJGE" "BDFGHIEKLMNJOAPC"},
467 {16L, 16, 2, "MIHPFDJONBLEKCGA" "BDFGHIEKLMNJOAPC"},
468 {32L, 16, 2, "MOHPFDCINBLEKJGA" "BAFGHCDELMNIJKPO"},
469 {16L, 16, 2, "MIHPFKJONBLADCGE" "GDPHNOEKFLBCIAMJ"},
470 {32L, 16, 2, "MIBPFDJOGHLEKCNA" "CLEIJGMPKAOHNFDB"},
472 "MCHPFAIJNBLDEOGK" "BEFGHJKALMNOCDPI" "GKLBNOEDFPHJIAMC"},
473 {64L, 16, 3, "MCHPFAIJNBLDEOGK" "LOGFPKJIBNMEDCHA" "CMAIJHPFDEONBLKG"},
475 "IPKCOGMLEDJBNFAH" "BEFGHJKALMNOCDPI" "CMEIJBPFKAOGHLDN"},
476 {48L, 16, 3, "IPDJONFLEKCBGMAH" "FJBLMEOCGHPKAIND" "DGIEKLHNJOAMPBCF"},
478 "EHJKAMNBOCDPFGIL" "BAFGHCDELMNIJKPO" "CFAIJBLMDEOGHPKN"
481 {16L, 17, 1, "EFGHIJKLMNOPQABCD"},
483 {54L, 18, 2, "MKIQOPNAGLRECDBJHF" "BAEFCDJKLGHIOPMNRQ"},
484 {6L, 18, 1, "ECJKGHFOPDMNLRIQBA"},
485 {12L, 18, 2, "ECJKGHBOPAMNFRDQLI" "KNOPQCFREIGHLJAMBD"},
487 "IFNAKLQCDOPBGHREMJ" "NOQCFRIGHKLJAMPBDE" "BAEFCDJKLGHIOPMNRQ"},
488 {48L, 18, 2, "ECJKGHBOPAMNFRDQLI" "FDKLHIOPBMNAREQCJG"},
490 {18L, 19, 1, "EFGHIJKLMNOPQRSABCD"},
492 {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "EABICDFMGHJQKLNTOPRS"},
493 {8L, 20, 1, "EHIJLCMNPGQRSKBTDOFA"},
494 {20L, 20, 2, "DJSHQNCLTRGPEBKAIFOM" "EABICDFMGHJQKLNTOPRS"},
495 {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "ECBIAGFMDKJQHONTLSRP"},
496 {24L, 20, 2, "IGFMDKJQHONTLSREPCBA" "FDIJGHMNKLQROPTBSAEC"},
498 {42L, 21, 2, "ITLSBOUERDHAGKCJNFMQP" "EJHLMKOPNRSQAUTCDBFGI"},
499 {12L, 21, 1, "EGHCJKFMNIPQLSTOUBRDA"},
501 {110L, 22, 2, "ETGVIBKDMFOHQJSLUNAPCR" "BADCFEHGJILKNMPORQTSVU"},
502 {10L, 22, 1, "FEHGJILKNMPORQTSVUBADC"},
504 {22L, 23, 1, "EFGHIJKLMNOPQRSTUVWABCD"},
506 {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "HRNOPSWCTUVBLDIJXFGAKQME"},
507 {8L, 24, 1, "MQBTUDRWFGHXJELINOPKSAVC"},
508 {24L, 24, 2, "IOQRBEUVFWGHKLAXMNPSCDTJ" "NJXOVGDKSMTFIPQELCURBWAH"},
509 {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "HSNOPWLDTUVBRIAKXFGCQEMJ"},
510 {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "TWHNXLRIOPUMSACQVBFDEJGK"},
511 {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "BAFGHCDEMNOPIJKLTUVQRSXW"},
513 "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO"
514 "HSNOPWLDTUVBRIAKXFGCQEMJ"},
516 "QUKJWPXFESRIVBMNLDCGHTAO" "JXEQRVUMKLWCPGFTSAIBONDH"
517 "TRONXLWCHVUMSAIJPGFDEQBK"},
518 {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "VKXHOQASNTPBCWDEUFGIJLMR"},
519 {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "RMLWIGTUSDJQOPFXEKCBVNAH"},
520 {48L, 24, 2, "IULQRGXMSDCWOPNTEKJBVFAH" "GLMOPRSDTUBVWIEKFXHJQANC"},
521 {24L, 24, 2, "UJPXMRCSNHGTLWIKFVBEDQOA" "NRUFVLWIPXMOJEDQHGTCSABK"},
522 {24L, 24, 2, "MIBTUAQRFGHXCDEWNOPJKLVS" "OKXVFWSCGUTNDRQJBPMALIHE"},
524 "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO"
525 "BAFGHCDEMNOPIJKLTUVQRSXW"},
527 "QTKJWONXESRIHVUMLDCPGFAB" "JNEQRHTUKLWCOPXFSAIVBMDG"
528 "HENOPJKLTUVBQRSAXFGWCDMI"},
530 {20L, 25, 1, "EHILMNPQRSFTUVBJWXDOYGAKC"},
531 {480L, 25, 2, "EHILMNPQRSCTUVBFWXDJYGOKA" "BDEGHIKLMNAPQRSCTUVFWXJYO"},
534 "EXGZIBKDMFOHQJSLUNWPYRATCV" "BADCFEHGJILKNMPORQTSVUXWZY"},
535 {12L, 26, 1, "FEHGJILKNMPORQTSVUXWZYBADC"},
538 static const struct groups groups
[] = {
539 {0, NULL
}, /* trivial case: 0 */
540 {0, NULL
}, /* trivial case: 1 */
541 {1, groupdata
+ 0}, /* 2 */
542 {1, groupdata
+ 1}, /* 3 */
543 {2, groupdata
+ 2}, /* 4 */
544 {1, groupdata
+ 4}, /* 5 */
545 {2, groupdata
+ 5}, /* 6 */
546 {1, groupdata
+ 7}, /* 7 */
547 {5, groupdata
+ 8}, /* 8 */
548 {2, groupdata
+ 13}, /* 9 */
549 {2, groupdata
+ 15}, /* 10 */
550 {1, groupdata
+ 17}, /* 11 */
551 {5, groupdata
+ 18}, /* 12 */
552 {1, groupdata
+ 23}, /* 13 */
553 {2, groupdata
+ 24}, /* 14 */
554 {1, groupdata
+ 26}, /* 15 */
555 {14, groupdata
+ 27}, /* 16 */
556 {1, groupdata
+ 41}, /* 17 */
557 {5, groupdata
+ 42}, /* 18 */
558 {1, groupdata
+ 47}, /* 19 */
559 {5, groupdata
+ 48}, /* 20 */
560 {2, groupdata
+ 53}, /* 21 */
561 {2, groupdata
+ 55}, /* 22 */
562 {1, groupdata
+ 57}, /* 23 */
563 {15, groupdata
+ 58}, /* 24 */
564 {2, groupdata
+ 73}, /* 25 */
565 {2, groupdata
+ 75}, /* 26 */
568 /* ----- data generated by group.gap ends ----- */
570 static char *new_game_desc(game_params
*params
, random_state
*rs
,
571 char **aux
, int interactive
)
573 int w
= params
->w
, a
= w
*w
;
574 digit
*grid
, *soln
, *soln2
;
577 int diff
= params
->diff
;
578 const struct group
*group
;
582 * Difficulty exceptions: some combinations of size and
583 * difficulty cannot be satisfied, because all puzzles of at
584 * most that difficulty are actually even easier.
586 * Remember to re-test this whenever a change is made to the
589 * I tested it using the following shell command:
591 for d in t n h x u; do
594 echo -n "./group --generate 1 ${i}d${d}${id}: "
595 perl -e 'alarm 30; exec @ARGV' \
596 ./group --generate 1 ${i}d${d}${id} >/dev/null && echo ok
601 * Of course, it's better to do that after taking the exceptions
602 * _out_, so as to detect exceptions that should be removed as
603 * well as those which should be added.
606 if (w
< 5 && diff
== DIFF_UNREASONABLE
)
608 if ((w
< 5 || ((w
== 6 || w
== 8) && params
->id
)) && diff
== DIFF_EXTREME
)
610 if ((w
< 6 || (w
== 6 && params
->id
)) && diff
== DIFF_HARD
)
612 if ((w
< 4 || (w
== 4 && params
->id
)) && diff
== DIFF_NORMAL
)
616 grid
= snewn(a
, digit
);
617 soln
= snewn(a
, digit
);
618 soln2
= snewn(a
, digit
);
619 indices
= snewn(a
, int);
623 * Construct a valid group table, by picking a group from
624 * the above data table, decompressing it into a full
625 * representation by BFS, and then randomly permuting its
626 * non-identity elements.
628 * We build the canonical table in 'soln' (and use 'grid' as
629 * our BFS queue), then transfer the table into 'grid'
630 * having shuffled the rows.
633 assert(w
< lenof(groups
));
634 group
= groups
[w
].groups
+ random_upto(rs
, groups
[w
].ngroups
);
635 assert(group
->order
== w
);
637 for (i
= 0; i
< w
; i
++)
645 row
= soln
+ (i
-1)*w
;
647 for (j
= 0; j
< group
->ngens
; j
++) {
649 const char *gen
= group
->gens
+ j
*w
;
652 * Apply each group generator to row, constructing a
655 nri
= gen
[row
[0]-1] - 'A' + 1; /* which row is it? */
656 newrow
= soln
+ (nri
-1)*w
;
657 if (!newrow
[0]) { /* not done yet */
658 for (k
= 0; k
< w
; k
++)
659 newrow
[k
] = gen
[row
[k
]-1] - 'A' + 1;
664 /* That's got the canonical table. Now shuffle it. */
665 for (i
= 0; i
< w
; i
++)
667 if (params
->id
) /* do we shuffle in the identity? */
668 shuffle(soln2
+1, w
-1, sizeof(*soln2
), rs
);
670 shuffle(soln2
, w
, sizeof(*soln2
), rs
);
671 for (i
= 0; i
< w
; i
++)
672 for (j
= 0; j
< w
; j
++)
673 grid
[(soln2
[i
])*w
+(soln2
[j
])] = soln2
[soln
[i
*w
+j
]-1]+1;
676 * Remove entries one by one while the puzzle is still
677 * soluble at the appropriate difficulty level.
679 memcpy(soln
, grid
, a
);
682 * Start by blanking the entire identity row and column,
683 * and also another row and column so that the player
684 * can't trivially determine which element is the
688 j
= 1 + random_upto(rs
, w
-1); /* pick a second row/col to blank */
689 for (i
= 0; i
< w
; i
++) {
690 grid
[(soln2
[0])*w
+i
] = grid
[i
*w
+(soln2
[0])] = 0;
691 grid
[(soln2
[j
])*w
+i
] = grid
[i
*w
+(soln2
[j
])] = 0;
694 memcpy(soln2
, grid
, a
);
695 if (solver(params
, soln2
, diff
) > diff
)
696 continue; /* go round again if that didn't work */
700 for (i
= (params
->id ?
1 : 0); i
< w
; i
++)
701 for (j
= (params
->id ?
1 : 0); j
< w
; j
++)
703 indices
[k
++] = i
*w
+j
;
704 shuffle(indices
, k
, sizeof(*indices
), rs
);
706 for (i
= 0; i
< k
; i
++) {
707 memcpy(soln2
, grid
, a
);
708 soln2
[indices
[i
]] = 0;
709 if (solver(params
, soln2
, diff
) <= diff
)
710 grid
[indices
[i
]] = 0;
714 * Make sure the puzzle isn't too easy.
717 memcpy(soln2
, grid
, a
);
718 if (solver(params
, soln2
, diff
-1) < diff
)
719 continue; /* go round and try again */
729 * Encode the puzzle description.
731 desc
= snewn(a
*20, char);
732 p
= encode_grid(desc
, grid
, a
);
734 desc
= sresize(desc
, p
- desc
, char);
737 * Encode the solution.
739 *aux
= snewn(a
+2, char);
741 for (i
= 0; i
< a
; i
++)
742 (*aux
)[i
+1] = TOCHAR(soln
[i
], params
->id
);
753 /* ----------------------------------------------------------------------
757 static char *validate_grid_desc(const char **pdesc
, int range
, int area
)
759 const char *desc
= *pdesc
;
761 while (*desc
&& *desc
!= ',') {
763 if (n
>= 'a' && n
<= 'z') {
764 squares
+= n
- 'a' + 1;
765 } else if (n
== '_') {
767 } else if (n
> '0' && n
<= '9') {
768 int val
= atoi(desc
-1);
769 if (val
< 1 || val
> range
)
770 return "Out-of-range number in game description";
772 while (*desc
>= '0' && *desc
<= '9')
775 return "Invalid character in game description";
779 return "Not enough data to fill grid";
782 return "Too much data to fit in grid";
787 static char *validate_desc(game_params
*params
, char *desc
)
789 int w
= params
->w
, a
= w
*w
;
790 const char *p
= desc
;
792 return validate_grid_desc(&p
, w
, a
);
795 static char *spec_to_grid(char *desc
, digit
*grid
, int area
)
798 while (*desc
&& *desc
!= ',') {
800 if (n
>= 'a' && n
<= 'z') {
801 int run
= n
- 'a' + 1;
802 assert(i
+ run
<= area
);
805 } else if (n
== '_') {
807 } else if (n
> '0' && n
<= '9') {
809 grid
[i
++] = atoi(desc
-1);
810 while (*desc
>= '0' && *desc
<= '9')
813 assert(!"We can't get here");
820 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
822 int w
= params
->w
, a
= w
*w
;
823 game_state
*state
= snew(game_state
);
826 state
->par
= *params
; /* structure copy */
827 state
->grid
= snewn(a
, digit
);
828 state
->immutable
= snewn(a
, unsigned char);
829 state
->pencil
= snewn(a
, int);
830 for (i
= 0; i
< a
; i
++) {
832 state
->immutable
[i
] = 0;
833 state
->pencil
[i
] = 0;
836 desc
= spec_to_grid(desc
, state
->grid
, a
);
837 for (i
= 0; i
< a
; i
++)
838 if (state
->grid
[i
] != 0)
839 state
->immutable
[i
] = TRUE
;
841 state
->completed
= state
->cheated
= FALSE
;
846 static game_state
*dup_game(game_state
*state
)
848 int w
= state
->par
.w
, a
= w
*w
;
849 game_state
*ret
= snew(game_state
);
851 ret
->par
= state
->par
; /* structure copy */
853 ret
->grid
= snewn(a
, digit
);
854 ret
->immutable
= snewn(a
, unsigned char);
855 ret
->pencil
= snewn(a
, int);
856 memcpy(ret
->grid
, state
->grid
, a
*sizeof(digit
));
857 memcpy(ret
->immutable
, state
->immutable
, a
*sizeof(unsigned char));
858 memcpy(ret
->pencil
, state
->pencil
, a
*sizeof(int));
860 ret
->completed
= state
->completed
;
861 ret
->cheated
= state
->cheated
;
866 static void free_game(game_state
*state
)
869 sfree(state
->immutable
);
870 sfree(state
->pencil
);
874 static char *solve_game(game_state
*state
, game_state
*currstate
,
875 char *aux
, char **error
)
877 int w
= state
->par
.w
, a
= w
*w
;
885 soln
= snewn(a
, digit
);
886 memcpy(soln
, state
->grid
, a
*sizeof(digit
));
888 ret
= solver(&state
->par
, soln
, DIFFCOUNT
-1);
890 if (ret
== diff_impossible
) {
891 *error
= "No solution exists for this puzzle";
893 } else if (ret
== diff_ambiguous
) {
894 *error
= "Multiple solutions exist for this puzzle";
897 out
= snewn(a
+2, char);
899 for (i
= 0; i
< a
; i
++)
900 out
[i
+1] = TOCHAR(soln
[i
], state
->par
.id
);
908 static int game_can_format_as_text_now(game_params
*params
)
913 static char *game_text_format(game_state
*state
)
915 int w
= state
->par
.w
;
919 ret
= snewn(2*w
*w
+1, char); /* leave room for terminating NUL */
922 for (y
= 0; y
< w
; y
++) {
923 for (x
= 0; x
< w
; x
++) {
924 digit d
= state
->grid
[y
*w
+x
];
929 ch
= TOCHAR(d
, state
->par
.id
);
941 assert(p
- ret
== 2*w
*w
);
948 * These are the coordinates of the currently highlighted
949 * square on the grid, if hshow = 1.
953 * This indicates whether the current highlight is a
954 * pencil-mark one or a real one.
958 * This indicates whether or not we're showing the highlight
959 * (used to be hx = hy = -1); important so that when we're
960 * using the cursor keys it doesn't keep coming back at a
961 * fixed position. When hshow = 1, pressing a valid number
962 * or letter key or Space will enter that number or letter in the grid.
966 * This indicates whether we're using the highlight as a cursor;
967 * it means that it doesn't vanish on a keypress, and that it is
968 * allowed on immutable squares.
973 static game_ui
*new_ui(game_state
*state
)
975 game_ui
*ui
= snew(game_ui
);
978 ui
->hpencil
= ui
->hshow
= ui
->hcursor
= 0;
983 static void free_ui(game_ui
*ui
)
988 static char *encode_ui(game_ui
*ui
)
993 static void decode_ui(game_ui
*ui
, char *encoding
)
997 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
998 game_state
*newstate
)
1000 int w
= newstate
->par
.w
;
1002 * We prevent pencil-mode highlighting of a filled square, unless
1003 * we're using the cursor keys. So if the user has just filled in
1004 * a square which we had a pencil-mode highlight in (by Undo, or
1005 * by Redo, or by Solve), then we cancel the highlight.
1007 if (ui
->hshow
&& ui
->hpencil
&& !ui
->hcursor
&&
1008 newstate
->grid
[ui
->hy
* w
+ ui
->hx
] != 0) {
1013 #define PREFERRED_TILESIZE 48
1014 #define TILESIZE (ds->tilesize)
1015 #define BORDER (TILESIZE / 2)
1016 #define LEGEND (TILESIZE)
1017 #define GRIDEXTRA max((TILESIZE / 32),1)
1018 #define COORD(x) ((x)*TILESIZE + BORDER + LEGEND)
1019 #define FROMCOORD(x) (((x)+(TILESIZE-BORDER-LEGEND)) / TILESIZE - 1)
1021 #define FLASH_TIME 0.4F
1023 #define DF_HIGHLIGHT 0x0400
1024 #define DF_HIGHLIGHT_PENCIL 0x0200
1025 #define DF_IMMUTABLE 0x0100
1026 #define DF_DIGIT_MASK 0x001F
1028 #define EF_DIGIT_SHIFT 5
1029 #define EF_DIGIT_MASK ((1 << EF_DIGIT_SHIFT) - 1)
1030 #define EF_LEFT_SHIFT 0
1031 #define EF_RIGHT_SHIFT (3*EF_DIGIT_SHIFT)
1032 #define EF_LEFT_MASK ((1UL << (3*EF_DIGIT_SHIFT)) - 1UL)
1033 #define EF_RIGHT_MASK (EF_LEFT_MASK << EF_RIGHT_SHIFT)
1034 #define EF_LATIN (1UL << (6*EF_DIGIT_SHIFT))
1036 struct game_drawstate
{
1040 long *tiles
, *pencil
, *errors
;
1044 static int check_errors(game_state
*state
, long *errors
)
1046 int w
= state
->par
.w
, a
= w
*w
;
1047 digit
*grid
= state
->grid
;
1048 int i
, j
, k
, x
, y
, errs
= FALSE
;
1051 * To verify that we have a valid group table, it suffices to
1052 * test latin-square-hood and associativity only. All the other
1053 * group axioms follow from those two.
1057 * Associativity is given; closure is obvious from latin-
1058 * square-hood. We need to show that an identity exists and that
1059 * every element has an inverse.
1061 * Identity: take any element a. There will be some element e
1062 * such that ea=a (in a latin square, every element occurs in
1063 * every row and column, so a must occur somewhere in the a
1064 * column, say on row e). For any other element b, there must
1065 * exist x such that ax=b (same argument from latin-square-hood
1066 * again), and then associativity gives us eb = e(ax) = (ea)x =
1067 * ax = b. Hence eb=b for all b, i.e. e is a left-identity. A
1068 * similar argument tells us that there must be some f which is
1069 * a right-identity, and then we show they are the same element
1070 * by observing that ef must simultaneously equal e and equal f.
1072 * Inverses: given any a, by the latin-square argument again,
1073 * there must exist p and q such that pa=e and aq=e (i.e. left-
1074 * and right-inverses). We can show these are equal by
1075 * associativity: p = pe = p(aq) = (pa)q = eq = q. []
1079 for (i
= 0; i
< a
; i
++)
1082 for (y
= 0; y
< w
; y
++) {
1083 unsigned long mask
= 0, errmask
= 0;
1084 for (x
= 0; x
< w
; x
++) {
1085 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1086 errmask
|= (mask
& bit
);
1090 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1094 for (x
= 0; x
< w
; x
++)
1095 if (errmask
& (1UL << grid
[y
*w
+x
]))
1096 errors
[y
*w
+x
] |= EF_LATIN
;
1101 for (x
= 0; x
< w
; x
++) {
1102 unsigned long mask
= 0, errmask
= 0;
1103 for (y
= 0; y
< w
; y
++) {
1104 unsigned long bit
= 1UL << grid
[y
*w
+x
];
1105 errmask
|= (mask
& bit
);
1109 if (mask
!= (1 << (w
+1)) - (1 << 1)) {
1113 for (y
= 0; y
< w
; y
++)
1114 if (errmask
& (1UL << grid
[y
*w
+x
]))
1115 errors
[y
*w
+x
] |= EF_LATIN
;
1120 for (i
= 1; i
< w
; i
++)
1121 for (j
= 1; j
< w
; j
++)
1122 for (k
= 1; k
< w
; k
++)
1123 if (grid
[i
*w
+j
] && grid
[j
*w
+k
] &&
1124 grid
[(grid
[i
*w
+j
]-1)*w
+k
] &&
1125 grid
[i
*w
+(grid
[j
*w
+k
]-1)] &&
1126 grid
[(grid
[i
*w
+j
]-1)*w
+k
] != grid
[i
*w
+(grid
[j
*w
+k
]-1)]) {
1128 int a
= i
+1, b
= j
+1, c
= k
+1;
1129 int ab
= grid
[i
*w
+j
], bc
= grid
[j
*w
+k
];
1130 int left
= (ab
-1)*w
+(c
-1), right
= (a
-1)*w
+(bc
-1);
1132 * If the appropriate error slot is already
1133 * used for one of the squares, we don't
1134 * fill either of them.
1136 if (!(errors
[left
] & EF_LEFT_MASK
) &&
1137 !(errors
[right
] & EF_RIGHT_MASK
)) {
1140 err
= (err
<< EF_DIGIT_SHIFT
) | b
;
1141 err
= (err
<< EF_DIGIT_SHIFT
) | c
;
1142 errors
[left
] |= err
<< EF_LEFT_SHIFT
;
1143 errors
[right
] |= err
<< EF_RIGHT_SHIFT
;
1152 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
1153 int x
, int y
, int button
)
1155 int w
= state
->par
.w
;
1159 button
&= ~MOD_MASK
;
1164 if (tx
>= 0 && tx
< w
&& ty
>= 0 && ty
< w
) {
1165 if (button
== LEFT_BUTTON
) {
1166 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1167 ui
->hshow
&& ui
->hpencil
== 0) {
1172 ui
->hshow
= !state
->immutable
[ty
*w
+tx
];
1176 return ""; /* UI activity occurred */
1178 if (button
== RIGHT_BUTTON
) {
1180 * Pencil-mode highlighting for non filled squares.
1182 if (state
->grid
[ty
*w
+tx
] == 0) {
1183 if (tx
== ui
->hx
&& ty
== ui
->hy
&&
1184 ui
->hshow
&& ui
->hpencil
) {
1196 return ""; /* UI activity occurred */
1199 if (IS_CURSOR_MOVE(button
)) {
1200 move_cursor(button
, &ui
->hx
, &ui
->hy
, w
, w
, 0);
1201 ui
->hshow
= ui
->hcursor
= 1;
1205 (button
== CURSOR_SELECT
)) {
1206 ui
->hpencil
= 1 - ui
->hpencil
;
1212 ((ISCHAR(button
) && FROMCHAR(button
, state
->par
.id
) <= w
) ||
1213 button
== CURSOR_SELECT2
|| button
== '\b')) {
1214 int n
= FROMCHAR(button
, state
->par
.id
);
1215 if (button
== CURSOR_SELECT2
|| button
== '\b')
1219 * Can't make pencil marks in a filled square. This can only
1220 * become highlighted if we're using cursor keys.
1222 if (ui
->hpencil
&& state
->grid
[ui
->hy
*w
+ui
->hx
])
1226 * Can't do anything to an immutable square.
1228 if (state
->immutable
[ui
->hy
*w
+ui
->hx
])
1231 sprintf(buf
, "%c%d,%d,%d",
1232 (char)(ui
->hpencil
&& n
> 0 ?
'P' : 'R'), ui
->hx
, ui
->hy
, n
);
1234 if (!ui
->hcursor
) ui
->hshow
= 0;
1239 if (button
== 'M' || button
== 'm')
1245 static game_state
*execute_move(game_state
*from
, char *move
)
1247 int w
= from
->par
.w
, a
= w
*w
;
1251 if (move
[0] == 'S') {
1252 ret
= dup_game(from
);
1253 ret
->completed
= ret
->cheated
= TRUE
;
1255 for (i
= 0; i
< a
; i
++) {
1256 if (!ISCHAR(move
[i
+1]) || FROMCHAR(move
[i
+1], from
->par
.id
) > w
) {
1260 ret
->grid
[i
] = FROMCHAR(move
[i
+1], from
->par
.id
);
1264 if (move
[a
+1] != '\0') {
1270 } else if ((move
[0] == 'P' || move
[0] == 'R') &&
1271 sscanf(move
+1, "%d,%d,%d", &x
, &y
, &n
) == 3 &&
1272 x
>= 0 && x
< w
&& y
>= 0 && y
< w
&& n
>= 0 && n
<= w
) {
1273 if (from
->immutable
[y
*w
+x
])
1276 ret
= dup_game(from
);
1277 if (move
[0] == 'P' && n
> 0) {
1278 ret
->pencil
[y
*w
+x
] ^= 1 << n
;
1280 ret
->grid
[y
*w
+x
] = n
;
1281 ret
->pencil
[y
*w
+x
] = 0;
1283 if (!ret
->completed
&& !check_errors(ret
, NULL
))
1284 ret
->completed
= TRUE
;
1287 } else if (move
[0] == 'M') {
1289 * Fill in absolutely all pencil marks everywhere. (I
1290 * wouldn't use this for actual play, but it's a handy
1291 * starting point when following through a set of
1292 * diagnostics output by the standalone solver.)
1294 ret
= dup_game(from
);
1295 for (i
= 0; i
< a
; i
++) {
1297 ret
->pencil
[i
] = (1 << (w
+1)) - (1 << 1);
1301 return NULL
; /* couldn't parse move string */
1304 /* ----------------------------------------------------------------------
1308 #define SIZE(w) ((w) * TILESIZE + 2*BORDER + LEGEND)
1310 static void game_compute_size(game_params
*params
, int tilesize
,
1313 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1314 struct { int tilesize
; } ads
, *ds
= &ads
;
1315 ads
.tilesize
= tilesize
;
1317 *x
= *y
= SIZE(params
->w
);
1320 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1321 game_params
*params
, int tilesize
)
1323 ds
->tilesize
= tilesize
;
1326 static float *game_colours(frontend
*fe
, int *ncolours
)
1328 float *ret
= snewn(3 * NCOLOURS
, float);
1330 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1332 ret
[COL_GRID
* 3 + 0] = 0.0F
;
1333 ret
[COL_GRID
* 3 + 1] = 0.0F
;
1334 ret
[COL_GRID
* 3 + 2] = 0.0F
;
1336 ret
[COL_USER
* 3 + 0] = 0.0F
;
1337 ret
[COL_USER
* 3 + 1] = 0.6F
* ret
[COL_BACKGROUND
* 3 + 1];
1338 ret
[COL_USER
* 3 + 2] = 0.0F
;
1340 ret
[COL_HIGHLIGHT
* 3 + 0] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 0];
1341 ret
[COL_HIGHLIGHT
* 3 + 1] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 1];
1342 ret
[COL_HIGHLIGHT
* 3 + 2] = 0.78F
* ret
[COL_BACKGROUND
* 3 + 2];
1344 ret
[COL_ERROR
* 3 + 0] = 1.0F
;
1345 ret
[COL_ERROR
* 3 + 1] = 0.0F
;
1346 ret
[COL_ERROR
* 3 + 2] = 0.0F
;
1348 ret
[COL_PENCIL
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1349 ret
[COL_PENCIL
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1350 ret
[COL_PENCIL
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2];
1352 *ncolours
= NCOLOURS
;
1356 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1358 int w
= state
->par
.w
, a
= w
*w
;
1359 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1363 ds
->par
= state
->par
; /* structure copy */
1365 ds
->started
= FALSE
;
1366 ds
->tiles
= snewn(a
, long);
1367 ds
->pencil
= snewn(a
, long);
1368 ds
->errors
= snewn(a
, long);
1369 for (i
= 0; i
< a
; i
++)
1370 ds
->tiles
[i
] = ds
->pencil
[i
] = -1;
1371 ds
->errtmp
= snewn(a
, long);
1376 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1385 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, int x
, int y
, long tile
,
1386 long pencil
, long error
)
1388 int w
= ds
->w
/* , a = w*w */;
1393 tx
= BORDER
+ LEGEND
+ x
* TILESIZE
+ 1;
1394 ty
= BORDER
+ LEGEND
+ y
* TILESIZE
+ 1;
1398 cw
= tw
= TILESIZE
-1;
1399 ch
= th
= TILESIZE
-1;
1401 clip(dr
, cx
, cy
, cw
, ch
);
1403 /* background needs erasing */
1404 draw_rect(dr
, cx
, cy
, cw
, ch
,
1405 (tile
& DF_HIGHLIGHT
) ? COL_HIGHLIGHT
: COL_BACKGROUND
);
1407 /* pencil-mode highlight */
1408 if (tile
& DF_HIGHLIGHT_PENCIL
) {
1412 coords
[2] = cx
+cw
/2;
1415 coords
[5] = cy
+ch
/2;
1416 draw_polygon(dr
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
1419 /* new number needs drawing? */
1420 if (tile
& DF_DIGIT_MASK
) {
1422 str
[0] = TOCHAR(tile
& DF_DIGIT_MASK
, ds
->par
.id
);
1423 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/2,
1424 FONT_VARIABLE
, TILESIZE
/2, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1425 (error
& EF_LATIN
) ? COL_ERROR
:
1426 (tile
& DF_IMMUTABLE
) ? COL_GRID
: COL_USER
, str
);
1428 if (error
& EF_LEFT_MASK
) {
1429 int a
= (error
>> (EF_LEFT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1430 int b
= (error
>> (EF_LEFT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1431 int c
= (error
>> (EF_LEFT_SHIFT
))&EF_DIGIT_MASK
;
1433 sprintf(buf
, "(%c%c)%c", TOCHAR(a
, ds
->par
.id
),
1434 TOCHAR(b
, ds
->par
.id
), TOCHAR(c
, ds
->par
.id
));
1435 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/6,
1436 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1439 if (error
& EF_RIGHT_MASK
) {
1440 int a
= (error
>> (EF_RIGHT_SHIFT
+2*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1441 int b
= (error
>> (EF_RIGHT_SHIFT
+1*EF_DIGIT_SHIFT
))&EF_DIGIT_MASK
;
1442 int c
= (error
>> (EF_RIGHT_SHIFT
))&EF_DIGIT_MASK
;
1444 sprintf(buf
, "%c(%c%c)", TOCHAR(a
, ds
->par
.id
),
1445 TOCHAR(b
, ds
->par
.id
), TOCHAR(c
, ds
->par
.id
));
1446 draw_text(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
- TILESIZE
/6,
1447 FONT_VARIABLE
, TILESIZE
/6, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1454 int pw
, ph
, minph
, pbest
, fontsize
;
1456 /* Count the pencil marks required. */
1457 for (i
= 1, npencil
= 0; i
<= w
; i
++)
1458 if (pencil
& (1 << i
))
1465 * Determine the bounding rectangle within which we're going
1466 * to put the pencil marks.
1468 /* Start with the whole square */
1469 pl
= tx
+ GRIDEXTRA
;
1470 pr
= pl
+ TILESIZE
- GRIDEXTRA
;
1471 pt
= ty
+ GRIDEXTRA
;
1472 pb
= pt
+ TILESIZE
- GRIDEXTRA
;
1475 * We arrange our pencil marks in a grid layout, with
1476 * the number of rows and columns adjusted to allow the
1477 * maximum font size.
1479 * So now we work out what the grid size ought to be.
1484 for (pw
= 3; pw
< max(npencil
,4); pw
++) {
1487 ph
= (npencil
+ pw
- 1) / pw
;
1488 ph
= max(ph
, minph
);
1489 fw
= (pr
- pl
) / (float)pw
;
1490 fh
= (pb
- pt
) / (float)ph
;
1492 if (fs
> bestsize
) {
1499 ph
= (npencil
+ pw
- 1) / pw
;
1500 ph
= max(ph
, minph
);
1503 * Now we've got our grid dimensions, work out the pixel
1504 * size of a grid element, and round it to the nearest
1505 * pixel. (We don't want rounding errors to make the
1506 * grid look uneven at low pixel sizes.)
1508 fontsize
= min((pr
- pl
) / pw
, (pb
- pt
) / ph
);
1511 * Centre the resulting figure in the square.
1513 pl
= tx
+ (TILESIZE
- fontsize
* pw
) / 2;
1514 pt
= ty
+ (TILESIZE
- fontsize
* ph
) / 2;
1517 * Now actually draw the pencil marks.
1519 for (i
= 1, j
= 0; i
<= w
; i
++)
1520 if (pencil
& (1 << i
)) {
1521 int dx
= j
% pw
, dy
= j
/ pw
;
1524 str
[0] = TOCHAR(i
, ds
->par
.id
);
1525 draw_text(dr
, pl
+ fontsize
* (2*dx
+1) / 2,
1526 pt
+ fontsize
* (2*dy
+1) / 2,
1527 FONT_VARIABLE
, fontsize
,
1528 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_PENCIL
, str
);
1536 draw_update(dr
, cx
, cy
, cw
, ch
);
1539 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1540 game_state
*state
, int dir
, game_ui
*ui
,
1541 float animtime
, float flashtime
)
1543 int w
= state
->par
.w
/*, a = w*w */;
1548 * The initial contents of the window are not guaranteed and
1549 * can vary with front ends. To be on the safe side, all
1550 * games should start by drawing a big background-colour
1551 * rectangle covering the whole window.
1553 draw_rect(dr
, 0, 0, SIZE(w
), SIZE(w
), COL_BACKGROUND
);
1556 * Big containing rectangle.
1558 draw_rect(dr
, COORD(0) - GRIDEXTRA
, COORD(0) - GRIDEXTRA
,
1559 w
*TILESIZE
+1+GRIDEXTRA
*2, w
*TILESIZE
+1+GRIDEXTRA
*2,
1565 for (x
= 0; x
< w
; x
++) {
1568 str
[0] = TOCHAR(x
+1, ds
->par
.id
);
1569 draw_text(dr
, COORD(x
) + TILESIZE
/2, BORDER
+ TILESIZE
/2,
1570 FONT_VARIABLE
, TILESIZE
/2,
1571 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_GRID
, str
);
1572 draw_text(dr
, BORDER
+ TILESIZE
/2, COORD(x
) + TILESIZE
/2,
1573 FONT_VARIABLE
, TILESIZE
/2,
1574 ALIGN_VCENTRE
| ALIGN_HCENTRE
, COL_GRID
, str
);
1577 draw_update(dr
, 0, 0, SIZE(w
), SIZE(w
));
1582 check_errors(state
, ds
->errtmp
);
1584 for (y
= 0; y
< w
; y
++) {
1585 for (x
= 0; x
< w
; x
++) {
1586 long tile
= 0L, pencil
= 0L, error
;
1588 if (state
->grid
[y
*w
+x
])
1589 tile
= state
->grid
[y
*w
+x
];
1591 pencil
= (long)state
->pencil
[y
*w
+x
];
1593 if (state
->immutable
[y
*w
+x
])
1594 tile
|= DF_IMMUTABLE
;
1596 if (ui
->hshow
&& ui
->hx
== x
&& ui
->hy
== y
)
1597 tile
|= (ui
->hpencil ? DF_HIGHLIGHT_PENCIL
: DF_HIGHLIGHT
);
1599 if (flashtime
> 0 &&
1600 (flashtime
<= FLASH_TIME
/3 ||
1601 flashtime
>= FLASH_TIME
*2/3))
1602 tile
|= DF_HIGHLIGHT
; /* completion flash */
1604 error
= ds
->errtmp
[y
*w
+x
];
1606 if (ds
->tiles
[y
*w
+x
] != tile
||
1607 ds
->pencil
[y
*w
+x
] != pencil
||
1608 ds
->errors
[y
*w
+x
] != error
) {
1609 ds
->tiles
[y
*w
+x
] = tile
;
1610 ds
->pencil
[y
*w
+x
] = pencil
;
1611 ds
->errors
[y
*w
+x
] = error
;
1612 draw_tile(dr
, ds
, x
, y
, tile
, pencil
, error
);
1618 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1619 int dir
, game_ui
*ui
)
1624 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1625 int dir
, game_ui
*ui
)
1627 if (!oldstate
->completed
&& newstate
->completed
&&
1628 !oldstate
->cheated
&& !newstate
->cheated
)
1633 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1635 if (state
->completed
)
1640 static void game_print_size(game_params
*params
, float *x
, float *y
)
1645 * We use 9mm squares by default, like Solo.
1647 game_compute_size(params
, 900, &pw
, &ph
);
1652 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1654 int w
= state
->par
.w
;
1655 int ink
= print_mono_colour(dr
, 0);
1658 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1659 game_drawstate ads
, *ds
= &ads
;
1660 game_set_size(dr
, ds
, NULL
, tilesize
);
1665 print_line_width(dr
, 3 * TILESIZE
/ 40);
1666 draw_rect_outline(dr
, BORDER
+ LEGEND
, BORDER
+ LEGEND
,
1667 w
*TILESIZE
, w
*TILESIZE
, ink
);
1672 for (x
= 0; x
< w
; x
++) {
1675 str
[0] = TOCHAR(x
+1, state
->par
.id
);
1676 draw_text(dr
, BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1677 BORDER
+ TILESIZE
/2,
1678 FONT_VARIABLE
, TILESIZE
/2,
1679 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1680 draw_text(dr
, BORDER
+ TILESIZE
/2,
1681 BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1682 FONT_VARIABLE
, TILESIZE
/2,
1683 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1689 for (x
= 1; x
< w
; x
++) {
1690 print_line_width(dr
, TILESIZE
/ 40);
1691 draw_line(dr
, BORDER
+LEGEND
+x
*TILESIZE
, BORDER
+LEGEND
,
1692 BORDER
+LEGEND
+x
*TILESIZE
, BORDER
+LEGEND
+w
*TILESIZE
, ink
);
1694 for (y
= 1; y
< w
; y
++) {
1695 print_line_width(dr
, TILESIZE
/ 40);
1696 draw_line(dr
, BORDER
+LEGEND
, BORDER
+LEGEND
+y
*TILESIZE
,
1697 BORDER
+LEGEND
+w
*TILESIZE
, BORDER
+LEGEND
+y
*TILESIZE
, ink
);
1703 for (y
= 0; y
< w
; y
++)
1704 for (x
= 0; x
< w
; x
++)
1705 if (state
->grid
[y
*w
+x
]) {
1708 str
[0] = TOCHAR(state
->grid
[y
*w
+x
], state
->par
.id
);
1709 draw_text(dr
, BORDER
+LEGEND
+ x
*TILESIZE
+ TILESIZE
/2,
1710 BORDER
+LEGEND
+ y
*TILESIZE
+ TILESIZE
/2,
1711 FONT_VARIABLE
, TILESIZE
/2,
1712 ALIGN_VCENTRE
| ALIGN_HCENTRE
, ink
, str
);
1717 #define thegame group
1720 const struct game thegame
= {
1721 "Group", NULL
, NULL
,
1728 TRUE
, game_configure
, custom_params
,
1736 TRUE
, game_can_format_as_text_now
, game_text_format
,
1744 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
1747 game_free_drawstate
,
1751 TRUE
, FALSE
, game_print_size
, game_print
,
1752 FALSE
, /* wants_statusbar */
1753 FALSE
, game_timing_state
,
1754 REQUIRE_RBUTTON
| REQUIRE_NUMPAD
, /* flags */
1757 #ifdef STANDALONE_SOLVER
1761 int main(int argc
, char **argv
)
1765 char *id
= NULL
, *desc
, *err
;
1768 int ret
, diff
, really_show_working
= FALSE
;
1770 while (--argc
> 0) {
1772 if (!strcmp(p
, "-v")) {
1773 really_show_working
= TRUE
;
1774 } else if (!strcmp(p
, "-g")) {
1776 } else if (*p
== '-') {
1777 fprintf(stderr
, "%s: unrecognised option `%s'\n", argv
[0], p
);
1785 fprintf(stderr
, "usage: %s [-g | -v] <game_id>\n", argv
[0]);
1789 desc
= strchr(id
, ':');
1791 fprintf(stderr
, "%s: game id expects a colon in it\n", argv
[0]);
1796 p
= default_params();
1797 decode_params(p
, id
);
1798 err
= validate_desc(p
, desc
);
1800 fprintf(stderr
, "%s: %s\n", argv
[0], err
);
1803 s
= new_game(NULL
, p
, desc
);
1805 grid
= snewn(p
->w
* p
->w
, digit
);
1808 * When solving a Normal puzzle, we don't want to bother the
1809 * user with Hard-level deductions. For this reason, we grade
1810 * the puzzle internally before doing anything else.
1812 ret
= -1; /* placate optimiser */
1813 solver_show_working
= FALSE
;
1814 for (diff
= 0; diff
< DIFFCOUNT
; diff
++) {
1815 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1816 ret
= solver(&s
->par
, grid
, diff
);
1821 if (diff
== DIFFCOUNT
) {
1823 printf("Difficulty rating: ambiguous\n");
1825 printf("Unable to find a unique solution\n");
1828 if (ret
== diff_impossible
)
1829 printf("Difficulty rating: impossible (no solution exists)\n");
1831 printf("Difficulty rating: %s\n", group_diffnames
[ret
]);
1833 solver_show_working
= really_show_working
;
1834 memcpy(grid
, s
->grid
, p
->w
* p
->w
);
1835 ret
= solver(&s
->par
, grid
, diff
);
1837 printf("Puzzle is inconsistent\n");
1839 memcpy(s
->grid
, grid
, p
->w
* p
->w
);
1840 fputs(game_text_format(s
), stdout
);
1850 /* vim: set shiftwidth=4 tabstop=8: */