group.c 70 KB

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