self._values = range(self._grid.size)
self._normalized = False
- def set_naively(self, value):
- """Sets the value of this cell, WITHOUT eliminating the value from
- every other cell in its row/column/box.
+ def set(self, value, normalize=True):
+ """Sets the value of this cell. If `normalize` is True or omitted, the
+ grid will be updated accordingly.
"""
-
self._values = [value]
-
- def set(self, value):
- """Sets the value of this cell and adjusts the grid accordingly."""
- self.set_naively(value)
- self._normalized = False
- self.normalize()
+ if normalize:
+ self._normalized = False
+ self.normalize()
self.normalize()
- def __str__(self):
- """Stringification for pretty-printing."""
- if self.value != None:
- return symbols[self.value]
-
- return '.'
-
-
class Grid(object):
"""Represents a Sudoku grid."""
value = rows[row][col]
if not value:
continue
- self.cell(row, col).set_naively(value - 1)
+ self.cell(row, col).set(value - 1, normalize=False)
return self
ch = grid[ self._cellidx(row, col) ]
if ch == '0':
continue
- self.cell(row, col).set_naively(symbols.index(ch))
+ self.cell(row, col).set(symbols.index(ch), normalize=False)
return self
"""Normalizes every cell in the grid."""
for cell in self._cells:
cell.normalize()
-
-
- def __str__(self):
- """Pretty-printing."""
- divider = '+'
- for box in xrange(self._box_height):
- for col in xrange(self._box_width):
- divider += '-'
- divider += '+'
-
- res = ''
- for row in xrange(self._size):
- if row % self._box_height == 0:
- res += divider
- res += "\n"
-
- for col in xrange(self._size):
- if col % self._box_width == 0:
- res += '|'
- res += str(self.cell(row, col))
-
- res += '|'
- res += "\n"
-
- res += divider
- res += "\n"
-
- return res