Sudoku/Solver/sudoku_printer.py

83 lines
1.9 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod:`sudoku_printer` module
:author: `FIL - IEEA - Univ. Lille1.fr <http://portail.fil.univ-lille1.fr>`_
:date: 2016, october
:Provides:
* print
:Example: with the grid
.. code-block:: text
+-------+-------+-------+
| 9 . 6 | . 2 8 | . . 3 |
| . 4 . | 5 . . | . . 1 |
| . 8 . | 9 . . | . 4 . |
+-------+-------+-------+
| 6 . . | . . . | . 7 . |
| . . 8 | 2 . 6 | 9 . . |
| . 3 . | . . . | . . 5 |
+-------+-------+-------+
| . 5 . | . . 3 | . 6 . |
| 1 . . | . . 2 | . 8 . |
| 2 . . | 8 7 . | 5 . . |
+-------+-------+-------+
>>> import sudoku_grid
>>> strgrid = '906028003040500001080900040600000070008206900030000005050003060100002080200870500'
>>> grid = sudoku_grid.from_string(strgrid)
>>> print(grid)
+-------+-------+-------+
| 9 . 6 | . 2 8 | . . 3 |
| . 4 . | 5 . . | . . 1 |
| . 8 . | 9 . . | . 4 . |
+-------+-------+-------+
| 6 . . | . . . | . 7 . |
| . . 8 | 2 . 6 | 9 . . |
| . 3 . | . . . | . . 5 |
+-------+-------+-------+
| . 5 . | . . 3 | . 6 . |
| 1 . . | . . 2 | . 8 . |
| 2 . . | 8 7 . | 5 . . |
+-------+-------+-------+
"""
import builtins
import sudoku_grid
LINE_SEP = ('+' + '-' * 7) * 3 + '+'
LINE_TO_FILL = '| {} {} {} ' * 3 + '|'
def print(grid):
for r in range(9):
if r % 3 == 0: builtins.print(LINE_SEP)
row = sudoku_grid.get_row(grid, r).replace(sudoku_grid.EMPTY_SYMBOL, '.')
builtins.print(LINE_TO_FILL.format(*row))
builtins.print(LINE_SEP)
def reset():
builtins.print("\x1b[2J", end='')
def color_print(grid):
reset()
for r in range(9):
if r % 3 == 0: builtins.print(LINE_SEP)
row = sudoku_grid.get_row(grid, r).replace(sudoku_grid.EMPTY_SYMBOL, '.')
builtins.print(LINE_TO_FILL.format(*row))
builtins.print(LINE_SEP)
if __name__ == '__main__':
import doctest
doctest.testmod ()