Sudoku/main7.py

72 lines
2.1 KiB
Python
Executable File

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
:mod: module
:author: `Éric W`
:date: 2021, january
Create an animated GIF to show the resolution of a Sudoku grid.
"""
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 . . |
+-------+-------+-------+
'''
def usage():
print('Usage: {:s} <grid> <filename>'.format(sys.argv[0]))
print('where\n\t<grid> is a 81 characters string describing a valid sudoku grid')
print('\t<filename> is the basename of the animated GIF to create')
print('\nExample: with the grid')
print(GRID)
print('$ {:s} 906028003040500001080900040600000070008206900030000005050003060100002080200870500 images/mondrian_exple1_resolution'.format(sys.argv[0]))
exit(1)
if __name__ == '__main__':
import sys, os
import sudoku_grid, sudoku_solver, sudoku_mondrian
if len(sys.argv) != 3:
usage()
else:
strgrid = sys.argv[1]
try:
grid = sudoku_grid.SudokuGrid(strgrid)
except sudoku_grid.SudokuGridError:
print('grid description is not valid')
usage()
filename = sys.argv[2]
print('Sudoku to solve')
grid.pretty_print()
_ = input('Tapez entrée pour continuer')
with open(filename, 'wt') as sortie:
sols = sudoku_solver.solve_one(grid, pause=0, out_channel=sortie)
print('Résolution terminée.')
print('Création des images.')
with open(filename, 'rt') as entree:
images = list(sudoku_mondrian.colorie_sudoku(sudoku_grid.SudokuGrid(descr.rstrip()))
for descr in entree)
print('Création du GIF animé.')
images[0].save(filename+'.gif', save_all=True,
append_images=images, optimize=True,
duration=500, loop=1)
os.system('rm -f {:s}'.format(filename))
exit(0)