Miscellaneous cleanup.
authorEevee <eevee@nyarumaa.(none)>
Wed, 7 Jan 2009 14:42:43 +0000 (09:42 -0500)
committerEevee <eevee@nyarumaa.(none)>
Wed, 7 Jan 2009 14:42:43 +0000 (09:42 -0500)
Changed Grid.is_filled() to a property.
Removed Grid.check(), which didn't do anything anyway.
Cleaned up Grid.solve() slightly and fixed an egregious logic error.
Clarified Cell.normalize() docstring.

pseudoku/grid/__init__.py
pseudoku/grid/cell.py
pseudoku/tests/integrity/simple.py

index caf135d..83e358f 100644 (file)
@@ -82,6 +82,13 @@ class Grid(object):
     size = property(attrgetter('_size'))
     constraints = property(attrgetter('_constraints'))
 
     size = property(attrgetter('_size'))
     constraints = property(attrgetter('_constraints'))
 
+    def _is_filled(self):
+        for cell in self._cells:
+            if cell.value == None:
+                return False
+        return True
+    filled = property(_is_filled)
+
     ### Constructors
 
     def __init__(self, box_height=3, box_width=None):
     ### Constructors
 
     def __init__(self, box_height=3, box_width=None):
@@ -180,29 +187,14 @@ class Grid(object):
     def cell(self, row, column):
         return self._cells[self._cellidx(row, column)]
 
     def cell(self, row, column):
         return self._cells[self._cellidx(row, column)]
 
-    def is_filled(self):
-        for cell in self._cells:
-            if cell.value == None:
-                return False
-        return True
-
     ### Solving
 
     ### Solving
 
-    def check(self):
-        """Returns True iff the grid is solved.  Raises an exception if an
-        integrity problem is found, such as a value appearing twice in a row.
-        """
-        # TODO remove this; name sucks and concept also sucks
-        return None
-
-
     def normalize_cells(self):
         """Normalizes every cell in the grid.
         
         Returns the number of cell changes."""
 
         cell_changes = 0
     def normalize_cells(self):
         """Normalizes every cell in the grid.
         
         Returns the number of cell changes."""
 
         cell_changes = 0
-
         for cell in self._cells:
             cell_changes += cell.normalize()
 
         for cell in self._cells:
             cell_changes += cell.normalize()
 
@@ -215,7 +207,6 @@ class Grid(object):
         Returns the number of cell changes."""
 
         cell_changes = 0
         Returns the number of cell changes."""
 
         cell_changes = 0
-
         for constraint in self.constraints:
             cell_changes += constraint.resolve_uniques()
 
         for constraint in self.constraints:
             cell_changes += constraint.resolve_uniques()
 
@@ -229,18 +220,15 @@ class Grid(object):
     def solve(self):
         """Attempts to solve the grid by running through various methods of
         elimination one at a time, from simplest to most complex."""
     def solve(self):
         """Attempts to solve the grid by running through various methods of
         elimination one at a time, from simplest to most complex."""
-        # XXX track how many cells are changed and repeat as appropriate
-
-        while not self.is_filled():
-            cell_changes = 0
 
 
+        while True:
             for method in self._solution_steps:
             for method in self._solution_steps:
-                cell_changes += method(self)
+                cell_changes = method(self)
 
                 # If we changed something, start over with simple steps again
 
                 # If we changed something, start over with simple steps again
-                if x != 0:
+                if cell_changes:
                     break
 
                     break
 
-            # If we didn't do anything this round, we're stuck
-            if cell_changes == 0:
+            # If we didn't do anything this round, we're done
+            if not cell_changes:
                 break
                 break
index 66eb446..dd839c2 100644 (file)
@@ -47,8 +47,8 @@ class Cell(object):
 
 
     def normalize(self):
 
 
     def normalize(self):
-        """Checks to see if this cell has only one possible value left.  If
-        so, sets that as its value and eliminates it from every related cell.
+        """If this cell has been solved, eliminates its value as a candidate
+        from every other cell in every group it's in.
         This method is exhaustive; repeated calls should have no effect.
 
         Returns the number of cell changes.
         This method is exhaustive; repeated calls should have no effect.
 
         Returns the number of cell changes.
index c85f206..9558ba3 100644 (file)
@@ -14,7 +14,7 @@ class SimpleGridTestCase(TestCase):
         
         grid = Grid.from_string(puzzle)
         grid.solve()
         
         grid = Grid.from_string(puzzle)
         grid.solve()
-        self.assertTrue(grid.is_filled(), msg)
+        self.assertTrue(grid.filled, msg)
 
     def test_simple(self):
         self._test_single_puzzle("""
 
     def test_simple(self):
         self._test_single_puzzle("""