An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

sudoku-php
=====

[![Unit Tests](https://github.com/AbcAeffchen/sudoku-php/actions/workflows/php.yml/badge.svg)](https://github.com/AbcAeffchen/sudoku-php/actions/workflows/php.yml)
[![Latest Stable Version](https://poser.pugx.org/abcaeffchen/sudoku-php/v/stable.svg)](https://packagist.org/packages/abcaeffchen/sudoku-php)
[![Total Downloads](https://poser.pugx.org/abcaeffchen/sudoku-php/downloads.svg)](https://packagist.org/packages/abcaeffchen/sudoku-php)
[![License](https://poser.pugx.org/abcaeffchen/sudoku-php/license.svg)](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.