Stop the analysis pass in Loopy's redraw routine from being
[sgt/puzzles] / unfinished / group.c
index 142654c..1c2c02e 100644 (file)
  *      (e.g. a group of order divisible by 5 must contain an
  *      element of order 5), but I think in fact this is probably
  *      silly.
- *
- *  - a mode which shuffles the identity element into the mix
- *    instead of keeping it clearly shown for you?
- *     * shuffle more fully during table generation
- *     * start clue removal by clearing the identity row and column
- *      completely, or else it'll be totally obvious where it is
- *     * have to print the group elements outside the grid
- *     * new_ui should start the cursor at 0,0 not 1,1, and cursor
- *      should not be constrained to x,y >= 1
- *     * get rid of the COL_IDENTITY highlights
- *     * will we need more checks in check_errors?
  */
 
 #include <stdio.h>
@@ -68,7 +57,6 @@ static char const group_diffchars[] = DIFFLIST(ENCODE);
 
 enum {
     COL_BACKGROUND,
-    COL_IDENTITY,
     COL_GRID,
     COL_USER,
     COL_HIGHLIGHT,
@@ -77,14 +65,19 @@ enum {
     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;
+    int w, diff, id;
 };
 
 struct game_state {
@@ -93,6 +86,23 @@ struct game_state {
     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)
@@ -101,13 +111,19 @@ static game_params *default_params(void)
 
     ret->w = 6;
     ret->diff = DIFF_NORMAL;
+    ret->id = TRUE;
 
     return ret;
 }
 
 const static struct game_params group_presets[] = {
-    {  4, DIFF_NORMAL         },
-    {  6, DIFF_NORMAL         },
+    {  6, DIFF_NORMAL, TRUE },
+    {  6, DIFF_NORMAL, FALSE },
+    {  8, DIFF_NORMAL, TRUE },
+    {  8, DIFF_NORMAL, FALSE },
+    {  8, DIFF_HARD, TRUE },
+    {  8, DIFF_HARD, FALSE },
+    { 12, DIFF_NORMAL, TRUE },
 };
 
 static int game_fetch_preset(int i, char **name, game_params **params)
@@ -121,7 +137,8 @@ static int game_fetch_preset(int i, char **name, game_params **params)
     ret = snew(game_params);
     *ret = group_presets[i]; /* structure copy */
 
-    sprintf(buf, "%dx%d %s", ret->w, ret->w, group_diffnames[ret->diff]);
+    sprintf(buf, "%dx%d %s%s", ret->w, ret->w, group_diffnames[ret->diff],
+           ret->id ? "" : ", identity hidden");
 
     *name = dupstr(buf);
     *params = ret;
@@ -146,18 +163,28 @@ static void decode_params(game_params *params, char const *string)
 
     params->w = atoi(p);
     while (*p && isdigit((unsigned char)*p)) p++;
-
-    if (*p == 'd') {
-        int i;
-        p++;
-        params->diff = DIFFCOUNT+1; /* ...which is invalid */
-        if (*p) {
-            for (i = 0; i < DIFFCOUNT; i++) {
-                if (*p == group_diffchars[i])
-                    params->diff = i;
-            }
-            p++;
-        }
+    params->diff = DIFF_NORMAL;
+    params->id = TRUE;
+
+    while (*p) {
+       if (*p == 'd') {
+           int i;
+           p++;
+           params->diff = DIFFCOUNT+1; /* ...which is invalid */
+           if (*p) {
+               for (i = 0; i < DIFFCOUNT; i++) {
+                   if (*p == group_diffchars[i])
+                       params->diff = i;
+               }
+               p++;
+           }
+       } else if (*p == 'i') {
+           params->id = FALSE;
+           p++;
+       } else {
+           /* unrecognised character */
+           p++;
+       }
     }
 }
 
@@ -168,6 +195,8 @@ static char *encode_params(game_params *params, int full)
     sprintf(ret, "%d", params->w);
     if (full)
         sprintf(ret + strlen(ret), "d%c", group_diffchars[params->diff]);
+    if (!params->id)
+        sprintf(ret + strlen(ret), "i");
 
     return dupstr(ret);
 }
