xeeeveee/sudoku
Composer 安装命令:
composer require xeeeveee/sudoku
包简介
PHP sudoku logic library - Generate and solve sudoku puzzles
README 文档
README
PHP Sudoku Generator & Solver
A PHP Sudoku generate and solver implemented via a bruteforce backtracking algorithm.
Installation
Install via composer with php composer require xeeeveee/sudoku:*
Usage
TL;DR full examples
// Generate a new puzzle $puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->generatePuzzle(); $puzzle = $puzzle->getPuzzle(); // Solve a pre-determined puzzle $puzzle = new Xeeeveee\Sudoku\Puzzle($puzzle); $puzzle->solve(); $solution = $puzzle->getSolution(); // Check a puzzle is solvable $puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->setPuzzle($puzzle); $solvable = $puzzle->isSolvable(); // Check a puzzle is solved $puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->setPuzzle($puzzle); $puzzle->solve($puzzle); $solved = $puzzle->isSolved(); // Generate a puzzle with a different cell size $puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->setCellSize(5); // 25 * 25 grid $puzzle->generatePuzzle(); // Setting properties in the constructor $puzzle = new Xeeeveee\Sudoku\Puzzle($cellSize, $puzzle, $solution);
Generator
Once an instance has been initialized you can generate a new sudoku puzzle by calling the generatePuzzle() method as below:
$puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->generatePuzzle();
You can also specify the difficulty of the puzzle to generate by passing an integer between 0 and the maximum number of cells in the puzzle (81 for a 3*3 $cellSize). This represents how many of the cells will be pre-populated in the puzzle. For example, the below snippet should generate a puzzle with 25 of the cells pre-populated.
$puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->generatePuzzle(25);
Solver
Solving a puzzle is as simple as calling the solve() method on the object, which will return either true or false depending on the outcome, see below for example:
$puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->generatePuzzle(25); $puzzle->solve();
You can use the isSolved() method to check if the object contains a solved solution, and use the getSolution method to retrieve the array, a more complete example might look like the below:
$puzzle = new Xeeeveee\Sudoku\Puzzle(); $puzzle->generatePuzzle(25); if($puzzle->isSolvable() && $puzzle->isSolved() !== true) { $puzzle->solve(); } $solution = $puzzle->getSolution();
Puzzle & solution format
A standard ($cellSize 3) puzzle and solution is represented as 3 dimensional array, effectively 9 ($cellSize * $cellSize) rows with the same number of columns. Blank values are represented as 0. The definition for a complete empty (`$cellSize 3) puzzle or solution would look like the below:
$puzzle = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ];
Performance notice
Due to the nature of the algorithm used to generate and solve puzzles, the execution time for these commands increases
exponentially as the $cellSize used is increased, puzzles with a $cellSize exceeding 4 will take an exceptionally
long time to solve on average.
Available methods
integer getCellSize()
Returns cell size of the puzzle.
boolean setCellSize()
Sets the cell size of the puzzle. Note - This will reset the $puzzle and $solution properties on the object.
integer getGridSize()
Returns cell size of the puzzle. Note - This is calculated based on the $cellSize property.
array getPuzzle()
Returns the puzzle array.
boolean setPuzzle(array $puzzle = [])
Sets the puzzle array - If the $puzzle parameter is omitted or an invalid array structure is pass, a empty grid will be generated and false will be returned.
Note - Setting the puzzle always resets the solution to an empty grid.
array getSolution()
Returns the solution array.
boolean setSolution(array $solution)
Sets the solution, if the $solution parameter supplied is an invalid format false is returned and the solution is not modified.
boolean solve()
Attempts to solve the puzzle.
boolean isSolved()
Returns true if a the solution is valid for the current puzzle.
boolean isSolvable()
Returns true if the puzzle is solvable - This is significantly quicker then actually solving the puzzle.
boolean generatePuzzle($cellCount = 15)
Generates a new puzzle, the $cellCount parameter specifies how many cells will be pre-populated, effectively manipulating the difficulty. 0 - 81 are valid values for $cellCount if any other value is supplied false is returned.
Note - Generating a puzzle always resets the solution to an empty grid.
xeeeveee/sudoku 适用场景与选型建议
xeeeveee/sudoku 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.47k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2015 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「generation」 「sudoku」 「solver」 「solving」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xeeeveee/sudoku 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xeeeveee/sudoku 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xeeeveee/sudoku 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Toolset for generating PHP code
Fabricates patterns.
PHP PDF library for generating and importing PDF documents. A component of the Pop PHP Framework
Endroid Sudoku solver
REST PDF Invoices for Magento 2
统计信息
- 总下载量: 15.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-12