[gnome-sudoku] Allow clearing the board when only earmarks are placed



commit 560849fc62567f7ca2026654fcac264de4bd1aa9
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Fri Sep 5 16:05:35 2014 -0500

    Allow clearing the board when only earmarks are placed
    
    Also, track the number of earmarks present on the board, to make this
    simple.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=735495

 lib/sudoku-board.vala  |   31 +++++++++++++++++++++++++++++--
 lib/sudoku-saver.vala  |    6 +++---
 src/number-picker.vala |    2 +-
 src/sudoku-view.vala   |   13 ++++++++++---
 4 files changed, 43 insertions(+), 9 deletions(-)
---
diff --git a/lib/sudoku-board.vala b/lib/sudoku-board.vala
index e8e2c34..76052fe 100644
--- a/lib/sudoku-board.vala
+++ b/lib/sudoku-board.vala
@@ -12,7 +12,8 @@ public class SudokuBoard : Object
     private bool[,] possible_in_col;            /* if specific value is possible in specific col */
     private bool[,,] possible_in_block;         /* if specific value is possible in specific block */
 
-    public bool[,,] earmarks;                  /* Earmarks set by the user */
+    private bool[,,] earmarks;                  /* Earmarks set by the user */
+    private int n_earmarks;                     /* The number of earmarks on the board */
 
     public double previous_played_time { set; get; default = 0; }
 
@@ -54,7 +55,7 @@ public class SudokuBoard : Object
 
     public bool is_empty ()
     {
-        return filled == fixed;
+        return filled == fixed && n_earmarks == 0;
     }
 
     public signal void completed ();
@@ -163,6 +164,7 @@ public class SudokuBoard : Object
         board.possible_in_block = possible_in_block;
         board.filled = filled;
         board.fixed = fixed;
+        board.n_earmarks = n_earmarks;
         board.broken_coords.add_all (broken_coords);
         board.earmarks = earmarks;
         board.difficulty_category = difficulty_category;
@@ -170,6 +172,31 @@ public class SudokuBoard : Object
         return board;
     }
 
+    public void enable_earmark (int row, int column, int digit)
+        ensures (n_earmarks > 0)
+    {
+        if (!earmarks[row, column, digit-1])
+        {
+            earmarks[row, column, digit-1] = true;
+            n_earmarks++;
+        }
+    }
+
+    public void disable_earmark (int row, int column, int digit)
+        ensures (n_earmarks >= 0)
+    {
+        if (earmarks[row, column, digit-1])
+        {
+            earmarks[row, column, digit-1] = false;
+            n_earmarks--;
+        }
+    }
+
+    public bool is_earmark_enabled (int row, int column, int digit)
+    {
+        return earmarks[row, column, digit-1];
+    }
+
     public bool is_possible (int row, int col, int val)
     {
         val--;
diff --git a/lib/sudoku-saver.vala b/lib/sudoku-saver.vala
index c096a17..cd48f07 100644
--- a/lib/sudoku-saver.vala
+++ b/lib/sudoku-saver.vala
@@ -98,8 +98,8 @@ public class SudokuSaver : Object
             for (var j = 0; j < board.cols; j++)
             {
                 int[] earmarks = {};
-                for (var k = 0; k < board.max_val; k++)
-                    if (board.earmarks[i, j, k])
+                for (var k = 1; k <= board.max_val; k++)
+                    if (board.is_earmark_enabled(i, j, k))
                         earmarks += k;
 
                 if (board_cells[i, j] == 0 && earmarks.length == 0)
@@ -191,7 +191,7 @@ public class SudokuSaver : Object
             {
                 reader.read_element (k);
                 return_val_if_fail (reader.is_value (), null);
-                board.earmarks[row, col, (int) reader.get_int_value ()] = true;
+                board.enable_earmark (row, col, (int) reader.get_int_value ());
                 reader.end_element ();
             }
             reader.end_member ();
diff --git a/src/number-picker.vala b/src/number-picker.vala
index 2c346f3..771f2a5 100644
--- a/src/number-picker.vala
+++ b/src/number-picker.vala
@@ -96,7 +96,7 @@ private class NumberPicker : Gtk.Grid
     public void set_earmarks (int row, int col)
     {
         for (var i = 0; i < board.max_val; i++)
-            set_earmark (row, col, i, board.earmarks[row, col, i]);
+            set_earmark (row, col, i, board.is_earmark_enabled(row, col, i + 1));
     }
 
     public bool set_earmark (int row, int col, int index, bool state)
diff --git a/src/sudoku-view.vala b/src/sudoku-view.vala
index 8773d5d..831b95f 100644
--- a/src/sudoku-view.vala
+++ b/src/sudoku-view.vala
@@ -116,7 +116,11 @@ private class SudokuCellView : Gtk.DrawingArea
 
         earmark_picker = new NumberPicker (ref game.board, true);
         earmark_picker.earmark_state_changed.connect ((number, state) => {
-            this.game.board.earmarks[row, col, number-1] = state;
+            if (state)
+                this.game.board.enable_earmark(row, col, number);
+            else
+                this.game.board.disable_earmark(row, col, number);
+            this.game.cell_changed(row, col, value, value);
             queue_draw ();
         });
         earmark_picker.set_earmarks (row, col);
@@ -225,10 +229,13 @@ private class SudokuCellView : Gtk.DrawingArea
         {
             if ((event.state & ModifierType.CONTROL_MASK) > 0)
             {
-                var new_state = !game.board.earmarks[row, col, k_no-1];
+                var new_state = !game.board.is_earmark_enabled(row, col, k_no);
                 if (earmark_picker.set_earmark (row, col, k_no-1, new_state))
                 {
-                    game.board.earmarks[row, col, k_no-1] = new_state;
+                    if (new_state)
+                        game.board.enable_earmark(row, col, k_no);
+                    else
+                        game.board.disable_earmark(row, col, k_no);
                     queue_draw ();
                 }
             }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]