Completed
Push — develop ( e322b0...461e3c )
by Barry
02:41
created

Grid::setNextState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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