To gain deeper experience programming in Prolog, and to implement a simple Sudoku solver.
Use the latest version of SWI-Prolog, as in the previous Prolog lab.
Template code (described below).
You will write a program in prolog that can solve 9×9 Sudoku puzzles. We've provided template code to help solve the problem. To solve this problem, fill in the procedure named solve
.
solve
takes a list of nine lists. Each list represents a row in the puzzle. Each item in the list is either an integer 1 to 9 or an unbound variable.
You must meet the following requirements:
clpfd
library. (The library was written around Sudoku, and a solution to Sudoku using the library is provided in the documentation.)You should turn in a single file consisting of your code.
The goal of Sudoku is to fill in all the empty squares of the grid with respect to the following constraints:
The idea is to implement a (mostly) naive solution, that follows this general pattern:
is_set/1
in the lists library :- use_module(library(lists)).
predicate should be useful. (nonvar(S00); var(S00), digit(S00), is_set(Row0), is_set(Col0), is_set(Cub0))
will check if a single square is correct, where S00 is the spot at (0,0) and Row0, Col0, and Cub0 are the first row, col, and cube respectively. You shouldn't explicitly make variables for each of them, but access each space recursively. Do this for each spot, and use lists and helpers to traverse through the board.
If your code takes longer than a minute to run, consider the following ideas:
getCube/3
and columnAsList/3
helper functions are expensive and should not be repeated often.