CS 3060: Programming Languages

Prolog

Robert C. Green II, Ph.D.

Day 3

Rain Man

Solving Soduko

                        

soduko(Puzzle, Solution).

sudoku([
_, _, 2, 3,
_, _, _, _,
_, _, _, _,
3, 4, _, _],
Solution).

                        
                    

Solving Soduko

  • For a solved puzzle, the numbers in the puzzle and solution should be the same.
  • A Sudoku board is a grid of sixteen cells, with values from 1-4.
  • The board has four rows, four columns, and four squares.
  • A puzzle is valid if the elements in each row, column, and square has no repeated elements

Solving Soduko

Attempt 1: Rule #1

                        
sudoku(Puzzle, Solution) :- Solution = Puzzle.
                        
                    
What case does this solve?
When does it fail?

Solving Soduko

Attempt 2: Rule #2

                        
sudoku(Puzzle, Solution) :- Solution = Puzzle, 
                            fd_domain(Puzzle, 1, 4).
                        
                    
What case does this solve?
When does it fail?

Solving Soduko

Attempt 3: Rule #3

sudoku4.pl
What case does this solve?
When does it fail?

Assignment

Continue the Assignment

Reflect on Prolog

Resources

  • http://www.lix.polytechnique.fr/~liberti/public/computing/prog/prolog/prolog-tutorial.html
  • http://www.cpp.edu/~jrfisher/www/prolog_tutorial/contents.html