@@ -177,7 +206,7 @@ static config_item *game_configure(game_params *params)
     config_item *ret;
     char buf[80];
 
-    ret = snewn(3, config_item);
+    ret = snewn(4, config_item);
 
     ret[0].name = "Grid size";
     ret[0].type = C_STRING;
@@ -190,10 +219,15 @@ static config_item *game_configure(game_params *params)
     ret[1].sval = DIFFCONFIG;
     ret[1].ival = params->diff;
 
-    ret[2].name = NULL;
-    ret[2].type = C_END;
+    ret[2].name = "Show identity";
+    ret[2].type = C_BOOLEAN;
     ret[2].sval = NULL;
-    ret[2].ival = 0;
+    ret[2].ival = params->id;
+
+    ret[3].name = NULL;
+    ret[3].type = C_END;
+    ret[3].sval = NULL;
+    ret[3].ival = 0;
 
     return ret;
 }
@@ -204,16 +238,38 @@ static game_params *custom_params(config_item *cfg)
 
     ret->w = atoi(cfg[0].sval);
     ret->diff = cfg[1].ival;
+    ret->id = cfg[2].ival;
 
     return ret;
 }
 
 static char *validate_params(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;
 }
 
@@ -224,6 +280,9 @@ static char *validate_params(game_params *params, int full)
 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;
 
@@ -244,13 +303,14 @@ static int solver_normal(struct latin_solver *solver, void *vctx)
                    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]) {
@@ -271,13 +331,14 @@ static int solver_normal(struct latin_solver *solver, void *vctx)
                    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]) {
@@ -300,14 +361,32 @@ static int solver_normal(struct latin_solver *solver, void *vctx)
 #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(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;
 }
@@ -367,187 +446,151 @@ struct groups {
 
 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 ----- */
@@ -574,10 +617,12 @@ static char *new_game_desc(game_params *params, random_state *rs,
      * 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
 
@@ -585,12 +630,14 @@ 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 < 5 || ((w == 6 || w == 8) && params->id)) && diff == DIFF_EXTREME)
+       diff--;
+    if ((w < 6 || (w == 6 && params->id)) && diff == DIFF_HARD)
        diff--;
-    if (w <= 6 && diff == DIFF_HARD)
+    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);
@@ -631,41 +678,61 @@ done
                 * 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;
                }
            }
        }
        /* That's got the canonical table. Now shuffle it. */
        for (i = 0; i < w; i++)
-           grid[i] = i+1;
-       shuffle(grid+1, w-1, sizeof(*grid), rs);
-       for (i = 1; i < w; i++)
-           for (j = 0; j < w; j++)
-               grid[(grid[i]-1)*w+(grid[j]-1)] = grid[soln[i*w+j]-1];
+           soln2[i] = i;
+       if (params->id)                /* do we shuffle in the identity? */
+           shuffle(soln2+1, w-1, sizeof(*soln2), rs);
+       else
+           shuffle(soln2, w, sizeof(*soln2), rs);
        for (i = 0; i < w; i++)
-           grid[i] = i+1;
+           for (j = 0; j < w; j++)
+               grid[(soln2[i])*w+(soln2[j])] = soln2[soln[i*w+j]-1]+1;
 
        /*
         * Remove entries one by one while the puzzle is still
         * soluble at the appropriate difficulty level.
         */
        memcpy(soln, grid, a);
+       if (!params->id) {
+           /*
+            * Start by blanking the entire identity row and column,
+            * and also another row and column so that the player
+            * can't trivially determine which element is the
+            * identity.
+            */
+
+           j = 1 + random_upto(rs, w-1);  /* pick a second row/col to blank */
+           for (i = 0; i < w; i++) {
+               grid[(soln2[0])*w+i] = grid[i*w+(soln2[0])] = 0;
+               grid[(soln2[j])*w+i] = grid[i*w+(soln2[j])] = 0;
+           }
+
+           memcpy(soln2, grid, a);
+           if (solver(params, soln2, diff) > diff)
+               continue;              /* go round again if that didn't work */
+       }
 
        k = 0;
