863ca4e054b28c8044dd1a47b088aa97dfaa61f3
1 from __future__
import division
3 from weakref
import proxy
5 class CellGroup(object):
6 """Represents any group of cells in a grid."""
13 # XXX inherited __init__
15 def find_value(self
, value
):
16 """Returns the cells that can be a specific value."""
18 for cell
in self
.cells
:
19 if value
in cell
._values
:
20 possible_cells
.append(cell
)
22 if len(possible_cells
) == 0:
27 def resolve_uniques(self
):
28 for value
in xrange(self
._grid
._size
):
29 # XXX cache values that are taken care of
30 possible_cells
= self
.find_value(value
)
32 if len(possible_cells
) > 1:
36 target_cell
= possible_cells
[0]
37 if target_cell
.solved
:
39 # XXX this is what cache is for
42 # Only cell in the group that can be value
43 target_cell
.set(value
)
47 def _get_box_row(self
):
48 return self
._pos
// self
._grid
._box_width
49 box_row
= property(_get_box_row
)
51 def _get_box_column(self
):
52 return self
._pos % self
._grid
._box_height
53 box_column
= property(_get_box_column
)
56 # XXX generator + docstring
58 for row
in xrange(self
._grid
._box_height
):
59 for col
in xrange(self
._grid
._box_width
):
60 cell_row
= row
+ self
.box_row
* self
._grid
._box_height
61 cell_col
= col
+ self
.box_column
* self
._grid
._box_width
62 cells
.append(self
._grid
.cell(cell_row
, cell_col
))
64 cells
= property(_get_cells
)
66 def __init__(self
, grid
, position
):
67 self
._grid
= proxy(grid
)
73 # XXX generator + docstring
75 for col
in xrange(self
._grid
._size
):
76 cells
.append(self
._grid
.cell(self
._pos
, col
))
78 cells
= property(_get_cells
)
80 def __init__(self
, grid
, position
):
81 self
._grid
= proxy(grid
)
85 class Column(CellGroup
):
87 # XXX generator + docstring
89 for row
in xrange(self
._grid
._size
):
90 cells
.append(self
._grid
.cell(row
, self
._pos
))
92 cells
= property(_get_cells
)
94 def __init__(self
, grid
, position
):
95 self
._grid
= proxy(grid
)