863ca4e054b28c8044dd1a47b088aa97dfaa61f3
[pseudoku.git] / pseudoku / grid / cellgroup.py
1 from __future__ import division
2
3 from weakref import proxy
4
5 class CellGroup(object):
6 """Represents any group of cells in a grid."""
7
8 ### Accessors
9
10 cells = []
11
12 ### Methods
13 # XXX inherited __init__
14
15 def find_value(self, value):
16 """Returns the cells that can be a specific value."""
17 possible_cells = []
18 for cell in self.cells:
19 if value in cell._values:
20 possible_cells.append(cell)
21
22 if len(possible_cells) == 0:
23 raise Exception # XXX
24
25 return possible_cells
26
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)
31
32 if len(possible_cells) > 1:
33 # Not unique
34 continue
35
36 target_cell = possible_cells[0]
37 if target_cell.solved:
38 # Already seen this
39 # XXX this is what cache is for
40 continue
41
42 # Only cell in the group that can be value
43 target_cell.set(value)
44
45
46 class Box(CellGroup):
47 def _get_box_row(self):
48 return self._pos // self._grid._box_width
49 box_row = property(_get_box_row)
50
51 def _get_box_column(self):
52 return self._pos % self._grid._box_height
53 box_column = property(_get_box_column)
54
55 def _get_cells(self):
56 # XXX generator + docstring
57 cells = []
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))
63 return cells
64 cells = property(_get_cells)
65
66 def __init__(self, grid, position):
67 self._grid = proxy(grid)
68 self._pos = position
69
70
71 class Row(CellGroup):
72 def _get_cells(self):
73 # XXX generator + docstring
74 cells = []
75 for col in xrange(self._grid._size):
76 cells.append(self._grid.cell(self._pos, col))
77 return cells
78 cells = property(_get_cells)
79
80 def __init__(self, grid, position):
81 self._grid = proxy(grid)
82 self._pos = position
83
84
85 class Column(CellGroup):
86 def _get_cells(self):
87 # XXX generator + docstring
88 cells = []
89 for row in xrange(self._grid._size):
90 cells.append(self._grid.cell(row, self._pos))
91 return cells
92 cells = property(_get_cells)
93
94 def __init__(self, grid, position):
95 self._grid = proxy(grid)
96 self._pos = position