[gnome-games/sudoku-tube] Fix impossible hints



commit 7c24d8cfb0aac48242d2f73b55fda46d0d67563d
Author: Zhang Sen <zh jesse gmail com>
Date:   Wed Jul 22 20:05:29 2009 +0800

    Fix impossible hints

 gnome-sudoku/src/lib/main.py       |   12 +++-----
 gnome-sudoku/src/lib/model.py      |   48 +++++++++++++++++++++++------------
 gnome-sudoku/src/lib/number_box.py |   11 +++-----
 3 files changed, 40 insertions(+), 31 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index 6ba8ca4..e8af818 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -375,11 +375,12 @@ class UI (gconf_wrapper.GConfWrapper):
                 self._notes_model.get_hints_count()) % {
                         'n': self._notes_model.get_hints_count()}
         sublabel += "\n"
-        if self._main_model.impossible_hints:
+        impossible_hints = self._main_model.get_impossible_hints()
+        if impossible_hints:
             sublabel += ngettext("You had %(n)s impossibility pointed out.",
                                  "You had %(n)s impossibilities pointed out.",
-                                 self._main_model.impossible_hints) % {
-                                         'n': self._main_model.impossible_hints}
+                                 impossible_hints) % {
+                                         'n': impossible_hints}
             sublabel += "\n"
         autofill_count = self._main_model.get_autofill_count()
         if autofill_count:
@@ -501,10 +502,7 @@ class UI (gconf_wrapper.GConfWrapper):
         self._notes_model.toggle_auto_hint(action.get_active())
 
     def impossible_implication_cb (self, action):
-        if action.get_active():
-            self.gsd.show_impossible_implications = True
-        else:
-            self.gsd.show_impossible_implications = False
+        self._main_model.toggle_impossible_implications(True)
 
     def auto_fill_cb (self, action):
         self._main_model.auto_fill()
diff --git a/gnome-sudoku/src/lib/model.py b/gnome-sudoku/src/lib/model.py
index 3d49f7c..c1b4fce 100644
--- a/gnome-sudoku/src/lib/model.py
+++ b/gnome-sudoku/src/lib/model.py
@@ -169,8 +169,7 @@ class SudokuModel:
     def __init__ (self, virgin_grid, group_size, initial_grid=None):
         self.autofill_count = 0
         self.show_impossible_implications = False
-        self.impossible_hints = 0
-        self.impossibilities = []
+        self.impossibilities = set()
         self.__trackers_tracking__ = {}
         self.group_size = group_size
         self.grid = None
@@ -188,6 +187,7 @@ class SudokuModel:
     def _signal_observers(self, values):
         for observer in self._observers:
             observer.update(values)
+        self._check_for_completeness()
 
     def _signal_completeness(self):
         for observer in self._observers:
@@ -236,9 +236,9 @@ class SudokuModel:
         result = [NumberBoxModel(x, y, value=value, conflict=flag)]
         result.extend(old_conflicts)
         result.extend(new_conflicts)
+        result.extend(self._update_impossible_implications(x, y))
 
         self._signal_observers(result)
-        self._check_for_completeness()
 
     def get_value(self, x, y):
         return self.grid._get_(x, y)
@@ -295,26 +295,40 @@ class SudokuModel:
             result.append(NumberBoxModel(x, y, value=val))
 
         self._signal_observers(result)
-        self._check_for_completeness()
 
     def auto_fill_current(self, x, y):
         filled = self.grid.auto_fill_for_xy(x, y)
         if filled and filled != -1:
             self.set_value(x, y, filled[1])
 
-    def _mark_impossible_implications(self, x, y):
-        if not self.grid:
-            return
-        implications = self.grid.find_impossible_implications(x, y)
-        if implications:
-            for x, y in implications:
-                self.__entries__[(x, y)].set_impossible(True)
-                if not (x, y) in self.impossibilities:
-                    self.impossible_hints += 1
-        for x, y in self.impossibilities:
-            if not (x, y) in implications:
-                self.__entries__[(x, y)].set_impossible(False)
-        self.impossibilities = implications
+    def toggle_impossible_implications(self, show):
+        self.show_impossible_implications = show
+        if not show:
+            self._clean_impossible_implications()
+
+    def _clean_impossible_implications(self):
+        for (x, y) in self.impossibilities:
+            self.set_value(x, y, 0)
+
+    def _update_impossible_implications(self, x, y):
+        if not self.show_impossible_implications:
+            return []
+
+        changes = []
+        new_imposs = set(self.grid.find_impossible_implications(x, y))
+
+        for coord in new_imposs - self.impossibilities:
+            # -1 means impossible
+            changes.append(NumberBoxModel(coord[0], coord[1], value=-1))
+        for coord in self.impossibilities - new_imposs:
+            # remove the 'X' mark
+            changes.append(NumberBoxModel(coord[0], coord[1], value=0))
+
+        self.impossibilities = new_imposs
+        return changes
+
+    def get_impossible_hints(self):
+        return len(self.impossibilities)
 
     def _trackers_for_point(self, x, y, val = None):
         if val:
diff --git a/gnome-sudoku/src/lib/number_box.py b/gnome-sudoku/src/lib/number_box.py
index 00c289c..fabcb11 100644
--- a/gnome-sudoku/src/lib/number_box.py
+++ b/gnome-sudoku/src/lib/number_box.py
@@ -85,7 +85,7 @@ class _NumberBox (gtk.Widget):
     npicker = None
     draw_boxes = False
 
-    def __init__(self, upper=9, value=''):
+    def __init__(self, upper=9, value=0):
         gtk.Widget.__init__(self)
         self.upper = upper
         self.font = self.style.font_desc
@@ -507,8 +507,10 @@ class _NumberBox (gtk.Widget):
         self.value = v
         if 0 < v <= self.upper:
             self.set_text(str(v))
+        elif v == 0:
+             self.set_text('')
         else:
-            self.set_text('')
+            self.set_text("X")
         self.queue_draw()
 
     def get_value (self):
@@ -547,11 +549,6 @@ class SudokuNumberBox (_NumberBox):
             self.set_font(self.normal_font)
         self.queue_draw()
 
-    def set_impossible (self, val):
-        if val:
-            self.set_text('X')
-        else: self.set_text('')
-
 
 if __name__ == '__main__':
     window = gtk.Window()



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