Sudoku animation in SWI Prolog
Prolog code: sudoku.pl
Sample PostScript file, a self-contained saved animation
for a Sudoku puzzle (open it with "gv" or "gs" to view
it): solved.ps.gz
You need SWI-Prolog >
5.10 (recommended: latest git version). Start it with
swipl -f sudoku.pl
You also need to have the PostScript viewer "gs" installed.
Here are a few example queries that you can try:
?- problem(1, Rows), show([], Rows).
?- problem(3, Rows), show([ff], Rows).
?- show([], Rows).
The first argument is a list of labeling options as described in
SWI-Prolog's CLP(FD)
library documentation.
The second argument is a list of 9 rows that are to be
completed to a Sudoku Latin square. Each row is a list of 9
variables, which can also be already instantiated to integers to
fill in initial elements. The predicate can be used in all
directions: You can complete partial squares, test complete
squares, and enumerate all possible Sudoku Latin squares. As a
side-effect, you see an animation of the constraint solving
process. A few sample puzzles are included.
Here's an intermediate state:
And here is a picture of a finished animation:
Further reading:
Neumerkel
at
al., Visualizing
Solutions with Viewers.
Main page