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