https://github.com/abcaeffchen/sudoku-php
Generate random Sudokus of different size and difficulty and check the solution.
https://github.com/abcaeffchen/sudoku-php
php sudoku
Last synced: 9 months ago
JSON representation
Generate random Sudokus of different size and difficulty and check the solution.
- Host: GitHub
- URL: https://github.com/abcaeffchen/sudoku-php
- Owner: AbcAeffchen
- License: lgpl-3.0
- Created: 2016-03-25T14:12:52.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-04-05T12:20:17.000Z (over 3 years ago)
- Last Synced: 2025-03-23T23:16:23.492Z (over 1 year ago)
- Topics: php, sudoku
- Language: PHP
- Size: 17.6 KB
- Stars: 10
- Watchers: 2
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
sudoku-php
=====
[](https://github.com/AbcAeffchen/sudoku-php/actions/workflows/php.yml)
[](https://packagist.org/packages/abcaeffchen/sudoku-php)
[](https://packagist.org/packages/abcaeffchen/sudoku-php)
[](https://packagist.org/packages/abcaeffchen/sudoku-php)
Genral
-----
Generate random Sudokus of different size and difficulty and check the solution. Features:
- 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
```json
{
"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`.
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`.
`$task` contains a two dimensional array of integers, where the gaps are set to `null`.
You can use
```
list($task,$solution) = Sudoku::generateWithSolution(9, Sudoku::NORMAL)
```
to generate a task and a possible solution. Maybe to give hints?
You can reproduce the Sudoku by providing a seed.
```
$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.
#### Check a solution
You can check a solution by using
```
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:
```
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).
```
$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`.
License
----
Licensed under the LGPL v3.0 License.