Table of Contents

Objective:

To gain deeper experience programming in Prolog, and to implement a simple Sudoku solver.


Prerequisites:

Use the latest version of SWI-Prolog, as in the previous Prolog lab.

Template code (described below).


Deliverables:

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:

You should turn in a single file consisting of your code.


Sudoku Explanation

The goal of Sudoku is to fill in all the empty squares of the grid with respect to the following constraints:

  1. Each 9-Square row must contain all numbers 1 to 9 (9 squares and 9 required numbers means no duplicates can exist in a row).
  2. Each 9-square column must contain all numbers 1 to 9 (9 squares and 9 required numbers means no duplicates can exist in a column).
  3. Each 9-square 3×3 subgrid must contain all numbers 1 to 9 (9 squares and 9 required numbers means no duplicates can exist in a subgrid).

The idea is to implement a (mostly) naive solution, that follows this general pattern:

  1. Bind each blank space to a number between 1 and 9.
  2. Ensure no Sudoku rules are violated.
  3. Repeat the previous two steps until a solution is found or we've tried all possible solutions.

Hints

Tips to Make Faster (if needed)

If your code takes longer than a minute to run, consider the following ideas: