Sudoku solver and puzzle generator.

Summary

sudoku | Sudoku solver and puzzle generator. |

Functions | |

solveSudoku | This solver uses the above implementation to efficiently solve a Sudoku puzzle. |

makeSolution | This function makes a random solution to a Sudoku puzzle. |

makePuzzle | This function makes a puzzle (a partially filled grid), given a solution (completely filled grid) and a number of cells to leave filled. |

FUNCTION makeSolution(): multiarray.ArrayNumber2D

This function makes a random solution to a Sudoku puzzle.

It does this by filling in the first row randomly, and then calling the solver. Due to the way the solver works, this produces a bias which can be observed by looking at the first few numbers in the second row. The first three numbers will be the lowest numbers that don’t appear in the first three cells of the first row, in order. An improvement would be to remove this bias.

FUNCTION makePuzzle( solution: multiarray.ArrayNumber2D, filled: Number, callback: FUNCTION(n: Number) ): multiarray.ArrayNumber2D

This function makes a puzzle (a partially filled grid), given a solution (completely filled grid) and a number of cells to leave filled.

This works by removing pairs of numbers (symmetric around 180 degree rotation), checking the resulting puzzle to see that it still has only one solution, and repeating. If removing a pair of numbers results in a puzzle with more than one solution, the numbers are left in place and another pair is tried.

This function has considerable opportunity for improvements in efficiency. Also, it makes no attempt to estimate the “difficulty” of the resulting puzzle.

sudoku

This solver uses the above implementation to efficiently solve a Sudoku puzzle.

FUNCTION solveSudoku( INOUT grid: multiarray.ArrayNumber2D ): Number

This function makes a random solution to a Sudoku puzzle.

FUNCTION makeSolution(): multiarray.ArrayNumber2D

This function makes a puzzle (a partially filled grid), given a solution (completely filled grid) and a number of cells to leave filled.

FUNCTION makePuzzle( solution: multiarray.ArrayNumber2D, filled: Number, callback: FUNCTION(n: Number) ): multiarray.ArrayNumber2D