projects
/
pseudoku.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Documented constraints.diagonal test more better.
[pseudoku.git]
/
pseudoku
/
grid
/
constraints.py
diff --git
a/pseudoku/grid/constraints.py
b/pseudoku/grid/constraints.py
index
2495102
..
e1ea667
100644
(file)
--- a/
pseudoku/grid/constraints.py
+++ b/
pseudoku/grid/constraints.py
@@
-7,6
+7,11
@@
class Constraint(object):
### Accessors
### 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 ])
# 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):
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)
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
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 Box(Constraint):
@@
-68,6
+83,8
@@
class Box(Constraint):
class Row(Constraint):
class Row(Constraint):
+ is_linear = True
+
def __init__(self, grid, position):
self._grid = ref(grid)
self._pos = position
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):
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
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):
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
def __init__(self, grid, direction='down', offset=0):
self._grid = ref(grid)
self._direction = direction