From: Eevee Date: Wed, 7 Jan 2009 13:40:03 +0000 (-0500) Subject: Added a Diagonal constraint. X-Git-Url: http://git.veekun.com/pseudoku.git/commitdiff_plain/bf40f463be2e8809afbaed42abdd00105053a19d Added a Diagonal constraint. Also fixed a bug when rendering: value 1 would be shown as a space. --- diff --git a/pseudoku/__init__.py b/pseudoku/__init__.py index 853904b..2fe15f3 100644 --- a/pseudoku/__init__.py +++ b/pseudoku/__init__.py @@ -1,4 +1,5 @@ from grid import Grid +from grid.cellgroup import Diagonal import render.text def main(): @@ -14,6 +15,9 @@ def main(): 1...6...9 """) + grid.add_constraint(Diagonal(grid, direction='down', offset=0)) + grid.add_constraint(Diagonal(grid, direction='up', offset=0)) + r = render.text.AsciiArtGridRenderer() print r.render_grid(grid) diff --git a/pseudoku/grid/cellgroup.py b/pseudoku/grid/cellgroup.py index 374bd20..841a490 100644 --- a/pseudoku/grid/cellgroup.py +++ b/pseudoku/grid/cellgroup.py @@ -84,3 +84,28 @@ class Column(CellConstraint): self._cells = [] for row in xrange(self.grid._size): self._cells.append(ref(self.grid.cell(row, self._pos))) + +class Diagonal(CellConstraint): + def __init__(self, grid, direction='down', offset=0): + self._grid = ref(grid) + self._direction = direction + self._offset = offset + + if direction == 'down': + coords = {'row': 0, 'column': 0} + increment = 1 + elif direction == 'up': + coords = {'row': 0, 'column': grid.size - 1} + increment = -1 + else: + raise Exception # XXX + + # XXX + if offset != 0: + raise NotImplementedError + + self._cells = [] + for i in xrange(self.grid.size): + self._cells.append(ref(self.grid.cell(coords['row'], coords['column']))) + coords['row'] += 1 + coords['column'] += increment diff --git a/pseudoku/render/__init__.py b/pseudoku/render/__init__.py index a3593df..4b6bd65 100644 --- a/pseudoku/render/__init__.py +++ b/pseudoku/render/__init__.py @@ -83,7 +83,7 @@ class GridRenderer(object): def inside_cell(self, cell): """Content used to represent the actual cell value.""" - if cell.value: + if cell.solved: return symbols[cell.value] else: return '.' diff --git a/pseudoku/render/text.py b/pseudoku/render/text.py index 3a49134..2c8a6fd 100644 --- a/pseudoku/render/text.py +++ b/pseudoku/render/text.py @@ -31,7 +31,7 @@ class AsciiArtGridRenderer(SquareGridRenderer): return '|\n' def inside_cell(self, cell): - if cell.value: + if cell.solved: return symbols[cell.value] else: return ' '