Total Complexity | 10 |
Total Lines | 63 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
6 | class Life |
||
7 | { |
||
8 | private $world; |
||
9 | private $organisms; |
||
10 | private $generations = 0; |
||
11 | |||
12 | public function __construct(World $world, Organisms $organisms) |
||
13 | { |
||
14 | $this->world = $world; |
||
15 | $this->organisms = $organisms; |
||
16 | } |
||
17 | |||
18 | public function start($verbose = true) |
||
19 | { |
||
20 | if ($this->generations == 0) { |
||
21 | while ($this->generations < $this->world->getIterations()) { |
||
22 | $this->organisms->iterate(); |
||
23 | if ($verbose) { |
||
24 | self::printMatrixCli($this->organisms->getCells()); |
||
25 | } |
||
26 | $this->generations++; |
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | |||
31 | public function isEnded() |
||
32 | { |
||
33 | return $this->generations == $this->world->getIterations(); |
||
34 | } |
||
35 | |||
36 | private static function printMatrixCli($matrix) |
||
69 | } |
||
70 | |||
71 | } |
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: