@algorithm.ts/sudoku

中文文档
4.0.4发布于 2026/03/23MITTypeScriptESM + CJS近 30 天下载 1.3万

A efficient Sudoku solver and creator.

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. Examples
  4. Solve a sudoku puzzle
  5. Create a sudoku game data
  6. Related

@algorithm.ts/sudoku


中文文档

This library is used to solve Sudoku problems and generate Sudoku problems with unique solution.

The Sudoku problem is described as a square matrix of size (a*a) * (a*a), and the value of a position in the square matrix is an integer between [-1, a*a). Among them, -1 means no value is filled in this position yet. A classic Sudoku problem needs to satisfy the following constraints:

  1. Each position in the square matrix needs to be filled with an integer between [0, a*a)
  2. There are no repeated numbers in each row of the square matrix
  3. There are no repeated numbers in each column of the square matrix
  4. The square matrix can be divided into aa sub-squares of size aa, and there are no repeated numbers in each column of the sub-squares A typescript implementation of the sudoku algorithm.

sudoku is a shuffle algorithm, which can complete the shuffle in $O(N)$ time complexity on the basis of only using a constant level of extra space.

If you are curious about this algorithm, you can visit [here][sudoku] for more details.

Install

  • npm

    npm install --save @algorithm.ts/sudoku
    
  • pnpm

    pnpm add @algorithm.ts/sudoku
    

Usage

  • SudokuSolver: Solve a Sudoku puzzle.

  • SudokuCreator: Create a Sudoku data (puzzle and solution) with only unique solution.

  • Utility functions

    FunctionDescription
    createSudokuBoardDataCreate a sudoku board (one-dimensional array).
    fillSudokuBoardDataFill the sudoku board with the given value.
    copySudokuBoardDataCopy sudoku board from src to dst.
    verifySolutionCheck if the solution of the given Sudoku game is valid.
    toMatrixStyleBoardDataConvert the one-dimensional array to two-dimensional array.
    createMatrixCodeMap
    createMatrixCoordinateMap

Examples

