X-Git-Url: http://git.veekun.com/pseudoku.git/blobdiff_plain/a8065344ec1b764d28481b2c654de43ecd7f6b81..6c46caf7943b1beaadf32f5bbd78b1aee647b825:/pseudoku/grid/constraints.py diff --git a/pseudoku/grid/constraints.py b/pseudoku/grid/constraints.py index 2495102..e1ea667 100644 --- a/pseudoku/grid/constraints.py +++ b/pseudoku/grid/constraints.py @@ -7,6 +7,11 @@ class Constraint(object): ### 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 ]) @@ -28,6 +33,14 @@ class Constraint(object): 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) @@ -43,7 +56,9 @@ class Constraint(object): 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): @@ -68,6 +83,8 @@ class Box(Constraint): class Row(Constraint): + is_linear = True + def __init__(self, grid, position): self._grid = ref(grid) self._pos = position @@ -77,6 +94,8 @@ class Row(Constraint): 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 @@ -86,6 +105,8 @@ class Column(Constraint): 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