@forfuns/sudoku
v1.2.2
Published
sudoku solver and generator
Downloads
32
Maintainers
Readme
@forfuns/sudoku
关于 about
一款基于javascript / nodejs 开源的数独 计算器 和 生成器 依赖库
opensource sudoku solver and generator with javascript
/nodejs
library
功能 features
- [√] 完整数独解题器 - complete Sudoku solver
- [√] 题目生成功能 - random one-solution generator with five level
安装 install
npm i @forfuns/sudoku
使用 tutorial
Solver & Generator
// Solver
// 9 * 9 矩阵数独题目,-1值为待输入值
// 9 * 9 matrix for the puzzle , -1 mean input position
let puzzle = [
-1,-1,8, 9,-1,6, -1,-1,5,
-1,4,3, -1,-1,-1, -1,2,-1,
-1,-1,-1, -1,-1,-1, -1,-1,-1,
-1,-1,4, -1,-1,-1, 9,-1,-1,
5,-1,-1, -1,4,-1, 6,8,-1,
-1,-1,-1, 1,-1,-1, -1,-1,-1,
2,-1,-1, -1,8,-1, -1,7,-1,
-1,-1,-1, -1,3,4, 1,-1,-1,
-1,6,-1, -1,-1,9, -1,-1,-1,
]
const { Sudoku , generator } = require('@forfuns/sudoku')
let sudoku
// 数独解题(不区分是否唯一数独,回溯出结果直接返回)
// solve sudoku with puzzle
sudoku = new Sudoku(puzzle)
// 数独解题 只能计算唯一解的数独,如果该puzzle拥有多解,则抛出错误Error('puzzle is not one-solution sudoku')
// 一般来说,只是单纯解题,使用上面的构造器即可,速度最快
// if you need to sure puzzle is one solution or not , you can use strict=true (default is false), that will take longer
// if puzzle is not one solution sudoku , will throw Error('puzzle is not one-solution sudoku')
sudoku = new Sudoku(puzzle, true)
// 获取原题目
// get sudoku puzzle
sudoku.getPuzzle()
// 获取完整答案
// get sudoku solution
sudoku.getSolution()
// 查看debug信息
// show debug infomation
sudoku.debug()
-----------------------------------------------------------------------------
// Generator
// 数独题目生成
// make puzzle with generate function , four level 0:easy / 1:medium / 2:hard / 3:expert / 4:hell(that may take long times)
puzzle = generator(0) // level 可选 0 ~ 4 分别代表:简单 / 中等 / 困难 / 专家 / "地狱"
相关测试用例,请查看 /test/sudoku/*.js
npm run test-solver
npm run test-generator
with any idea welcome open issue to make me know
if you want same project with other language like go
/ dart
and flutter app
, here they are :