X-Git-Url: http://git.veekun.com/pseudoku.git/blobdiff_plain/625dc584bed28259ed4db1665b99c8d6f4261033..6c46caf7943b1beaadf32f5bbd78b1aee647b825:/pseudoku/grid/cell.py diff --git a/pseudoku/grid/cell.py b/pseudoku/grid/cell.py index 0262130..dd839c2 100644 --- a/pseudoku/grid/cell.py +++ b/pseudoku/grid/cell.py @@ -35,47 +35,58 @@ class Cell(object): def set(self, value, normalize=True): """Sets the value of this cell. If `normalize` is True or omitted, the grid will be updated accordingly. + + Returns the number of cell changes. """ self._values = [value] if normalize: self._normalized = False - self.normalize() - + return self.normalize() + 1 + + return 1 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. - This method is exhaustive; that repeated calls should have no effect. + """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. """ if self._normalized: # Already done - return + return 0 # Set this now just in case of infinite looping self._normalized = True if not self.solved: # Don't know the value yet - return + return 0 # Elimination time + cell_changes = 0 for constraint in self.constraints: for cell in constraint.cells: if cell == self: continue - cell.eliminate(self.value) + cell_changes += cell.eliminate(self.value) + return cell_changes def eliminate(self, value): - """Eliminates the given value as a possibility for this cell.""" - if value in self._values: - self._values.remove(value) + """Eliminates the given value as a possibility for this cell. + + Returns the number of cell changes.""" + if value not in self._values: + return 0 - if len(self._values) == 0: - # XXX give me a real exception here - raise Exception + self._values.remove(value) - self._normalized = False - self.normalize() + if len(self._values) == 0: + # XXX give me a real exception here + raise Exception + + self._normalized = False + return self.normalize() + 1