PHPackages                             xeeeveee/sudoku - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. xeeeveee/sudoku

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

xeeeveee/sudoku
===============

PHP sudoku logic library - Generate and solve sudoku puzzles

0.2.1(10y ago)2815.5k↓46.3%7MITPHP

Since Nov 12Pushed 9y ago7 watchersCompare

[ Source](https://github.com/xeeeveee/sudoku)[ Packagist](https://packagist.org/packages/xeeeveee/sudoku)[ Docs](https://github.com/xeeeveee/sudoku)[ RSS](/packages/xeeeveee-sudoku/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/95ffca618f04d04a7f3f2f5dbf90a8081541138d0b912e7003600c0b3b0f8ba4/68747470733a2f2f7472617669732d63692e6f72672f78656565766565652f7375646f6b752e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/xeeeveee/sudoku)

PHP Sudoku Generator &amp; Solver
=================================

[](#php-sudoku-generator--solver)

A PHP Sudoku generate and solver implemented via a bruteforce backtracking algorithm.

Installation
------------

[](#installation)

Install via composer with `php composer require xeeeveee/sudoku:*`

Usage
-----

[](#usage)

### TL;DR full examples

[](#tldr-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

[](#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

[](#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 &amp; solution format

[](#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

[](#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

[](#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.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

4

Last Release

3882d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f98a57cfcf48c0ba9ed550033e878315c10d5d858ad374170b7dc7ec1e961977?d=identicon)[xeeeveee](/maintainers/xeeeveee)

---

Top Contributors

[![xeeeveee](https://avatars.githubusercontent.com/u/2545022?v=4)](https://github.com/xeeeveee "xeeeveee (83 commits)")

---

Tags

generatorgenerationsolversudokusolving

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/xeeeveee-sudoku/health.svg)

```
[![Health](https://phpackages.com/badges/xeeeveee-sudoku/health.svg)](https://phpackages.com/packages/xeeeveee-sudoku)
```

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k118.7M723](/packages/symfony-maker-bundle)[simplesoftwareio/simple-qrcode

Simple QrCode is a QR code generator made for Laravel.

2.9k30.5M113](/packages/simplesoftwareio-simple-qrcode)[okipa/laravel-table

Generate tables from Eloquent models.

57353.5k](/packages/okipa-laravel-table)[okipa/laravel-form-components

Ready-to-use and customizable form components.

208.1k1](/packages/okipa-laravel-form-components)[christianessl/landmap-generation

Generate pixelated, random world maps in PHP.

173.8k](/packages/christianessl-landmap-generation)[dmamontov/favicon

Class generation favicon for browsers and devices Android, Apple, Windows and display of html code. It supports a large number of settings such as margins, color, compression, three different methods of crop and screen orientation.

533.7k](/packages/dmamontov-favicon)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
