Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Sudoku

Abstract

Sudoku is a logic-based, combinatorial number-placement puzzle. The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (also called boxes, blocks, or regions) contain all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution. A typical sudoku puzzle and its solution are shown below.

The multiple sudoku consists of several overlapping sudoku grids. Like the normal sudoku each 9×9 grid must be filled as before, but some parts are shared between the grids, and these common parts must be filled while no law is broken in any of the grids they belong to.

This project solves both variants in Prolog, using constraint logic programming over finite domains. Instead of searching the grid cell by cell, the solver states the rules of the puzzle as constraints, every row, every column and every 3×3 block holding nine distinct values from 1 to 9, and lets the constraint solver narrow the candidates down before a search assigns the rest. The multiple version needs no new rules: the five grids simply share variables wherever they overlap, so one set of constraints covers them all. Five sample puzzles are included in the source.

To use this work on your researches or projects you need:

  • SWI-Prolog

To install SWI-Prolog:

First, check if you already have it installed or not.

swipl --version

If you don't have SWI-Prolog in your computer you can use the code below:

sudo apt-get update
sudo apt-get install swi-prolog

To run this project:

Load the source file:

swipl Source.pl

To solve one of the sample puzzles:

?- problem(0, Rows), sudoku_solver(Rows), maplist(label, Rows), maplist(portray_clause, Rows).

To solve the five overlapping grids together:

?- multiple_sudoku_solver(S0, S1, S2, S3, S4), maplist(maplist(label), [S0, S1, S2, S3, S4]).

About

A Sudoku solver in Prolog using constraint logic programming, for single and overlapping grids.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages