npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@luckyfoxdesign/sudoku-generator

v1.1.2

Published

A lightweight Sudoku puzzle generator with difficulty control (easy–expert). Generates complete solutions and playable puzzles using backtracking and constraint-solving techniques.

Downloads

264

Readme

Sudoku Generator

A lightweight Sudoku puzzle generator that creates complete solutions and playable puzzles with controlled difficulty. Works in browser and Node.js environments.

npm version License: MIT

Features

  • 🎲 Generates randomized, valid Sudoku puzzles and solutions
  • 🎯 Difficulty-based generation: easy, medium, hard, expert
  • 🧩 Creates playable puzzles with controlled cell removal
  • ⚡ Fast generation using backtracking + constraint solving
  • 🌐 Works in browser and Node.js
  • 📦 Zero dependencies
  • 🔧 Multiple export formats (ESM, CommonJS, IIFE)
  • ✅ Fully tested

Installation

npm install @luckyfoxdesign/sudoku-generator

Quick Start

import { generateSudoku, generateSudokuGrid, generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';

// Generate a puzzle with controlled difficulty
const { puzzle, solution, difficulty, score } = generateSudoku('hard');
console.log(difficulty); // 'hard'
console.log(score);      // 41-80
// puzzle: [[5, 0, 0, 6, 0, 0, 9, 0, 2], ...]  — zeros are empty cells
// solution: [[5, 3, 4, 6, 7, 8, 9, 1, 2], ...] — complete grid

// Or generate a simple puzzle (legacy API)
const simplePuzzle = generateSudokuGrid();
// [[5, 0, 4, 6, 0, 8, 9, 0, 2], ...]

Usage

Browser (CDN)

<!DOCTYPE html>
<html>
<head>
  <title>Sudoku Generator</title>
</head>
<body>
  <script src="https://unpkg.com/@luckyfoxdesign/sudoku-generator/dist/index.global.js"></script>
  <script>
    // Generate a puzzle with difficulty
    const { puzzle, solution, difficulty } = Sudoku.generateSudoku('medium');
    console.log(difficulty); // 'medium'

    // Legacy API still works
    const simplePuzzle = Sudoku.generateSudokuGrid();
  </script>
</body>
</html>

React / Vue / Svelte

import { generateSudoku } from '@luckyfoxdesign/sudoku-generator';

function SudokuGame() {
  const [{ puzzle, solution }] = useState(() => generateSudoku('medium'));

  return (
    <div>
      {puzzle.map((row, i) => (
        <div key={i}>
          {row.map((cell, j) => (
            <span key={j}>
              {cell === 0 ? '_' : cell}
            </span>
          ))}
        </div>
      ))}
    </div>
  );
}

Node.js (ESM)

import { generateSudoku, generateSudokuGrid, generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';

const { puzzle, solution, difficulty, score } = generateSudoku('expert');

Node.js (CommonJS)

const { generateSudoku, generateSudokuGrid, generateCompleteSudokuGrid } = require('@luckyfoxdesign/sudoku-generator');

const { puzzle, solution, difficulty, score } = generateSudoku('hard');

API

generateSudoku(difficulty?)

Generates a Sudoku puzzle with controlled difficulty. Returns both the puzzle and solution together.

Parameters:

  • difficulty'easy' | 'medium' | 'hard' | 'expert' (default: 'easy')

Returns: { puzzle: number[][], solution: number[][], difficulty: string, score: number }

| Difficulty | Score range | Description | |------------|-------------|-------------| | easy | 0–15 | Solved by naked singles only | | medium | 16–40 | Requires hidden singles | | hard | 41–80 | Requires more advanced techniques | | expert | 81+ | Requires complex constraint solving |

Example:

const { puzzle, solution, difficulty, score } = generateSudoku('hard');
console.log(difficulty); // 'hard'
console.log(score);      // e.g. 57
console.log(puzzle[0]);  // [5, 0, 0, 6, 0, 0, 9, 0, 2]
console.log(solution[0]); // [5, 3, 4, 6, 7, 8, 9, 1, 2]

generateSudokuGrid()

Generates a playable Sudoku puzzle with some cells removed (marked as 0). Approximately 50% of cells are removed, with first columns of each 3×3 block always filled.

Returns: number[][] - A 9×9 2D array where 0 = empty cell, 1-9 = filled cell

Example:

const puzzle = generateSudokuGrid();
console.log(puzzle[0][0]); // 5 or 0 (empty)
console.log(puzzle[0][3]); // 7 (always filled - column 3 never empty)

generateCompleteSudokuGrid()

Generates a complete Sudoku solution with all cells filled (no empty cells).

Returns: number[][] - A 9×9 2D array where each cell contains a number from 1-9

Example:

const solution = generateCompleteSudokuGrid();
console.log(solution[0][0]); // 5 (always filled)

generateSudokuGridWithMetadata()

Generates a Sudoku puzzle with metadata for each cell. Useful for advanced Sudoku solving/generation algorithms.

Returns: Object[][] - A 9×9 array of cell objects

Cell Object Structure:

{
  chosenValue: number;      // The number in cell (0 = empty, 1-9 = filled)
  removedValues: number[];  // Values tried and rejected during generation
  gameSet: Set<number>;     // Available values for this cell
}

Example:

const grid = generateSudokuGridWithMetadata();
console.log(grid[0][0].chosenValue);    // 5 or 0
console.log(grid[0][0].removedValues);  // [2, 7]

Puzzle Generation Details

Difficulty-based generation (generateSudoku)

Puzzles are generated by iteratively removing cells and scoring the result using a constraint-solving engine. The score is determined by the techniques required to solve the puzzle:

  • Naked singles — only one candidate in a cell (low weight)
  • Hidden singles — only one cell in a unit can hold a value (medium weight)
  • More advanced techniques — pointing pairs, naked/hidden subsets, etc. (higher weights)

Cells are removed until the puzzle's score falls within the target difficulty range. If the target cannot be reached within a time limit, the closest available result is returned.

Legacy cell removal strategy (generateSudokuGrid)

When generating puzzles with the legacy API:

  • ~50% of cells are randomly removed
  • Columns 0, 3, and 6 (first column of each 3×3 block) are never removed

Example Grid Structure

[5] 0  4  [6] 0  8  [9] 0  2   ← Columns 0,3,6 always filled
[6] 7  0  [1] 9  0  [3] 4  0
[8] 0  9  [5] 0  2  [7] 0  6
[2] 0  0  [8] 0  7  [4] 0  0
...

Development

Setup

# Clone the repository
git clone https://github.com/luckyfoxdesign/sudoku-generator.git
cd sudoku-generator

# Install dependencies
npm install

# Build the project
npm run build

Build

npm run build

This creates three files in dist/:

  • index.js - ESM format for React/Vue/Svelte
  • index.cjs - CommonJS format for Node.js
  • index.global.js - IIFE format for browser <script> tags

Testing

npm test

Tests cover:

  • ✅ Puzzle generation (with empty cells)
  • ✅ Difficulty-based generation (all 4 levels)
  • ✅ Complete solution generation (no empty cells)
  • ✅ Grid structure validation
  • ✅ Sudoku rules (rows, columns, 3×3 blocks)
  • ✅ Performance benchmarks
  • ✅ Edge cases

Publishing (for maintainers)

Pre-publish Checklist

  1. Run tests:
npm test
  1. Build the project:
npm run build
  1. Check what will be published:
npm pack --dry-run

Publishing to NPM

  1. Login to NPM:
npm login
  1. Publish the package:
npm publish --access public

Version Updates

# Patch (1.0.0 → 1.0.1)
npm version patch

# Minor (1.0.0 → 1.1.0)
npm version minor

# Major (1.0.0 → 2.0.0)
npm version major

# Then publish
npm publish --access public

Post-publish Verification

Check the package:

  • NPM: https://www.npmjs.com/package/@luckyfoxdesign/sudoku-generator
  • unpkg CDN: https://unpkg.com/@luckyfoxdesign/sudoku-generator/dist/index.global.js
  • jsDelivr CDN: https://cdn.jsdelivr.net/npm/@luckyfoxdesign/sudoku-generator/dist/index.global.js

How It Works

Generation Algorithm

  1. Generate Complete Solution:

    • Uses a backtracking algorithm on an empty 9×9 grid
    • Tries random numbers 1–9 per cell; backtracks on conflicts
    • Produces a guaranteed valid, fully filled solution
  2. Create Puzzle:

    • generateSudoku(difficulty) — removes cells one by one, scores the result using a constraint solver, and stops when the score hits the target range
    • generateSudokuGrid() — removes ~50% of cells randomly, preserving columns 0, 3, and 6
  3. Difficulty Scoring:

    • The solver applies human-like techniques (naked singles, hidden singles, pointing pairs, etc.)
    • Each technique has a weight; score = sum of (weight × applications)
    • Score determines difficulty: easy (0–15), medium (16–40), hard (41–80), expert (81+)

Every generated puzzle has a unique solution.

Performance

  • Single puzzle generation: < 100ms
  • Single solution generation: < 100ms
  • Average generation time: ~50ms
  • Tested up to 10,000+ generations without issues

Browser Compatibility

  • Chrome/Edge 90+
  • Firefox 88+
  • Safari 14+
  • Node.js 18+

Use Cases

Game Development

import { generateSudoku } from '@luckyfoxdesign/sudoku-generator';

// Puzzle and solution in one call, matched by difficulty
const { puzzle, solution, difficulty } = generateSudoku('medium');
// Use puzzle for the board, solution for validation

Puzzle Books / Print

import { generateSudoku } from '@luckyfoxdesign/sudoku-generator';

// Generate 100 hard puzzles
for (let i = 0; i < 100; i++) {
  const { puzzle } = generateSudoku('hard');
  printPuzzle(puzzle);
}

Educational Tools

import { generateSudokuGridWithMetadata } from '@luckyfoxdesign/sudoku-generator';

// Analyze generation process
const grid = generateSudokuGridWithMetadata();
grid.forEach(row => {
  row.forEach(cell => {
    console.log(`Value: ${cell.chosenValue}, Tried: ${cell.removedValues}`);
  });
});

License

MIT © Lucky Fox Design

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Links

Author

Lucky Fox Design

  • Website: https://luckyfox.design/
  • Email: [email protected]
  • NPM: https://www.npmjs.com/~luckyfoxdesign