Solve a sudoku puzzle

  • Solve a 16x16 sudoku puzzle

    import type { ISudokuBoardData } from '@algorithm.ts/sudoku'
    import { SudokuSolver, createSudokuBoardData } from '@algorithm.ts/sudoku'
    
    // 4 x 4 = 16
    const solver = new SudokuSolver({ childMatrixSize: 4 })
    
    const puzzle: ISudokuBoardData =  [
       2,  3, 14, -1,  8,  6, -1, -1, -1, 13, -1, -1, -1, 10, 12, -1,
       0, -1, -1, -1, -1, -1,  1,  9, -1, -1,  4, -1,  8, -1, -1,  2,
      -1, -1, -1,  6,  7, -1,  2, -1, -1, -1, 10,  1, -1,  0, -1, 15,
      -1,  8, -1, -1, -1, -1, 12, -1, -1,  5,  0,  7,  4, -1,  1,  6,
       7,  1, -1,  3, -1,  4, 10, 14, -1, -1, -1, -1,  2, -1, 15,  9,
      14, 12, -1, -1,  2,  3, -1, -1,  1, -1,  7, -1, -1,  6, 13, -1,
      10,  2,  5, -1, -1,  0,  7, -1,  3, -1, -1,  8, 11,  1, -1,  4,
      -1, 13,  6, -1, -1, -1, -1,  1, -1, -1, 14,  2, -1,  3,  7, -1,
      -1, -1, -1,  2,  1, -1, -1, -1, -1, -1,  8,  9, 14,  7, -1, -1,
       8,  9, 10, -1,  3, -1, -1,  2,  6, -1, 11, -1, -1, -1,  5,  0,
       1, -1,  3,  5, -1, -1, -1, -1,  4,  0, -1, -1, -1, 11, -1, 12,
       6, -1, -1,  4,  5, -1, 15, 11, 14, -1,  1, -1, 13,  9,  8, -1,
      -1,  6, -1, -1, -1,  2, -1, 12, -1, -1, -1, 14,  9, -1,  0, 13,
      -1, -1,  0,  7, -1,  9, 13,  8, -1, -1,  3, -1, -1,  2,  6, 14,
      -1, -1, -1,  8, -1, 11, -1, -1, -1, 12, -1, -1, 15,  5,  3,  1,
       3, -1, -1, -1, -1, -1, -1, -1,  9, 15,  2,  0, -1,  8, -1, 11
    ]
    
    solver.solve(puzzle, null)  // => true
    
    const solution: ISudokuBoardData = createSudokuBoardData(16)
    solver.solve(puzzle, solution)  // => true
    
    solution
    /** ===>
     * [
     *    2,  3, 14,  1,  8,  6,  0,  4, 11, 13,  9, 15,  5, 10, 12,  7,
     *    0,  5,  7, 10, 14, 15,  1,  9, 12,  3,  4,  6,  8, 13, 11,  2,
     *   12, 11,  4,  6,  7,  5,  2, 13,  8, 14, 10,  1,  3,  0,  9, 15,
     *   15,  8,  9, 13, 11, 10, 12,  3,  2,  5,  0,  7,  4, 14,  1,  6,
     *    7,  1,  8,  3,  6,  4, 10, 14,  0, 11, 13,  5,  2, 12, 15,  9,
     *   14, 12, 15, 11,  2,  3,  9,  5,  1,  4,  7, 10,  0,  6, 13,  8,
     *   10,  2,  5,  9, 13,  0,  7, 15,  3,  6, 12,  8, 11,  1, 14,  4,
     *    4, 13,  6,  0, 12,  8, 11,  1, 15,  9, 14,  2, 10,  3,  7,  5,
     *   13, 15, 11,  2,  1, 12,  6,  0,  5, 10,  8,  9, 14,  7,  4,  3,
     *    8,  9, 10, 14,  3, 13,  4,  2,  6,  7, 11, 12,  1, 15,  5,  0,
     *    1,  7,  3,  5,  9, 14,  8, 10,  4,  0, 15, 13,  6, 11,  2, 12,
     *    6,  0, 12,  4,  5,  7, 15, 11, 14,  2,  1,  3, 13,  9,  8, 10,
     *   11,  6,  1, 15, 10,  2,  3, 12,  7,  8,  5, 14,  9,  4,  0, 13,
     *    5,  4,  0,  7, 15,  9, 13,  8, 10,  1,  3, 11, 12,  2,  6, 14,
     *    9, 10,  2,  8,  0, 11, 14,  7, 13, 12,  6,  4, 15,  5,  3,  1,
     *    3, 14, 13, 12,  4,  1,  5,  6,  9, 15,  2,  0,  7,  8, 10, 11
     * ]
     */
    

