[gnome-games/applygsoc2009: 34/76] XXX: Make autofill-all work



commit 6a8c8e0e8391f08eee70cc496ec24f5959045640
Author: Pablo Castellano <pablog src gnome org>
Date:   Thu Aug 26 15:18:21 2010 +0200

    XXX: Make autofill-all work
    
    * Make SudokuSolver (sudoku.py)'s auto_fill only return the possibility, not
     actually do the fill, which should be the duty of SudokuModel.
    * Break Undo/Redo

 gnome-sudoku/src/lib/gsudoku.py |   31 +++++++++++++++++--------------
 gnome-sudoku/src/lib/sudoku.py  |    9 +--------
 2 files changed, 18 insertions(+), 22 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/gsudoku.py b/gnome-sudoku/src/lib/gsudoku.py
index 040c445..44fb99d 100644
--- a/gnome-sudoku/src/lib/gsudoku.py
+++ b/gnome-sudoku/src/lib/gsudoku.py
@@ -73,6 +73,9 @@ class SudokuNumberGrid (gtk.AspectFrame):
 
 class SudokuView (SudokuNumberGrid, gobject.GObject):
 
+    __gsignals__ = {
+        'puzzle-finished':(gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())}
+
     def __init__(self, group_size):
         SudokuNumberGrid.__init__(self, group_size)
 #        self.hint_square = None
@@ -116,6 +119,9 @@ class SudokuView (SudokuNumberGrid, gobject.GObject):
             if box.conflict is not None:
                 self._show_conflict(x, y, box.conflict)
 
+    def puzzle_finished_cb(self):
+        self.emit("puzzle-finished")
+
     def _show_conflict(self, x, y, conflict):
         self.__entries__[(x, y)].set_error_highlight(conflict)
 
@@ -227,14 +233,9 @@ class NumberBoxModel:
         self.conflict = conflict
 
 
-class SudokuModel(gobject.GObject):
-
-    __gsignals__ = {
-        'puzzle-finished':(gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
-        }
+class SudokuModel:
 
     def __init__ (self, grid, group_size):
-        group_size = int(group_size)
         self.hints = 0
         self.always_show_hints = False
         self.auto_fills = 0
@@ -243,7 +244,6 @@ class SudokuModel(gobject.GObject):
         self.impossibilities = []
         self.trackers = {}
         self.__trackers_tracking__ = {}
-        gobject.GObject.__init__(self)
         self._setup_grid(grid, group_size)
 
         self._observers = []
@@ -283,6 +283,10 @@ class SudokuModel(gobject.GObject):
             self.animate_hint()
             self.hint_timer = gobject.timeout_add(150, self.animate_hint)
 
+    def _signal_completeness(self):
+        for observer in self._observers:
+            observer.puzzle_finished_cb()
+
     def show_hint (self):
         min_options = 10;
         squares = []
@@ -554,6 +558,9 @@ class SudokuModel(gobject.GObject):
             result.append(change)
         self._signal_observers(result)
 
+        if self.grid.check_for_completeness():
+            self._signal_completeness()
+
     def _remove_related_conflicts(self, x, y):
         result = []
         if self.__error_pairs__.has_key((x, y)):
@@ -635,14 +642,10 @@ class SudokuModel(gobject.GObject):
             self._mark_impossible_implications(x, y)
 
     def auto_fill (self):
-        changed = self.grid.auto_fill()
-        retval = []
+        changed = set(self.grid.auto_fill()) # there are duplicate?
         for coords, val in changed:
-            self.add_value(coords[0], coords[1], val)
-            retval.append((coords[0], coords[1], val))
-        if self.grid.check_for_completeness():
-            self.emit('puzzle-finished')
-        return retval
+		    self.set_value(coords[0], coords[1], val)
+        return changed
 
     def auto_fill_current_entry (self):
         focused = self.get_focus()
diff --git a/gnome-sudoku/src/lib/sudoku.py b/gnome-sudoku/src/lib/sudoku.py
index f85948a..7188285 100644
--- a/gnome-sudoku/src/lib/sudoku.py
+++ b/gnome-sudoku/src/lib/sudoku.py
@@ -376,13 +376,7 @@ class SudokuSolver (SudokuGrid):
                     if not coords:
                         raise UnsolvablePuzzle('Missing a %s in %s' % (n, label))
                     else:
-                        try:
-                            self.add(coords[0], coords[1], n)
-                            changed.append((coords, n))
-                        except AlreadySetError:
-                            raise UnsolvablePuzzle(
-                                "%s,%s must be two values at once!" % (coords)
-                                )
+                        changed.append((coords, n)) 
         return changed
 
     def fill_deterministically (self):
@@ -393,7 +387,6 @@ class SudokuSolver (SudokuGrid):
             if self.verbose:
                 print 'Deterministically adding ', coords, choices
             val = choices.pop()
-            self.add(coords[0], coords[1], val)
             retval.append([(coords[0], coords[1]), val])
         if self.verbose:
             print 'deterministically returning ', retval



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