-       for (i = 1; i < w; i++)
-           for (j = 1; j < w; j++)
-               indices[k++] = i*w+j;
-       shuffle(indices, k, sizeof(indices), rs);
+       for (i = (params->id ? 1 : 0); i < w; i++)
+           for (j = (params->id ? 1 : 0); j < w; j++)
+               if (grid[i*w+j])
+                   indices[k++] = i*w+j;
+       shuffle(indices, k, sizeof(*indices), rs);
 
        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;
        }
 
@@ -674,7 +741,7 @@ done
         */
        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 */
        }
 
@@ -698,7 +765,7 @@ done
     *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);
@@ -791,6 +858,12 @@ static game_state *new_game(midend *me, game_params *params, char *desc)
        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++)
@@ -812,9 +885,13 @@ static game_state *dup_game(game_state *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;
@@ -827,6 +904,7 @@ static void free_game(game_state *state)
     sfree(state->grid);
     sfree(state->immutable);
     sfree(state->pencil);
+    sfree(state->sequence);
     sfree(state);
 }
 
@@ -844,7 +922,7 @@ static char *solve_game(game_state *state, game_state *currstate,
     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";
@@ -856,7 +934,7 @@ static char *solve_game(game_state *state, game_state *currstate,
        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';
     }
 
@@ -885,7 +963,7 @@ static char *game_text_format(game_state *state)
             if (d == 0) {
                ch = '.';
            } else {
-               ch = TOCHAR(d);
+               ch = TOCHAR(d, state->par.id);
            }
 
            *p++ = ch;
@@ -927,14 +1005,23 @@ struct game_ui {
      * 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)
 {
     game_ui *ui = snew(game_ui);
 
-    ui->hx = ui->hy = 1;
+    ui->hx = ui->hy = 0;
     ui->hpencil = ui->hshow = ui->hcursor = 0;
+    ui->drag = 0;
 
     return ui;
 }
@@ -972,15 +1059,21 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
 #define PREFERRED_TILESIZE 48
 #define TILESIZE (ds->tilesize)
 #define BORDER (TILESIZE / 2)
+#define LEGEND (TILESIZE)
 #define GRIDEXTRA max((TILESIZE / 32),1)
-#define COORD(x) ((x)*TILESIZE + BORDER)
-#define FROMCOORD(x) (((x)+(TILESIZE-BORDER)) / TILESIZE - 1)
+#define COORD(x) ((x)*TILESIZE + BORDER + LEGEND)
+#define FROMCOORD(x) (((x)+(TILESIZE-BORDER-LEGEND)) / TILESIZE - 1)
 
 #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
@@ -992,10 +1085,12 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
 #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)
@@ -1004,6 +1099,34 @@ static int check_errors(game_state *state, long *errors)
     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;
@@ -1078,8 +1201,20 @@ static int check_errors(game_state *state, long *errors)
     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(game_state *state, game_ui *ui,
+                            const game_drawstate *ds, int x, int y, int button)
 {
     int w = state->par.w;
     int tx, ty;
@@ -1090,45 +1225,90 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
     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 "";
+            }
+        } else if (IS_MOUSE_RELEASE(button)) {
+            if (ui->drag & 4) {
+                ui->drag = 0;          /* end drag */
+                if (state->sequence[ui->dragpos] == ui->dragnum)
+                    return "";         /* 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 "";         /* 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) {
+            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->hshow = !state->immutable[ty*w+tx];
+                    ui->hpencil = 0;
                 }
-            } else {
-                ui->hshow = 0;
+                ui->hcursor = 0;
+                return "";                    /* UI activity occurred */
             }
-            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->hshow = 1;
+                    }
+                } else {
+                    ui->hshow = 0;
+                }
+                ui->hcursor = 0;
+                return "";                    /* UI activity occurred */
+            }
+        } 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 "";
+        } 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 "";
         }
     }
+
     if (IS_CURSOR_MOVE(button)) {
-       ui->hx--; ui->hy--;
-        move_cursor(button, &ui->hx, &ui->hy, w-1, w-1, 0);
-       ui->hx++; ui->hy++;
+        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 "";
     }
