sudoku Module

Sudoku module

@author: Vincent Magnin and Norwid Behrnd

This module contains a set of procedures to generate sudokus grids and solve them.


Functions

public pure function is_full(grid)

Returns true if the grid is full.

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(9, 9) :: grid

Sudoky grid.

Return Value logical

public pure function valid_colum_or_row(vector)

Validation routines

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(1:9) :: vector

A row or a column

Return Value logical

public pure function valid_digit(grid, row, col)

Returns true if the row, column and region of a digit are all valid:

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(9, 9) :: grid

Sudoky grid.

integer, intent(in) :: row

Row number of the region.

integer, intent(in) :: col

Column number of the region.

Return Value logical

public pure function valid_grid(grid)

Check if the whole grid is valid.

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(9, 9) :: grid

Sudoku grid.

Return Value logical

public pure function valid_zone(region)

Validation of a zone/region.

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(1:3, 1:3) :: region

Sudoku’s subregion

Return Value logical


Subroutines

public subroutine cli_solver(grid, file)

Provides a solution for a puzzle passed by CLI:

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), dimension(9, 9) :: grid

Sudoku Grid

character(len=*), intent(in) :: file

Filepath

public subroutine create_filled_grid(grid)

Puzzle generators

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(out), dimension(9, 9) :: grid

Sudoku grid

public subroutine create_puzzle(grid, givens)

Creates a puzzle by brute force. But we are not 100% sure that the solution is unique (just a “high” probability).

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), dimension(9, 9) :: grid

Sudoku grid.

integer, intent(in) :: givens

Number of given digits in the puzzle.

public subroutine create_puzzle_with_unique_solution(grid, nb_empty)

Creates a minimal puzzle. Digits are randomly removed one by one. The process ends when it is not possible anymore to remove a digit while keeping a unique solution. The number of remaining digits is therefore a priori unknown.

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), dimension(9, 9) :: grid
integer, intent(out) :: nb_empty

public subroutine display_grid(grid)

Display a grid on the terminal.

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(9, 9) :: grid

public subroutine initialize_random_number_generator(user_seed)

System independent initialization of pseudo-random generator

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(in), optional :: user_seed

public pure subroutine list_possible_digits(grid, row, col, nb_possible, possible_digit)

Procedure to create a list of allowed digits in the present empty cell.

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(9, 9) :: grid

Sudoku grid

integer, intent(in) :: row

Row number

integer, intent(in) :: col

Column number

integer, intent(out) :: nb_possible
integer, intent(out), optional, dimension(1:9) :: possible_digit

public subroutine read_grid(grid, filename)

Read a grid from a provided the path to a file

Arguments

Type IntentOptional Attributes Name
integer, intent(out), dimension(9, 9) :: grid

Output grid

character(len=*), intent(in) :: filename

File path

public subroutine request_grid(grid)

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), dimension(9, 9) :: grid

public subroutine save_grid(grid, filename)

Input/Output routines

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(9, 9) :: grid
character(len=*), intent(in) :: filename

public subroutine solve_puzzle(grid)

Receives a puzzle grid and solves it.

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), dimension(9, 9) :: grid

Input problem grid and returns solved grid.

public pure subroutine sort(empty_cells, p, n)

Solver routines

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), dimension(1:81, 1:3) :: empty_cells
integer, intent(in) :: p

The sort starts at position p (included)

integer, intent(in) :: n

Number of empty cells in the list