SimpleSolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 25
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A solve() 0 18 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CoenMooij\Sudoku\Solver;
6
7
use CoenMooij\Sudoku\Puzzle\Grid;
8
use CoenMooij\Sudoku\Puzzle\Location;
9
10
/**
11
 * Solves the given grid using only row, column, and block checks.
12
 */
13
class SimpleSolver implements GridSolverInterface
14
{
15
    /**
16
     * @var Grid
17
     */
18
    private $grid;
19
20
    public function solve(Grid $grid): Grid
21
    {
22
        $this->grid = $grid;
23
        for ($row = 0; $row < Grid::NUMBER_OF_ROWS; $row++) {
24
            for ($column = 0; $column < Grid::NUMBER_OF_COLUMNS; $column++) {
25
                $location = new Location($row, $column);
26
                if ($this->grid->isEmpty($location)) {
27
                    $possibleValues = $this->grid->getAllPossibilitiesFor($location);
28
                    if (count($possibleValues) === 1) {
29
                        $grid->set($location, $possibleValues[0]);
30
                        $column = 0;
31
                        $row = 0;
32
                    }
33
                }
34
            }
35
        }
36
37
        return $grid;
38
    }
39
}
40