PuzzleGenerator::digLocations()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CoenMooij\Sudoku\Generator;
6
7
use CoenMooij\Sudoku\Puzzle\Difficulty;
8
use CoenMooij\Sudoku\Puzzle\Grid;
9
use CoenMooij\Sudoku\Puzzle\Location;
10
use CoenMooij\Sudoku\Puzzle\Puzzle;
11
use CoenMooij\Sudoku\Validator\DigValidator;
12
13
class PuzzleGenerator
14
{
15
    /**
16
     * @var Grid
17
     */
18
    private $grid;
19
20
    /**
21
     * @var DigValidator
22
     */
23
    private $digValidator;
24
25
    public function __construct(DigValidator $digValidator)
26
    {
27
        $this->digValidator = $digValidator;
28
    }
29
30
    public function generate(Grid $solutionGrid, Difficulty $difficulty): Puzzle
31
    {
32
        $this->grid = $solutionGrid;
33
        $locationList = $this->getRandomLocations($difficulty);
34
        $this->digLocations($difficulty, ...$locationList);
35
36
        return new Puzzle($this->grid);
37
    }
38
39
    /**
40
     * @return Location[]
41
     */
42
    private function getRandomLocations(Difficulty $difficulty): array
43
    {
44
        $locationList = [];
45
        $numberOfHoles = $difficulty->getNumberOfHoles();
46
        for ($i = 0; $i < $numberOfHoles; $i++) {
47
            do {
48
                $location = new Location(random_int(0, 8), random_int(0, 8));
49
            } while ($this->locationInList($location, ...$locationList));
50
51
            $locationList[] = $location;
52
        }
53
54
        return $locationList;
55
    }
56
57
    private function digLocations(Difficulty $difficulty, Location ...$locationList): void
58
    {
59
        $bound = $difficulty->getBound();
60
61
        foreach ($locationList as $location) {
62
            if ($this->digValidator->isDiggableAndUniquelySolvableAfterDigging($this->grid, $location, $bound)) {
63
                $this->grid->empty($location);
64
            }
65
        }
66
    }
67
68
    private function locationInList(Location $needle, Location ...$locationList): bool
0 ignored issues
show
Unused Code introduced by
The method locationInList() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
69
    {
70
        foreach ($locationList as $location) {
71
            if (Location::match($needle, $location)) {
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
}
79