sudoku-php
, (*1)
Genral
Generate random Sudokus of different size and difficulty and check the solution. Features:, (*2)
- Choose from the following sizes: 4, 9, 16, 25, 36.
- Choose from fife degrees of difficulty.
- Sudokus are reproducible via a integer seed.
- Check if a input is a solution of any Sudoku or a solution to a specific task.
- Solve Sudokus.
- works with PHP 7.4 up to 8.2 (maybe also work with PHP < 7.4, but these versions are not supported anymore)
- backend only, so you can build your own frontend however you like it.
Installation
You can install this via composer using, (*3)
{
"require": {
"abcaeffchen/sudoku-php": "~1.1.0"
}
}
or just download the Sudoku.php
file and include it to your project.
Make sure to use the namespace AbcAeffchen\sudoku
., (*4)
How to use
Generate a task
use AbcAeffchen\sudoku\Sudoku;
$task = Sudoku::generate(9, Sudoku::NORMAL);
Generates a standard 9x9 Sudoku with normal difficulty. You can use the difficulties
VERY_EASY
, EASY
, NORMAL
, MEDIUM
, HARD
., (*5)
$task
contains a two dimensional array of integers, where the gaps are set to null
.
You can use, (*6)
list($task,$solution) = Sudoku::generateWithSolution(9, Sudoku::NORMAL)
to generate a task and a possible solution. Maybe to give hints?, (*7)
You can reproduce the Sudoku by providing a seed., (*8)
$seed = 0;
$task = Sudoku::generate(9, Sudoku::NORMAL, $seed);
This way cou could store a seed seed and reproduce the task at any time.
The seed can be any positive integer., (*9)
Check a solution
You can check a solution by using, (*10)
if(Sudoku::checkSolution($solution))
{
echo 'Nice done!';
}
else
{
echo 'Try again!';
}
If you also want to also check if the solution relates to the task, you can just also provide the task like this:, (*11)
if(Sudoku::checkSolution($solution,$task))
...
Solve Sudokus
This function is used to generate the Sudokus, but you can also use it to solve some you generated by hand (or get it from somewhere else)., (*12)
$solution = Sudoku::solve($task);
You only have to make sure, that $task
is a two dimensional int array containing only numbers
from 1 to the size and all gaps contain null
., (*13)
License
Licensed under the LGPL v3.0 License., (*14)