← All projects

Sudoku Generator & Player

A dependency-free Sudoku generator and player in vanilla JavaScript, where every puzzle is generated in-browser and mathematically guaranteed to have a single unique solution.

  • JavaScript
  • Algorithms
  • Backtracking
  • CSS

A full Sudoku generator and player built from scratch in vanilla JavaScript — no frameworks, no build step, no dependencies. Every puzzle is generated in the browser and is guaranteed to have exactly one solution, in around 600 lines of code.

Aim

A focused piece to show algorithm design and clean separation of concerns rather than framework familiarity. The hard requirement set the shape of the project: every generated board must have a single, unique solution — no ambiguous puzzles, no unsolvable ones — with correctness a property of the generator itself rather than something checked after the fact.

How it works

Puzzles are built in three steps:

  1. Fill a complete, valid 9×9 grid using randomised backtracking. A minimum-remaining-values (MRV) heuristic always expands the most-constrained cell first, pruning the search tree so generation stays near-instant.
  2. Carve cells out in random order. After each removal a solution counter re-solves the board but short-circuits the moment it finds a second solution — all that’s needed to prove non-uniqueness. If the board becomes ambiguous, the digit is restored.
  3. Stop at the target clue count for the chosen difficulty (Easy ~40 down to Very Hard ~23 clues).

The solving and generation engine (sudoku.js) is fully decoupled from the UI layer (app.js), so the logic is independently testable in Node.

Result

A polished, playable app that runs anywhere a browser does — straight from the filesystem, no server needed. Beyond the core guarantee, the UX reflects how a person actually solves Sudoku: notes prune themselves when a peer cell is filled, mistakes name the conflict (“5 already appears in this row”) instead of just flashing red, and per-digit counters fade out as each number is fully placed. Hints, a timer and full keyboard control round it out.