Stop the analysis pass in Loopy's redraw routine from being
[sgt/puzzles] / blackbox.c
index 005ba3d..1546d91 100644 (file)
@@ -32,7 +32,7 @@ enum {
     COL_TEXT, COL_FLASHTEXT,
     COL_HIGHLIGHT, COL_LOWLIGHT, COL_GRID,
     COL_BALL, COL_WRONG, COL_BUTTON,
-    COL_LASER, COL_DIMLASER,
+    COL_CURSOR,
     NCOLOURS
 };
 
@@ -191,15 +191,15 @@ static game_params *custom_params(config_item *cfg)
 static char *validate_params(game_params *params, int full)
 {
     if (params->w < 2 || params->h < 2)
-        return "Grid must be at least 2 wide and 2 high";
+        return "Width and height must both be at least two";
     /* next one is just for ease of coding stuff into 'char'
      * types, and could be worked around if required. */
     if (params->w > 255 || params->h > 255)
-        return "Grid must be < 255 in each direction";
+        return "Widths and heights greater than 255 are not supported";
     if (params->minballs > params->maxballs)
-        return "Min. balls must be <= max. balls";
+        return "Minimum number of balls may not be greater than maximum";
     if (params->minballs >= params->w * params->h)
-        return "Too many balls for grid";
+        return "Too many balls to fit in grid";
     return NULL;
 }
 
@@ -218,7 +218,7 @@ static char *new_game_desc(game_params *params, random_state *rs,
     unsigned char *bmp;
 
     if (params->maxballs > params->minballs)
-        nballs += random_upto(rs, params->maxballs-params->minballs);
+        nballs += random_upto(rs, params->maxballs - params->minballs + 1);
 
     grid = snewn(params->w*params->h, char);
     memset(grid, 0, params->w * params->h * sizeof(char));
@@ -231,11 +231,14 @@ static char *new_game_desc(game_params *params, random_state *rs,
 
     for (i = 0; i < nballs; i++) {
         int x, y;
-newball:
-        x = random_upto(rs, params->w);
-        y = random_upto(rs, params->h);
-        if (grid[y*params->h + x]) goto newball;
-        grid[y*params->h + x] = 1;
+
+        do {
+            x = random_upto(rs, params->w);
+            y = random_upto(rs, params->h);
+        } while (grid[y*params->w + x]);
+
+        grid[y*params->w + x] = 1;
+
         bmp[(i+1)*2 + 0] = x;
         bmp[(i+1)*2 + 1] = y;
     }
@@ -270,7 +273,7 @@ static char *validate_desc(game_params *params, char *desc)
     /* check each ball will fit on that grid */
     for (i = 0; i < nballs; i++) {
         int x = bmp[(i+1)*2 + 0], y = bmp[(i+1)*2 + 1];
-        if (x < 0 || y < 0 || x > params->w || y > params->h)
+        if (x < 0 || y < 0 || x >= params->w || y >= params->h)
             goto done;
     }
     ret = NULL;
@@ -284,13 +287,15 @@ done:
 #define BALL_GUESS      0x02
 #define BALL_LOCK       0x04
 
-#define LASER_FLAGMASK  0xf800
-#define LASER_OMITTED   0x0800
-#define LASER_REFLECT   0x1000
-#define LASER_HIT       0x2000
-#define LASER_WRONG     0x4000
-#define LASER_FLASHED   0x8000
-#define LASER_EMPTY     (~0)
+#define LASER_FLAGMASK  0x1f800
+#define LASER_OMITTED    0x0800
+#define LASER_REFLECT    0x1000
+#define LASER_HIT        0x2000
+#define LASER_WRONG      0x4000
+#define LASER_FLASHED    0x8000
+#define LASER_EMPTY      (~0)
+
+#define FLAG_CURSOR     0x10000 /* needs to be disjoint from both sets */
 
 struct game_state {
     int w, h, minballs, maxballs, nballs, nlasers;
@@ -298,17 +303,19 @@ struct game_state {
     unsigned int *exits; /* one per laser */
     int done;           /* user has finished placing his own balls. */
     int laserno;        /* number of next laser to be fired. */
-    int nguesses, reveal, nright, nwrong, nmissed;
+    int nguesses, reveal, justwrong, nright, nwrong, nmissed;
 };
 
-#define GRID(s,x,y) ((s)->grid[(y)*((s)->h+2) + (x)])
+#define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)])
+
+#define RANGECHECK(s,x) ((x) >= 0 && (x) <= (s)->nlasers)
 
 /* specify numbers because they must match array indexes. */
 enum { DIR_UP = 0, DIR_RIGHT = 1, DIR_DOWN = 2, DIR_LEFT = 3 };
 
-struct _off { int x, y; };
+struct offset { int x, y; };
 
-static const struct _off offsets[] = {
+static const struct offset offsets[] = {
     {  0, -1 }, /* up */
     {  1,  0 }, /* right */
     {  0,  1 }, /* down */
@@ -365,7 +372,7 @@ static int grid2range(game_state *state, int x, int y, int *rangeno)
     int ret, x1 = state->w+1, y1 = state->h+1;
 
     if (x > 0 && x < x1 && y > 0 && y < y1) return 0; /* in arena */
-    if (x < 0 || x > y1 || y < 0 || y > y1) return 0; /* outside grid */
+    if (x < 0 || x > x1 || y < 0 || y > y1) return 0; /* outside grid */
 
     if ((x == 0 || x == x1) && (y == 0 || y == y1))
         return 0; /* one of 4 corners */
@@ -384,7 +391,7 @@ static int grid2range(game_state *state, int x, int y, int *rangeno)
     return 1;
 }
 
-static game_state *new_game(midend_data *me, game_params *params, char *desc)
+static game_state *new_game(midend *me, game_params *params, char *desc)
 {
     game_state *state = snew(game_state);
     int dlen = strlen(desc), i;
@@ -411,7 +418,7 @@ static game_state *new_game(midend_data *me, game_params *params, char *desc)
     }
     sfree(bmp);
 
-    state->done = state->nguesses = state->reveal =
+    state->done = state->nguesses = state->reveal = state->justwrong =
         state->nright = state->nwrong = state->nmissed = 0;
     state->laserno = 1;
 
@@ -437,6 +444,7 @@ static game_state *dup_game(game_state *state)
     XFER(laserno);
     XFER(nguesses);
     XFER(reveal);
+    XFER(justwrong);
     XFER(nright); XFER(nwrong); XFER(nmissed);
 
     return ret;
@@ -457,6 +465,11 @@ static char *solve_game(game_state *state, game_state *currstate,
     return dupstr("S");
 }
 
+static int game_can_format_as_text_now(game_params *params)
+{
+    return TRUE;
+}
+
 static char *game_text_format(game_state *state)
 {
     return NULL;
@@ -464,12 +477,23 @@ static char *game_text_format(game_state *state)
 
 struct game_ui {
     int flash_laserno;
+    int errors, newmove;
+    int cur_x, cur_y, cur_visible;
+    int flash_laser; /* 0 = never, 1 = always, 2 = if anim. */
 };
 
 static game_ui *new_ui(game_state *state)
 {
-    game_ui *ui = snew(struct game_ui);
+    game_ui *ui = snew(game_ui);
     ui->flash_laserno = LASER_EMPTY;
+    ui->errors = 0;
+    ui->newmove = FALSE;
+
+    ui->cur_x = ui->cur_y = 1;
+    ui->cur_visible = 0;
+
+    ui->flash_laser = 0;
+
     return ui;
 }
 
@@ -480,20 +504,33 @@ static void free_ui(game_ui *ui)
 
 static char *encode_ui(game_ui *ui)
 {
-    return NULL;
+    char buf[80];
+    /*
+     * The error counter needs preserving across a serialisation.
+     */
+    sprintf(buf, "E%d", ui->errors);
+    return dupstr(buf);
 }
 
 static void decode_ui(game_ui *ui, char *encoding)
 {
+    sscanf(encoding, "E%d", &ui->errors);
 }
 
 static void game_changed_state(game_ui *ui, game_state *oldstate,
                                game_state *newstate)
 {
+    /*
+     * If we've encountered a `justwrong' state as a result of
+     * actually making a move, increment the ui error counter.
+     */
+    if (newstate->justwrong && ui->newmove)
+       ui->errors++;
+    ui->newmove = FALSE;
 }
 
 #define OFFSET(gx,gy,o) do {                                    \
-    int off = (o); while (off < 0) { off += 4; }  off %= 4;     \
+    int off = (4 + (o) % 4) % 4;                                \
     (gx) += offsets[off].x;                                     \
     (gy) += offsets[off].y;                                     \
 } while(0)
@@ -518,7 +555,7 @@ static int isball(game_state *state, int gx, int gy, int direction, int lookwher
     debug(("isball, new (%d, %d)\n", gx, gy));
 
     /* if we're off the grid (into the firing range) there's never a ball. */
-    if (gx < 1 || gy < 1 || gx > state->h || gy > state->w)
+    if (gx < 1 || gy < 1 || gx > state->w || gy > state->h)
         return 0;
 
     if (GRID(state, gx,gy) & BALL_CORRECT)
@@ -527,11 +564,12 @@ static int isball(game_state *state, int gx, int gy, int direction, int lookwher
     return 0;
 }
 
-static void fire_laser(game_state *state, int x, int y, int direction)
+static int fire_laser_internal(game_state *state, int x, int y, int direction)
 {
-    int xstart = x, ystart = y, unused, lno;
+    int unused, lno, tmp;
 
-    assert(grid2range(state, x, y, &lno));
+    tmp = grid2range(state, x, y, &lno);
+    assert(tmp);
 
     /* deal with strange initial reflection rules (that stop
      * you turning down the laser range) */
@@ -540,17 +578,13 @@ static void fire_laser(game_state *state, int x, int y, int direction)
      * I can't find anywhere that gives me a definite algorithm for this. */
     if (isball(state, x, y, direction, LOOK_FORWARD)) {
         debug(("Instant hit at (%d, %d)\n", x, y));
-        GRID(state, x, y) = LASER_HIT;
-        state->exits[lno] = LASER_HIT;
-        return;
+       return LASER_HIT;              /* hit */
     }
 
     if (isball(state, x, y, direction, LOOK_LEFT) ||
         isball(state, x, y, direction, LOOK_RIGHT)) {
         debug(("Instant reflection at (%d, %d)\n", x, y));
-        GRID(state, x, y) = LASER_REFLECT;
-        state->exits[lno] = LASER_REFLECT;
-        return;
+       return LASER_REFLECT;          /* reflection */
     }
     /* move us onto the grid. */
     OFFSET(x, y, direction);
@@ -559,34 +593,20 @@ static void fire_laser(game_state *state, int x, int y, int direction)
         debug(("fire_laser: looping at (%d, %d) pointing %s\n",
                x, y, dirstrs[direction]));
         if (grid2range(state, x, y, &unused)) {
-            int newno = state->laserno++, exitno;
-            debug(("Back on range; (%d, %d) --> (%d, %d)\n",
-                   xstart, ystart, x, y));
-            /* We're back out of the grid; the move is complete. */
-            if (xstart == x && ystart == y) {
-                GRID(state, x, y) = LASER_REFLECT;
-                state->exits[lno] = LASER_REFLECT;
-            } else {
-                /* it wasn't a reflection */
-                GRID(state, xstart, ystart) = newno;
-                GRID(state, x, y) = newno;
+            int exitno;
 
-                assert(grid2range(state, x, y, &exitno));
-                state->exits[lno] = exitno;
-                state->exits[exitno] = lno;
-            }
-            return;
+           tmp = grid2range(state, x, y, &exitno);
+           assert(tmp);
+
+           return (lno == exitno ? LASER_REFLECT : exitno);
         }
         /* paranoia. This obviously should never happen */
         assert(!(GRID(state, x, y) & BALL_CORRECT));
 
         if (isball(state, x, y, direction, LOOK_FORWARD)) {
             /* we're facing a ball; send back a reflection. */
-            GRID(state, xstart, ystart) = LASER_HIT;
-            state->exits[lno] = LASER_HIT;
-            debug(("Ball ahead of (%d, %d); HIT at (%d, %d), new grid 0x%x\n",
-                   x, y, xstart, ystart, GRID(state, xstart, ystart)));
-            return;
+            debug(("Ball ahead of (%d, %d)", x, y));
+            return LASER_HIT;         /* hit */
         }
 
         if (isball(state, x, y, direction, LOOK_LEFT)) {
@@ -607,23 +627,151 @@ static void fire_laser(game_state *state, int x, int y, int direction)
     }
 }
 
+static int laser_exit(game_state *state, int entryno)
+{
+    int tmp, x, y, direction;
+
+    tmp = range2grid(state, entryno, &x, &y, &direction);
+    assert(tmp);
+
+    return fire_laser_internal(state, x, y, direction);
+}
+
+static void fire_laser(game_state *state, int entryno)
+{
+    int tmp, exitno, x, y, direction;
+
+    tmp = range2grid(state, entryno, &x, &y, &direction);
+    assert(tmp);
+
+    exitno = fire_laser_internal(state, x, y, direction);
+
+    if (exitno == LASER_HIT || exitno == LASER_REFLECT) {
+       GRID(state, x, y) = state->exits[entryno] = exitno;
+    } else {
+       int newno = state->laserno++;
+       int xend, yend, unused;
+       tmp = range2grid(state, exitno, &xend, &yend, &unused);
+       assert(tmp);
+       GRID(state, x, y) = GRID(state, xend, yend) = newno;
+       state->exits[entryno] = exitno;
+       state->exits[exitno] = entryno;
+    }
+}
+
 /* Checks that the guessed balls in the state match up with the real balls
  * for all possible lasers (i.e. not just the ones that the player might
  * have already guessed). This is required because any layout with >4 balls
  * might have multiple valid solutions. Returns non-zero for a 'correct'
  * (i.e. consistent) layout. */
-static int check_guesses(game_state *state)
+static int check_guesses(game_state *state, int cagey)
 {
     game_state *solution, *guesses;
-    int i, x, y, dir, unused;
+    int i, x, y, n, unused, tmp;
     int ret = 0;
 
+    if (cagey) {
+       /*
+        * First, check that each laser the player has already
+        * fired is consistent with the layout. If not, show them
+        * one error they've made and reveal no further
+        * information.
+        *
+        * Failing that, check to see whether the player would have
+        * been able to fire any laser which distinguished the real
+        * solution from their guess. If so, show them one such
+        * laser and reveal no further information.
+        */
+       guesses = dup_game(state);
+       /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
+       for (x = 1; x <= state->w; x++) {
+           for (y = 1; y <= state->h; y++) {
+               GRID(guesses, x, y) &= ~BALL_CORRECT;
+               if (GRID(guesses, x, y) & BALL_GUESS)
+                   GRID(guesses, x, y) |= BALL_CORRECT;
+           }
+       }
+       n = 0;
+       for (i = 0; i < guesses->nlasers; i++) {
+           if (guesses->exits[i] != LASER_EMPTY &&
+               guesses->exits[i] != laser_exit(guesses, i))
+               n++;
+       }
+       if (n) {
+           /*
+            * At least one of the player's existing lasers
+            * contradicts their ball placement. Pick a random one,
+            * highlight it, and return.
+            *
+            * A temporary random state is created from the current
+            * grid, so that repeating the same marking will give
+            * the same answer instead of a different one.
+            */
+           random_state *rs = random_new((char *)guesses->grid,
+                                         (state->w+2)*(state->h+2) *
+                                         sizeof(unsigned int));
+           n = random_upto(rs, n);
+           random_free(rs);
+           for (i = 0; i < guesses->nlasers; i++) {
+               if (guesses->exits[i] != LASER_EMPTY &&
+                   guesses->exits[i] != laser_exit(guesses, i) &&
+                   n-- == 0) {
+                   state->exits[i] |= LASER_WRONG;
+                   tmp = laser_exit(state, i);
+                   if (RANGECHECK(state, tmp))
+                       state->exits[tmp] |= LASER_WRONG;
+                   state->justwrong = TRUE;
+                   free_game(guesses);
+                   return 0;
+               }
+           }
+       }
+       n = 0;
+       for (i = 0; i < guesses->nlasers; i++) {
+           if (guesses->exits[i] == LASER_EMPTY &&
+               laser_exit(state, i) != laser_exit(guesses, i))
+               n++;
+       }
+       if (n) {
+           /*
+            * At least one of the player's unfired lasers would
+            * demonstrate their ball placement to be wrong. Pick a
+            * random one, highlight it, and return.
+            *
+            * A temporary random state is created from the current
+            * grid, so that repeating the same marking will give
+            * the same answer instead of a different one.
+            */
+           random_state *rs = random_new((char *)guesses->grid,
+                                         (state->w+2)*(state->h+2) *
+                                         sizeof(unsigned int));
+           n = random_upto(rs, n);
+           random_free(rs);
+           for (i = 0; i < guesses->nlasers; i++) {
+               if (guesses->exits[i] == LASER_EMPTY &&
+                   laser_exit(state, i) != laser_exit(guesses, i) &&
+                   n-- == 0) {
+                   fire_laser(state, i);
+                   state->exits[i] |= LASER_OMITTED;
+                   tmp = laser_exit(state, i);
+                   if (RANGECHECK(state, tmp))
+                       state->exits[tmp] |= LASER_OMITTED;
+                   state->justwrong = TRUE;
+                   free_game(guesses);
+                   return 0;
+               }
+           }
+       }
+       free_game(guesses);
+    }
+
     /* duplicate the state (to solution) */
     solution = dup_game(state);
 
     /* clear out the lasers of solution */
     for (i = 0; i < solution->nlasers; i++) {
-        assert(range2grid(solution, i, &x, &y, &unused));
+        tmp = range2grid(solution, i, &x, &y, &unused);
+        assert(tmp);
         GRID(solution, x, y) = 0;
         solution->exits[i] = LASER_EMPTY;
     }
@@ -644,17 +792,17 @@ static int check_guesses(game_state *state)
      * If one has been fired (or received a hit) and another hasn't, we know
      * the ball layouts didn't match and can short-circuit return. */
     for (i = 0; i < solution->nlasers; i++) {
-        assert(range2grid(solution, i, &x, &y, &dir));
         if (solution->exits[i] == LASER_EMPTY)
-            fire_laser(solution, x, y, dir);
+            fire_laser(solution, i);
         if (guesses->exits[i] == LASER_EMPTY)
-            fire_laser(guesses, x, y, dir);
+            fire_laser(guesses, i);
     }
 
     /* check each game_state's laser against the other; if any differ, return 0 */
     ret = 1;
     for (i = 0; i < solution->nlasers; i++) {
-        assert(range2grid(solution, i, &x, &y, &unused));
+        tmp = range2grid(solution, i, &x, &y, &unused);
+        assert(tmp);
 
         if (solution->exits[i] != guesses->exits[i]) {
             /* If the original state didn't have this shot fired,
@@ -668,7 +816,8 @@ static int check_guesses(game_state *state)
                 else {
                     /* add a new shot, incrementing state's laser count. */
                     int ex, ey, newno = state->laserno++;
-                    assert(range2grid(state, state->exits[i], &ex, &ey, &unused));
+                    tmp = range2grid(state, state->exits[i], &ex, &ey, &unused);
+                    assert(tmp);
                     GRID(state, x, y) = newno;
                     GRID(state, ex, ey) = newno;
                 }
@@ -679,7 +828,9 @@ static int check_guesses(game_state *state)
             ret = 0;
         }
     }
-    if (ret == 0) goto done;
+    if (ret == 0 ||
+       state->nguesses < state->minballs ||
+       state->nguesses > state->maxballs) goto done;
 
     /* fix up original state so the 'correct' balls end up matching the guesses,
      * as we've just proved that they were equivalent. */
@@ -708,6 +859,7 @@ done:
     }
     free_game(solution);
     free_game(guesses);
+    state->reveal = 1;
     return ret;
 }
 
@@ -716,24 +868,64 @@ done:
 #define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2))
 #define FROMDRAW(x) (((x) - (TILE_SIZE / 2)) / TILE_SIZE)
 
+#define CAN_REVEAL(state) ((state)->nguesses >= (state)->minballs && \
+                          (state)->nguesses <= (state)->maxballs && \
+                          !(state)->reveal && !(state)->justwrong)
+
 struct game_drawstate {
     int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
     unsigned int *grid;          /* as the game_state grid */
-    int started, canreveal, reveal;
-    int flash_laserno;
+    int started, reveal;
+    int flash_laserno, isflash;
 };
 
-static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
+static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds,
                            int x, int y, int button)
 {
-    int gx = -1, gy = -1, rangeno = -1;
+    int gx = -1, gy = -1, rangeno = -1, wouldflash = 0;
     enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL,
            TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE;
     char buf[80], *nullret = NULL;
 
+    if (IS_CURSOR_MOVE(button)) {
+        int cx = ui->cur_x, cy = ui->cur_y;
+
+        move_cursor(button, &cx, &cy, state->w+2, state->h+2, 0);
+        if ((cx == 0 && cy == 0 && !CAN_REVEAL(state)) ||
+            (cx == 0 && cy == state->h+1) ||
+            (cx == state->w+1 && cy == 0) ||
+            (cx == state->w+1 && cy == state->h+1))
+            return NULL; /* disallow moving cursor to corners. */
+        ui->cur_x = cx;
+        ui->cur_y = cy;
+        ui->cur_visible = 1;
+        return "";
+    }
+
     if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
         gx = FROMDRAW(x);
         gy = FROMDRAW(y);
+        ui->cur_visible = 0;
+        wouldflash = 1;
+    } else if (button == LEFT_RELEASE) {
+        ui->flash_laser = 0;
+        return "";
+    } else if (IS_CURSOR_SELECT(button)) {
+        if (ui->cur_visible) {
+            gx = ui->cur_x;
+            gy = ui->cur_y;
+            ui->flash_laser = 0;
+            wouldflash = 2;
+        } else {
+            ui->cur_visible = 1;
+            return "";
+        }
+        /* Fix up 'button' for the below logic. */
+        if (button == CURSOR_SELECT2) button = RIGHT_BUTTON;
+        else button = LEFT_BUTTON;
+    }
+
+    if (gx != -1 && gy != -1) {
         if (gx == 0 && gy == 0 && button == LEFT_BUTTON)
             action = REVEAL;
         if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
@@ -751,9 +943,6 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
             else
                 action = TOGGLE_ROW_LOCK;    /* and use gy */
         }
-    } else if (button == LEFT_RELEASE) {
-        ui->flash_laserno = LASER_EMPTY;
-        return "";
     }
 
     switch (action) {
@@ -777,6 +966,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
        if (state->reveal && state->exits[rangeno] == LASER_EMPTY)
            return nullret;
         ui->flash_laserno = rangeno;
+        ui->flash_laser = wouldflash;
         nullret = "";
         if (state->exits[rangeno] != LASER_EMPTY)
             return "";
@@ -784,7 +974,8 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
         break;
 
     case REVEAL:
-        if (!ds->canreveal) return nullret;
+        if (!CAN_REVEAL(state)) return nullret;
+        if (ui->cur_visible == 1) ui->cur_x = ui->cur_y = 1;
         sprintf(buf, "R");
         break;
 
@@ -792,21 +983,30 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
         return nullret;
     }
     if (state->reveal) return nullret;