Create a sudoku game data

  • Create a 9x9 sudoku puzzle

    import { SudokuCreator } from '@algorithm.ts/sudoku'
    
    // 3 x 3 = 9
    const creator = new SudokuCreator({ childMatrixSize: 3 })
    
    • Create a easy puzzle.

      creator.createSudoku()
      // or
      creator.createSudoku(0.2)
      

      The result is a js object similar to the one shown below:

      {
        "puzzle": [
           8, -1, 6,  1,  0,  2,  3,  5, -1,
           0, -1, 5, -1,  4, -1,  8,  1,  7,
           4,  3, 1,  8, -1, -1,  2,  6,  0,
          -1,  0, 7,  4, -1,  1,  5,  8,  2,
           2,  1, 4,  5,  3, -1, -1,  7,  6,
           5,  6, 8,  0,  2,  7,  4,  3,  1,
           1,  4, 3,  2, -1,  6,  7,  0,  8,
           6,  5, 0,  7,  8,  4,  1,  2,  3,
          -1, -1, 2,  3, -1, -1,  6,  4,  5
        ],
        "solution": [
          8, 7, 6, 1, 0, 2, 3, 5, 4,
          0, 2, 5, 6, 4, 3, 8, 1, 7,
          4, 3, 1, 8, 7, 5, 2, 6, 0,
          3, 0, 7, 4, 6, 1, 5, 8, 2,
          2, 1, 4, 5, 3, 8, 0, 7, 6,
          5, 6, 8, 0, 2, 7, 4, 3, 1,
          1, 4, 3, 2, 5, 6, 7, 0, 8,
          6, 5, 0, 7, 8, 4, 1, 2, 3,
          7, 8, 2, 3, 1, 0, 6, 4, 5
        ]
      }
      
    • Create a hard puzzle.

      creator.createSudoku(1.0)
      

      The result is a js object similar to the one shown below:

      {
        "puzzle": [
          -1, -1, -1, -1, -1,  8, -1, -1,  4,
          -1, -1, -1,  3, -1, -1,  5,  8, -1,
          -1, -1, -1,  6,  4, -1,  0, -1, -1,
          -1, -1, -1, -1,  2, -1,  6, -1, -1,
          -1,  0, -1, -1, -1, -1,  7,  3,  5,
          -1,  7, -1, -1,  3, -1, -1, -1, -1,
           3, -1, -1, -1, -1,  2, -1, -1,  0,
          -1, -1, -1,  8,  6,  5, -1, -1, -1,
           2, -1,  4, -1, -1, -1, -1, -1, -1
        ],
        "solution": [
          0, 6, 1, 2, 5, 8, 3, 7, 4,
          4, 2, 7, 3, 0, 1, 5, 8, 6,
          8, 3, 5, 6, 4, 7, 0, 2, 1,
          5, 4, 3, 7, 2, 0, 6, 1, 8,
          6, 0, 2, 1, 8, 4, 7, 3, 5,
          1, 7, 8, 5, 3, 6, 4, 0, 2,
          3, 8, 6, 4, 7, 2, 1, 5, 0,
          7, 1, 0, 8, 6, 5, 2, 4, 3,
          2, 5, 4, 0, 1, 3, 8, 6, 7
        ]
      }
      
  • Create a 16x16 sudoku puzzle

    import { SudokuCreator } from '@algorithm.ts/sudoku'
    
    // 4 x 4 = 16
    const creator = new SudokuCreator({ childMatrixSize: 4 })
    creator.createSudoku(0.8)
    

    The result is a js object similar to the one shown below:

    {
      "puzzle": [
        -1, -1, -1, 12, -1,  5, -1,  9, -1, -1,  6, -1, -1,  1, -1, 14,
        -1,  2,  4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, -1,
        -1, -1,  8, -1, 12, 13,  4,  7,  2, -1, -1, -1, -1, -1, -1, -1,
         7, -1, -1, -1, -1, -1, -1, 14, -1, -1, -1, 12, -1, -1, -1, -1,
         4, -1, 15, -1,  6, -1, -1, -1, -1, -1, 11, -1,  3, -1, -1, -1,
        -1,  8, 10, 14, -1, -1, -1, -1,  5, 13, -1, -1, -1,  9, -1, -1,
        -1, -1,  6, 11,  3, 14, -1, -1,  0,  2, -1,  4, -1, -1, -1, 15,
        -1,  5, 13, -1, -1,  7, -1, -1,  9, -1, -1,  3,  2, -1, 10, -1,
        -1, -1, -1, -1, -1, -1, 14, -1,  6,  9, -1, -1, 10,  8, -1, -1,
        -1, -1, 14,  0,  7,  3, -1,  2, -1, 10, -1, -1, 15, 13, -1,  9,
        -1,  9,  7, -1, -1, -1, -1, 12, -1, 15, -1,  1, -1, -1,  0, -1,
        11, -1,  3, -1, -1,  0, -1, 15, 14,  5, -1, -1, -1,  2, 12,  6,
        -1, -1,  0, 13, -1, -1, -1,  5, -1, 11,  9, -1,  4, -1,  6,  3,
        -1, -1,  2, -1, -1, -1, 10, -1, 15,  6, -1, -1, 14, -1, -1, -1,
        -1, -1, -1, -1, 15, 12, -1, -1, -1,  4,  7, -1, -1, -1,  9, -1,
         6, -1, -1, -1, -1, -1,  8, -1, -1, -1,  5, -1, -1,  0, -1, -1
      ],
      "solution": [
        10,  3, 11, 12,  8,  5,  2,  9,  4,  7,  6, 15,  0,  1, 13, 14,
        13,  2,  4,  9,  1,  6,  3,  0, 11,  8, 14,  5, 12,  7, 15, 10,
         0, 14,  8, 15, 12, 13,  4,  7,  2,  1, 10,  9,  6,  3, 11,  5,
         7,  6,  1,  5, 11, 10, 15, 14, 13,  0,  3, 12,  9,  4,  8,  2,
         4,  0, 15,  2,  6,  9,  1, 13,  7, 12, 11, 10,  3,  5, 14,  8,
         3,  8, 10, 14,  2, 15, 12,  4,  5, 13,  1,  6, 11,  9,  7,  0,
         9,  7,  6, 11,  3, 14,  5, 10,  0,  2,  8,  4, 13, 12,  1, 15,
        12,  5, 13,  1,  0,  7, 11,  8,  9, 14, 15,  3,  2,  6, 10,  4,
        15, 13, 12,  4,  5, 11, 14,  1,  6,  9,  2,  0, 10,  8,  3,  7,
         5,  1, 14,  0,  7,  3,  6,  2,  8, 10, 12, 11, 15, 13,  4,  9,
         2,  9,  7,  6, 10,  8, 13, 12,  3, 15,  4,  1,  5, 14,  0, 11,
        11, 10,  3,  8,  4,  0,  9, 15, 14,  5, 13,  7,  1,  2, 12,  6,
         1, 12,  0, 13, 14,  2,  7,  5, 10, 11,  9,  8,  4, 15,  6,  3,
         8,  4,  2,  7,  9,  1, 10,  3, 15,  6,  0, 13, 14, 11,  5, 12,
        14, 11,  5,  3, 15, 12,  0,  6,  1,  4,  7,  2,  8, 10,  9, 13,
         6, 15,  9, 10, 13,  4,  8, 11, 12,  3,  5, 14,  7,  0,  2,  1
      ]
    }
    

