Sudoku Generation - Design Philosophy of Puzzle Auto-Generation Algorithms

·2 min read

Generating quality Sudoku puzzles automatically cannot be achieved by simply placing numbers randomly. This article explains how to guarantee unique solutions, control difficulty, and achieve aesthetically pleasing clue arrangements.

The Three Steps of Generation

Sudoku puzzle generation follows three steps: (1) Randomly generate a valid completed grid. (2) Remove cells one at a time, verifying that the puzzle still has a unique solution after each removal. (3) Analyze the difficulty of the resulting puzzle and confirm it matches the target difficulty. Steps 2 and 3 are the core quality-determining parts, where the bulk of computational cost is concentrated.

Guaranteeing a Unique Solution

For a puzzle to be valid, it must have exactly one solution. After each cell removal, you must verify that the remaining puzzle still has a unique solution. This verification uses a solver to count the number of solutions. As soon as two or more solutions are found, the puzzle is deemed non-unique and that cell removal is undone. This verification is computationally expensive but essential for guaranteeing puzzle quality.

Controlling Difficulty

Difficulty is defined by the complexity of techniques required to solve the puzzle. A human solving process is simulated on the generated puzzle to analyze which techniques are needed. If solvable by Naked Singles alone, it is classified as Easy; if Hidden Singles are needed, Medium; if Naked Pairs are needed, Hard; if X-Wing is needed, Master. If the difficulty does not match the target, the puzzle is discarded and regenerated.

Reproducibility Through Seed Values

To implement features like daily challenges where all users solve the same puzzle, a seed value is provided to the random number generator to ensure reproducibility. A seed is calculated from the date, and the same seed always produces the same puzzle. This eliminates the need to store puzzles on a server, as the same puzzle can be reproduced client-side.