Completed
Branch master (2d3735)
by Midori
15:34 queued 05:33
created

Life::start()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 9
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace MidoriKocak\GameOfLife;
4
5
/**
6
 * Class Life
7
 * @package MidoriKocak\GameOfLife
8
 */
9
class Life
10
{
11
    /**
12
     * World object acts as an info card.
13
     *
14
     * @var World
15
     */
16
    private $world;
17
18
    /**
19
     * DAO of organisms
20
     *
21
     * @var Organisms
22
     */
23
    private $organisms;
24
25
    /**
26
     * Keeps the number of current generation.
27
     *
28
     * @var int
29
     */
30
    private $generations = 0;
31
32
    /**
33
     * Life constructor.
34
     * @param World $world
35
     * @param Organisms $organisms
36
     */
37
    public function __construct(World $world, Organisms $organisms)
38
    {
39
        $this->world = $world;
40
        $this->organisms = $organisms;
41
    }
42
43
    /**
44
     * Initializes the game
45
     *
46
     * @param bool $verbose
47
     */
48
    public function start($verbose = true)
49
    {
50
        if ($this->generations == 0) {
51
            while ($this->generations < $this->world->getIterations()) {
52
                $this->organisms->iterate();
53
                if ($verbose) {
54
                    self::printMatrixCli($this->organisms->getCells());
55
                }
56
                $this->generations++;
57
            }
58
        }
59
    }
60
61
    /**
62
     * Shows if the game reached max iteration
63
     *
64
     * @return bool
65
     */
66
    public function isEnded()
67
    {
68
        return $this->generations == $this->world->getIterations();
69
    }
70
71
    /**
72
     * Helper method to print organisms to CLI.
73
     *
74
     * @param $matrix
75
     */
76
    private static function printMatrixCli($matrix)
77
    {
78
        system('clear');
79
        $colors = [
80
            "0;34",
81
            "0;32",
82
            "0;36",
83
            "0;31",
84
            "0;35",
85
            "0;33",
86
            "0;37",
87
            "1;30",
88
            "1;34",
89
            "1;32",
90
            "1;36",
91
            "1;31",
92
            "1;35",
93
            "1;33",
94
            "1;37"
95
        ];
96
97
        for ($i = 0; $i < sizeof($matrix); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
98
            $out = implode($matrix[$i]) . "\n";
0 ignored issues
show
Bug introduced by
The call to implode() has too few arguments starting with pieces. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            $out = /** @scrutinizer ignore-call */ implode($matrix[$i]) . "\n";

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
99
            for ($k = 0; $k < strlen($out); $k++) {
100
                if ($out[$k] > 0) {
101
                    $colorNumber = $out[$k] % 16;
102
                    echo "\033[" . $colors[$colorNumber] . "m" . $out[$k] . "\033[0m";
103
                } else {
104
                    echo $out[$k];
105
                }
106
            }
107
        }
108
        usleep(100000);
109
    }
110
111
}