Function Repository Resource:

DisplaySudokuPuzzle

Source Notebook

Display a square matrix in a sudoku-puzzle format

Contributed by: Paritosh Mokhasi

ResourceFunction["DisplaySudokuPuzzle"][mat]

displays the matrix mat as a sudoku board.

Details and Options

The input matrix must be square and the row length must likewise be the square of an integer (e.g. 9, 16, …).
Some values can be left "blank" by using a SparseArray that omits them. Using the underscore character as the default element gives a nice presentation.
No check is made to determine whether the input values actually lie in the appropriate range or satisfy the requirements of a sudoku solution.

Examples

Basic Examples (1) 

Display an unsolved sudoku board:

In[1]:=
ResourceFunction["DisplaySudokuPuzzle"][
 SparseArray[{{1, 3} -> 5, {1, 4} -> 3, {2, 1} -> 8, {2, 8} -> 2, {3, 2} -> 7, {3, 5} -> 1, {3, 7} -> 5, {4, 1} -> 4, {4, 6} -> 5, {4, 7} -> 3, {5, 2} -> 1, {5, 5} -> 7, {5, 9} -> 6, {6, 3} -> 3, {6, 4} -> 2, {6, 8} -> 8, {7, 2} -> 6, {7, 4} -> 5, {7, 9} -> 9, {8, 3} -> 4, {8, 8} -> 3, {9, 6} -> 9, {9, 7} -> 7}, {9, 9}, _]]
Out[1]=

Scope (2) 

Display a 1616 sudoku board:

In[2]:=
ResourceFunction["DisplaySudokuPuzzle"][
 Block[{row, n = 4}, row = {
RandomSample[
Range[n^2], n^2]}; Apply[Join, 
Table[row = NestList[RotateLeft[#, n]& , 
RotateLeft[
Last[row], 1], n - 1], n]]]]
Out[2]=

Use an empty string to show elements that are not filled in:

In[3]:=
ResourceFunction["DisplaySudokuPuzzle"][
 SparseArray[{{1, 3} -> 5, {1, 4} -> 3, {2, 1} -> 8, {2, 8} -> 2, {3, 2} -> 7, {3, 5} -> 1, {3, 7} -> 5, {4, 1} -> 4, {4, 6} -> 5, {4, 7} -> 3, {5, 2} -> 1, {5, 5} -> 7, {5, 9} -> 6, {6, 3} -> 3, {6, 4} -> 2, {6, 8} -> 8, {7, 2} -> 6, {7, 4} -> 5, {7, 9} -> 9, {8, 3} -> 4, {8, 8} -> 3, {9, 6} -> 9, {9, 7} -> 7}, {9, 9}, ""]]
Out[3]=

Publisher

Paritosh Mokhasi

Version History

  • 1.0.0 – 19 May 2020

Related Resources

License Information