Play5sDeveloper notes

Sudoku Unique Solutions and Difficulty Graded by Technique

Play5s Sudoku checks uniqueness with a bitmask solver that stops at a second solution, and sets the level by the hardest human technique the puzzle needs, not by the number of clues. Clues are removed in symmetric pairs and a removal is kept only while exactly one solution remains. A simulated human solver then solves the grid with singles, locked candidates, pairs, triples and fish, and the tier of the hardest step names the level from Easy to Evil. The Hint button uses the same solver.

This note covers the 9 by 9 Sudoku logic: the uniqueness check, the clue digger, the grader and the hint.

How do you check that a puzzle has exactly one solution?

Techniques the grader may need, bar length = weightFull house4T1Naked single8T1Hidden single14T1Pointing50T2Box-line reduction55T2Naked pair70T3Hidden pair90T3Naked triple110T4Hidden triple130T4X-Wing180T4XY-Wing220T5Swordfish260T5Forcing chain500T6EasyMediumHardExpertEvilA puzzle takes the level of the hardest tier it needs. A clue is dug out only while exactly one solution remains.
Techniques and weightsFull house4Naked single8Hidden single14Pointing50Box-line55Naked pair70Hidden pair90Naked triple110Hidden triple130X-Wing180XY-Wing220Swordfish260Forcing chain500EasyMediumHardExpertEvilThe hardest tier a puzzle needs sets its level.A clue is dug out only while one solution remains.
The techniques a puzzle may require, with the weights the grader adds up. The hardest tier a puzzle needs sets its level.

A Sudoku is only fair if it has one answer. Proving that by hand is hard, but a program can count: search for solutions and stop as soon as the count reaches two. countSolutions(board, limit = 2) does exactly that, and a puzzle is accepted when it returns 1.

The solver keeps the state in three small arrays of 9 integers, one per row, column and box. Each integer is a bit mask, with bit 0 for the digit 1 up to bit 8 for the digit 9. A cell's candidates are the digits not yet used by its row, its column or its box, which is a bitwise OR of three masks and a complement. MDN's pages on bitwise operators and the typed Uint16Array describe the tools. A lookup table of 512 entries gives the number of set bits, so counting a cell's candidates is one array read.

The search picks the empty cell with the fewest candidates, the minimum remaining values rule, and stops looking the moment it finds a cell with a single candidate. It places a digit, flips three bits, recurses, and flips them back. As soon as solutions reaches the limit, the loops break. Wikipedia's page on Sudoku solving algorithms describes plain backtracking, and the fewest-candidates rule is the standard way to keep it short: dead ends appear early because the most constrained cell is tried first.

How are clues taken away?

The digger starts from a finished grid and removes clues. It works in pairs. Cell i and cell 80 - i are the same distance from the centre, so removing both keeps the printed pattern symmetric under a half turn, the look that printed puzzles have. After each removal the digger calls countSolutions(puzzle, 2). If the answer is 1 the clues stay removed, and if not they are put back. After the pairs it tries the centre cell on its own. For every level above Easy, a second phase removes single cells in a shuffled order, which can reach puzzles with fewer clues than symmetry allows.

Each level has its own clue range and its own attempt budget:

Target number of clues per level
LevelCluesAttempts
Easy36 to 438
Medium27 to 328
Hard24 to 298
Expert22 to 278
Evil21 to 268

The ranges overlap, which is the point. A grid with 25 clues can be Hard or Evil, depending on what it takes to solve it, so the clue count only decides how far to dig. It does not decide the level.

How is a level decided?

A dug puzzle goes to gradePuzzle, which plays it the way a person would. nextHumanStep tries techniques from the cheapest to the most expensive and applies the first that works: a full house (the last empty cell of a row, column or box), a hidden single, a naked single, a pointing pair or triple, box-line reduction, naked and hidden pairs, naked and hidden triples, X-Wing, XY-Wing and Swordfish. The names follow the usual teaching vocabulary, for example the techniques listed on SudokuWiki, where XY-Wing is called Y-Wing. Every technique carries a weight and a tier:

Technique weights and tiers in the grader
TierTechniques (weight)
1Full house (4), naked single (8), hidden single (14)
2Pointing (50), box-line reduction (55)
3Naked pair (70), hidden pair (90)
4Naked triple (110), hidden triple (130), X-Wing (180)
5XY-Wing (220), Swordfish (260)
6Forcing chains (500)

The grader adds the weights of every step it takes into a score and remembers the highest tier it needed. The level follows the peak, not the sum: tier 1 only is Easy, tier 2 Medium, tier 3 Hard, tiers 4 and 5 Expert. If the solver stalls with cells left, no technique in the list can move, and the puzzle is Evil. Using the peak is a fair choice because one X-Wing makes a puzzle harder than a hundred singles, and the sum would hide it.

The digger tries up to the attempt budget in the table and keeps the first grid that lands exactly on the target level. If none does, it keeps the closest one.

How does the Hint button explain itself?

The hint reuses the grader. findLogicalHint first compares the player's digits with the solution and returns an error hint for the first digit that disagrees: "Check row 3, column 7: 4 does not fit the solution." This comes first so that a logic hint never builds on a mistake. Without errors it runs the human solver from the current grid and returns the first placement it finds. If an elimination such as a pair or a pointing triple came before the placement, the hint reports that technique and the row, column or box where it works, with a line like "Hint (Hidden Single in row 4): try 6 at row 4, column 2."

For Evil grids the human solver may stall. The hint then falls back to the most constrained empty cell and gives its digit from the solution, labelled as a logical chain. Every other hint teaches a real technique, which is why the button is worth pressing more than once. A hint that only said "put a 6 here" would finish the puzzle. A hint that says where to look makes the next one easier to find without it.

Both checks, uniqueness and grading, sit in plain functions with no page code, so one file feeds the puzzle, the grader and the hint, and no level is ever sorted by hand. For the same idea applied to a different grid, see the Nonogram line solver.