Generator::getPossibleValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Chipulaja\Algo\Sudoku;
4
5
class Generator
6
{
7
    protected $helper;
8
    
9 1
    public function __construct()
10
    {
11 1
        $this->helper  = new GeneratorHelper();
12 1
    }
13
14 1
    public function generate()
15
    {
16 1
        $data = $this->getEmptyBoard();
17 1
        $exclude = [];
18 1
        for ($x = 0; $x <= 8; $x++) {
19 1
            $data[$x] = $this->getDataRow($data, $x);
20 1
            if (sizeof($data[$x]) <= 8) {
21 1
                $exclude[$x][0][] = array_slice($data[$x - 1], 0, 3);
22 1
                $data[$x] = [0,0,0, 0,0,0, 0,0,0];
23 1
                $data[$x - 1] = [0,0,0, 0,0,0, 0,0,0];
24 1
                $x = $x - 2;
25
            }
26
        }
27
28 1
        return $data;
29
    }
30
31 1
    private function getDataRow($data, $x, $dataRow = [], $exclude = [])
32
    {
33 1
        for ($y = 0; $y <= 8; $y++) {
34 1
            $location = ["x" => $x, "y" => $y];
35 1
            $excludeRow = (array)@$exclude[$x][$y];
36 1
            $randomNumber = $this->getRandomNumberPosible($location, $data, $dataRow, $excludeRow);
37 1
            $dataRow[$y] = $randomNumber;
38 1
            if ($randomNumber === 0 && $y === 0) {
39 1
                return [];
40 1
            } elseif ($randomNumber === 0) {
41 1
                unset($dataRow[$y]);
42 1
                if (isset($exclude[$x][$y])) {
43 1
                    unset($exclude[$x][$y]);
44
                }
45 1
                $yBack = $y - 1;
46 1
                if (isset($dataRow[$yBack])) {
47 1
                    $y = (($y - 2) >= -1) ? ($y - 2) : -1;
48 1
                    $exclude[$x][$yBack][] = $dataRow[$yBack];
49 1
                    unset($dataRow[$yBack]);
50
                };
51
            }
52
        }
53
54 1
        return $dataRow;
55
    }
56
57 1
    private function getRandomNumberPosible($location, $data, $dataRow = [], $exclude = [])
58
    {
59 1
        $horisontalValue = $this->helper->getAllHorisontalValue($location, $data);
60 1
        $verticalValue = $this->helper->getAllVerticalValue($location, $data);
61 1
        $squereValue = $this->helper->getAllSquereValue($location, $data);
62 1
        $allValue = array_merge(
63 1
            $horisontalValue,
64 1
            $verticalValue,
65 1
            $squereValue,
66 1
            $dataRow,
67 1
            $exclude
68
        );
69 1
        $allValueUnique = array_unique($allValue);
70 1
        $randomNumberPosible = $this->getRandomNumber($allValueUnique);
71 1
        return $randomNumberPosible;
72
    }
73
74 1
    private function getRandomNumber(array $exclude = [])
75
    {
76 1
        $numbers = $this->getPossibleValue($exclude);
77 1
        shuffle($numbers);
78 1
        return (int)@$numbers[0];
79
    }
80
81 1
    private function getPossibleValue($exclude, $data = [1,2,3,4,5,6,7,8,9])
82
    {
83 1
        $dataDiff = array_diff($data, $exclude);
84 1
        $possibleValue = [];
85 1
        foreach ($dataDiff as $index => $value) {
86 1
            if (in_array($value, range(1, 9))) {
87 1
                $possibleValue[$index] = $value;
88
            }
89
        }
90 1
        return $possibleValue;
91
    }
92
93 1
    private function getEmptyBoard()
94
    {
95 1
        $board = [];
96 1
        for ($i = 0; $i <= 8; $i++) {
97 1
            $board[$i] = array_fill(0, 9, 0);
98
        }
99 1
        return $board;
100
    }
101
}
102