I am writing an algorithm in PHP to solve this Sudoku puzzle. I created an object-oriented implementation with two classes: a class Square
for each individual fragment on the 9x9 board and a class Sudoku
that has a matrix Square
for representing the board.
The implementation of the algorithm that I use is a kind of triple level. The first step, which will solve only the most basic puzzles (but the most effective), is to fill in any squares that can take only one value based on the initial setup of the board and accordingly adjust the restrictions on the rest of the unresolved squares.
Usually this process of “constant distribution” does not completely resolve the issue, but it does solve a significant part. The second level will then begin. This analyzes each unit (or 9 squares, each of which must have unique number assignments, such as a row or column) for the “possible” values of each unresolved square. This list of possible values is presented as a string in the class Square
:
class Square {
private $name;
private $peers;
private $number;
private $possibles;
public function __construct($name, $p = 0) {
$this->name = $name;
$this->setNumber($p);
if ($p == 0) {
$this->possibles = "123456789";
}
}
( ), "" . - , . , , .
: , ? , , 1-9, 1 , , 1, , , - 27 .