1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MidoriKocak\GameOfLife; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class Organisms |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var int[][] |
10
|
|
|
*/ |
11
|
|
|
private $cells; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Organisms constructor. |
15
|
|
|
* @param int[][] $cells |
16
|
|
|
*/ |
17
|
|
|
public function __construct(array $cells) |
18
|
|
|
{ |
19
|
|
|
$this->cells = $cells; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function iterate() |
23
|
|
|
{ |
24
|
|
|
$next = $this->cells; |
25
|
|
|
|
26
|
|
|
for ($i = 0; $i < sizeof($this->cells); $i++) { |
|
|
|
|
27
|
|
|
for ($j = 0; $j < sizeof($this->cells[0]); $j++) { |
|
|
|
|
28
|
|
|
$next[$i][$j] = $this->checkCell($i, $j); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->cells = $next; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getCells() |
36
|
|
|
{ |
37
|
|
|
return $this->cells; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function getNeighborCount($i, $j) |
41
|
|
|
{ |
42
|
|
|
$neighbors = []; |
43
|
|
|
$neighbors[$this->cells[$i][$j]] = 0; |
44
|
|
|
$indexes = self::getNeighborIndexes($this->cells, $i, $j); |
45
|
|
|
foreach ($indexes as $coordinate) { |
46
|
|
|
$y = $coordinate[0]; |
47
|
|
|
$x = $coordinate[1]; |
48
|
|
|
$type = $this->cells[$y][$x]; |
49
|
|
|
|
50
|
|
|
if ($this->cells[$y][$x] > 0) { |
51
|
|
|
$neighbors[$type] = $neighbors[$type] ?? 0; |
52
|
|
|
$neighbors[$type]++; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
return $neighbors; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function checkCell($i, $j) |
59
|
|
|
{ |
60
|
|
|
$neighbors = $this->getNeighborCount($i, $j); |
61
|
|
|
if ($this->cells[$i][$j] > 0) { |
62
|
|
|
if ($neighbors[$this->cells[$i][$j]] == 2 || $neighbors[$this->cells[$i][$j]] == 3) { |
63
|
|
|
return $this->cells[$i][$j]; |
64
|
|
|
} elseif ($neighbors[$this->cells[$i][$j]] < 2) { |
65
|
|
|
return 0; |
66
|
|
|
} elseif ($neighbors[$this->cells[$i][$j]] >= 4) { |
67
|
|
|
return 0; |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$speciesEqualToTree = []; |
71
|
|
|
foreach ($neighbors as $type => $count) { |
72
|
|
|
if ($count == 3) { |
73
|
|
|
array_push($speciesEqualToTree, $type); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!empty($speciesEqualToTree)) { |
78
|
|
|
return $speciesEqualToTree[rand(0, sizeof($speciesEqualToTree) - 1)]; |
79
|
|
|
} else { |
80
|
|
|
return 0; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private static function getNeighborIndexes($matrix, $i, $j) |
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
$indexes = []; |
90
|
|
|
|
91
|
|
|
if ($i > 0 && $j > 0) array_push($indexes, [$i - 1, $j - 1]); |
92
|
|
|
if ($i > 0) array_push($indexes, [$i - 1, $j]); |
93
|
|
View Code Duplication |
if ($i > 0 && $j < sizeof($matrix[0]) - 1) array_push($indexes, [$i - 1, $j + 1]); |
|
|
|
|
94
|
|
|
if ($j > 0) array_push($indexes, [$i, $j - 1]); |
95
|
|
|
if ($j < sizeof($matrix[0]) - 1) array_push($indexes, [$i, $j + 1]); |
96
|
|
View Code Duplication |
if ($i < sizeof($matrix) - 1 && $j > 0) array_push($indexes, [$i + 1, $j - 1]); |
|
|
|
|
97
|
|
|
if ($i < sizeof($matrix) - 1) array_push($indexes, [$i + 1, $j]); |
98
|
|
View Code Duplication |
if ($i < sizeof($matrix) - 1 && $j < sizeof($matrix[0]) - 1) array_push($indexes, [$i + 1, $j + 1]); |
|
|
|
|
99
|
|
|
|
100
|
|
|
return $indexes; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
} |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: