Fix some border drawing issues.
[sgt/puzzles] / filling.c
index ac5b6d9..f47f9f1 100644 (file)
--- a/filling.c
+++ b/filling.c
@@ -47,7 +47,6 @@
 
 #include <assert.h>
 #include <ctype.h>
-#include <errno.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -300,7 +299,7 @@ static game_state *new_game(midend *, game_params *, char *);
 static void free_game(game_state *);
 
 /* generate a random valid board; uses validate_board.  */
-void make_board(int *board, int w, int h, random_state *rs) {
+static void make_board(int *board, int w, int h, random_state *rs) {
     int *dsf;
 
     const unsigned int sz = w * h;
@@ -962,33 +961,37 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
     {
         const int i = w*ui->y + ui->x;
         char buf[64];
-        sprintf(buf, "%d_%d", i, button);
         ui->x = ui->y = -1;
-        return dupstr(buf);
+       if (state->board[i] == button) {
+           return "";                 /* no change - just update ui */
+       } else {
+           sprintf(buf, "%d_%d", i, button);
+           return dupstr(buf);
+       }
     }
 }
 
 static game_state *execute_move(game_state *state, char *move)
 {
     game_state *new_state;
+    const int sz = state->shared->params.w * state->shared->params.h;
 
     if (*move == 's') {
-        const int sz = state->shared->params.w * state->shared->params.h;
         int i = 0;
         new_state = dup_game(state);
         for (++move; i < sz; ++i) new_state->board[i] = move[i] - '0';
         new_state->cheated = TRUE;
     } else {
         char *endptr;
-        const int i = strtol(move, &endptr, errno = 0);
+        const int i = strtol(move, &endptr, 0);
         int value;
-        if (errno == ERANGE) return NULL;
         if (endptr == move) return NULL;
         if (*endptr != '_') return NULL;
         move = endptr + 1;
         value = strtol(move, &endptr, 0);
         if (endptr == move) return NULL;
         if (*endptr != '\0') return NULL;
+        if (i < 0 || i >= sz || value < 0 || value > 9) return NULL;
         new_state = dup_game(state);
         new_state->board[i] = value;
     }
@@ -1118,18 +1121,32 @@ static void draw_square(drawing *dr, game_drawstate *ds, int x, int y,
     assert(ds);
 
     /*
+     * Clip to the grid square.
+     */
+    clip(dr, BORDER + x*TILE_SIZE, BORDER + y*TILE_SIZE,
+        TILE_SIZE, TILE_SIZE);
+
+    /*
      * Clear the square.
      */
     draw_rect(dr,
-              BORDER + x*TILE_SIZE + 1,
-              BORDER + y*TILE_SIZE + 1,
-              TILE_SIZE - 1,
-              TILE_SIZE - 1,
+              BORDER + x*TILE_SIZE,
+              BORDER + y*TILE_SIZE,
+              TILE_SIZE,
+              TILE_SIZE,
               (flags & CURSOR_BG ? COL_HIGHLIGHT :
                flags & ERROR_BG ? COL_ERROR :
                flags & CORRECT_BG ? COL_CORRECT : COL_BACKGROUND));
 
     /*
+     * Draw the grid lines.
+     */
+    draw_line(dr, BORDER + x*TILE_SIZE, BORDER + y*TILE_SIZE,
+             BORDER + (x+1)*TILE_SIZE, BORDER + y*TILE_SIZE, COL_GRID);
+    draw_line(dr, BORDER + x*TILE_SIZE, BORDER + y*TILE_SIZE,
+             BORDER + x*TILE_SIZE, BORDER + (y+1)*TILE_SIZE, COL_GRID);
+
+    /*
      * Draw the number.
      */
     if (n) {
@@ -1205,12 +1222,14 @@ static void draw_square(drawing *dr, game_drawstate *ds, int x, int y,
                   BORDER_WIDTH,
                   BORDER_WIDTH,
                   COL_GRID);
-    
+
+    unclip(dr);
+
     draw_update(dr,
-               BORDER + x*TILE_SIZE - 1,
-               BORDER + y*TILE_SIZE - 1,
-               TILE_SIZE + 3,
-               TILE_SIZE + 3);
+               BORDER + x*TILE_SIZE,
+               BORDER + y*TILE_SIZE,
+               TILE_SIZE,
+               TILE_SIZE);
 }
 
 static void draw_grid(drawing *dr, game_drawstate *ds, game_state *state,
@@ -1370,7 +1389,8 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
          * should start by drawing a big background-colour rectangle
          * covering the whole window.
          */
-        draw_rect(dr, 0, 0, 10*ds->tilesize, 10*ds->tilesize, COL_BACKGROUND);
+        draw_rect(dr, 0, 0, w*TILE_SIZE + 2*BORDER, h*TILE_SIZE + 2*BORDER,
+                  COL_BACKGROUND);
 
        /*
         * Smaller black rectangle which is the main grid.
@@ -1380,6 +1400,8 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                  h*TILE_SIZE + 2*BORDER_WIDTH + 1,
                  COL_GRID);
 
+        draw_update(dr, 0, 0, w*TILE_SIZE + 2*BORDER, h*TILE_SIZE + 2*BORDER);
+
         ds->started = TRUE;
     }
 
@@ -1459,6 +1481,7 @@ static void game_print(drawing *dr, game_state *state, int tilesize)
     /*
      * Draw grid.
      */
+    print_line_width(dr, TILE_SIZE / 64);
     draw_grid(dr, ds, state, NULL, FALSE, borders, FALSE);
 
     /*
@@ -1505,7 +1528,7 @@ const struct game thegame = {
     TRUE, FALSE, game_print_size, game_print,
     FALSE,                                /* wants_statusbar */
     FALSE, game_timing_state,
-    0,                                    /* flags */
+    REQUIRE_NUMPAD,                   /* flags */
 };
 
 #ifdef STANDALONE_SOLVER /* solver? hah! */