03
11
Simple Sudoku Web Application Using PHP

Hi friends today in this tutorial we showing how to create sudoku using php. Sudoku is one of the most popular puzzle game all time. Most of them showing their talent by solving the puzzle in a limited number of time. If we take sudoku, there will be different types of ratings like 1 to 5 stars, 1 star indicates it was easy, 2 star indicates it was standard, 3 star indicates hard, 4 star indicates expert and 5 star indicates extreme and challenging one.

Sudoku is a puzzle with a grid containing nine large blocks. Each block is divided into its own matrix of nine cells. The rules for solving Sudoku puzzles are very simple: each row, column and block must contain one of the numbers from "1" to "9". No number may appear more than once in any row, column, or block. When you've filled the entire grid, the puzzle is solved.

The objective of sudoku is to enter a digit from 1 through 9 in each cell, in such a way that:

  • Each horizontal row (shown in red) contains each digit exactly once
  • Each vertical column (shown in black) contains each digit exactly once
  • Each sub-grid or region (shown in green) contains each digit exactly once
sudoku rules hack and php programming blog

This explains the name of the game; in Japanese, sudoku means something like "numbers singly".

It is a small and fast script for a sudoku online-game on your website. It can generate many puzzle as much you want. It provides the answer along with the puzzle. It is very easy to integrate in every website and your existing webdesign. It requires PHP 4 or higher. There is no need of MySQL.

Solve sudoku hack and php programming blog

Generate sudoku hack and php programming blog

Reference:

Sudoku Source Code

Class.Sudoku.php:

__construct() function PHP in the below class file will automatically call the __construct() method/function when you create an object from your class.
class Sudoku {
 
    private $_matrix;
 
    public function __construct(array $matrix = null) {
        if (!isset($matrix)) {
            $this->_matrix = $this->_getEmptyMatrix();
        } else {
            $this->_matrix = $matrix;
        }
    }
 
    public function generate() {
        $this->_matrix = $this->_solve($this->_getEmptyMatrix());
        $cells = array_rand(range(0, 80), 30);
        $i = 0;
        foreach ($this->_matrix as &$row) {
            foreach ($row as &$cell) {
                if (!in_array($i++, $cells)) {
                    $cell = null;
                }
            }
        }
        return $this->_matrix;
    }
 
    public function solve() {
        $this->_matrix = $this->_solve($this->_matrix);
        return $this->_matrix;
    }
 
    public function getHtml() {
        echo '';
        for ($row = 0; $row < 9; $row++) {
            echo '';
            for ($column = 0; $column < 9; $column++) {
                echo '';
            }
            echo '';
        }
        echo '
' . $this->_matrix[$row][$column] . '
'; } private function _getEmptyMatrix() { return array_fill(0, 9, array_fill(0, 9, 0)); } private function _solve($matrix) { while(true) { $options = array(); foreach ($matrix as $rowIndex => $row) { foreach ($row as $columnIndex => $cell) { if (!empty($cell)) { continue; } $permissible = $this->_getPermissible($matrix, $rowIndex, $columnIndex); if (count($permissible) == 0) { return false; } $options[] = array( 'rowIndex' => $rowIndex, 'columnIndex' => $columnIndex, 'permissible' => $permissible ); } } if (count($options) == 0) { return $matrix; } usort($options, array($this, '_sortOptions')); if (count($options[0]['permissible']) == 1) { $matrix[$options[0]['rowIndex']][$options[0]['columnIndex']] = current($options[0]['permissible']); continue; } foreach ($options[0]['permissible'] as $value) { $tmp = $matrix; $tmp[$options[0]['rowIndex']][$options[0]['columnIndex']] = $value; if ($result = $this->_solve($tmp)) { return $result; } } return false; } } private function _getPermissible($matrix, $rowIndex, $columnIndex) { $valid = range(1, 9); $invalid = $matrix[$rowIndex]; for ($i = 0; $i < 9; $i++) { $invalid[] = $matrix[$i][$columnIndex]; } $box_row = $rowIndex % 3 == 0 ? $rowIndex : $rowIndex - $rowIndex % 3; $box_col = $columnIndex % 3 == 0 ? $columnIndex : $columnIndex - $columnIndex % 3; $invalid = array_unique(array_merge( $invalid, array_slice($matrix[$box_row], $box_col, 3), array_slice($matrix[$box_row + 1], $box_col, 3), array_slice($matrix[$box_row + 2], $box_col, 3) )); $valid = array_diff($valid, $invalid); shuffle($valid); return $valid; } private function _sortOptions($a, $b) { $a = count($a['permissible']); $b = count($b['permissible']); if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } }

Generate New Sudoku:

To generate a new puzzle just call the below function
generate();

Sample Example:

Below is the sample code to generate and solve sudoku puzzle and get in a html format
$grid = array(
    array(0,0,0,0,0,0,2,0,3),
    array(8,0,7,0,0,0,0,6,0),
    array(0,0,2,6,5,0,0,0,8),
    array(0,3,0,0,0,0,0,0,0),
    array(7,5,0,2,0,0,1,0,0),
    array(0,0,1,0,3,0,5,0,0),
    array(4,0,0,5,0,0,8,7,0),
    array(6,0,0,0,4,2,0,0,0),
    array(0,9,5,0,6,0,0,2,0)
);
$s = new Sudoku($grid);  // new class can be instantiated and stored in a variable using the new keyword and passing the variable into that function:
$s->solve();  /// This Function is to solve the puzzle..
echo $s->getHtml(); // DIsplay the solution or output in the html format

$s2 = new Sudoku();
$s2->generate();  // Generate the new sudoku puzzle
echo $s2->getHtml();

By posted on - 3rd Nov 2016