Print stuff in backtracking module only; clean up the \r stuff

This commit is contained in:
Eryn Wells 2017-10-11 20:53:10 -07:00
parent 3b380b17d2
commit a7dda24015
2 changed files with 6 additions and 4 deletions

View file

@ -162,7 +162,6 @@ class Sudoku:
if idx in self._clues: if idx in self._clues:
raise SquareIsClue('Cannot set clue square ({},{})'.format(x, y)) raise SquareIsClue('Cannot set clue square ({},{})'.format(x, y))
self._board[idx] = value self._board[idx] = value
print('({},{}) <- {} {!r}'.format(x, y, value, self))
def _xy_to_idx(self, x, y): def _xy_to_idx(self, x, y):
return y * self.row_size + x return y * self.row_size + x

View file

@ -26,8 +26,10 @@ def _solve_square(sudoku, x, y):
continue continue
except NoPossibleValues: except NoPossibleValues:
# Need to backtrack. # Need to backtrack.
sudoku.unset(x, y) should_backtrack = True
raise Backtrack() break
print('\r{!r}'.format(sudoku), end='', flush=True)
next_coord = _next_coord(sudoku, x, y) next_coord = _next_coord(sudoku, x, y)
if not next_coord: if not next_coord:
@ -40,13 +42,14 @@ def _solve_square(sudoku, x, y):
continue continue
if should_backtrack: if should_backtrack:
# Unhandled backtrack. Pop out of this one too.
try: try:
sudoku.unset(x, y) sudoku.unset(x, y)
except SquareIsClue: except SquareIsClue:
pass pass
raise Backtrack() raise Backtrack()
print()
return sudoku return sudoku
def _next_coord(sudoku, x, y): def _next_coord(sudoku, x, y):