@@ -1140,9 +1320,9 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
     }
 
     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);
        if (button == CURSOR_SELECT2 || button == '\b')
            n = 0;
 
@@ -1177,18 +1357,18 @@ static game_state *execute_move(game_state *from, char *move)
 {
     int w = from->par.w, a = w*w;
     game_state *ret;
-    int x, y, i, n;
+    int x, y, i, j, n;
 
     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;
        }
 
@@ -1228,6 +1408,39 @@ static game_state *execute_move(game_state *from, char *move)
                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+1 < w; x++) {
+            int i = ret->sequence[x], j = ret->sequence[x+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 */
 }
@@ -1236,7 +1449,7 @@ static game_state *execute_move(game_state *from, char *move)
  * Drawing routines.
  */
 
-#define SIZE(w) ((w) * TILESIZE + 2*BORDER)
+#define SIZE(w) ((w) * TILESIZE + 2*BORDER + LEGEND)
 
 static void game_compute_size(game_params *params, int tilesize,
                              int *x, int *y)
@@ -1264,10 +1477,6 @@ static float *game_colours(frontend *fe, int *ncolours)
     ret[COL_GRID * 3 + 1] = 0.0F;
     ret[COL_GRID * 3 + 2] = 0.0F;
 
-    ret[COL_IDENTITY * 3 + 0] = 0.89F * ret[COL_BACKGROUND * 3 + 0];
-    ret[COL_IDENTITY * 3 + 1] = 0.89F * ret[COL_BACKGROUND * 3 + 1];
-    ret[COL_IDENTITY * 3 + 2] = 0.89F * ret[COL_BACKGROUND * 3 + 2];
-
     ret[COL_USER * 3 + 0] = 0.0F;
     ret[COL_USER * 3 + 1] = 0.6F * ret[COL_BACKGROUND * 3 + 1];
     ret[COL_USER * 3 + 2] = 0.0F;
@@ -1295,13 +1504,18 @@ static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
     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;
@@ -1313,31 +1527,49 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds)
     sfree(ds->pencil);
     sfree(ds->errors);
     sfree(ds->errtmp);
+    sfree(ds->sequence);
     sfree(ds);
 }
 
