Passed
Push — develop ( 162129...5dab4c )
by Barry
01:26
created

Grid::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Barryvanveen\CCA;
4
5
class Grid
6
{
7
    /** @var Config */
8
    protected $config;
9
10
    /** @var Cell[] */
11
    protected $cells = [];
12
13
    /** @var Coordinate[][] */
14
    protected $neighbors = [];
15
16
    public function __construct(Config $config)
17
    {
18
        $this->config = $config;
19
20
        for ($row = 0; $row < $this->config->rows(); $row++) {
21
            for ($column = 0; $column < $this->config->columns(); $column++) {
22
                $coordinate = new Coordinate($row, $column, $this->config->columns());
23
24
                $this->cells[$coordinate->position()] = new Cell($this->config);
25
26
                $this->neighbors[$coordinate->position()] =
27
                    Neighborhood::createNeighborhoodForCoordinate($this->config, $coordinate);
28
            }
29
        }
30
    }
31
32
    public function computeNextState()
33
    {
34
        foreach ($this->cells as $position => $cell) {
35
            $neighborCoordinates = $this->neighbors[$position];
36
37
            $neighborStates = $this->getStatesForCoordinates($neighborCoordinates);
38
39
            $this->cells[$position]->computeNextState($neighborStates);
40
        }
41
    }
42
43
    protected function getStatesForCoordinates(array $coordinates): array
44
    {
45
        $states = [];
46
47
        /** @var Coordinate $coordinate */
48
        foreach ($coordinates as $coordinate) {
49
            $states[] = $this->cells[$coordinate->position()]->getState();
50
        }
51
52
        return $states;
53
    }
54
55
    public function setNextState()
56
    {
57
        foreach ($this->cells as $position => $cell) {
58
            $this->cells[$position]->setNextState();
59
        }
60
    }
61
62
    public function toArray(): array
63
    {
64
        $states = [];
65
66
        foreach ($this->cells as $position => $cell) {
67
            $states[] = $cell->getState();
68
        }
69
70
        return $states;
71
    }
72
    
73
    public function __toString(): string
74
    {
75
        $string = '';
76
77
        // first line: numbered columns
78
        for ($column = 0; $column < $this->config->columns(); $column++) {
79
            if ($column === 0) {
80
                $string .= sprintf("  ");
81
            }
82
            $string .= sprintf("%.2d ", $column);
83
        }
84
        $string .= sprintf("\n");
85
86
        // rows
87
        for ($row = 0; $row < $this->config->rows(); $row++) {
88
            // number of current row
89
            $string .= sprintf("%.2d ", $row);
90
91
            // cell states
92
            for ($column = 0; $column < $this->config->columns(); $column++) {
93
                $coordinate = new Coordinate($row, $column, $this->config->columns());
94
                $string .= sprintf("%s ", $this->cells[$coordinate->position()]);
95
            }
96
            $string .= sprintf("\n");
97
        }
98
99
        $string .= sprintf("\n");
100
101
        return $string;
102
    }
103
}
104