Getting started
Play Loop
Session pattern for moves, hints, cell display state, and importing string puzzles.
Use this pattern for any interactive client (React, canvas, CLI).
import type { Board, GeneratedPuzzle } from "@reetesh/sudoku-engine";
import { createEngine } from "@reetesh/sudoku-engine";
const engine = createEngine({ variant: "classic" }); // or any VariantId
type Session = {
game: GeneratedPuzzle;
board: Board;
};
function start(difficulty: "easy" | "medium" | "hard" | "expert"): Session | null {
const game = engine.generatePuzzle(difficulty);
if (!game) return null;
return { game, board: game.puzzle.map((r) => [...r]) };
}
function place(session: Session, row: number, col: number, value: number | null) {
const result = engine.applyMove(
session.board,
row,
col,
value,
session.game.puzzle, // protects givens
);
if (!result.success) return { ok: false as const, reason: result.reason };
return { ok: true as const, board: result.board };
}
function won(session: Session) {
return (
engine.isBoardComplete(session.board) &&
engine.isSolvedCorrectly(session.board, session.game.solution)
);
}Hints & cell UI state
| Goal | API |
|---|---|
| Reveal one empty cell (scan order) | revealNext(board, solution, puzzle?) |
| Reveal a random empty cell | revealRandom(...) |
| Reveal a specific cell | revealCell(board, solution, row, col, puzzle?) |
| Style a cell | getCellDisplayState(puzzle, board, solution, row, col) |
CellDisplayState: "given" | "empty" | "player" | "incorrect".
reveal* returns null when nothing left to reveal (or target is already correct / given).
Import a string puzzle
Classic 81-char strings (digits + . for empty):
import { puzzleFromString, validateImportedPuzzle } from "@reetesh/sudoku-engine";
const board = puzzleFromString(
"53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79",
);
if (!board) throw new Error("bad length / charset");
const check = validateImportedPuzzle(board);
// { valid, unique, solvable, puzzle?, error? }For variants, use engine.puzzleFromString / engine.validateImportedPuzzle so shape and uniqueness match that ruleset.