chiark / gitweb /
Net: reference-count the barriers array.
[sgt-puzzles.git] / unfinished / group.c
index d2ff194295655616a83e628e4c7251dd601123df..4a4ad6ce533de4a8cd96bd8a174f395cbc7851a4 100644 (file)
@@ -62,6 +62,7 @@ enum {
     COL_HIGHLIGHT,
     COL_ERROR,
     COL_PENCIL,
+    COL_DIAGONAL,
     NCOLOURS
 };
 
@@ -86,6 +87,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)
@@ -133,7 +151,7 @@ static void free_params(game_params *params)
     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 */
@@ -171,7 +189,7 @@ static void decode_params(game_params *params, char const *string)
     }
 }
 
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
 {
     char ret[80];
 
@@ -184,7 +202,7 @@ static char *encode_params(game_params *params, int full)
     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];
@@ -215,7 +233,7 @@ static config_item *game_configure(game_params *params)
     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);
 
@@ -226,7 +244,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params, int full)
+static char *validate_params(const game_params *params, int full)
 {
     if (params->w < 3 || params->w > 26)
         return "Grid size must be between 3 and 26";
@@ -242,6 +260,17 @@ static char *validate_params(game_params *params, int full)
         */
        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;
 }
 
@@ -333,7 +362,7 @@ 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(game_params *params, digit *grid, int maxdiff)
+static int solver(const game_params *params, digit *grid, int maxdiff)
 {
     int w = params->w;
     int ret;
@@ -567,7 +596,7 @@ static const struct groups groups[] = {
 
 /* ----- 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;
@@ -602,7 +631,6 @@ done
      * _out_, so as to detect exceptions that should be removed as
      * well as those which should be added.
      */
-#if 0
     if (w < 5 && diff == DIFF_UNREASONABLE)
        diff--;
     if ((w < 5 || ((w == 6 || w == 8) && params->id)) && diff == DIFF_EXTREME)
@@ -611,7 +639,6 @@ done
        diff--;
     if ((w < 4 || (w == 4 && params->id)) && diff == DIFF_NORMAL)
        diff--;
-#endif
 
     grid = snewn(a, digit);
     soln = snewn(a, digit);
@@ -784,7 +811,7 @@ static char *validate_grid_desc(const char **pdesc, int range, int area)
     return NULL;
 }
 
-static char *validate_desc(game_params *params, char *desc)
+static char *validate_desc(const game_params *params, const char *desc)
 {
     int w = params->w, a = w*w;
     const char *p = desc;
@@ -792,7 +819,7 @@ static char *validate_desc(game_params *params, char *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 != ',') {
@@ -817,7 +844,8 @@ static char *spec_to_grid(char *desc, digit *grid, int area)
     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);
@@ -832,6 +860,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++)
@@ -843,7 +877,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc)
     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);
@@ -853,9 +887,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;
@@ -868,11 +906,12 @@ static void free_game(game_state *state)
     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, char **error)
 {
     int w = state->par.w, a = w*w;
     int i, ret;
@@ -905,12 +944,12 @@ static char *solve_game(game_state *state, game_state *currstate,
     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;
@@ -945,10 +984,23 @@ static char *game_text_format(game_state *state)
 
 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.
@@ -968,14 +1020,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)
+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;
 }
@@ -985,17 +1046,17 @@ static void free_ui(game_ui *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;
     /*
@@ -1008,6 +1069,40 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
         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
@@ -1020,9 +1115,14 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
 
 #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
@@ -1037,16 +1137,45 @@ 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;
@@ -1121,8 +1250,21 @@ 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(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;
@@ -1133,43 +1275,111 @@ 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) {
+            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 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->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 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 "";
+        }
+    } 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 "";
     }
+
     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 "";
     }
@@ -1184,28 +1394,52 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
        ((ISCHAR(button) && FROMCHAR(button, state->par.id) <= w) ||
         button == CURSOR_SELECT2 || button == '\b')) {
        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')
@@ -1214,11 +1448,11 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
     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);
@@ -1240,21 +1474,40 @@ static game_state *execute_move(game_state *from, char *move)
 
        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') {
        /*
@@ -1269,6 +1522,40 @@ 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 < 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 */
 }
@@ -1279,8 +1566,8 @@ static game_state *execute_move(game_state *from, char *move)
 
 #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;
@@ -1290,7 +1577,7 @@ static void game_compute_size(game_params *params, int tilesize,
 }
 
 static void game_set_size(drawing *dr, game_drawstate *ds,
-                         game_params *params, int tilesize)
+                          const game_params *params, int tilesize)
 {
     ds->tilesize = tilesize;
 }
@@ -1321,11 +1608,15 @@ static float *game_colours(frontend *fe, int *ncolours)
     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);
@@ -1336,10 +1627,14 @@ static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
     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;
@@ -1351,6 +1646,7 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds)
     sfree(ds->pencil);
     sfree(ds->errors);
     sfree(ds->errtmp);
+    sfree(ds->sequence);
     sfree(ds);
 }
 
@@ -1370,11 +1666,30 @@ static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
     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) {
@@ -1508,12 +1823,13 @@ static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, long tile,
     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) {
        /*
@@ -1531,21 +1847,6 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                  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, ds->par.id);
-           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;
@@ -1553,27 +1854,96 @@ 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)
-               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 ||
@@ -1587,14 +1957,14 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
     }
 }
 
-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)
@@ -1602,14 +1972,19 @@ static float game_flash_length(game_state *oldstate, game_state *newstate,
     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;
 
@@ -1621,7 +1996,7 @@ static void game_print_size(game_params *params, float *x, float *y)
     *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);
@@ -1692,7 +2067,7 @@ static void game_print(drawing *dr, game_state *state, int tilesize)
 const struct game thegame = {
     "Group", NULL, NULL,
     default_params,
-    game_fetch_preset,
+    game_fetch_preset, NULL,
     decode_params,
     encode_params,
     free_params,
@@ -1720,6 +2095,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,