Function Repository Resource:

GenerateSudokuPuzzle

Source Notebook

Generate a sudoku puzzle

Contributed by: Paritosh Mokhasi

ResourceFunction["GenerateSudokuPuzzle"][]

generates a 9×9 sudoku puzzle with half of the elements exposed.

ResourceFunction["GenerateSudokuPuzzle"][d]

generates a d2×d2 sudoku puzzle with half of the elements exposed.

ResourceFunction["GenerateSudokuPuzzle"][d,p]

generates a d2×d2 sudoku puzzle with p percent of the elements exposed.

Details and Options

ResourceFunction["GenerateSudokuPuzzle"] returns a two-element List. The first element contains the completed/reference sudoku puzzle and the second element is the actual sudoku puzzle for solving.
The first argument specifies the size of the sudoku puzzle and must be a positive integer.
The second argument specifies the percentage of elements to be exposed. The second argument must be a number between 0 and 1.
The function generates a sudoku puzzle by starting with a full sudoku puzzle. From this, a randomly selected element from the puzzle is removed. The resulting puzzle is then solved with the additional constraint that the number removed cannot exist at that position. If the solver returns a result, it means that the solution is not unique and that element cannot leave the puzzle. If the solver comes back with a failed result, then that number at that position is unique and can be removed.

Examples

Basic Examples (2) 

Generate a standard sudoku puzzle:

In[1]:=
{refSudokuPuzzle, sudokuPuzzle} = ResourceFunction["GenerateSudokuPuzzle"][]
Out[1]=

Display the results using the resource function DisplaySudokuPuzzle:

In[2]:=
ResourceFunction["DisplaySudokuPuzzle"][#] & /@ {refSudokuPuzzle, sudokuPuzzle}
Out[2]=

Solve the resulting sudoku puzzle using the resource function SolveSudokuPuzzle to confirm that the reference puzzle is obtained:

In[3]:=
ResourceFunction["DisplaySudokuPuzzle"][
 ResourceFunction["SolveSudokuPuzzle"][sudokuPuzzle]]
Out[3]=

Generate a 1616 sudoku puzzle with 70% of the elements exposed:

In[4]:=
{refSudokuPuzzle, sudokuPuzzle} = ResourceFunction["GenerateSudokuPuzzle"][4, 0.7]
Out[4]=

Display the results using the resource function DisplaySudokuPuzzle:

In[5]:=
ResourceFunction["DisplaySudokuPuzzle"][#] & /@ {refSudokuPuzzle, sudokuPuzzle}
Out[5]=

Possible Issues (2) 

If the specified exposed percentage is too low then the function will return a puzzle with the smallest number of elements exposed such that the resulting sudoku puzzle is unique:

In[6]:=
{refSudokuPuzzle, sudokuPuzzle} = ResourceFunction["GenerateSudokuPuzzle"][3, 0]
Out[6]=
In[7]:=
ResourceFunction["DisplaySudokuPuzzle"][#] & /@ {refSudokuPuzzle, sudokuPuzzle}
Out[7]=

Solve the resulting sudoku puzzle using the resource function SolveSudokuPuzzle to confirm that the reference puzzle is obtained:

In[8]:=
ResourceFunction["DisplaySudokuPuzzle"][
 ResourceFunction["SolveSudokuPuzzle"][sudokuPuzzle]]
Out[8]=

Publisher

Paritosh Mokhasi

Version History

  • 1.0.0 – 21 May 2020

Related Resources

License Information