Life   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 101
ccs 13
cts 26
cp 0.5
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isEnded() 0 3 1
A printMatrixCli() 0 34 4
A start() 0 9 4
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 7
    public function __construct(World $world, Organisms $organisms)
38
    {
39 7
        $this->world = $world;
40 7
        $this->organisms = $organisms;
41 7
    }
42
43
    /**
44
     * Initializes the game
45
     *
46
     * @param bool $verbose
47
     */
48 3
    public function start($verbose = true)
49
    {
50 3
        if ($this->generations == 0) {
51 3
            while ($this->generations < $this->world->getIterations()) {
52 3
                $this->organisms->iterate();
53 3
                if ($verbose) {
54
                    self::printMatrixCli($this->organisms->getCells());
55
                }
56 3
                $this->generations++;
57
            }
58
        }
59 3
    }
60
61
    /**
62
     * Shows if the game reached max iteration
63
     *
64
     * @return bool
65
     */
66 2
    public function isEnded()
67
    {
68 2
        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
        $rows = sizeof($matrix);
98
        for ($i = 0; $i < $rows; $i++) {
99
            $out = implode($matrix[$i]) . "\n";
100
            for ($k = 0; $k < strlen($out); $k++) {
101
                if ($out[$k] > 0) {
102
                    $colorNumber = $out[$k] % 16;
103
                    echo "\033[" . $colors[$colorNumber] . "m" . $out[$k] . "\033[0m";
104
                } else {
105
                    echo $out[$k];
106
                }
107
            }
108
        }
109
        usleep(100000);
110
    }
111
112
}