-void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
-              long pencil, long error)
+static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
+                     long pencil, long error)
 {
     int w = ds->w /* , a = w*w */;
     int tx, ty, tw, th;
     int cx, cy, cw, ch;
     char str[64];
 
-    tx = BORDER + x * TILESIZE + 1;
-    ty = BORDER + y * TILESIZE + 1;
+    tx = BORDER + LEGEND + x * TILESIZE + 1;
+    ty = BORDER + LEGEND + y * TILESIZE + 1;
 
     cx = tx;
     cy = ty;
     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 :
-             (x == 0 || y == 0) ? COL_IDENTITY : COL_BACKGROUND);
+             (tile & DF_HIGHLIGHT) ? COL_HIGHLIGHT : 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) {
@@ -1354,7 +1586,7 @@ void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
     /* 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 :
@@ -1365,7 +1597,8 @@ void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
            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);
@@ -1375,7 +1608,8 @@ void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
            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);
@@ -1454,7 +1688,7 @@ void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
                    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,
@@ -1474,7 +1708,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                        float animtime, float flashtime)
 {
     int w = state->par.w /*, a = w*w */;
-    int x, y;
+    int x, y, i, j;
 
     if (!ds->started) {
        /*
@@ -1499,19 +1733,57 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
 
     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)
+            if ((ui->drag == 5 && ui->dragnum == sy) ||
+                (ui->drag == 6 && ui->dragnum == sx))
+                tile |= DF_HIGHLIGHT;
+           else if (ui->hshow && ui->hx == sx && ui->hy == sy)
                tile |= (ui->hpencil ? DF_HIGHLIGHT_PENCIL : DF_HIGHLIGHT);
 
             if (flashtime > 0 &&
@@ -1519,7 +1791,16 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                  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 ||
@@ -1548,6 +1829,11 @@ static float game_flash_length(game_state *oldstate, game_state *newstate,
     return 0.0F;
 }
 
+static int game_status(game_state *state)
+{
+    return state->completed ? +1 : 0;
+}
+
 static int game_timing_state(game_state *state, game_ui *ui)
 {
     if (state->completed)
@@ -1571,7 +1857,6 @@ static void game_print(drawing *dr, game_state *state, int tilesize)
 {
     int w = state->par.w;
     int ink = print_mono_colour(dr, 0);
-    int ehighlight = print_grey_colour(dr, 0.90F);
     int x, y;
 
     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
@@ -1579,33 +1864,41 @@ static void game_print(drawing *dr, game_state *state, int tilesize)
     game_set_size(dr, ds, NULL, tilesize);
 
     /*
-     * Highlight the identity row and column.
+     * Border.
      */
-    for (x = 1; x < w; x++)
-       draw_rect(dr, BORDER + x*TILESIZE, BORDER,
-                 TILESIZE, TILESIZE, ehighlight);
-    for (y = 0; y < w; y++)
-       draw_rect(dr, BORDER, BORDER + y*TILESIZE,
-                 TILESIZE, TILESIZE, ehighlight);
+    print_line_width(dr, 3 * TILESIZE / 40);
+    draw_rect_outline(dr, BORDER + LEGEND, BORDER + LEGEND,
+                     w*TILESIZE, w*TILESIZE, ink);
 
     /*
-     * Border.
+     * Legend on table.
      */
-    print_line_width(dr, 3 * TILESIZE / 40);
-    draw_rect_outline(dr, BORDER, BORDER, w*TILESIZE, w*TILESIZE, ink);
+    for (x = 0; x < w; x++) {
+       char str[2];
+       str[1] = '\0';
+       str[0] = TOCHAR(x+1, state->par.id);
+       draw_text(dr, BORDER+LEGEND + x*TILESIZE + TILESIZE/2,
+                 BORDER + TILESIZE/2,
+                 FONT_VARIABLE, TILESIZE/2,
+                 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
+       draw_text(dr, BORDER + TILESIZE/2,
+                 BORDER+LEGEND + x*TILESIZE + TILESIZE/2,
+                 FONT_VARIABLE, TILESIZE/2,
+                 ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
+    }
 
     /*
      * Main grid.
      */
     for (x = 1; x < w; x++) {
        print_line_width(dr, TILESIZE / 40);
-       draw_line(dr, BORDER+x*TILESIZE, BORDER,
-                 BORDER+x*TILESIZE, BORDER+w*TILESIZE, ink);
+       draw_line(dr, BORDER+LEGEND+x*TILESIZE, BORDER+LEGEND,
+                 BORDER+LEGEND+x*TILESIZE, BORDER+LEGEND+w*TILESIZE, ink);
     }
     for (y = 1; y < w; y++) {
        print_line_width(dr, TILESIZE / 40);
-       draw_line(dr, BORDER, BORDER+y*TILESIZE,
-                 BORDER+w*TILESIZE, BORDER+y*TILESIZE, ink);
+       draw_line(dr, BORDER+LEGEND, BORDER+LEGEND+y*TILESIZE,
+                 BORDER+LEGEND+w*TILESIZE, BORDER+LEGEND+y*TILESIZE, ink);
     }
 
     /*
@@ -1616,9 +1909,9 @@ static void game_print(drawing *dr, game_state *state, int tilesize)
            if (state->grid[y*w+x]) {
                char str[2];
                str[1] = '\0';
-               str[0] = TOCHAR(state->grid[y*w+x]);
-               draw_text(dr, BORDER + x*TILESIZE + TILESIZE/2,
-                         BORDER + y*TILESIZE + TILESIZE/2,
+               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,
                          ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
            }
@@ -1659,6 +1952,7 @@ const struct game thegame = {
     game_redraw,
     game_anim_length,
     game_flash_length,
+    game_status,
     TRUE, FALSE, game_print_size, game_print,
     FALSE,                            /* wants_statusbar */
     FALSE, game_timing_state,
@@ -1724,7 +2018,7 @@ int main(int argc, char **argv)
     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;
     }
@@ -1743,7 +2037,7 @@ int main(int argc, char **argv)
        } 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 {