No guessing allowed: a 60-line line solver that gates every nonogram board
Nonograms (also called picross or griddlers) have one rule that makes or breaks a puzzle: the player should never have to guess. If the row and column clues allow two different pictures, or force the player into trial an
Nonograms (also called picross or griddlers) have one rule that makes or breaks a puzzle: the player should never have to guess. If the row and column clues allow two different pictures, or force the player into trial and error, the puzzle feels broken even when it is technically "correct".
In Nonobud, our browser nonogram, every board goes through the same gate before a player sees it: a small line solver has to finish it by pure logic. This post walks through that solver, the random board generator around it, and the numbers I measured when I ran it again this week.
Clues are just run lengths
A clue is the list of filled runs in a row or column. Computing it from a solution grid is a few lines:
function runs(line) {
const out = []; let r = 0;
for (const v of line) { if (v === 1) r++; else if (r) { out.push(r); r = 0; } }
if (r) out.push(r);
return out.length ? out : [0]; // an empty line has the clue [0]
}
Every puzzle object is just {n, sol, rc, cc}: size, the solution, and row and column clues built with runs().
The line solver
Humans solve nonograms one line at a time. You look at a row, its clue, and the squares you already know, and ask: which squares are the same in every arrangement that still fits? Those squares are now certain. Then you move to the next line, and repeat until nothing changes.
The solver does exactly that. The grid starts as -1 (unknown). For each line it enumerates every placement of the clue that agrees with what is known, then locks in any cell where all placements agree:
function lineSolvable(n, rc, cc) {
const g = new Int8Array(n * n).fill(-1);
let changed = true;
while (changed) {
changed = false;
for (let axis = 0; axis < 2; axis++) for (let i = 0; i < n; i++) {
const idx = Array.from({length: n}, (_, j) => axis ? j * n + i : i * n + j);
const known = idx.map(k => g[k]);
if (!known.includes(-1)) continue;
const opts = linePlacements((axis ? cc : rc)[i], n, known);
if (!opts.length) return false; // contradiction
for (let j = 0; j < n; j++) if (known[j] < 0) {
const v = opts[0][j];
if (opts.every(o => o[j] === v)) { g[idx[j]] = v; changed = true; }
}
}
}
return !g.includes(-1); // fully solved, no guessing
}
linePlacements is a recursive placer: put block k at every start position that leaves room for the remaining blocks (sum of the rest plus one gap each), skip positions that cover a known empty square or leave a known filled square uncovered, then recurse. For a 10- or 15-cell line that brute force is cheap, and the known pruning keeps it cheap as the grid fills in.
Two useful properties fall out of this:
- If the line solver finishes, the solution is unique. Every cell it set was forced, so no other picture fits the clues.
- It matches how people actually play. The solver never looks at two lines at once or tries "what if". So a board it accepts can be finished by the same overlap-and-edge reasoning the in-game tips teach.
The second point matters more than the first. A full backtracking solver would also prove uniqueness, but it would happily accept boards that need deep lookahead, which on a phone feels like a wall.
Generating random boards that look like something
Pure random noise makes ugly, spiky boards with dozens of 1-length runs. Nonobud's generator does one smoothing pass, which turns noise into blobs that read more like shapes:
for (let attempt = 0; attempt < 400; attempt++) {
let g = Uint8Array.from({length: n * n}, () => rng() < .56 ? 1 : 0);
// majority vote over the 3x3 neighbourhood, but keep 25% of cells as-is
// ... smoothing ...
const fill = g.reduce((a, b) => a + b, 0);
if (fill < n * n * .4 || fill > n * n * .72) continue; // too sparse or too dense
const p = build('', '#34405a', g, n);
if (lineSolvable(n, p.rc, p.cc)) return p; // the gate
}
The RNG is mulberry32, seeded either with Math.random() for "Random" boards or with the date for the Daily board (dateKey(date) * 17 + 3), so everyone gets the same daily puzzle without a server.
The numbers
I pulled the generator out of game.js and ran it in Node (seeds 1 to 200 per size):
| Size | Boards made | Fallbacks | Candidates tried per board | Time per board | Average fill |
|---|---|---|---|---|---|
| 5×5 | 200 | 0 | 1.9 | 0.27 ms | 57.5% |
| 10×10 | 200 | 0 | 1.4 | 0.27 ms | 58.7% |
| 15×15 | 200 | 0 | 1.5 | 0.54 ms | 61.3% |
And how strict the gate is, over 5,000 raw candidates:
- 10×10: 4,500 were in the fill range, 3,282 of those (72.9%) were line-solvable.
- 15×15: 4,893 in range, 2,897 (59.2%) line-solvable.
So roughly one in four 10×10 blobs and two in five 15×15 blobs would have needed guessing, and get thrown away. The bigger the grid, the more often a pair of lines ends up ambiguous. Even so, the generator never needed its 400-attempt fallback, and a board costs well under a millisecond, so it runs on tap with no loading state.
The hand-drawn picture pack (21 original pictures: 5 at 5×5, 8 at 10×10, 8 at 15×15) goes through the same check. I re-ran it on all 21 this week and all 21 pass. That is the other use of the line solver: a level-design check for hand-made pictures, not just a runtime filter for random ones.
Small touches that came from the same idea
- Hints fix mistakes first. A hint first corrects a square you filled wrongly; only if there are none does it reveal a missing one.
- Drag locks to one axis, and only changes cells that started in the same state as the first cell, like a paint stroke. On a phone that stops a slightly diagonal swipe from wiping out a neighbouring row.
- 15×15 boards draw thicker lines every 5 cells so counting runs on a small screen is less error-prone.
Takeaway
If you build a logic puzzle, write the solver that mirrors human reasoning, not the one that is most powerful, and use it as the acceptance test for every level. It gives you uniqueness, a difficulty ceiling, and a design tool in about 60 lines.
You can try the daily board, the picture pack and random 10×10 / 15×15 boards here: play Nonobud on JumpBud.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.