|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chipulaja\Algo\Backtrack\Sudoku; |
|
4
|
|
|
|
|
5
|
|
|
class SudokuValidator |
|
6
|
|
|
{ |
|
7
|
1 |
|
public function isValidSudoku($data) |
|
8
|
|
|
{ |
|
9
|
1 |
|
$isValid = true; |
|
|
|
|
|
|
10
|
1 |
|
$locationHaveContents = $this->getLocationHaveContents($data); |
|
11
|
1 |
|
foreach ($locationHaveContents as $location) { |
|
12
|
1 |
|
$x = $location['x']; |
|
13
|
1 |
|
$y = $location['y']; |
|
14
|
1 |
|
$value = $data[$x][$y]; |
|
15
|
1 |
|
$vertical = $this->isValidVertical($location, $value, $data); |
|
16
|
1 |
|
if ($vertical === false) { |
|
17
|
1 |
|
return false; |
|
18
|
|
|
} |
|
19
|
1 |
|
$horisontal = $this->isValidHorisontal($location, $value, $data); |
|
20
|
1 |
|
if ($horisontal === false) { |
|
21
|
1 |
|
return false; |
|
22
|
|
|
} |
|
23
|
1 |
|
$squere = $this->isValidSquere($location, $value, $data); |
|
24
|
1 |
|
if ($squere === false) { |
|
25
|
1 |
|
return false; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
1 |
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
View Code Duplication |
public function getLocationHaveContents($data) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$location = []; |
|
34
|
1 |
|
foreach ($data as $x => $cells) { |
|
35
|
1 |
|
foreach ($cells as $y => $value) { |
|
36
|
1 |
|
if (!empty($value)) { |
|
37
|
1 |
|
$position = (int)($x."".$y); |
|
38
|
1 |
|
$location[$position] = [ |
|
39
|
1 |
|
'x' => $x, |
|
40
|
1 |
|
'y' => $y |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return $location; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
protected function isValidVertical($location, $value, $data) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$x = $location["x"]; |
|
52
|
1 |
|
$count = array_count_values($data[$x]); |
|
53
|
1 |
|
if ($count[$value] > 1) { |
|
54
|
1 |
|
return false; |
|
55
|
|
|
} |
|
56
|
1 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
protected function isValidHorisontal($location, $value, $data) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$dataY = []; |
|
62
|
1 |
|
foreach ($data as $x => $cells) { |
|
63
|
1 |
|
foreach ($cells as $y => $cellValue) { |
|
64
|
1 |
|
if ($y == $location["y"]) { |
|
65
|
1 |
|
$dataY[] = $cellValue; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
1 |
|
$count = array_count_values($dataY); |
|
70
|
1 |
|
if ($count[$value] > 1) { |
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
1 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
protected function isValidSquere($location, $value, $data) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$squereData = []; |
|
79
|
1 |
|
$x = $location['x']; |
|
80
|
1 |
|
$y = $location['y']; |
|
81
|
|
|
|
|
82
|
1 |
|
$iStart = 3 * floor($x/3); |
|
83
|
1 |
|
$iEnd = $iStart + 2; |
|
84
|
1 |
|
$jStart = 3 * floor($y/3); |
|
85
|
1 |
|
$jEnd = $jStart + 2; |
|
86
|
|
|
|
|
87
|
1 |
View Code Duplication |
for ($i=$iStart; $i<=$iEnd; $i++) { |
|
|
|
|
|
|
88
|
1 |
|
for ($j=$jStart; $j<=$jEnd; $j++) { |
|
89
|
1 |
|
if (!empty($data[$i][$j])) { |
|
90
|
1 |
|
$squereData[] = $data[$i][$j]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
$count = array_count_values($squereData); |
|
96
|
1 |
|
if ($count[$value] > 1) { |
|
97
|
1 |
|
return false; |
|
98
|
|
|
} |
|
99
|
1 |
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.