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.0.3

Published

A lightweight Sudoku puzzle generator that creates randomized 9×9 grids using a backtracking algorithm. Generates complete, valid Sudoku boards suitable for games and puzzle applications.

Readme

Sudoku Generator

A lightweight Sudoku puzzle generator that creates complete solutions and playable puzzles using a backtracking algorithm. Works in browser and Node.js environments.

npm version License: MIT

Features

  • 🎲 Generates randomized, valid Sudoku puzzles and solutions
  • 🧩 Creates playable puzzles with ~50% cells removed
  • ⚡ Fast generation using backtracking algorithm
  • 🌐 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 { generateSudokuGrid, generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';

// Generate a puzzle (with empty cells marked as 0)
const puzzle = generateSudokuGrid();
console.log(puzzle);
// [[5, 0, 4, 6, 0, 8, 9, 0, 2],
//  [6, 7, 0, 1, 9, 0, 3, 4, 0],
//  ...]

// Generate a complete solution (all cells filled)
const solution = generateCompleteSudokuGrid();
console.log(solution);
// [[5, 3, 4, 6, 7, 8, 9, 1, 2],
//  [6, 7, 2, 1, 9, 5, 3, 4, 8],
//  ...]

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
    const puzzle = Sudoku.generateSudokuGrid();
    console.log(puzzle);
    
    // Generate a complete solution
    const solution = Sudoku.generateCompleteSudokuGrid();
    console.log(solution);
  </script>
</body>
</html>

React / Vue / Svelte

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

function SudokuGame() {
  const [grid, setGrid] = useState(generateSudokuGrid());
  
  return (
    <div>
      {grid.map((row, i) => (
        <div key={i}>
          {row.map((cell, j) => (
            <span key={j}>
              {cell === 0 ? '_' : cell}
            </span>
          ))}
        </div>
      ))}
    </div>
  );
}

Node.js (ESM)

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

const puzzle = generateSudokuGrid();
const solution = generateCompleteSudokuGrid();

Node.js (CommonJS)

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

const puzzle = generateSudokuGrid();
const solution = generateCompleteSudokuGrid();

API

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

Cell Removal Strategy

When generating puzzles:

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

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)
  • ✅ 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

The generator uses a backtracking algorithm to fill the grid:

  1. Generate Complete Solution:

    • Start with an empty 9×9 grid
    • For each cell (left to right, top to bottom):
      • Try a random number from 1-9
      • Check if it's valid (no duplicates in row, column, or 3×3 block)
      • If valid, move to next cell
      • If no valid number exists, backtrack to previous cell
    • Repeat until the entire grid is filled
  2. Create Puzzle (optional):

    • Remove approximately 50% of cells randomly
    • Preserve columns 0, 3, and 6 for structure
    • Empty cells are marked as 0

This ensures every generated grid is a complete, valid Sudoku solution, and every puzzle is solvable.

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 { generateSudokuGrid, generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';

const puzzle = generateSudokuGrid();        // For player to solve
const solution = generateCompleteSudokuGrid(); // For validation

Puzzle Books / Print

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

// Generate 100 unique puzzles
for (let i = 0; i < 100; i++) {
  const puzzle = generateSudokuGrid();
  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