COL_HIGHLIGHT,
COL_ERROR,
COL_PENCIL,
+ COL_DIAGONAL,
NCOLOURS
};
-#define FROMCHAR(c) ((c)>='0'&&(c)<='9' ? (c)-'0' : \
- (c)>='A'&&(c)<='Z' ? (c)-'A'+10 : (c)-'a'+10)
-#define ISCHAR(c) (((c)>='0'&&(c)<='9') || \
- ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
-#define TOCHAR(c) ((c)>=10 ? (c)-10+'a' : (c)+'0')
+/*
+ * In identity mode, we number the elements e,a,b,c,d,f,g,h,...
+ * Otherwise, they're a,b,c,d,e,f,g,h,... in the obvious way.
+ */
+#define E_TO_FRONT(c,id) ( (id) && (c)<=5 ? (c) % 5 + 1 : (c) )
+#define E_FROM_FRONT(c,id) ( (id) && (c)<=5 ? ((c) + 3) % 5 + 1 : (c) )
+
+#define FROMCHAR(c,id) E_TO_FRONT((((c)-('A'-1)) & ~0x20), id)
+#define ISCHAR(c) (((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
+#define TOCHAR(c,id) (E_FROM_FRONT(c,id) + ('a'-1))
struct game_params {
int w, diff, id;
unsigned char *immutable;
int *pencil; /* bitmaps using bits 1<<1..1<<n */
int completed, cheated;
+ digit *sequence; /* sequence of group elements shown */
+
+ /*
+ * This array indicates thick lines separating rows and columns
+ * placed and unplaced manually by the user as a visual aid, e.g.
+ * to delineate a subgroup and its cosets.
+ *
+ * When a line is placed, it's deemed to be between the two
+ * particular group elements that are on either side of it at the
+ * time; dragging those two away from each other automatically
+ * gets rid of the line. Hence, for a given element i, dividers[i]
+ * is either -1 (indicating no divider to the right of i), or some
+ * other element (indicating a divider to the right of i iff that
+ * element is the one right of it). These are eagerly cleared
+ * during drags.
+ */
+ int *dividers; /* thick lines between rows/cols */
};
static game_params *default_params(void)
const static struct game_params group_presets[] = {
{ 6, DIFF_NORMAL, TRUE },
{ 6, DIFF_NORMAL, FALSE },
- { 6, DIFF_HARD, TRUE },
- { 6, DIFF_HARD, FALSE },
{ 8, DIFF_NORMAL, TRUE },
{ 8, DIFF_NORMAL, FALSE },
+ { 8, DIFF_HARD, TRUE },
+ { 8, DIFF_HARD, FALSE },
{ 12, DIFF_NORMAL, TRUE },
};
sfree(params);
}
-static game_params *dup_params(game_params *params)
+static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
}
} else if (*p == 'i') {
params->id = FALSE;
+ p++;
+ } else {
+ /* unrecognised character */
+ p++;
}
}
}
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
{
char ret[80];
return dupstr(ret);
}
-static config_item *game_configure(game_params *params)
+static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret[0].name = "Grid size";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
- ret[0].sval = dupstr(buf);
- ret[0].ival = 0;
+ ret[0].u.string.sval = dupstr(buf);
ret[1].name = "Difficulty";
ret[1].type = C_CHOICES;
- ret[1].sval = DIFFCONFIG;
- ret[1].ival = params->diff;
+ ret[1].u.choices.choicenames = DIFFCONFIG;
+ ret[1].u.choices.selected = params->diff;
ret[2].name = "Show identity";
ret[2].type = C_BOOLEAN;
- ret[2].sval = NULL;
- ret[2].ival = params->id;
+ ret[2].u.boolean.bval = params->id;
ret[3].name = NULL;
ret[3].type = C_END;
- ret[3].sval = NULL;
- ret[3].ival = 0;
return ret;
}
-static game_params *custom_params(config_item *cfg)
+static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
- ret->w = atoi(cfg[0].sval);
- ret->diff = cfg[1].ival;
- ret->id = cfg[2].ival;
+ ret->w = atoi(cfg[0].u.string.sval);
+ ret->diff = cfg[1].u.choices.selected;
+ ret->id = cfg[2].u.boolean.bval;
return ret;
}
-static char *validate_params(game_params *params, int full)
+static const char *validate_params(const game_params *params, int full)
{
- if (params->w < 3 || params->w > 31)
- return "Grid size must be between 3 and 31";
+ if (params->w < 3 || params->w > 26)
+ return "Grid size must be between 3 and 26";
if (params->diff >= DIFFCOUNT)
return "Unknown difficulty rating";
+ if (!params->id && params->diff == DIFF_TRIVIAL) {
+ /*
+ * We can't have a Trivial-difficulty puzzle (i.e. latin
+ * square deductions only) without a clear identity, because
+ * identityless puzzles always have two rows and two columns
+ * entirely blank, and no latin-square deduction permits the
+ * distinguishing of two such rows.
+ */
+ return "Trivial puzzles must have an identity";
+ }
+ if (!params->id && params->w == 3) {
+ /*
+ * We can't have a 3x3 puzzle without an identity either,
+ * because 3x3 puzzles can't ever be harder than Trivial
+ * (there are no 3x3 latin squares which aren't also valid
+ * group tables, so enabling group-based deductions doesn't
+ * rule out any possible solutions) and - as above - Trivial
+ * puzzles can't not have an identity.
+ */
+ return "3x3 puzzles must have an identity";
+ }
return NULL;
}
static int solver_normal(struct latin_solver *solver, void *vctx)
{
int w = solver->o;
+#ifdef STANDALONE_SOLVER
+ char **names = solver->names;
+#endif
digit *grid = solver->grid;
int i, j, k;
int n = grid[(grid[i*w+j]-1)*w+k];
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
- printf("%*sassociativity on %d,%d,%d: %d*%d = %d*%d\n",
+ printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n",
solver_recurse_depth*4, "",
- i+1, j+1, k+1,
- grid[i*w+j], k+1, i+1, grid[j*w+k]);
- printf("%*s placing %d at (%d,%d)\n",
+ names[i], names[j], names[k],
+ names[grid[i*w+j]-1], names[k],
+ names[i], names[grid[j*w+k]-1]);
+ printf("%*s placing %s at (%d,%d)\n",
solver_recurse_depth*4, "",
- n, x+1, y+1);
+ names[n-1], x+1, y+1);
}
#endif
if (solver->cube[(x*w+y)*w+n-1]) {
int n = grid[i*w+(grid[j*w+k]-1)];
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
- printf("%*sassociativity on %d,%d,%d: %d*%d = %d*%d\n",
+ printf("%*sassociativity on %s,%s,%s: %s*%s = %s*%s\n",
solver_recurse_depth*4, "",
- i+1, j+1, k+1,
- grid[i*w+j], k+1, i+1, grid[j*w+k]);
- printf("%*s placing %d at (%d,%d)\n",
+ names[i], names[j], names[k],
+ names[grid[i*w+j]-1], names[k],
+ names[i], names[grid[j*w+k]-1]);
+ printf("%*s placing %s at (%d,%d)\n",
solver_recurse_depth*4, "",
- n, x+1, y+1);
+ names[n-1], x+1, y+1);
}
#endif
if (solver->cube[(x*w+y)*w+n-1]) {
#define SOLVER(upper,title,func,lower) func,
static usersolver_t const group_solvers[] = { DIFFLIST(SOLVER) };
-static int solver(int w, digit *grid, int maxdiff)
+static int solver(const game_params *params, digit *grid, int maxdiff)
{
+ int w = params->w;
int ret;
-
- ret = latin_solver(grid, w, maxdiff,
- DIFF_TRIVIAL, DIFF_HARD, DIFF_EXTREME,
- DIFF_EXTREME, DIFF_UNREASONABLE,
- group_solvers, NULL, NULL, NULL);
+ struct latin_solver solver;
+#ifdef STANDALONE_SOLVER
+ char *p, text[100], *names[50];
+ int i;
+#endif
+
+ latin_solver_alloc(&solver, grid, w);
+#ifdef STANDALONE_SOLVER
+ for (i = 0, p = text; i < w; i++) {
+ names[i] = p;
+ *p++ = TOCHAR(i+1, params->id);
+ *p++ = '\0';
+ }
+ solver.names = names;
+#endif
+
+ ret = latin_solver_main(&solver, maxdiff,
+ DIFF_TRIVIAL, DIFF_HARD, DIFF_EXTREME,
+ DIFF_EXTREME, DIFF_UNREASONABLE,
+ group_solvers, NULL, NULL, NULL);
+
+ latin_solver_free(&solver);
return ret;
}
static const struct group groupdata[] = {
/* order 2 */
- {1L, 2, 1, "21"},
+ {1L, 2, 1, "BA"},
/* order 3 */
- {2L, 3, 1, "231"},
+ {2L, 3, 1, "BCA"},
/* order 4 */
- {2L, 4, 1, "2341"},
- {6L, 4, 2, "2143" "3412"},
+ {2L, 4, 1, "BCDA"},
+ {6L, 4, 2, "BADC" "CDAB"},
/* order 5 */
- {4L, 5, 1, "23451"},
+ {4L, 5, 1, "BCDEA"},
/* order 6 */
- {6L, 6, 2, "365214" "214365"},
- {2L, 6, 1, "436521"},
+ {6L, 6, 2, "CFEBAD" "BADCFE"},
+ {2L, 6, 1, "DCFEBA"},
/* order 7 */
- {6L, 7, 1, "2345671"},
+ {6L, 7, 1, "BCDEFGA"},
/* order 8 */
- {4L, 8, 1, "23564781"},
- {8L, 8, 2, "24567183" "57284361"},
- {8L, 8, 2, "57284361" "21563487"},
- {24L, 8, 2, "24567183" "38472516"},
- {168L, 8, 3, "21563487" "35172846" "46718235"},
+ {4L, 8, 1, "BCEFDGHA"},
+ {8L, 8, 2, "BDEFGAHC" "EGBHDCFA"},
+ {8L, 8, 2, "EGBHDCFA" "BAEFCDHG"},
+ {24L, 8, 2, "BDEFGAHC" "CHDGBEAF"},
+ {168L, 8, 3, "BAEFCDHG" "CEAGBHDF" "DFGAHBCE"},
/* order 9 */
- {6L, 9, 1, "245378691"},
- {48L, 9, 2, "245178396" "356781924"},
+ {6L, 9, 1, "BDECGHFIA"},
+ {48L, 9, 2, "BDEAGHCIF" "CEFGHAIBD"},
/* order 10 */
- {20L, 10, 2, "3A52749618" "21436587A9"},
- {4L, 10, 1, "436587A921"},
+ {20L, 10, 2, "CJEBGDIFAH" "BADCFEHGJI"},
+ {4L, 10, 1, "DCFEHGJIBA"},
/* order 11 */
- {10L, 11, 1, "23456789AB1"},
+ {10L, 11, 1, "BCDEFGHIJKA"},
/* order 12 */
- {12L, 12, 2, "7C4BA5832916" "2356179A4BC8"},
- {4L, 12, 1, "589AB32C4761"},
- {24L, 12, 2, "256719AB34C8" "6A2B8C574391"},
- {12L, 12, 2, "7C4BA5832916" "2156349A78CB"},
- {12L, 12, 2, "649A78C2B153" "794B6C83A512"},
+ {12L, 12, 2, "GLDKJEHCBIAF" "BCEFAGIJDKLH"},
+ {4L, 12, 1, "EHIJKCBLDGFA"},
+ {24L, 12, 2, "BEFGAIJKCDLH" "FJBKHLEGDCIA"},
+ {12L, 12, 2, "GLDKJEHCBIAF" "BAEFCDIJGHLK"},
+ {12L, 12, 2, "FDIJGHLBKAEC" "GIDKFLHCJEAB"},
/* order 13 */
- {12L, 13, 1, "23456789ABCD1"},
+ {12L, 13, 1, "BCDEFGHIJKLMA"},
/* order 14 */
- {42L, 14, 2, "5C7E92B4D6183A" "21436587A9CBED"},
- {6L, 14, 1, "6587A9CBED2143"},
+ {42L, 14, 2, "ELGNIBKDMFAHCJ" "BADCFEHGJILKNM"},
+ {6L, 14, 1, "FEHGJILKNMBADC"},
/* order 15 */
- {8L, 15, 1, "5783AB6DE9F2C41"},
+ {8L, 15, 1, "EGHCJKFMNIOBLDA"},
/* order 16 */
- {8L, 16, 1, "DBEG6F1427C3958A"},
- {96L, 16, 2, "9CB3FE6G54A87D12" "2467891BCDE3F5GA"},
- {32L, 16, 2, "D98G643FE2C1BA75" "25678AB1CDEF34G9"},
- {32L, 16, 2, "9613F7CD45A2EGB8" "25678AB1CDEF34G9"},
- {16L, 16, 2, "DF8G6B39E2C14A75" "2467895BCDEAF1G3"},
- {16L, 16, 2, "D98G64AFE2C5B371" "2467895BCDEAF1G3"},
- {32L, 16, 2, "DF8G6439E2C5BA71" "21678345CDE9ABGF"},
- {16L, 16, 2, "D98G6BAFE2C14375" "74G8EF5B6C2391DA"},
- {32L, 16, 2, "D92G64AF78C5B3E1" "3C59A7DGB1F8E642"},
+ {8L, 16, 1, "MKNPFOADBGLCIEHJ"},
+ {96L, 16, 2, "ILKCONFPEDJHGMAB" "BDFGHIAKLMNCOEPJ"},
+ {32L, 16, 2, "MIHPFDCONBLAKJGE" "BEFGHJKALMNOCDPI"},
+ {32L, 16, 2, "IFACOGLMDEJBNPKH" "BEFGHJKALMNOCDPI"},
+ {16L, 16, 2, "MOHPFKCINBLADJGE" "BDFGHIEKLMNJOAPC"},
+ {16L, 16, 2, "MIHPFDJONBLEKCGA" "BDFGHIEKLMNJOAPC"},
+ {32L, 16, 2, "MOHPFDCINBLEKJGA" "BAFGHCDELMNIJKPO"},
+ {16L, 16, 2, "MIHPFKJONBLADCGE" "GDPHNOEKFLBCIAMJ"},
+ {32L, 16, 2, "MIBPFDJOGHLEKCNA" "CLEIJGMPKAOHNFDB"},
{192L, 16, 3,
- "D38G619AE2C45F7B" "25678AB1CDEF34G9" "7BC2EF546G8A91D3"},
- {64L, 16, 3, "D38G619AE2C45F7B" "CF76GBA92ED54381" "3D19A8G645FE2CB7"},
+ "MCHPFAIJNBLDEOGK" "BEFGHJKALMNOCDPI" "GKLBNOEDFPHJIAMC"},
+ {64L, 16, 3, "MCHPFAIJNBLDEOGK" "LOGFPKJIBNMEDCHA" "CMAIJHPFDEONBLKG"},
{192L, 16, 3,
- "9GB3F7DC54A2E618" "25678AB1CDEF34G9" "3D59A2G6B1F78C4E"},
- {48L, 16, 3, "9G4AFE6C5B327D18" "6A2CD5F378GB19E4" "4795BC8EAF1DG236"},
+ "IPKCOGMLEDJBNFAH" "BEFGHJKALMNOCDPI" "CMEIJBPFKAOGHLDN"},
+ {48L, 16, 3, "IPDJONFLEKCBGMAH" "FJBLMEOCGHPKAIND" "DGIEKLHNJOAMPBCF"},
{20160L, 16, 4,
- "58AB1DE2F34G679C" "21678345CDE9ABGF" "3619A2CD45F78GBE"
- "4791BC2E3F56G8AD"},
+ "EHJKAMNBOCDPFGIL" "BAFGHCDELMNIJKPO" "CFAIJBLMDEOGHPKN"
+ "DGIAKLBNCOEFPHJM"},
/* order 17 */
- {16L, 17, 1, "56789ABCDEFGH1234"},
+ {16L, 17, 1, "EFGHIJKLMNOPQABCD"},
/* order 18 */
- {54L, 18, 2, "DB9HFGE17CI5342A86" "215634ABC789FGDEIH"},
- {6L, 18, 1, "53AB786FG4DECI9H21"},
- {12L, 18, 2, "53AB782FG1DE6I4HC9" "BEFGH36I5978CA1D24"},
+ {54L, 18, 2, "MKIQOPNAGLRECDBJHF" "BAEFCDJKLGHIOPMNRQ"},
+ {6L, 18, 1, "ECJKGHFOPDMNLRIQBA"},
+ {12L, 18, 2, "ECJKGHBOPAMNFRDQLI" "KNOPQCFREIGHLJAMBD"},
{432L, 18, 3,
- "96E1BCH34FG278I5DA" "EFH36I978BCA1DG245" "215634ABC789FGDEIH"},
- {48L, 18, 2, "53AB782FG1DE6I4HC9" "64BC89FG2DE1I5H3A7"},
+ "IFNAKLQCDOPBGHREMJ" "NOQCFRIGHKLJAMPBDE" "BAEFCDJKLGHIOPMNRQ"},
+ {48L, 18, 2, "ECJKGHBOPAMNFRDQLI" "FDKLHIOPBMNAREQCJG"},
/* order 19 */
- {18L, 19, 1, "56789ABCDEFGHIJ1234"},
+ {18L, 19, 1, "EFGHIJKLMNOPQRSABCD"},
/* order 20 */
- {40L, 20, 2, "7K4BI58F29CJ6DG3AH1E" "5129346D78AHBCEKFGIJ"},
- {8L, 20, 1, "589AC3DEG7HIJB2K4F61"},
- {20L, 20, 2, "4AJ8HE3CKI7G52B196FD" "5129346D78AHBCEKFGIJ"},
- {40L, 20, 2, "7K4BI58F29CJ6DG3AH1E" "5329176D4BAH8FEKCJIG"},
- {24L, 20, 2, "976D4BAH8FEKCJI5G321" "649A78DEBCHIFGK2J153"},
+ {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "EABICDFMGHJQKLNTOPRS"},
+ {8L, 20, 1, "EHIJLCMNPGQRSKBTDOFA"},
+ {20L, 20, 2, "DJSHQNCLTRGPEBKAIFOM" "EABICDFMGHJQKLNTOPRS"},
+ {40L, 20, 2, "GTDKREHOBILSFMPCJQAN" "ECBIAGFMDKJQHONTLSRP"},
+ {24L, 20, 2, "IGFMDKJQHONTLSREPCBA" "FDIJGHMNKLQROPTBSAEC"},
/* order 21 */
- {42L, 21, 2, "9KCJ2FL5I4817B3AE6DHG" "5A8CDBFGEIJH1LK342679"},
- {12L, 21, 1, "5783AB6DE9GHCJKFL2I41"},
+ {42L, 21, 2, "ITLSBOUERDHAGKCJNFMQP" "EJHLMKOPNRSQAUTCDBFGI"},
+ {12L, 21, 1, "EGHCJKFMNIPQLSTOUBRDA"},
/* order 22 */
- {110L, 22, 2, "5K7M92B4D6F8HAJCLE1G3I" "21436587A9CBEDGFIHKJML"},
- {10L, 22, 1, "6587A9CBEDGFIHKJML2143"},
+ {110L, 22, 2, "ETGVIBKDMFOHQJSLUNAPCR" "BADCFEHGJILKNMPORQTSVU"},
+ {10L, 22, 1, "FEHGJILKNMPORQTSVUBADC"},
/* order 23 */
- {22L, 23, 1, "56789ABCDEFGHIJKLMN1234"},
+ {22L, 23, 1, "EFGHIJKLMNOPQRSTUVWABCD"},
/* order 24 */
- {24L, 24, 2, "HO5ANGLDBCI9M26KJ1378E4F" "8IEFGJN3KLM2C49AO671BHD5"},
- {8L, 24, 1, "DH2KL4IN678OA5C9EFGBJ1M3"},
- {24L, 24, 2, "9FHI25LM6N78BC1ODEGJ34KA" "EAOFM74BJDK69GH5C3LI2N18"},
- {48L, 24, 2, "HL5ANMO6BCI9G7DEJ132FK48" "8JEFGNC4KLM2I91BO673H5DA"},
- {24L, 24, 2, "HO5ANGLDBCI9M26KJ1378E4F" "KN8EOCI9FGLDJ13HM2645A7B"},
- {48L, 24, 2, "HL5ANMO6BCI9G7DEJ132FK48" "21678345DEFG9ABCKLMHIJON"},
+ {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "HRNOPSWCTUVBLDIJXFGAKQME"},
+ {8L, 24, 1, "MQBTUDRWFGHXJELINOPKSAVC"},
+ {24L, 24, 2, "IOQRBEUVFWGHKLAXMNPSCDTJ" "NJXOVGDKSMTFIPQELCURBWAH"},
+ {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "HSNOPWLDTUVBRIAKXFGCQEMJ"},
+ {24L, 24, 2, "QXEJWPUMKLRIVBFTSACGHNDO" "TWHNXLRIOPUMSACQVBFDEJGK"},
+ {48L, 24, 2, "QUEJWVXFKLRIPGMNSACBOTDH" "BAFGHCDEMNOPIJKLTUVQRSXW"},
{48L, 24, 3,
- "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F"
- "8JEFGNC4KLM2I91BO673H5DA"},
+ "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO"
+ "HSNOPWLDTUVBRIAKXFGCQEMJ"},
{24L, 24, 3,
- "HLBANGO65JI9M2DEC4378K1F" "AO5HIMLDBCN3G76KJ192FE48"
- "KIFEOCN38MLDJ19AG7645H2B"},
- {16L, 24, 2, "DI7KLCN9FG6OJ4AH2ME5B381" "MBO8FH1JEKG23N45L679ACDI"},
- {16L, 24, 2, "DI7KLCN9FG6OJ4AH2ME5B381" "IDCN97KLJ4AHFG6O5B32ME18"},
- {48L, 24, 2, "9LCHI7ODJ43NFGEK5BA2M618" "7CDFGIJ4KL2MN95B6O8AH1E3"},
- {24L, 24, 2, "LAGODI3JE87KCN9B6M254HF1" "EIL6MCN9GODFA54H87K3J12B"},
- {24L, 24, 2, "D92KL1HI678O345NEFGABCMJ" "FBOM6NJ37LKE4IHA2GD1C985"},
+ "QUKJWPXFESRIVBMNLDCGHTAO" "JXEQRVUMKLWCPGFTSAIBONDH"
+ "TRONXLWCHVUMSAIJPGFDEQBK"},
+ {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "VKXHOQASNTPBCWDEUFGIJLMR"},
+ {16L, 24, 2, "MRGTULWIOPFXSDJQBVNEKCHA" "RMLWIGTUSDJQOPFXEKCBVNAH"},
+ {48L, 24, 2, "IULQRGXMSDCWOPNTEKJBVFAH" "GLMOPRSDTUBVWIEKFXHJQANC"},
+ {24L, 24, 2, "UJPXMRCSNHGTLWIKFVBEDQOA" "NRUFVLWIPXMOJEDQHGTCSABK"},
+ {24L, 24, 2, "MIBTUAQRFGHXCDEWNOPJKLVS" "OKXVFWSCGUTNDRQJBPMALIHE"},
{144L, 24, 3,
- "HOBANMLD5JI9G76KC432FE18" "AL5HIGO6BCN3M2DEJ1978K4F"
- "21678345DEFG9ABCKLMHIJON"},
+ "QXKJWVUMESRIPGFTLDCBONAH" "JUEQRPXFKLWCVBMNSAIGHTDO"
+ "BAFGHCDEMNOPIJKLTUVQRSXW"},
{336L, 24, 3,
- "HKBANFEO5JI98MLDC43G7612" "AE5HI8KLBCN3FGO6J19M2D47"
- "85EFGABCKLM2HIJ1O67N34D9"},
+ "QTKJWONXESRIHVUMLDCPGFAB" "JNEQRHTUKLWCOPXFSAIVBMDG"
+ "HENOPJKLTUVBQRSAXFGWCDMI"},
/* order 25 */
- {20L, 25, 1, "589CDEGHIJ6KLM2ANO4FP71B3"},
- {480L, 25, 2, "589CDEGHIJ3KLM26NO4AP7FB1" "245789BCDE1GHIJ3KLM6NOAPF"},
+ {20L, 25, 1, "EHILMNPQRSFTUVBJWXDOYGAKC"},
+ {480L, 25, 2, "EHILMNPQRSCTUVBFWXDJYGOKA" "BDEGHIKLMNAPQRSCTUVFWXJYO"},
/* order 26 */
{156L, 26, 2,
- "5O7Q92B4D6F8HAJCLENGPI1K3M" "21436587A9CBEDGFIHKJMLONQP"},
- {12L, 26, 1, "6587A9CBEDGFIHKJMLONQP2143"},
- /* order 27 */
- {18L, 27, 1, "53BC689IJKDE4GHOP7LMANRFQ12"},
- {108L, 27, 2,
- "54BC79AIJKEFGH1OPLM2N3RQ68D" "DI2LG5O67Q4NBCREF9A8JKMH1P3"},
- {432L, 27, 2,
- "51BC234IJK6789AOPDEFGHRLMNQ" "3E89PLM1GHRB7Q64NKIJFDA5O2C"},
- {54L, 27, 2,
- "54BC79AIJKEFGH1OPLM2N3RQ68D" "DR2LNKI67QA8P5OEFH1GBCM34J9"},
- {11232L, 27, 3,
- "51BC234IJK6789AOPDEFGHRLMNQ" "3689BDE1GHIJ2LM4N5OP7QACRFK"
- "479ACEFGH1JKLM2N3OP5Q68RBDI"},
- /* order 28 */
- {84L, 28, 2,
- "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5129346D78AHBCELFGIPJKMSNOQR"},
- {12L, 28, 1, "589AC3DEG7HIKBLMOFPQRJ2S4N61"},
- {84L, 28, 2,
- "7S4BQ58F29CJ6DGNAHKRELO3IP1M" "5329176D4BAH8FELCJIPGNMSKRQO"},
- {36L, 28, 2,
- "976D4BAH8FELCJIPGNMSKRQ5O321" "649A78DEBCHIFGLMJKPQNOS2R153"},
- /* order 29 */
- {28L, 29, 1, "56789ABCDEFGHIJKLMNOPQRST1234"},
- /* order 30 */
- {24L, 30, 2,
- "LHQ7NOTDERSA9JK6UGF1PBCM34I285" "BFHIL3NO5Q78RSATDE6UG9JKCM1P24"},
- {40L, 30, 2,
- "DU4JOA89PS2GEFT56MKL7BCRQ1HI3N" "BQGHT36MNL78CRS1DEIU54JKOA9P2F"},
- {120L, 30, 2,
- "DS4JU589POABEFT2GHKL76MNQ1CR3I" "215634ABC789GHIDEFMNOJKLRSPQUT"},
- {8L, 30, 1, "HEMNJKCRS9PQIU5FT3OABL782G1D64"},
- /* order 31 */
- {30L, 31, 1, "56789ABCDEFGHIJKLMNOPQRSTUV1234"},
+ "EXGZIBKDMFOHQJSLUNWPYRATCV" "BADCFEHGJILKNMPORQTSVUXWZY"},
+ {12L, 26, 1, "FEHGJILKNMPORQTSVUXWZYBADC"},
};
static const struct groups groups[] = {
- {0, NULL}, /* trivial case: 0 */
- {0, NULL}, /* trivial case: 1 */
- {1, groupdata + 0}, /* 2 */
- {1, groupdata + 1}, /* 3 */
- {2, groupdata + 2}, /* 4 */
- {1, groupdata + 4}, /* 5 */
- {2, groupdata + 5}, /* 6 */
- {1, groupdata + 7}, /* 7 */
- {5, groupdata + 8}, /* 8 */
- {2, groupdata + 13}, /* 9 */
- {2, groupdata + 15}, /* 10 */
- {1, groupdata + 17}, /* 11 */
- {5, groupdata + 18}, /* 12 */
- {1, groupdata + 23}, /* 13 */
- {2, groupdata + 24}, /* 14 */
- {1, groupdata + 26}, /* 15 */
- {14, groupdata + 27}, /* 16 */
- {1, groupdata + 41}, /* 17 */
- {5, groupdata + 42}, /* 18 */
- {1, groupdata + 47}, /* 19 */
- {5, groupdata + 48}, /* 20 */
- {2, groupdata + 53}, /* 21 */
- {2, groupdata + 55}, /* 22 */
- {1, groupdata + 57}, /* 23 */
- {15, groupdata + 58}, /* 24 */
- {2, groupdata + 73}, /* 25 */
- {2, groupdata + 75}, /* 26 */
- {5, groupdata + 77}, /* 27 */
- {4, groupdata + 82}, /* 28 */
- {1, groupdata + 86}, /* 29 */
- {4, groupdata + 87}, /* 30 */
- {1, groupdata + 91}, /* 31 */
+ {0, NULL}, /* trivial case: 0 */
+ {0, NULL}, /* trivial case: 1 */
+ {1, groupdata + 0}, /* 2 */
+ {1, groupdata + 1}, /* 3 */
+ {2, groupdata + 2}, /* 4 */
+ {1, groupdata + 4}, /* 5 */
+ {2, groupdata + 5}, /* 6 */
+ {1, groupdata + 7}, /* 7 */
+ {5, groupdata + 8}, /* 8 */
+ {2, groupdata + 13}, /* 9 */
+ {2, groupdata + 15}, /* 10 */
+ {1, groupdata + 17}, /* 11 */
+ {5, groupdata + 18}, /* 12 */
+ {1, groupdata + 23}, /* 13 */
+ {2, groupdata + 24}, /* 14 */
+ {1, groupdata + 26}, /* 15 */
+ {14, groupdata + 27}, /* 16 */
+ {1, groupdata + 41}, /* 17 */
+ {5, groupdata + 42}, /* 18 */
+ {1, groupdata + 47}, /* 19 */
+ {5, groupdata + 48}, /* 20 */
+ {2, groupdata + 53}, /* 21 */
+ {2, groupdata + 55}, /* 22 */
+ {1, groupdata + 57}, /* 23 */
+ {15, groupdata + 58}, /* 24 */
+ {2, groupdata + 73}, /* 25 */
+ {2, groupdata + 75}, /* 26 */
};
/* ----- data generated by group.gap ends ----- */
-static char *new_game_desc(game_params *params, random_state *rs,
+static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, int interactive)
{
int w = params->w, a = w*w;
* I tested it using the following shell command:
for d in t n h x u; do
- for i in {3..9}; do
- echo ./group --generate 1 ${i}d${d}
- perl -e 'alarm 30; exec @ARGV' ./group --generate 5 ${i}d${d} >/dev/null \
- || echo broken
+ for id in '' i; do
+ for i in {3..9}; do
+ echo -n "./group --generate 1 ${i}d${d}${id}: "
+ perl -e 'alarm 30; exec @ARGV' \
+ ./group --generate 1 ${i}d${d}${id} >/dev/null && echo ok
+ done
done
done
* _out_, so as to detect exceptions that should be removed as
* well as those which should be added.
*/
- if (w <= 9 && diff == DIFF_EXTREME)
+ if (w < 5 && diff == DIFF_UNREASONABLE)
diff--;
- if (w <= 6 && diff == DIFF_HARD)
+ if ((w < 5 || ((w == 6 || w == 8) && params->id)) && diff == DIFF_EXTREME)
+ diff--;
+ if ((w < 6 || (w == 6 && params->id)) && diff == DIFF_HARD)
+ diff--;
+ if ((w < 4 || (w == 4 && params->id)) && diff == DIFF_NORMAL)
diff--;
- if (w <= 4 && diff > DIFF_TRIVIAL)
- diff = DIFF_TRIVIAL;
grid = snewn(a, digit);
soln = snewn(a, digit);
* Apply each group generator to row, constructing a
* new row.
*/
- nri = FROMCHAR(gen[row[0]-1]); /* which row is it? */
+ nri = gen[row[0]-1] - 'A' + 1; /* which row is it? */
newrow = soln + (nri-1)*w;
if (!newrow[0]) { /* not done yet */
for (k = 0; k < w; k++)
- newrow[k] = FROMCHAR(gen[row[k]-1]);
+ newrow[k] = gen[row[k]-1] - 'A' + 1;
grid[qt++] = nri;
}
}
}
memcpy(soln2, grid, a);
- if (solver(w, soln2, diff) > diff)
+ if (solver(params, soln2, diff) > diff)
continue; /* go round again if that didn't work */
}
for (i = 0; i < k; i++) {
memcpy(soln2, grid, a);
soln2[indices[i]] = 0;
- if (solver(w, soln2, diff) <= diff)
+ if (solver(params, soln2, diff) <= diff)
grid[indices[i]] = 0;
}
*/
if (diff > 0) {
memcpy(soln2, grid, a);
- if (solver(w, soln2, diff-1) < diff)
+ if (solver(params, soln2, diff-1) < diff)
continue; /* go round and try again */
}
*aux = snewn(a+2, char);
(*aux)[0] = 'S';
for (i = 0; i < a; i++)
- (*aux)[i+1] = TOCHAR(soln[i]);
+ (*aux)[i+1] = TOCHAR(soln[i], params->id);
(*aux)[a+1] = '\0';
sfree(grid);
* Gameplay.
*/
-static char *validate_grid_desc(const char **pdesc, int range, int area)
+static const char *validate_grid_desc(const char **pdesc, int range, int area)
{
const char *desc = *pdesc;
int squares = 0;
return NULL;
}
-static char *validate_desc(game_params *params, char *desc)
+static const char *validate_desc(const game_params *params, const char *desc)
{
int w = params->w, a = w*w;
const char *p = desc;
return validate_grid_desc(&p, w, a);
}
-static char *spec_to_grid(char *desc, digit *grid, int area)
+static const char *spec_to_grid(const char *desc, digit *grid, int area)
{
int i = 0;
while (*desc && *desc != ',') {
return desc;
}
-static game_state *new_game(midend *me, game_params *params, char *desc)
+static game_state *new_game(midend *me, const game_params *params,
+ const char *desc)
{
int w = params->w, a = w*w;
game_state *state = snew(game_state);
state->immutable[i] = 0;
state->pencil[i] = 0;
}
+ state->sequence = snewn(w, digit);
+ state->dividers = snewn(w, int);
+ for (i = 0; i < w; i++) {
+ state->sequence[i] = i;
+ state->dividers[i] = -1;
+ }
desc = spec_to_grid(desc, state->grid, a);
for (i = 0; i < a; i++)
return state;
}
-static game_state *dup_game(game_state *state)
+static game_state *dup_game(const game_state *state)
{
int w = state->par.w, a = w*w;
game_state *ret = snew(game_state);
ret->grid = snewn(a, digit);
ret->immutable = snewn(a, unsigned char);
ret->pencil = snewn(a, int);
+ ret->sequence = snewn(w, digit);
+ ret->dividers = snewn(w, int);
memcpy(ret->grid, state->grid, a*sizeof(digit));
memcpy(ret->immutable, state->immutable, a*sizeof(unsigned char));
memcpy(ret->pencil, state->pencil, a*sizeof(int));
+ memcpy(ret->sequence, state->sequence, w*sizeof(digit));
+ memcpy(ret->dividers, state->dividers, w*sizeof(int));
ret->completed = state->completed;
ret->cheated = state->cheated;
sfree(state->grid);
sfree(state->immutable);
sfree(state->pencil);
+ sfree(state->sequence);
sfree(state);
}
-static char *solve_game(game_state *state, game_state *currstate,
- char *aux, char **error)
+static char *solve_game(const game_state *state, const game_state *currstate,
+ const char *aux, const char **error)
{
int w = state->par.w, a = w*w;
int i, ret;
soln = snewn(a, digit);
memcpy(soln, state->grid, a*sizeof(digit));
- ret = solver(w, soln, DIFFCOUNT-1);
+ ret = solver(&state->par, soln, DIFFCOUNT-1);
if (ret == diff_impossible) {
*error = "No solution exists for this puzzle";
out = snewn(a+2, char);
out[0] = 'S';
for (i = 0; i < a; i++)
- out[i+1] = TOCHAR(soln[i]);
+ out[i+1] = TOCHAR(soln[i], state->par.id);
out[a+1] = '\0';
}
return out;
}
-static int game_can_format_as_text_now(game_params *params)
+static int game_can_format_as_text_now(const game_params *params)
{
return TRUE;
}
-static char *game_text_format(game_state *state)
+static char *game_text_format(const game_state *state)
{
int w = state->par.w;
int x, y;
if (d == 0) {
ch = '.';
} else {
- ch = TOCHAR(d);
+ ch = TOCHAR(d, state->par.id);
}
*p++ = ch;
struct game_ui {
/*
- * These are the coordinates of the currently highlighted
- * square on the grid, if hshow = 1.
+ * These are the coordinates of the primary highlighted square on
+ * the grid, if hshow = 1.
*/
int hx, hy;
+ /*
+ * These are the coordinates hx,hy _before_ they go through
+ * state->sequence.
+ */
+ int ohx, ohy;
+ /*
+ * These variables give the length and displacement of a diagonal
+ * sequence of highlighted squares starting at ohx,ohy (still if
+ * hshow = 1). To find the squares' real coordinates, for 0<=i<dn,
+ * compute ohx+i*odx and ohy+i*ody and then map through
+ * state->sequence.
+ */
+ int odx, ody, odn;
/*
* This indicates whether the current highlight is a
* pencil-mark one or a real one.
* allowed on immutable squares.
*/
int hcursor;
+ /*
+ * This indicates whether we're dragging a table header to
+ * reposition an entire row or column.
+ */
+ int drag; /* 0=none 1=row 2=col */
+ int dragnum; /* element being dragged */
+ int dragpos; /* its current position */
+ int edgepos;
};
-static game_ui *new_ui(game_state *state)
+static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->hx = ui->hy = 0;
ui->hpencil = ui->hshow = ui->hcursor = 0;
+ ui->drag = 0;
return ui;
}
sfree(ui);
}
-static char *encode_ui(game_ui *ui)
+static char *encode_ui(const game_ui *ui)
{
return NULL;
}
-static void decode_ui(game_ui *ui, char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding)
{
}
-static void game_changed_state(game_ui *ui, game_state *oldstate,
- game_state *newstate)
+static void game_changed_state(game_ui *ui, const game_state *oldstate,
+ const game_state *newstate)
{
int w = newstate->par.w;
/*
newstate->grid[ui->hy * w + ui->hx] != 0) {
ui->hshow = 0;
}
+ if (ui->hshow && ui->odn > 1) {
+ /*
+ * Reordering of rows or columns within the range of a
+ * multifill selection cancels the multifill and deselects
+ * everything.
+ */
+ int i;
+ for (i = 0; i < ui->odn; i++) {
+ if (oldstate->sequence[ui->ohx + i*ui->odx] !=
+ newstate->sequence[ui->ohx + i*ui->odx]) {
+ ui->hshow = 0;
+ break;
+ }
+ if (oldstate->sequence[ui->ohy + i*ui->ody] !=
+ newstate->sequence[ui->ohy + i*ui->ody]) {
+ ui->hshow = 0;
+ break;
+ }
+ }
+ } else if (ui->hshow &&
+ (newstate->sequence[ui->ohx] != ui->hx ||
+ newstate->sequence[ui->ohy] != ui->hy)) {
+ /*
+ * Otherwise, reordering of the row or column containing the
+ * selection causes the selection to move with it.
+ */
+ int i;
+ for (i = 0; i < w; i++) {
+ if (newstate->sequence[i] == ui->hx)
+ ui->ohx = i;
+ if (newstate->sequence[i] == ui->hy)
+ ui->ohy = i;
+ }
+ }
}
#define PREFERRED_TILESIZE 48
#define FLASH_TIME 0.4F
+#define DF_DIVIDER_TOP 0x1000
+#define DF_DIVIDER_BOT 0x2000
+#define DF_DIVIDER_LEFT 0x4000
+#define DF_DIVIDER_RIGHT 0x8000
#define DF_HIGHLIGHT 0x0400
#define DF_HIGHLIGHT_PENCIL 0x0200
#define DF_IMMUTABLE 0x0100
+#define DF_LEGEND 0x0080
#define DF_DIGIT_MASK 0x001F
#define EF_DIGIT_SHIFT 5
#define EF_LATIN (1UL << (6*EF_DIGIT_SHIFT))
struct game_drawstate {
+ game_params par;
int w, tilesize;
int started;
- long *tiles, *pencil, *errors;
+ long *tiles, *legend, *pencil, *errors;
long *errtmp;
+ digit *sequence;
};
-static int check_errors(game_state *state, long *errors)
+static int check_errors(const game_state *state, long *errors)
{
int w = state->par.w, a = w*w;
digit *grid = state->grid;
int i, j, k, x, y, errs = FALSE;
+ /*
+ * To verify that we have a valid group table, it suffices to
+ * test latin-square-hood and associativity only. All the other
+ * group axioms follow from those two.
+ *
+ * Proof:
+ *
+ * Associativity is given; closure is obvious from latin-
+ * square-hood. We need to show that an identity exists and that
+ * every element has an inverse.
+ *
+ * Identity: take any element a. There will be some element e
+ * such that ea=a (in a latin square, every element occurs in
+ * every row and column, so a must occur somewhere in the a
+ * column, say on row e). For any other element b, there must
+ * exist x such that ax=b (same argument from latin-square-hood
+ * again), and then associativity gives us eb = e(ax) = (ea)x =
+ * ax = b. Hence eb=b for all b, i.e. e is a left-identity. A
+ * similar argument tells us that there must be some f which is
+ * a right-identity, and then we show they are the same element
+ * by observing that ef must simultaneously equal e and equal f.
+ *
+ * Inverses: given any a, by the latin-square argument again,
+ * there must exist p and q such that pa=e and aq=e (i.e. left-
+ * and right-inverses). We can show these are equal by
+ * associativity: p = pe = p(aq) = (pa)q = eq = q. []
+ */
+
if (errors)
for (i = 0; i < a; i++)
errors[i] = 0;
return errs;
}
-static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
- int x, int y, int button)
+static int find_in_sequence(digit *seq, int len, digit n)
+{
+ int i;
+
+ for (i = 0; i < len; i++)
+ if (seq[i] == n)
+ return i;
+
+ assert(!"Should never get here");
+ return -1;
+}
+
+static char *interpret_move(const game_state *state, game_ui *ui,
+ const game_drawstate *ds,
+ int x, int y, int button)
{
int w = state->par.w;
int tx, ty;
tx = FROMCOORD(x);
ty = FROMCOORD(y);
- if (tx >= 0 && tx < w && ty >= 0 && ty < w) {
- if (button == LEFT_BUTTON) {
- if (tx == ui->hx && ty == ui->hy &&
- ui->hshow && ui->hpencil == 0) {
- ui->hshow = 0;
+ if (ui->drag) {
+ if (IS_MOUSE_DRAG(button)) {
+ int tcoord = ((ui->drag &~ 4) == 1 ? ty : tx);
+ ui->drag |= 4; /* some movement has happened */
+ if (tcoord >= 0 && tcoord < w) {
+ ui->dragpos = tcoord;
+ return UI_UPDATE;
+ }
+ } else if (IS_MOUSE_RELEASE(button)) {
+ if (ui->drag & 4) {
+ ui->drag = 0; /* end drag */
+ if (state->sequence[ui->dragpos] == ui->dragnum)
+ return UI_UPDATE; /* drag was a no-op overall */
+ sprintf(buf, "D%d,%d", ui->dragnum, ui->dragpos);
+ return dupstr(buf);
} else {
- ui->hx = tx;
- ui->hy = ty;
- ui->hshow = !state->immutable[ty*w+tx];
- ui->hpencil = 0;
+ ui->drag = 0; /* end 'drag' */
+ if (ui->edgepos > 0 && ui->edgepos < w) {
+ sprintf(buf, "V%d,%d",
+ state->sequence[ui->edgepos-1],
+ state->sequence[ui->edgepos]);
+ return dupstr(buf);
+ } else
+ return UI_UPDATE; /* no-op */
}
- ui->hcursor = 0;
- return ""; /* UI activity occurred */
}
- if (button == RIGHT_BUTTON) {
- /*
- * Pencil-mode highlighting for non filled squares.
- */
- if (state->grid[ty*w+tx] == 0) {
+ } else if (IS_MOUSE_DOWN(button)) {
+ if (tx >= 0 && tx < w && ty >= 0 && ty < w) {
+ int otx = tx, oty = ty;
+ tx = state->sequence[tx];
+ ty = state->sequence[ty];
+ if (button == LEFT_BUTTON) {
if (tx == ui->hx && ty == ui->hy &&
- ui->hshow && ui->hpencil) {
+ ui->hshow && ui->hpencil == 0) {
ui->hshow = 0;
} else {
- ui->hpencil = 1;
ui->hx = tx;
ui->hy = ty;
- ui->hshow = 1;
+ ui->ohx = otx;
+ ui->ohy = oty;
+ ui->odx = ui->ody = 0;
+ ui->odn = 1;
+ ui->hshow = !state->immutable[ty*w+tx];
+ ui->hpencil = 0;
}
- } else {
- ui->hshow = 0;
+ ui->hcursor = 0;
+ return UI_UPDATE;
}
- ui->hcursor = 0;
- return ""; /* UI activity occurred */
+ if (button == RIGHT_BUTTON) {
+ /*
+ * Pencil-mode highlighting for non filled squares.
+ */
+ if (state->grid[ty*w+tx] == 0) {
+ if (tx == ui->hx && ty == ui->hy &&
+ ui->hshow && ui->hpencil) {
+ ui->hshow = 0;
+ } else {
+ ui->hpencil = 1;
+ ui->hx = tx;
+ ui->hy = ty;
+ ui->ohx = otx;
+ ui->ohy = oty;
+ ui->odx = ui->ody = 0;
+ ui->odn = 1;
+ ui->hshow = 1;
+ }
+ } else {
+ ui->hshow = 0;
+ }
+ ui->hcursor = 0;
+ return UI_UPDATE;
+ }
+ } else if (tx >= 0 && tx < w && ty == -1) {
+ ui->drag = 2;
+ ui->dragnum = state->sequence[tx];
+ ui->dragpos = tx;
+ ui->edgepos = FROMCOORD(x + TILESIZE/2);
+ return UI_UPDATE;
+ } else if (ty >= 0 && ty < w && tx == -1) {
+ ui->drag = 1;
+ ui->dragnum = state->sequence[ty];
+ ui->dragpos = ty;
+ ui->edgepos = FROMCOORD(y + TILESIZE/2);
+ return UI_UPDATE;
}
+ } else if (IS_MOUSE_DRAG(button)) {
+ if (!ui->hpencil &&
+ tx >= 0 && tx < w && ty >= 0 && ty < w &&
+ abs(tx - ui->ohx) == abs(ty - ui->ohy)) {
+ ui->odn = abs(tx - ui->ohx) + 1;
+ ui->odx = (tx < ui->ohx ? -1 : +1);
+ ui->ody = (ty < ui->ohy ? -1 : +1);
+ } else {
+ ui->odx = ui->ody = 0;
+ ui->odn = 1;
+ }
+ return UI_UPDATE;
}
+
if (IS_CURSOR_MOVE(button)) {
- move_cursor(button, &ui->hx, &ui->hy, w, w, 0);
+ int cx = find_in_sequence(state->sequence, w, ui->hx);
+ int cy = find_in_sequence(state->sequence, w, ui->hy);
+ move_cursor(button, &cx, &cy, w, w, 0);
+ ui->hx = state->sequence[cx];
+ ui->hy = state->sequence[cy];
ui->hshow = ui->hcursor = 1;
- return "";
+ return UI_UPDATE;
}
if (ui->hshow &&
(button == CURSOR_SELECT)) {
ui->hpencil = 1 - ui->hpencil;
ui->hcursor = 1;
- return "";
+ return UI_UPDATE;
}
if (ui->hshow &&
- ((ISCHAR(button) && FROMCHAR(button) <= w) ||
+ ((ISCHAR(button) && FROMCHAR(button, state->par.id) <= w) ||
button == CURSOR_SELECT2 || button == '\b')) {
- int n = FROMCHAR(button);
+ int n = FROMCHAR(button, state->par.id);
+ int i, buflen;
+ char *movebuf;
+
if (button == CURSOR_SELECT2 || button == '\b')
n = 0;
- /*
- * Can't make pencil marks in a filled square. This can only
- * become highlighted if we're using cursor keys.
- */
- if (ui->hpencil && state->grid[ui->hy*w+ui->hx])
- return NULL;
+ for (i = 0; i < ui->odn; i++) {
+ int x = state->sequence[ui->ohx + i*ui->odx];
+ int y = state->sequence[ui->ohy + i*ui->ody];
+ int index = y*w+x;
- /*
- * Can't do anything to an immutable square.
- */
- if (state->immutable[ui->hy*w+ui->hx])
- return NULL;
+ /*
+ * Can't make pencil marks in a filled square. This can only
+ * become highlighted if we're using cursor keys.
+ */
+ if (ui->hpencil && state->grid[index])
+ return NULL;
+
+ /*
+ * Can't do anything to an immutable square. Exception:
+ * trying to set it to what it already was is OK (so that
+ * multifilling can set a whole diagonal to a without
+ * having to detour round the one immutable square in the
+ * middle that already said a).
+ */
+ if (!ui->hpencil && state->grid[index] == n)
+ /* OK even if it is immutable */;
+ else if (state->immutable[index])
+ return NULL;
+ }
- sprintf(buf, "%c%d,%d,%d",
- (char)(ui->hpencil && n > 0 ? 'P' : 'R'), ui->hx, ui->hy, n);
+ movebuf = snewn(80 * ui->odn, char);
+ buflen = sprintf(movebuf, "%c%d,%d,%d",
+ (char)(ui->hpencil && n > 0 ? 'P' : 'R'),
+ ui->hx, ui->hy, n);
+ for (i = 1; i < ui->odn; i++) {
+ assert(buflen < i*80);
+ buflen += sprintf(movebuf + buflen, "+%d,%d",
+ state->sequence[ui->ohx + i*ui->odx],
+ state->sequence[ui->ohy + i*ui->ody]);
+ }
+ movebuf = sresize(movebuf, buflen+1, char);
if (!ui->hcursor) ui->hshow = 0;
- return dupstr(buf);
+ return movebuf;
}
if (button == 'M' || button == 'm')
return NULL;
}
-static game_state *execute_move(game_state *from, char *move)
+static game_state *execute_move(const game_state *from, const char *move)
{
int w = from->par.w, a = w*w;
game_state *ret;
- int x, y, i, n;
+ int x, y, i, j, n, pos;
if (move[0] == 'S') {
ret = dup_game(from);
ret->completed = ret->cheated = TRUE;
for (i = 0; i < a; i++) {
- if (!ISCHAR(move[i+1]) || FROMCHAR(move[i+1]) > w) {
+ if (!ISCHAR(move[i+1]) || FROMCHAR(move[i+1], from->par.id) > w) {
free_game(ret);
return NULL;
}
- ret->grid[i] = FROMCHAR(move[i+1]);
+ ret->grid[i] = FROMCHAR(move[i+1], from->par.id);
ret->pencil[i] = 0;
}
return ret;
} else if ((move[0] == 'P' || move[0] == 'R') &&
- sscanf(move+1, "%d,%d,%d", &x, &y, &n) == 3 &&
- x >= 0 && x < w && y >= 0 && y < w && n >= 0 && n <= w) {
- if (from->immutable[y*w+x])
- return NULL;
+ sscanf(move+1, "%d,%d,%d%n", &x, &y, &n, &pos) == 3 &&
+ n >= 0 && n <= w) {
+ const char *mp = move + 1 + pos;
+ int pencil = (move[0] == 'P');
+ ret = dup_game(from);
+
+ while (1) {
+ if (x < 0 || x >= w || y < 0 || y >= w) {
+ free_game(ret);
+ return NULL;
+ }
+ if (from->immutable[y*w+x] && !(!pencil && from->grid[y*w+x] == n))
+ return NULL;
- ret = dup_game(from);
- if (move[0] == 'P' && n > 0) {
- ret->pencil[y*w+x] ^= 1 << n;
- } else {
- ret->grid[y*w+x] = n;
- ret->pencil[y*w+x] = 0;
+ if (move[0] == 'P' && n > 0) {
+ ret->pencil[y*w+x] ^= 1 << n;
+ } else {
+ ret->grid[y*w+x] = n;
+ ret->pencil[y*w+x] = 0;
+ }
+
+ if (!*mp)
+ break;
- if (!ret->completed && !check_errors(ret, NULL))
- ret->completed = TRUE;
+ if (*mp != '+')
+ return NULL;
+ if (sscanf(mp, "+%d,%d%n", &x, &y, &pos) < 2)
+ return NULL;
+ mp += pos;
}
+
+ if (!ret->completed && !check_errors(ret, NULL))
+ ret->completed = TRUE;
+
return ret;
} else if (move[0] == 'M') {
/*
ret->pencil[i] = (1 << (w+1)) - (1 << 1);
}
return ret;
+ } else if (move[0] == 'D' &&
+ sscanf(move+1, "%d,%d", &x, &y) == 2) {
+ /*
+ * Reorder the rows and columns so that digit x is in position
+ * y.
+ */
+ ret = dup_game(from);
+ for (i = j = 0; i < w; i++) {
+ if (i == y) {
+ ret->sequence[i] = x;
+ } else {
+ if (from->sequence[j] == x)
+ j++;
+ ret->sequence[i] = from->sequence[j++];
+ }
+ }
+ /*
+ * Eliminate any obsoleted dividers.
+ */
+ for (x = 0; x < w; x++) {
+ int i = ret->sequence[x];
+ int j = (x+1 < w ? ret->sequence[x+1] : -1);
+ if (ret->dividers[i] != j)
+ ret->dividers[i] = -1;
+ }
+ return ret;
+ } else if (move[0] == 'V' &&
+ sscanf(move+1, "%d,%d", &i, &j) == 2) {
+ ret = dup_game(from);
+ if (ret->dividers[i] == j)
+ ret->dividers[i] = -1;
+ else
+ ret->dividers[i] = j;
+ return ret;
} else
return NULL; /* couldn't parse move string */
}
#define SIZE(w) ((w) * TILESIZE + 2*BORDER + LEGEND)
-static void game_compute_size(game_params *params, int tilesize,
- int *x, int *y)
+static void game_compute_size(const game_params *params, int tilesize,
+ int *x, int *y)
{
/* Ick: fake up `ds->tilesize' for macro expansion purposes */
struct { int tilesize; } ads, *ds = &ads;
}
static void game_set_size(drawing *dr, game_drawstate *ds,
- game_params *params, int tilesize)
+ const game_params *params, int tilesize)
{
ds->tilesize = tilesize;
}
ret[COL_PENCIL * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
ret[COL_PENCIL * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
+ ret[COL_DIAGONAL * 3 + 0] = 0.95F * ret[COL_BACKGROUND * 3 + 0];
+ ret[COL_DIAGONAL * 3 + 1] = 0.95F * ret[COL_BACKGROUND * 3 + 1];
+ ret[COL_DIAGONAL * 3 + 2] = 0.95F * ret[COL_BACKGROUND * 3 + 2];
+
*ncolours = NCOLOURS;
return ret;
}
-static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
{
int w = state->par.w, a = w*w;
struct game_drawstate *ds = snew(struct game_drawstate);
int i;
ds->w = w;
+ ds->par = state->par; /* structure copy */
ds->tilesize = 0;
ds->started = FALSE;
ds->tiles = snewn(a, long);
+ ds->legend = snewn(w, long);
ds->pencil = snewn(a, long);
ds->errors = snewn(a, long);
+ ds->sequence = snewn(a, digit);
for (i = 0; i < a; i++)
ds->tiles[i] = ds->pencil[i] = -1;
+ for (i = 0; i < w; i++)
+ ds->legend[i] = -1;
ds->errtmp = snewn(a, long);
return ds;
sfree(ds->pencil);
sfree(ds->errors);
sfree(ds->errtmp);
+ sfree(ds->sequence);
sfree(ds);
}
cw = tw = TILESIZE-1;
ch = th = TILESIZE-1;
+ if (tile & DF_LEGEND) {
+ cx += TILESIZE/10;
+ cy += TILESIZE/10;
+ cw -= TILESIZE/5;
+ ch -= TILESIZE/5;
+ tile |= DF_IMMUTABLE;
+ }
+
clip(dr, cx, cy, cw, ch);
/* background needs erasing */
draw_rect(dr, cx, cy, cw, ch,
- (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : COL_BACKGROUND);
+ (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT :
+ (x == y) ? COL_DIAGONAL : COL_BACKGROUND);
+
+ /* dividers */
+ if (tile & DF_DIVIDER_TOP)
+ draw_rect(dr, cx, cy, cw, 1, COL_GRID);
+ if (tile & DF_DIVIDER_BOT)
+ draw_rect(dr, cx, cy+ch-1, cw, 1, COL_GRID);
+ if (tile & DF_DIVIDER_LEFT)
+ draw_rect(dr, cx, cy, 1, ch, COL_GRID);
+ if (tile & DF_DIVIDER_RIGHT)
+ draw_rect(dr, cx+cw-1, cy, 1, ch, COL_GRID);
/* pencil-mode highlight */
if (tile & DF_HIGHLIGHT_PENCIL) {
/* new number needs drawing? */
if (tile & DF_DIGIT_MASK) {
str[1] = '\0';
- str[0] = TOCHAR(tile & DF_DIGIT_MASK);
+ str[0] = TOCHAR(tile & DF_DIGIT_MASK, ds->par.id);
draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2,
FONT_VARIABLE, TILESIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
(error & EF_LATIN) ? COL_ERROR :
int b = (error >> (EF_LEFT_SHIFT+1*EF_DIGIT_SHIFT))&EF_DIGIT_MASK;
int c = (error >> (EF_LEFT_SHIFT ))&EF_DIGIT_MASK;
char buf[10];
- sprintf(buf, "(%c%c)%c", TOCHAR(a), TOCHAR(b), TOCHAR(c));
+ sprintf(buf, "(%c%c)%c", TOCHAR(a, ds->par.id),
+ TOCHAR(b, ds->par.id), TOCHAR(c, ds->par.id));
draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/6,
FONT_VARIABLE, TILESIZE/6, ALIGN_VCENTRE | ALIGN_HCENTRE,
COL_ERROR, buf);
int b = (error >> (EF_RIGHT_SHIFT+1*EF_DIGIT_SHIFT))&EF_DIGIT_MASK;
int c = (error >> (EF_RIGHT_SHIFT ))&EF_DIGIT_MASK;
char buf[10];
- sprintf(buf, "%c(%c%c)", TOCHAR(a), TOCHAR(b), TOCHAR(c));
+ sprintf(buf, "%c(%c%c)", TOCHAR(a, ds->par.id),
+ TOCHAR(b, ds->par.id), TOCHAR(c, ds->par.id));
draw_text(dr, tx + TILESIZE/2, ty + TILESIZE - TILESIZE/6,
FONT_VARIABLE, TILESIZE/6, ALIGN_VCENTRE | ALIGN_HCENTRE,
COL_ERROR, buf);
int dx = j % pw, dy = j / pw;
str[1] = '\0';
- str[0] = TOCHAR(i);
+ str[0] = TOCHAR(i, ds->par.id);
draw_text(dr, pl + fontsize * (2*dx+1) / 2,
pt + fontsize * (2*dy+1) / 2,
FONT_VARIABLE, fontsize,
draw_update(dr, cx, cy, cw, ch);
}
-static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
- game_state *state, int dir, game_ui *ui,
- float animtime, float flashtime)
+static void game_redraw(drawing *dr, game_drawstate *ds,
+ const game_state *oldstate, const game_state *state,
+ int dir, const game_ui *ui,
+ float animtime, float flashtime)
{
int w = state->par.w /*, a = w*w */;
- int x, y;
+ int x, y, i, j;
if (!ds->started) {
/*
w*TILESIZE+1+GRIDEXTRA*2, w*TILESIZE+1+GRIDEXTRA*2,
COL_GRID);
- /*
- * Table legend.
- */
- for (x = 0; x < w; x++) {
- char str[2];
- str[1] = '\0';
- str[0] = TOCHAR(x+1);
- draw_text(dr, COORD(x) + TILESIZE/2, BORDER + TILESIZE/2,
- FONT_VARIABLE, TILESIZE/2,
- ALIGN_VCENTRE | ALIGN_HCENTRE, COL_GRID, str);
- draw_text(dr, BORDER + TILESIZE/2, COORD(x) + TILESIZE/2,
- FONT_VARIABLE, TILESIZE/2,
- ALIGN_VCENTRE | ALIGN_HCENTRE, COL_GRID, str);
- }
-
draw_update(dr, 0, 0, SIZE(w), SIZE(w));
ds->started = TRUE;
check_errors(state, ds->errtmp);
+ /*
+ * Construct a modified version of state->sequence which takes
+ * into account an unfinished drag operation.
+ */
+ if (ui->drag) {
+ x = ui->dragnum;
+ y = ui->dragpos;
+ } else {
+ x = y = -1;
+ }
+ for (i = j = 0; i < w; i++) {
+ if (i == y) {
+ ds->sequence[i] = x;
+ } else {
+ if (state->sequence[j] == x)
+ j++;
+ ds->sequence[i] = state->sequence[j++];
+ }
+ }
+
+ /*
+ * Draw the table legend.
+ */
+ for (x = 0; x < w; x++) {
+ int sx = ds->sequence[x];
+ long tile = (sx+1) | DF_LEGEND;
+ if (ds->legend[x] != tile) {
+ ds->legend[x] = tile;
+ draw_tile(dr, ds, -1, x, tile, 0, 0);
+ draw_tile(dr, ds, x, -1, tile, 0, 0);
+ }
+ }
+
for (y = 0; y < w; y++) {
+ int sy = ds->sequence[y];
for (x = 0; x < w; x++) {
long tile = 0L, pencil = 0L, error;
+ int sx = ds->sequence[x];
- if (state->grid[y*w+x])
- tile = state->grid[y*w+x];
+ if (state->grid[sy*w+sx])
+ tile = state->grid[sy*w+sx];
else
- pencil = (long)state->pencil[y*w+x];
+ pencil = (long)state->pencil[sy*w+sx];
- if (state->immutable[y*w+x])
+ if (state->immutable[sy*w+sx])
tile |= DF_IMMUTABLE;
- if (ui->hshow && ui->hx == x && ui->hy == y)
- tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT);
+ if ((ui->drag == 5 && ui->dragnum == sy) ||
+ (ui->drag == 6 && ui->dragnum == sx)) {
+ tile |= DF_HIGHLIGHT;
+ } else if (ui->hshow) {
+ int i = abs(x - ui->ohx);
+ int highlight = 0;
+ if (ui->odn > 1) {
+ /*
+ * When a diagonal multifill selection is shown,
+ * we show it in its original grid position
+ * regardless of in-progress row/col drags. Moving
+ * every square about would be horrible.
+ */
+ if (i >= 0 && i < ui->odn &&
+ x == ui->ohx + i*ui->odx &&
+ y == ui->ohy + i*ui->ody)
+ highlight = 1;
+ } else {
+ /*
+ * For a single square, we move its highlight
+ * around with the drag.
+ */
+ highlight = (ui->hx == sx && ui->hy == sy);
+ }
+ if (highlight)
+ tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT);
+ }
if (flashtime > 0 &&
(flashtime <= FLASH_TIME/3 ||
flashtime >= FLASH_TIME*2/3))
tile |= DF_HIGHLIGHT; /* completion flash */
- error = ds->errtmp[y*w+x];
+ if (y <= 0 || state->dividers[ds->sequence[y-1]] == sy)
+ tile |= DF_DIVIDER_TOP;
+ if (y+1 >= w || state->dividers[sy] == ds->sequence[y+1])
+ tile |= DF_DIVIDER_BOT;
+ if (x <= 0 || state->dividers[ds->sequence[x-1]] == sx)
+ tile |= DF_DIVIDER_LEFT;
+ if (x+1 >= w || state->dividers[sx] == ds->sequence[x+1])
+ tile |= DF_DIVIDER_RIGHT;
+
+ error = ds->errtmp[sy*w+sx];
if (ds->tiles[y*w+x] != tile ||
ds->pencil[y*w+x] != pencil ||
}
}
-static float game_anim_length(game_state *oldstate, game_state *newstate,
- int dir, game_ui *ui)
+static float game_anim_length(const game_state *oldstate,
+ const game_state *newstate, int dir, game_ui *ui)
{
return 0.0F;
}
-static float game_flash_length(game_state *oldstate, game_state *newstate,
- int dir, game_ui *ui)
+static float game_flash_length(const game_state *oldstate,
+ const game_state *newstate, int dir, game_ui *ui)
{
if (!oldstate->completed && newstate->completed &&
!oldstate->cheated && !newstate->cheated)
return 0.0F;
}
-static int game_timing_state(game_state *state, game_ui *ui)
+static int game_status(const game_state *state)
+{
+ return state->completed ? +1 : 0;
+}
+
+static int game_timing_state(const game_state *state, game_ui *ui)
{
if (state->completed)
return FALSE;
return TRUE;
}
-static void game_print_size(game_params *params, float *x, float *y)
+static void game_print_size(const game_params *params, float *x, float *y)
{
int pw, ph;
*y = ph / 100.0F;
}
-static void game_print(drawing *dr, game_state *state, int tilesize)
+static void game_print(drawing *dr, const game_state *state, int tilesize)
{
int w = state->par.w;
int ink = print_mono_colour(dr, 0);
for (x = 0; x < w; x++) {
char str[2];
str[1] = '\0';
- str[0] = TOCHAR(x+1);
+ str[0] = TOCHAR(x+1, state->par.id);
draw_text(dr, BORDER+LEGEND + x*TILESIZE + TILESIZE/2,
BORDER + TILESIZE/2,
FONT_VARIABLE, TILESIZE/2,
if (state->grid[y*w+x]) {
char str[2];
str[1] = '\0';
- str[0] = TOCHAR(state->grid[y*w+x]);
+ str[0] = TOCHAR(state->grid[y*w+x], state->par.id);
draw_text(dr, BORDER+LEGEND + x*TILESIZE + TILESIZE/2,
BORDER+LEGEND + y*TILESIZE + TILESIZE/2,
FONT_VARIABLE, TILESIZE/2,
const struct game thegame = {
"Group", NULL, NULL,
default_params,
- game_fetch_preset,
+ game_fetch_preset, NULL,
decode_params,
encode_params,
free_params,
game_redraw,
game_anim_length,
game_flash_length,
+ game_status,
TRUE, FALSE, game_print_size, game_print,
FALSE, /* wants_statusbar */
FALSE, game_timing_state,
{
game_params *p;
game_state *s;
- char *id = NULL, *desc, *err;
+ char *id = NULL, *desc;
+ const char *err;
digit *grid;
int grade = FALSE;
int ret, diff, really_show_working = FALSE;
solver_show_working = FALSE;
for (diff = 0; diff < DIFFCOUNT; diff++) {
memcpy(grid, s->grid, p->w * p->w);
- ret = solver(p->w, grid, diff);
+ ret = solver(&s->par, grid, diff);
if (ret <= diff)
break;
}
} else {
solver_show_working = really_show_working;
memcpy(grid, s->grid, p->w * p->w);
- ret = solver(p->w, grid, diff);
+ ret = solver(&s->par, grid, diff);
if (ret != diff)
printf("Puzzle is inconsistent\n");
else {