| Total Complexity | 70 |
| Total Lines | 387 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Sudoku often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sudoku, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Sudoku |
||
| 12 | { |
||
| 13 | |||
| 14 | public $sudoku, |
||
| 15 | $result, |
||
| 16 | $number; |
||
| 17 | private $limit, |
||
| 18 | $sq, |
||
| 19 | $chars, |
||
| 20 | $stack = array('1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Sudoku constructor |
||
| 24 | * |
||
| 25 | * @param int $limit |
||
| 26 | */ |
||
| 27 | public function __construct($limit = 9) |
||
| 28 | { |
||
| 29 | ini_set('memory_limit', -1); |
||
| 30 | set_time_limit(0); |
||
| 31 | |||
| 32 | $this->number = 0; |
||
| 33 | $this->limit = $limit; |
||
| 34 | $this->sq = (int)sqrt($this->limit); |
||
| 35 | |||
| 36 | $this->logic(); |
||
| 37 | |||
| 38 | return $this; |
||
| 39 | |||
| 40 | } |
||
| 41 | public static function world() |
||
| 42 | { |
||
| 43 | return 'Hello World, Composer!'; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function logic() |
||
| 47 | { |
||
| 48 | if(count($this->stack) < $this->limit) |
||
| 49 | { |
||
| 50 | echo 'error'; |
||
| 51 | exit; |
||
|
|
|||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | public function set($number) |
||
| 61 | |||
| 62 | } |
||
| 63 | |||
| 64 | private function return_row($cell) |
||
| 65 | { |
||
| 66 | return floor($cell / $this->limit); |
||
| 67 | |||
| 68 | } |
||
| 69 | |||
| 70 | private function return_col($cell) |
||
| 71 | { |
||
| 72 | return $cell % $this->limit; |
||
| 73 | |||
| 74 | } |
||
| 75 | |||
| 76 | private function return_block($cell) |
||
| 77 | { |
||
| 78 | return floor($this->return_row($cell) / $this->sq) * $this->sq + floor($this->return_col($cell) / $this->sq); |
||
| 79 | |||
| 80 | } |
||
| 81 | |||
| 82 | private function is_possible_row($number, $row) |
||
| 83 | { |
||
| 84 | $possible = true; |
||
| 85 | for ($x = 0; $x < $this->limit; $x++) |
||
| 86 | { |
||
| 87 | if (isset($this->sudoku[$row * $this->limit + $x]) && $this->sudoku[$row * $this->limit + $x] == $number) |
||
| 88 | { |
||
| 89 | $possible = false; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | return $possible; |
||
| 94 | } |
||
| 95 | |||
| 96 | private function is_possible_col($number, $col) |
||
| 97 | { |
||
| 98 | $possible = true; |
||
| 99 | for ($x = 0; $x < $this->limit; $x++) |
||
| 100 | { |
||
| 101 | if (isset($this->sudoku[$col + $this->limit * $x]) && $this->sudoku[$col + $this->limit * $x] == $number) |
||
| 102 | { |
||
| 103 | $possible = false; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return $possible; |
||
| 108 | } |
||
| 109 | |||
| 110 | private function is_possible_block($number, $block) |
||
| 111 | { |
||
| 112 | $possible = true; |
||
| 113 | for ($x = 0; $x < $this->limit; $x++) |
||
| 114 | { |
||
| 115 | $index = floor($block / $this->sq) * $this->sq * $this->limit + $x % $this->sq + $this->limit * floor($x / $this->sq) + $this->sq * ($block % $this->sq); |
||
| 116 | if (isset($this->sudoku[$index]) && $this->sudoku[$index] == $number) |
||
| 117 | { |
||
| 118 | $possible = false; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $possible; |
||
| 123 | } |
||
| 124 | |||
| 125 | private function is_possible_number($cell, $number) |
||
| 126 | { |
||
| 127 | $row = $this->return_row($cell); |
||
| 128 | $col = $this->return_col($cell); |
||
| 129 | $block = $this->return_block($cell); |
||
| 130 | |||
| 131 | return ($this->is_possible_row($number, $row) && $this->is_possible_col($number, $col) && $this->is_possible_block($number, $block)); |
||
| 132 | } |
||
| 133 | |||
| 134 | private function is_correct_row($row) |
||
| 135 | { |
||
| 136 | $row_temp = array(); |
||
| 137 | for ($x = 0; $x < $this->limit; $x++) |
||
| 138 | { |
||
| 139 | if(!isset($this->sudoku[$row * $this->limit + $x])) |
||
| 140 | { |
||
| 141 | $this->sudoku[$row * $this->limit + $x] = null; |
||
| 142 | } |
||
| 143 | $row_temp[$x] = $this->sudoku[$row * $this->limit + $x]; |
||
| 144 | } |
||
| 145 | |||
| 146 | return count(array_diff($this->chars, $row_temp)) == 0; |
||
| 147 | } |
||
| 148 | |||
| 149 | private function is_correct_col($col) |
||
| 150 | { |
||
| 151 | $col_temp = array(); |
||
| 152 | for ($x = 0; $x < $this->limit; $x++) |
||
| 153 | { |
||
| 154 | $col_temp[$x] = $this->sudoku[$col + $x * $this->limit]; |
||
| 155 | } |
||
| 156 | return count(array_diff($this->chars, $col_temp)) == 0; |
||
| 157 | } |
||
| 158 | |||
| 159 | private function is_correct_block($block) |
||
| 160 | { |
||
| 161 | $block_temp = array(); |
||
| 162 | for ($x = 0; $x < $this->limit; $x++) |
||
| 163 | { |
||
| 164 | $lookingfor = floor($block / $this->sq) * ($this->sq * $this->limit) + ($x % $this->sq) + $this->limit * floor($x / $this->sq) + $this->sq * ($block % $this->sq); |
||
| 165 | |||
| 166 | if (!isset($this->sudoku[$lookingfor])) |
||
| 167 | { |
||
| 168 | $this->sudoku[$lookingfor] = null; |
||
| 169 | } |
||
| 170 | |||
| 171 | $block_temp[$x] = $this->sudoku[$lookingfor]; |
||
| 172 | } |
||
| 173 | return count(array_diff($this->chars, $block_temp)) == 0; |
||
| 174 | } |
||
| 175 | |||
| 176 | private function is_solved_sudoku() |
||
| 177 | { |
||
| 178 | for ($x = 0; $x < $this->limit; $x++) |
||
| 179 | { |
||
| 180 | if (!$this->is_correct_block($x) or !$this->is_correct_row($x) or !$this->is_correct_col($x)) |
||
| 181 | { |
||
| 182 | return false; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | return true; |
||
| 186 | } |
||
| 187 | |||
| 188 | private function determine_possible_values($cell) |
||
| 189 | { |
||
| 190 | $possible = array(); |
||
| 191 | for ($x = 0; $x < $this->limit; $x++) |
||
| 192 | { |
||
| 193 | if ($this->is_possible_number($cell, $this->chars[$x])) |
||
| 194 | { |
||
| 195 | array_unshift($possible, $this->chars[$x]); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | return $possible; |
||
| 200 | } |
||
| 201 | |||
| 202 | private function determine_random_possible_value($possible, $cell) |
||
| 203 | { |
||
| 204 | return $possible[$cell][rand(0, count($possible[$cell]) - 1)]; |
||
| 205 | } |
||
| 206 | |||
| 207 | private function scan_sudoku_for_unique() |
||
| 208 | { |
||
| 209 | $possible = false; |
||
| 210 | for ($x = 0; $x < $this->limit * $this->limit; $x++) |
||
| 211 | { |
||
| 212 | if (!isset($this->sudoku[$x])) |
||
| 213 | { |
||
| 214 | $possible[$x] = $this->determine_possible_values($x, $this->sudoku); |
||
| 215 | if (count($possible[$x]) == 0) |
||
| 216 | { |
||
| 217 | return (false); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | return $possible; |
||
| 223 | } |
||
| 224 | |||
| 225 | private function remove_attempt($attempt_array, $number) |
||
| 226 | { |
||
| 227 | $new_array = array(); |
||
| 228 | $count = count($attempt_array); |
||
| 229 | for ($x = 0; $x < $count; $x++) |
||
| 230 | { |
||
| 231 | if ($attempt_array[$x] != $number) |
||
| 232 | { |
||
| 233 | array_unshift($new_array, $attempt_array[$x]); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | return $new_array; |
||
| 237 | } |
||
| 238 | |||
| 239 | |||
| 240 | private function next_random($possible) |
||
| 241 | { |
||
| 242 | $min_choices = null; |
||
| 243 | $max = $this->limit; |
||
| 244 | for ($x = 0; $x < $this->limit * $this->limit; $x++) |
||
| 245 | { |
||
| 246 | if (!isset($possible[$x])) |
||
| 247 | { |
||
| 248 | $possible[$x] = null; |
||
| 249 | } |
||
| 250 | |||
| 251 | if ((count($possible[$x]) <= $max) && (count($possible[$x]) > 0)) |
||
| 252 | { |
||
| 253 | $max = count($possible[$x]); |
||
| 254 | $min_choices = $x; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | return $min_choices; |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | private function build() |
||
| 262 | { |
||
| 263 | $this->sudoku = array(); |
||
| 264 | $this->chars = array(); |
||
| 265 | |||
| 266 | for ($i = 0; $i < $this->limit; $i++) |
||
| 267 | { |
||
| 268 | $this->chars[] = $this->stack[$i]; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | public function microtime() |
||
| 273 | { |
||
| 274 | return microtime(true); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function generate() |
||
| 316 | } |
||
| 317 | |||
| 318 | private function array2export() |
||
| 319 | { |
||
| 320 | return array( |
||
| 321 | 'sudoku' => $this->sudoku, |
||
| 322 | 'limit' => $this->limit, |
||
| 323 | 'result' => $this->result, |
||
| 324 | |||
| 325 | ); |
||
| 326 | } |
||
| 327 | |||
| 328 | public function to($to) |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | |||
| 345 | public function draw($echo = true) |
||
| 398 | } |
||
| 399 | |||
| 400 | } |
||
| 401 | |||
| 402 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.