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

Organisms::getNeighborIndexes()   C

Complexity

Conditions 13
Paths 256

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 10
nc 256
nop 3
dl 0
loc 15
rs 5.6601
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MidoriKocak\GameOfLife;
4
5
/**
6
 * Class Organisms
7
 *
8
 * @package MidoriKocak\GameOfLife
9
 */
10
class Organisms
11
{
12
    /**
13
     * Multidimensional array of organisms
14
     *
15
     * @var int[][]
16
     */
17
    private $cells;
18
19
    /**
20
     * Organisms constructor.
21
     * @param int[][] $cells
22
     */
23
    public function __construct(array $cells)
24
    {
25
        $this->cells = $cells;
26
    }
27
28
    /**
29
     * Runs the game.
30
     * Checks each cell for the next iteration.
31
     */
32
    public function iterate()
33
    {
34
        $next = $this->cells;
35
36
        for ($i = 0; $i < sizeof($this->cells); $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...
37
            for ($j = 0; $j < sizeof($this->cells[0]); $j++) {
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...
38
                $next[$i][$j] = $this->checkCell($i, $j);
39
            }
40
        }
41
42
        $this->cells = $next;
43
    }
44
45
    /**
46
     * Returns the array of cells
47
     *
48
     * @return array|\int[][]
49
     */
50
    public function getCells()
51
    {
52
        return $this->cells;
53
    }
54
55
    /**
56
     * Helper function to get neighbor count of matrix element
57
     *
58
     * @param $i
59
     * @param $j
60
     * @return array
61
     */
62
    private function getNeighborCount($i, $j)
63
    {
64
        $neighbors = [];
65
        $neighbors[$this->cells[$i][$j]] = 0;
66
        $indexes = self::getNeighborIndexes($this->cells, $i, $j);
67
        foreach ($indexes as $coordinate) {
68
            $y = $coordinate[0];
69
            $x = $coordinate[1];
70
            $type = $this->cells[$y][$x];
71
72
            if ($this->cells[$y][$x] > 0) {
73
                $neighbors[$type] = $neighbors[$type] ?? 0;
74
                $neighbors[$type]++;
75
            }
76
        }
77
        return $neighbors;
78
    }
79
80
    /**
81
     * Check the matrix cell if it will survive, die or reproduce.
82
     *
83
     * @param $i
84
     * @param $j
85
     * @return int|mixed
86
     */
87
    private function checkCell($i, $j)
88
    {
89
        $neighbors = $this->getNeighborCount($i, $j);
90
        if ($this->cells[$i][$j] > 0) {
91
            if ($neighbors[$this->cells[$i][$j]] == 2 || $neighbors[$this->cells[$i][$j]] == 3) {
92
                return $this->cells[$i][$j];
93
            } elseif ($neighbors[$this->cells[$i][$j]] < 2) {
94
                return 0;
95
            } elseif ($neighbors[$this->cells[$i][$j]] >= 4) {
96
                return 0;
97
            }
98
        } else {
99
            $speciesEqualToTree = [];
100
            foreach ($neighbors as $type => $count) {
101
                if ($count == 3) {
102
                    array_push($speciesEqualToTree, $type);
103
                }
104
            }
105
106
            if (!empty($speciesEqualToTree)) {
107
                return $speciesEqualToTree[rand(0, sizeof($speciesEqualToTree) - 1)];
108
            } else {
109
                return 0;
110
            }
111
        }
112
        return 0;
113
    }
114
115
    /**
116
     * Helper method that returns indexes of neighbors of a Matrix element.
117
     *
118
     * @param $matrix
119
     * @param $i
120
     * @param $j
121
     * @return array
122
     */
123
    private static function getNeighborIndexes($matrix, $i, $j)
124
    {
125
126
        $indexes = [];
127
128
        if ($i > 0 && $j > 0) array_push($indexes, [$i - 1, $j - 1]);
129
        if ($i > 0) array_push($indexes, [$i - 1, $j]);
130
        if ($i > 0 && $j < sizeof($matrix[0]) - 1) array_push($indexes, [$i - 1, $j + 1]);
131
        if ($j > 0) array_push($indexes, [$i, $j - 1]);
132
        if ($j < sizeof($matrix[0]) - 1) array_push($indexes, [$i, $j + 1]);
133
        if ($i < sizeof($matrix) - 1 && $j > 0) array_push($indexes, [$i + 1, $j - 1]);
134
        if ($i < sizeof($matrix) - 1) array_push($indexes, [$i + 1, $j]);
135
        if ($i < sizeof($matrix) - 1 && $j < sizeof($matrix[0]) - 1) array_push($indexes, [$i + 1, $j + 1]);
136
137
        return $indexes;
138
    }
139
140
}