版本与发布记录

@algorithm.ts/sudoku 共发布 60 个版本,首次发布于 2021/07/26;过去 12 个月发布了 1 个版本。

版本发布日期距今
4.0.4latest2026/03/237 个月前
4.0.32024/10/062 年前
4.0.22024/09/142 年前
4.0.12024/05/292 年前
4.0.02024/03/302 年前
3.1.12023/06/103 年前

关于 @algorithm.ts/sudoku 的常见问题

@algorithm.ts/sudoku 是什么?

A efficient Sudoku solver and creator.

如何安装 @algorithm.ts/sudoku?

在项目目录运行 npm install @algorithm.ts/sudoku,或使用 pnpm add / yarn add / bun add。

@algorithm.ts/sudoku 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@algorithm.ts/sudoku 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@algorithm.ts/sudoku 可以商用吗?

@algorithm.ts/sudoku 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@algorithm.ts/sudoku 的下载量有多少?

根据 npm 官方下载统计,@algorithm.ts/sudoku 在 2026/10/06 – 2026/11/04 这 30 天内被下载 12,748 次,日均约 425 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@algorithm.ts/sudoku 还在维护吗?

最新版本 4.0.4 发布于 2026/03/23(7 个月前);至今共发布 60 个版本,其中过去 12 个月发布了 1 个;首次发布于 2021/07/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。