Coding7 min read

How to Code a Sudoku Generator That Stays Unique

To code a sudoku generator you build a complete valid grid first, then carefully remove symbols while checking that exactly one solution survives each removal. Generation is really two familiar problems joined together, filling a grid and counting solutions, so most of the code is machinery you already have from a solver.

What a generator must guarantee

A good puzzle has three properties: it follows Sudoku rules, it has a solution, and that solution is unique. The third is the hard one, because a puzzle with two valid completions is considered broken, logic alone cannot choose between them. So when you code a sudoku generator, the uniqueness check is the core of the whole process, not optional polish. Begin by building a complete solved grid: start from an empty board and fill it using the same backtracking solver you would use to solve a puzzle, but randomize the order in which you try symbols in each cell. Randomizing the candidate order means each run produces a different finished grid, all of them valid. A common speed trick is to fill the three diagonal 3 by 3 boxes first, since they share no row or column, then let backtracking complete the rest with far fewer conflicts. This finished grid becomes your answer key.

How to code a sudoku generator: digging holes

  1. Copy the full solved grid; this copy is the answer key you never change.
  2. List all 81 cell positions and shuffle them into a random order.
  3. Walk the shuffled list and temporarily remove the symbol from the current cell.
  4. Run a solution counter on the resulting board, stopping the moment it finds a second solution.
  5. If exactly one solution remains, keep the cell empty; if a second appeared, put the symbol back.
  6. Continue until you have removed as many symbols as your difficulty target allows.

The counter in that loop is just a modified solver that does not stop at the first answer. Give it a limit of two, because the instant it reaches two solutions you can abort; you only need to know whether the answer is unique, not how many answers exist. This makes the check cheap even though counting every solution in general could be slow. When you code a sudoku generator this way, each accepted removal is proven safe, so the finished puzzle is guaranteed to have one and only one solution. Order matters less than you might expect: because you always verify uniqueness before committing a removal, you can never accidentally create an ambiguous board. The trade-off is time, since each removal triggers a solve, but for a single 9 by 9 puzzle this is fast on any modern machine.

Controlling difficulty and rendering shapes

The number of remaining clues is a rough dial, but it is not the whole story, since two puzzles with the same clue count can feel very different. A more honest measure is which solving techniques a human needs. Puzzles solvable with singles feel easy, while puzzles that demand pairs, pointing, or deeper chains feel hard. A practical approach is to run a logic based solver that only applies human style techniques and rate each puzzle by the hardest technique it required, which lets you sort output into clean difficulty tiers instead of guessing from clue counts alone.

  • Easy: solvable with naked and hidden singles alone.
  • Medium: needs locked candidates and naked pairs.
  • Hard: needs hidden subsets or box line reduction.
  • Extreme: needs advanced chains, yet must still be solvable without pure guessing.

Because the generator works on internal indices, the visible symbols are only a skin. Swap the digits for nine shapes and you have a shape based puzzle with identical guarantees, which is exactly how Shapedoku produces its boards: the logic ensures a single solution while the shapes make the grid feel calm and playful instead of numeric. You can play the shape version at app.shapedoku.com, and shapedoku.com offers printable puzzles made with the same uniqueness rules. The mental model to keep is short: build a full grid, dig holes one at a time, and never accept a removal that lets a second solution sneak in. If you can already solve and count solutions, then to code a sudoku generator you mostly reuse code you have, wrapped in a shuffle and a uniqueness guard, which is why writing the solver first pays off.

Ready to put it into practice?

Play Shapedoku free in your browser. No download, no login, just colorful shape Sudoku.

Play the Web App

Keep reading