When I started building a Sudoku generator, I did what most tutorials do: generate a full grid, dig out holes, and call the puzzle "hard" if it had few givens left. It took about a week of playtesting to learn that hole count is a terrible difficulty metric. A 30-given puzzle can collapse in two minutes if every step is a naked single, while a 27-given puzzle can stall an experienced solver for half an hour because one step needs an X-Wing. The number of holes tells you how empty the grid looks. It says almost nothing about how hard the grid thinks.
Here is the approach I ended up with, running entirely in the browser.
Step 1: Fill a complete grid with backtracking
Generation starts from an empty grid and a plain backtracking filler. At each empty cell, try the digits 1–9 in a shuffled order, keep the first digit that doesn't conflict with the row, column, or 3×3 box, and recurse. On conflict, backtrack. The shuffle is what makes each completed grid different; without it you get the same canonical grid every time.
function fill(grid: Grid): boolean { const cell = findEmptyCell(grid); if (!cell) return true; // grid complete for (const d of shuffled([1,2,3,4,5,6,7,8,9])) { if (isLegal(grid, cell, d)) { grid[cell.r][cell.c] = d; if (fill(grid)) return true; grid[cell.r][cell.c] = 0; } } return false; } This is fast enough that a full grid costs a few milliseconds. The interesting work is in the next two steps.
Step 2: Dig holes, but guard uniqueness
A Sudoku with two solutions isn't a hard puzzle, it's a broken one. So digging is paired with a solution counter: remove a pair of cells (I dig in 180° rotational symmetry, which looks better and plays better), then run the solver in counting mode. If the counter finds a second solution, restore the pair and try elsewhere. The counter stops at 2 — you only need to know "unique" vs "not unique", so there's no reason to enumerate further.
The digging stops at a target given count per tier — but as we'll see, that count is a budget, not the grade.
Step 3: Grade with a solver that thinks like a human
This is the part that fixed my difficulty problem. Alongside the backtracking solver, I keep a logical solver that only applies human techniques, in a fixed order of complexity:
- Naked singles and hidden singles
- Naked pairs / hidden pairs
- Pointing pairs and box-line reduction
- X-Wing
- XY-Wing, simple colouring
- Swordfish and beyond
The grader runs the logical solver on the dug puzzle. If it solves the grid, the puzzle's score is driven by the hardest technique it had to use, plus how many steps needed techniques above the singles level. If the logical solver gets stuck, the puzzle requires guessing — and for a logic-puzzle site, that puzzle is rejected outright, whatever its hole count.
The score then maps to tiers. On NetSudoku, the engine bands work out roughly as: Easy is singles only, Medium introduces pairs, Hard needs hidden pairs and box-line work, Expert reaches X-Wings and XY-Wings, and the top tier needs fish patterns. Two puzzles with identical given counts routinely land in different tiers — which matches how they actually feel to solve.
Why grade this way?
Because players don't experience "number of givens". They experience the hardest moment of the solve — the step where the obvious moves run out. A generator that grades on that moment produces tiers that feel honest: Easy really is a warm-up, and the top tier really does demand the full toolkit.
Everything runs client-side in TypeScript: generation, uniqueness checking, and grading happen in the browser in well under a second, so a fresh graded puzzle costs no server round-trip. If you want to see the output of this pipeline, the daily puzzle and the difficulty tiers on NetSudoku are generated exactly this way.