X-Git-Url: http://git.veekun.com/pseudoku.git/blobdiff_plain/282da8de86748e42e0891daaa7ce81aa423fed44:/pseudoku.py..bfc07962deafde17e2b41f8d659313b7746dca07:/lib/pseudoku/grid/__init__.py diff --git a/pseudoku.py b/lib/pseudoku/grid/__init__.py similarity index 76% rename from pseudoku.py rename to lib/pseudoku/grid/__init__.py index d48c656..d1906ae 100644 --- a/pseudoku.py +++ b/lib/pseudoku/grid/__init__.py @@ -4,106 +4,13 @@ from math import sqrt import re from weakref import proxy -symbols = [str(x + 1) for x in range(9)] + [chr(x + 97) for x in xrange(26)] +from cellgroup import Row, Column, Box +symbols = [str(x + 1) for x in range(9)] + [chr(x + 97) for x in xrange(26)] class GridSizeError(Exception): pass -class CellGroup(object): - """Represents any group of cells in a grid.""" - - ### Accessors - - cells = [] - - ### Methods - # XXX inherited __init__ - - def find_value(self, value): - """Returns the cells that can be a specific value.""" - possible_cells = [] - for cell in self.cells: - if value in cell._values: - possible_cells.append(cell) - - if len(possible_cells) == 0: - raise Exception # XXX - - return possible_cells - - def resolve_uniques(self): - for value in xrange(self._grid._size): - # XXX cache values that are taken care of - possible_cells = self.find_value(value) - - if len(possible_cells) > 1: - # Not unique - continue - - target_cell = possible_cells[0] - if target_cell.solved: - # Already seen this - # XXX this is what cache is for - continue - - # Only cell in the group that can be value - target_cell.set(value) - - -class Box(CellGroup): - def _get_box_row(self): - return self._pos // self._grid._box_width - box_row = property(_get_box_row) - - def _get_box_column(self): - return self._pos % self._grid._box_height - box_column = property(_get_box_column) - - def _get_cells(self): - # XXX generator + docstring - cells = [] - for row in xrange(self._grid._box_height): - for col in xrange(self._grid._box_width): - cell_row = row + self.box_row * self._grid._box_height - cell_col = col + self.box_column * self._grid._box_width - cells.append(self._grid.cell(cell_row, cell_col)) - return cells - cells = property(_get_cells) - - def __init__(self, grid, position): - self._grid = proxy(grid) - self._pos = position - - -class Row(CellGroup): - def _get_cells(self): - # XXX generator + docstring - cells = [] - for col in xrange(self._grid._size): - cells.append(self._grid.cell(self._pos, col)) - return cells - cells = property(_get_cells) - - def __init__(self, grid, position): - self._grid = proxy(grid) - self._pos = position - - -class Column(CellGroup): - def _get_cells(self): - # XXX generator + docstring - cells = [] - for row in xrange(self._grid._size): - cells.append(self._grid.cell(row, self._pos)) - return cells - cells = property(_get_cells) - - def __init__(self, grid, position): - self._grid = proxy(grid) - self._pos = position - - class Cell(object): """Represents a single cell/value within a sudoku grid.""" @@ -211,8 +118,6 @@ class Cell(object): return '.' - - class Grid(object): """Represents a Sudoku grid.""" @@ -334,7 +239,6 @@ class Grid(object): # Figure out the length of one side/box size = int(sqrt(len(grid))) if size ** 2 != len(grid): - # XXX raise GridSizeError("Provided string does not form a square") # Set height/width.. @@ -413,23 +317,3 @@ class Grid(object): res += "\n" return res - - -grid = Grid.from_string("..... ..... ..... ..... ....") -grid = Grid.from_string(""" - ...69.... - 9.5..876. - ..4..1.2. - 6...5...3 - 38.....49 - 7...3...2 - .7.9..3.. - .231..4.8 - ....83... -""") - -print grid - -grid.solve() - -print grid