### Accessors
+ # True iff a constraint consists of a straight line of cells. Used to
+ # quickly skip over pairs of constraints that cannot possibly have more
+ # than one cell in common.
+ is_linear = False
+
# NOTE: _cells is a list of refs; _grid is a ref
# XXX document this
cells = property(lambda self: [ x() for x in self._cells ])
return possible_cells
def resolve_uniques(self):
+ """Searches for values that can only appear in one cell, and sets them
+ appropriately.
+
+ Returns the number of cell changes.
+ """
+
+ cell_changes = 0
+
for value in xrange(self.grid.size):
# XXX cache values that are taken care of
possible_cells = self.find_value(value)
continue
# Only cell in the group that can be value
- target_cell.set(value)
+ cell_changes += target_cell.set(value)
+
+ return cell_changes
class Box(Constraint):
class Row(Constraint):
+ is_linear = True
+
def __init__(self, grid, position):
self._grid = ref(grid)
self._pos = position
self._cells.append(ref(self.grid.cell(self._pos, col)))
class Column(Constraint):
+ is_linear = True
+
def __init__(self, grid, position):
self._grid = ref(grid)
self._pos = position
self._cells.append(ref(self.grid.cell(row, self._pos)))
class Diagonal(Constraint):
+ is_linear = True
+
def __init__(self, grid, direction='down', offset=0):
self._grid = ref(grid)
self._direction = direction