Games
Sudoku
Enigmistics.Sudoku — TypeSudokuStructure for the Sudoku puzzle game.
Rules: fill the grid with numbers from 1 to n (where n is the dimension of the grid) so that each number appears exactly once in each row, column, and sector. (Sectors are defined by the sector_col_span and sector_row_span parameters.) Empty cells are represented with 0s.
Enigmistics.generate_sudoku — Functiongenerate_sudoku(sector_row_span::Int, sector_col_span::Int; seed=rand(Int),
holes_num::Int=0, holes_pct::Float64=0.0, symmetry=false, double_symmetry=false)Generates a valid, randomized Sudoku puzzle.
Arguments
sector_row_span,sector_col_span: dimensions of the sub-gridsholes_num: absolute number of empty cells to createholes_pct: percentage of empty cells to create (alternative to the previous argument, will be ignored ifholes_numis provided)symmetry: whether to leave holes in a symmetric structuredouble_symmetry: whether to leave holes in a double symmetric structureseed: optional seed for reproducibility
Examples
julia> generate_sudoku(2, 3, seed=1)
[ Info: seed = 1
┌───────────────┐
│ 2 5 4 │ 3 1 6 │
│ 3 1 6 │ 2 5 4 │
│───────#───────│
│ 5 4 3 │ 1 6 2 │
│ 1 6 2 │ 5 4 3 │
│───────#───────│
│ 6 2 5 │ 4 3 1 │
│ 4 3 1 │ 6 2 5 │
└───────────────┘
julia> generate_sudoku(4, 4, seed=1, holes_pct=0.3, symmetry=true)
[ Info: seed = 1
┌───────────────────────────────────────────────────────┐
│ 12 ⋅ ⋅ 15 │ 14 8 ⋅ 10 │ ⋅ 7 5 1 │ 13 ⋅ ⋅ 9 │
│ ⋅ 8 4 14 │ ⋅ 7 1 2 │ 16 3 13 9 │ 15 12 6 11 │
│ 16 3 9 13 │ 15 6 11 12 │ 10 ⋅ ⋅ ⋅ │ ⋅ ⋅ 7 ⋅ │
│ 2 7 1 ⋅ │ 13 3 9 16 │ 12 ⋅ 15 11 │ ⋅ 10 8 4 │
│─────────────#─────────────#─────────────#─────────────│
│ 8 ⋅ 15 12 │ 10 4 ⋅ 7 │ 3 1 ⋅ ⋅ │ 16 ⋅ 9 13 │
│ 7 4 14 10 │ 2 1 5 3 │ 6 9 16 ⋅ │ 12 ⋅ 11 15 │
│ ⋅ 9 13 ⋅ │ 12 11 15 ⋅ │ 7 ⋅ ⋅ 14 │ 2 ⋅ ⋅ 5 │
│ 3 ⋅ ⋅ ⋅ │ 16 ⋅ ⋅ ⋅ │ ⋅ 11 12 15 │ 10 7 4 ⋅ │
│─────────────#─────────────#─────────────#─────────────│
│ ⋅ 13 16 6 │ 8 15 12 ⋅ │ ⋅ ⋅ ⋅ 10 │ ⋅ ⋅ ⋅ 2 │
│ 4 ⋅ ⋅ 8 │ 7 ⋅ ⋅ 1 │ ⋅ 5 3 2 │ ⋅ 11 13 ⋅ │
│ 1 14 ⋅ 7 │ ⋅ 5 2 9 │ 11 13 6 16 │ 8 4 15 12 │
│ 9 5 ⋅ 3 │ ⋅ ⋅ 16 11 │ 4 ⋅ 8 12 │ 7 1 ⋅ 10 │
│─────────────#─────────────#─────────────#─────────────│
│ 15 16 6 ⋅ │ 4 12 ⋅ 14 │ 5 10 1 7 │ ⋅ 13 2 3 │
│ ⋅ 10 ⋅ ⋅ │ ⋅ ⋅ ⋅ 13 │ 15 16 11 6 │ 4 14 12 8 │
│ 14 12 8 4 │ 1 10 7 5 │ 13 2 9 ⋅ │ 11 15 16 ⋅ │
│ 13 ⋅ ⋅ 9 │ 11 16 6 ⋅ │ 14 ⋅ 4 8 │ 1 ⋅ ⋅ 7 │
└───────────────────────────────────────────────────────┘Enigmistics.show_sudoku — Functionshow_sudoku(io::IO, sdk::Sudoku)Display the Sudoku puzzle.
Examples
julia> show_sudoku(generate_sudoku(3, 3, seed=1))
[ Info: seed = 1
┌───────────────────────┐
│ 7 2 8 │ 5 1 6 │ 4 3 9 │
│ 6 5 1 │ 3 4 9 │ 8 2 7 │
│ 9 3 4 │ 2 8 7 │ 1 5 6 │
│───────#───────#───────│
│ 8 9 3 │ 7 2 1 │ 5 6 4 │
│ 1 7 2 │ 6 5 4 │ 3 9 8 │
│ 4 6 5 │ 9 3 8 │ 2 7 1 │
│───────#───────#───────│
│ 5 1 7 │ 4 6 3 │ 9 8 2 │
│ 2 8 9 │ 1 7 5 │ 6 4 3 │
│ 3 4 6 │ 8 9 2 │ 7 1 5 │
└───────────────────────┘Enigmistics.is_valid — Functionis_valid(s::Sudoku)Check if the current grid violates any sudoku rules. Empty cells (0s) are ignored, so also partially filled grids can be validated.
Enigmistics.is_solved — Functionis_solved(s::Sudoku)Check if the sudoku is solved, i.e. if it is valid and has no empty cells left.
Enigmistics.solve! — Functionsolve!(sdk::Sudoku, solutions=Vector{Sudoku}())Solve the sudoku puzzle through recursion, returning all the found solutions.
Examples
julia> sdk = generate_sudoku(3, 3, holes_pct=0.4, seed=1, symmetry=true)
[ Info: seed = 1
┌───────────────────────┐
│ ⋅ 2 ⋅ │ 5 1 ⋅ │ ⋅ 3 ⋅ │
│ 6 ⋅ ⋅ │ 3 4 9 │ 8 2 ⋅ │
│ 9 ⋅ ⋅ │ 2 ⋅ ⋅ │ 1 ⋅ ⋅ │
│───────#───────#───────│
│ 8 9 ⋅ │ ⋅ 2 1 │ 5 ⋅ 4 │
│ 1 7 2 │ 6 5 4 │ 3 9 8 │
│ 4 ⋅ 5 │ 9 3 ⋅ │ ⋅ 7 1 │
│───────#───────#───────│
│ ⋅ ⋅ 7 │ ⋅ ⋅ 3 │ ⋅ ⋅ 2 │
│ ⋅ 8 9 │ 1 7 5 │ ⋅ ⋅ 3 │
│ ⋅ 4 ⋅ │ ⋅ 9 2 │ ⋅ 1 ⋅ │
└───────────────────────┘
julia> solve!(sdk)
1-element Vector{Sudoku}:
Sudoku([7 2 … 3 9; 6 5 … 2 7; … ; 2 8 … 4 3; 3 4 … 1 5], 3, 3)
julia> sdk = generate_sudoku(3, 3, holes_pct=0.5, seed=1, symmetry=true)
[ Info: seed = 1
┌───────────────────────┐
│ ⋅ 2 ⋅ │ ⋅ 1 ⋅ │ ⋅ ⋅ ⋅ │
│ 6 ⋅ ⋅ │ 3 4 9 │ 8 2 ⋅ │
│ ⋅ ⋅ ⋅ │ 2 ⋅ ⋅ │ ⋅ ⋅ ⋅ │
│───────#───────#───────│
│ 8 9 ⋅ │ ⋅ 2 1 │ 5 ⋅ 4 │
│ 1 7 2 │ 6 5 4 │ 3 9 8 │
│ 4 ⋅ 5 │ 9 3 ⋅ │ ⋅ 7 1 │
│───────#───────#───────│
│ ⋅ ⋅ ⋅ │ ⋅ ⋅ 3 │ ⋅ ⋅ ⋅ │
│ ⋅ 8 9 │ 1 7 5 │ ⋅ ⋅ 3 │
│ ⋅ ⋅ ⋅ │ ⋅ 9 ⋅ │ ⋅ 1 ⋅ │
└───────────────────────┘
julia> solve!(sdk)
2-element Vector{Sudoku}:
Sudoku([7 2 … 3 9; 6 5 … 2 7; … ; 2 8 … 4 3; 3 4 … 1 5], 3, 3)
Sudoku([9 2 … 3 6; 6 5 … 2 7; … ; 2 8 … 4 3; 3 4 … 1 5], 3, 3)Abbicì
Enigmistics.Abbicì — TypeAbbicìStructure for the Abbicì puzzle game.
Rules: enter the letters A, B, C, and possibly D into the diagram, knowing that each letter appears only once in each row and column (thus possibly leaving some blank boxes). Letters outside the schema indicate which letter is closest to the border in the corresponding row or column.
Enigmistics.generate_abbicì — Functiongenerate_abbicì(letters=3, dim=5; check=false, seed=rand(Int))Generate a random Abbicì puzzle, with the specified number of letters (max 4) and dimension of the grid.
The check flag can be set to true to print the positions of the letters in the grid, for debugging purposes. The seed parameter can be used to reproduce specific puzzles.
Examples
julia> abc = generate_abbicì(3, 5, seed=1)
[ Info: seed = 1
B C C A B
┌───────────────┐
B │ ⋅ ⋅ ⋅ ⋅ ⋅ │ A
C │ ⋅ ⋅ ⋅ ⋅ ⋅ │ B
C │ ⋅ ⋅ ⋅ ⋅ ⋅ │ A
A │ ⋅ ⋅ ⋅ ⋅ ⋅ │ C
A │ ⋅ ⋅ ⋅ ⋅ ⋅ │ C
└───────────────┘
A B B C C Enigmistics.show_abbicì — Functionshow_abbicì(abc::Abbicì; solution=false)Display the Abbicì puzzle. If solution is set to true, the grid will show also the inside letters; otherwise they will be hidden by placeholders.
Examples
julia> show_abbicì(generate_abbicì(3, 5, seed=1), solution=true)
[ Info: seed = 1
B C C A B
┌───────────────┐
B │ B ⋅ C A ⋅ │ A
C │ ⋅ C A ⋅ B │ B
C │ C ⋅ ⋅ B A │ A
A │ ⋅ A B C ⋅ │ C
A │ A B ⋅ ⋅ C │ C
└───────────────┘
A B B C C
julia> show_abbicì(generate_abbicì(4, 7, seed=1), solution=true)
[ Info: seed = 1
B D C A B D C
┌─────────────────────┐
B │ B ⋅ C A ⋅ D ⋅ │ D
D │ ⋅ D A ⋅ B ⋅ C │ C
C │ ⋅ ⋅ ⋅ C A B D │ D
D │ D A B ⋅ ⋅ C ⋅ │ C
C │ ⋅ C ⋅ D ⋅ A B │ B
A │ A B D ⋅ C ⋅ ⋅ │ C
C │ C ⋅ ⋅ B D ⋅ A │ A
└─────────────────────┘
C B D B D A A