+    ui->newmove = TRUE;
     return dupstr(buf);
 }
 
 static game_state *execute_move(game_state *from, char *move)
 {
     game_state *ret = dup_game(from);
-    int gx = -1, gy = -1, rangeno = -1, direction;
+    int gx = -1, gy = -1, rangeno = -1;
+
+    if (ret->justwrong) {
+       int i;
+       ret->justwrong = FALSE;
+       for (i = 0; i < ret->nlasers; i++)
+           if (ret->exits[i] != LASER_EMPTY)
+               ret->exits[i] &= ~(LASER_OMITTED | LASER_WRONG);
+    }
 
     if (!strcmp(move, "S")) {
-        ret->reveal = 1;
+        check_guesses(ret, FALSE);
         return ret;
     }
 
     if (from->reveal) goto badmove;
-    if (strlen(move) < 1) goto badmove;
+    if (!*move) goto badmove;
 
     switch (move[0]) {
     case 'T':
@@ -826,17 +1026,16 @@ static game_state *execute_move(game_state *from, char *move)
         sscanf(move+1, "%d", &rangeno);
         if (ret->exits[rangeno] != LASER_EMPTY)
             goto badmove;
-        if (!range2grid(ret, rangeno, &gx, &gy, &direction))
+        if (!RANGECHECK(ret, rangeno))
             goto badmove;
-        fire_laser(ret, gx, gy, direction);
+        fire_laser(ret, rangeno);
         break;
 
     case 'R':
         if (ret->nguesses < ret->minballs ||
             ret->nguesses > ret->maxballs)
             goto badmove;
-        check_guesses(ret);
-        ret->reveal = 1;
+        check_guesses(ret, TRUE);
         break;
 
     case 'L':
@@ -905,15 +1104,15 @@ static void game_compute_size(game_params *params, int tilesize,
     *y = (params->h + 3) * tilesize;
 }
 
-static void game_set_size(game_drawstate *ds, game_params *params,
-                         int tilesize)
+static void game_set_size(drawing *dr, game_drawstate *ds,
+                         game_params *params, int tilesize)
 {
     ds->tilesize = tilesize;
     ds->crad = (tilesize-1)/2;
     ds->rrad = (3*tilesize)/8;
 }
 
-static float *game_colours(frontend *fe, game_state *state, int *ncolours)
+static float *game_colours(frontend *fe, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
     int i;
@@ -932,13 +1131,9 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours)
     ret[COL_BUTTON * 3 + 1] = 1.0F;
     ret[COL_BUTTON * 3 + 2] = 0.0F;
 
-    ret[COL_LASER * 3 + 0] = 1.0F;
-    ret[COL_LASER * 3 + 1] = 0.0F;
-    ret[COL_LASER * 3 + 2] = 0.0F;
-
-    ret[COL_DIMLASER * 3 + 0] = 0.5F;
-    ret[COL_DIMLASER * 3 + 1] = 0.0F;
-    ret[COL_DIMLASER * 3 + 2] = 0.0F;
+    ret[COL_CURSOR * 3 + 0] = 1.0F;
+    ret[COL_CURSOR * 3 + 1] = 0.0F;
+    ret[COL_CURSOR * 3 + 2] = 0.0F;
 
     for (i = 0; i < 3; i++) {
         ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
@@ -955,7 +1150,7 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours)
     return ret;
 }
 
-static game_drawstate *game_new_drawstate(game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
 {
     struct game_drawstate *ds = snew(struct game_drawstate);
 
@@ -963,33 +1158,47 @@ static game_drawstate *game_new_drawstate(game_state *state)
     ds->w = state->w; ds->h = state->h;
     ds->grid = snewn((state->w+2)*(state->h+2), unsigned int);
     memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
-    ds->started = 0;
+    ds->started = ds->reveal = 0;
     ds->flash_laserno = LASER_EMPTY;
+    ds->isflash = 0;
 
     return ds;
 }
 
-static void game_free_drawstate(game_drawstate *ds)
+static void game_free_drawstate(drawing *dr, game_drawstate *ds)
 {
     sfree(ds->grid);
     sfree(ds);
 }
 
-static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
-                            int ax, int ay, int force, int isflash)
+static void draw_square_cursor(drawing *dr, game_drawstate *ds, int dx, int dy)
+{
+    int coff = TILE_SIZE/8;
+    draw_rect_outline(dr, dx + coff, dy + coff,
+                      TILE_SIZE - coff*2,
+                      TILE_SIZE - coff*2,
+                      COL_CURSOR);
+}
+
+
+static void draw_arena_tile(drawing *dr, game_state *gs, game_drawstate *ds,
+                            game_ui *ui, int ax, int ay, int force, int isflash)
 {
     int gx = ax+1, gy = ay+1;
     int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy);
     int dx = TODRAW(gx), dy = TODRAW(gy);
 
+    if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy)
+        gs_tile |= FLAG_CURSOR;
+
     if (gs_tile != ds_tile || gs->reveal != ds->reveal || force) {
-        int bcol, bg;
+        int bcol, ocol, bg;
 
-        bg = (gs_tile & BALL_LOCK) ? COL_LOCK :
-            gs->reveal ? COL_BACKGROUND : COL_COVER;
+        bg = (gs->reveal ? COL_BACKGROUND :
+              (gs_tile & BALL_LOCK) ? COL_LOCK : COL_COVER);
 
-        draw_rect(fe, dx, dy, TILE_SIZE, TILE_SIZE, bg);
-        draw_rect_outline(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
+        draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, bg);
+        draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
 
         if (gs->reveal) {
             /* Guessed balls are always black; if they're incorrect they'll
@@ -1010,10 +1219,17 @@ static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
                 bcol = bg;
             }
         }
+        ocol = (gs_tile & FLAG_CURSOR && bcol != bg) ? COL_CURSOR : bcol;
 
-        draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
+        draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
+                    ocol, ocol);
+        draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-3,
                     bcol, bcol);
 
+
+        if (gs_tile & FLAG_CURSOR && bcol == bg)
+            draw_square_cursor(dr, ds, dx, dy);
+
         if (gs->reveal &&
             (gs_tile & BALL_GUESS) &&
             !(gs_tile & BALL_CORRECT)) {
@@ -1030,7 +1246,7 @@ static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
            coords[5] = y2-1;
            coords[6] = x2-1;
            coords[7] = y2+1;
-           draw_polygon(fe, coords, 4, COL_WRONG, COL_WRONG);
+           draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
            coords[0] = x2+1;
            coords[1] = y1+1;
            coords[2] = x2-1;
@@ -1039,21 +1255,22 @@ static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
            coords[5] = y2-1;
            coords[6] = x1+1;
            coords[7] = y2+1;
-           draw_polygon(fe, coords, 4, COL_WRONG, COL_WRONG);
+           draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
         }
-        draw_update(fe, dx, dy, TILE_SIZE, TILE_SIZE);
+        draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
     }
     GRID(ds,gx,gy) = gs_tile;
 }
 
-static void draw_laser_tile(frontend *fe, game_state *gs, game_drawstate *ds,
+static void draw_laser_tile(drawing *dr, game_state *gs, game_drawstate *ds,
                             game_ui *ui, int lno, int force)
 {
     int gx, gy, dx, dy, unused;
-    int wrong, omitted, reflect, hit, laserval, flash = 0;
+    int wrong, omitted, reflect, hit, laserval, flash = 0, tmp;
     unsigned int gs_tile, ds_tile, exitno;
 
-    assert(range2grid(gs, lno, &gx, &gy, &unused));
+    tmp = range2grid(gs, lno, &gx, &gy, &unused);
+    assert(tmp);
     gs_tile = GRID(gs, gx, gy);
     ds_tile = GRID(ds, gx, gy);
     dx = TODRAW(gx);
@@ -1067,22 +1284,25 @@ static void draw_laser_tile(frontend *fe, game_state *gs, game_drawstate *ds,
     hit = gs_tile & LASER_HIT;
     laserval = gs_tile & ~LASER_FLAGMASK;
 
-    if (lno == ui->flash_laserno)
+    if (lno == ds->flash_laserno)
         gs_tile |= LASER_FLASHED;
     else if (!(gs->exits[lno] & (LASER_HIT | LASER_REFLECT))) {
-        if (exitno == ui->flash_laserno)
+        if (exitno == ds->flash_laserno)
             gs_tile |= LASER_FLASHED;
     }
     if (gs_tile & LASER_FLASHED) flash = 1;
 
     gs_tile |= wrong | omitted;
 
+    if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy)
+        gs_tile |= FLAG_CURSOR;
+
     if (gs_tile != ds_tile || force) {
-        draw_rect(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
-        draw_rect_outline(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
+        draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
+        draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
 
-        if (gs_tile &~ (LASER_WRONG | LASER_OMITTED)) {
-            char str[10];
+        if (gs_tile &~ (LASER_WRONG | LASER_OMITTED | FLAG_CURSOR)) {
+            char str[32];
             int tcol = flash ? COL_FLASHTEXT : omitted ? COL_WRONG : COL_TEXT;
 
             if (reflect || hit)
@@ -1091,25 +1311,29 @@ static void draw_laser_tile(frontend *fe, game_state *gs, game_drawstate *ds,
                 sprintf(str, "%d", laserval);
 
             if (wrong) {
-                draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
+                draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
                             ds->rrad,
                             COL_WRONG, COL_WRONG);
-                draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
+                draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
                             ds->rrad - TILE_SIZE/16,
                             COL_BACKGROUND, COL_WRONG);
             }
 
-            draw_text(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
+            draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
                       FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
                       tcol, str);
         }
-        draw_update(fe, dx, dy, TILE_SIZE, TILE_SIZE);
+        if (gs_tile & FLAG_CURSOR)
+            draw_square_cursor(dr, ds, dx, dy);
+
+        draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
     }
     GRID(ds, gx, gy) = gs_tile;
 }
 
+#define CUR_ANIM 0.2F
 
-static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
+static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                        game_state *state, int dir, game_ui *ui,
                        float animtime, float flashtime)
 {
@@ -1118,7 +1342,6 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     if (flashtime > 0) {
         int frame = (int)(flashtime / FLASH_FRAME);
         isflash = (frame % 2) == 0;
-        force = 1;
         debug(("game_redraw: flashtime = %f", flashtime));
     }
 
@@ -1126,60 +1349,68 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
         int x0 = TODRAW(0)-1, y0 = TODRAW(0)-1;
         int x1 = TODRAW(state->w+2), y1 = TODRAW(state->h+2);
 
-        draw_rect(fe, 0, 0,
+        draw_rect(dr, 0, 0,
                   TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3),
                   COL_BACKGROUND);
 
         /* clockwise around the outline starting at pt behind (1,1). */
-        draw_line(fe, x0+ts, y0+ts, x0+ts, y0,    COL_HIGHLIGHT);
-        draw_line(fe, x0+ts, y0,    x1-ts, y0,    COL_HIGHLIGHT);
-        draw_line(fe, x1-ts, y0,    x1-ts, y0+ts, COL_LOWLIGHT);
-        draw_line(fe, x1-ts, y0+ts, x1,    y0+ts, COL_HIGHLIGHT);
-        draw_line(fe, x1,    y0+ts, x1,    y1-ts, COL_LOWLIGHT);
-        draw_line(fe, x1,    y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
-        draw_line(fe, x1-ts, y1-ts, x1-ts, y1,    COL_LOWLIGHT);
-        draw_line(fe, x1-ts, y1,    x0+ts, y1,    COL_LOWLIGHT);
-        draw_line(fe, x0+ts, y1,    x0+ts, y1-ts, COL_HIGHLIGHT);
-        draw_line(fe, x0+ts, y1-ts, x0,    y1-ts, COL_LOWLIGHT);
-        draw_line(fe, x0,    y1-ts, x0,    y0+ts, COL_HIGHLIGHT);
-        draw_line(fe, x0,    y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
+        draw_line(dr, x0+ts, y0+ts, x0+ts, y0,    COL_HIGHLIGHT);
+        draw_line(dr, x0+ts, y0,    x1-ts, y0,    COL_HIGHLIGHT);
+        draw_line(dr, x1-ts, y0,    x1-ts, y0+ts, COL_LOWLIGHT);
+        draw_line(dr, x1-ts, y0+ts, x1,    y0+ts, COL_HIGHLIGHT);
+        draw_line(dr, x1,    y0+ts, x1,    y1-ts, COL_LOWLIGHT);
+        draw_line(dr, x1,    y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
+        draw_line(dr, x1-ts, y1-ts, x1-ts, y1,    COL_LOWLIGHT);
+        draw_line(dr, x1-ts, y1,    x0+ts, y1,    COL_LOWLIGHT);
+        draw_line(dr, x0+ts, y1,    x0+ts, y1-ts, COL_HIGHLIGHT);
+        draw_line(dr, x0+ts, y1-ts, x0,    y1-ts, COL_LOWLIGHT);
+        draw_line(dr, x0,    y1-ts, x0,    y0+ts, COL_HIGHLIGHT);
+        draw_line(dr, x0,    y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
         /* phew... */
 
-        draw_update(fe, 0, 0,
+        draw_update(dr, 0, 0,
                     TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3));
         force = 1;
         ds->started = 1;
     }
 
+    if (isflash != ds->isflash) force = 1;
+
     /* draw the arena */
     for (x = 0; x < state->w; x++) {
         for (y = 0; y < state->h; y++) {
-            draw_arena_tile(fe, state, ds, x, y, force, isflash);
+            draw_arena_tile(dr, state, ds, ui, x, y, force, isflash);
         }
     }
 
     /* draw the lasers */
+    ds->flash_laserno = LASER_EMPTY;
+    if (ui->flash_laser == 1)
+        ds->flash_laserno = ui->flash_laserno;
+    else if (ui->flash_laser == 2 && animtime > 0)
+        ds->flash_laserno = ui->flash_laserno;
+
     for (i = 0; i < 2*(state->w+state->h); i++) {
-        draw_laser_tile(fe, state, ds, ui, i, force);
+        draw_laser_tile(dr, state, ds, ui, i, force);
     }
 
     /* draw the 'finish' button */
-    if (state->nguesses >= state->minballs &&
-        state->nguesses <= state->maxballs &&
-        !state->reveal) {
-        clip(fe, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1);
-        draw_circle(fe, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
-                    COL_BUTTON, COL_BALL);
-       unclip(fe);
-        ds->canreveal = 1;
+    if (CAN_REVEAL(state)) {
+        int outline = (ui->cur_visible && ui->cur_x == 0 && ui->cur_y == 0)
+            ? COL_CURSOR : COL_BALL;
+        clip(dr, TODRAW(0)-1, TODRAW(0)-1, TILE_SIZE+1, TILE_SIZE+1);
+        draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
+                    outline, outline);
+        draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad-2,
+                    COL_BUTTON, COL_BUTTON);
+       unclip(dr);
     } else {
-        draw_rect(fe, TODRAW(0), TODRAW(0),
-                 TILE_SIZE-1, TILE_SIZE-1, COL_BACKGROUND);
-        ds->canreveal = 0;
+        draw_rect(dr, TODRAW(0)-1, TODRAW(0)-1,
+                 TILE_SIZE+1, TILE_SIZE+1, COL_BACKGROUND);
     }
-    draw_update(fe, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
+    draw_update(dr, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
     ds->reveal = state->reveal;
-    ds->flash_laserno = ui->flash_laserno;
+    ds->isflash = isflash;
 
     {
         char buf[256];
@@ -1192,7 +1423,9 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
             else
                 sprintf(buf, "%d wrong and %d missed balls.",
                         state->nwrong, state->nmissed);
-        } else {
+        } else if (state->justwrong) {
+           sprintf(buf, "Wrong! Guess again.");
+       } else {
             if (state->nguesses > state->maxballs)
                 sprintf(buf, "%d too many balls marked.",
                         state->nguesses - state->maxballs);
@@ -1206,14 +1439,18 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
                 sprintf(buf, "Balls marked: %d / %d-%d.",
                         state->nguesses, state->minballs, state->maxballs);
         }
-        status_bar(fe, buf);
+       if (ui->errors) {
+           sprintf(buf + strlen(buf), " (%d error%s)",
+                   ui->errors, ui->errors > 1 ? "s" : "");
+       }
+        status_bar(dr, buf);
     }
 }
 
 static float game_anim_length(game_state *oldstate, game_state *newstate,
                              int dir, game_ui *ui)
 {
-    return 0.0F;
+    return (ui->flash_laser == 2) ? CUR_ANIM : 0.0F;
 }
 
 static float game_flash_length(game_state *oldstate, game_state *newstate,
@@ -1225,9 +1462,21 @@ static float game_flash_length(game_state *oldstate, game_state *newstate,
         return 0.0F;
 }
 
-static int game_wants_statusbar(void)
+static int game_status(game_state *state)
 {
-    return TRUE;
+    if (state->reveal) {
+        /*
+         * We return nonzero whenever the solution has been revealed,
+         * even (on spoiler grounds) if it wasn't guessed correctly.
+         */
+        if (state->nwrong == 0 &&
+            state->nmissed == 0 &&
+            state->nright >= state->minballs)
+            return +1;
+        else
+            return -1;
+    }
+    return 0;
 }
 
 static int game_timing_state(game_state *state, game_ui *ui)
@@ -1235,12 +1484,20 @@ static int game_timing_state(game_state *state, game_ui *ui)
     return TRUE;
 }
 
+static void game_print_size(game_params *params, float *x, float *y)
+{
+}
+
+static void game_print(drawing *dr, game_state *state, int tilesize)
+{
+}
+
 #ifdef COMBINED
 #define thegame blackbox
 #endif
 
 const struct game thegame = {
-    "Black Box", "games.blackbox",
+    "Black Box", "games.blackbox", "blackbox",
     default_params,
     game_fetch_preset,
     decode_params,
@@ -1255,7 +1512,7 @@ const struct game thegame = {
     dup_game,
     free_game,
     TRUE, solve_game,
-    FALSE, game_text_format,
+    FALSE, game_can_format_as_text_now, game_text_format,
     new_ui,
     free_ui,
     encode_ui,
@@ -1270,9 +1527,11 @@ const struct game thegame = {
     game_redraw,
     game_anim_length,
     game_flash_length,
-    game_wants_statusbar,
+    game_status,
+    FALSE, FALSE, game_print_size, game_print,
+    TRUE,                             /* wants_statusbar */
     FALSE, game_timing_state,
-    0,                                /* mouse_priorities */
+    REQUIRE_RBUTTON,                  /* flags */
 };
 
 /* vim: set shiftwidth=4 tabstop=8: */