| Total Complexity | 11 |
| Total Lines | 60 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | class Cell |
||
| 8 | { |
||
| 9 | /** @var Config */ |
||
| 10 | protected $config; |
||
| 11 | |||
| 12 | /** @var int */ |
||
| 13 | protected $state; |
||
| 14 | |||
| 15 | /** @var int */ |
||
| 16 | protected $nextState; |
||
| 17 | |||
| 18 | 12 | public function __construct(Config $config) |
|
| 19 | { |
||
| 20 | 12 | $this->config = $config; |
|
| 21 | |||
| 22 | 12 | $this->state = $this->getRandomState(); |
|
| 23 | 12 | } |
|
| 24 | |||
| 25 | 12 | protected function getRandomState(): int |
|
| 26 | { |
||
| 27 | 12 | return mt_rand(0, $this->config->states() - 1); |
|
| 28 | } |
||
| 29 | |||
| 30 | 12 | public function getState(): int |
|
| 31 | { |
||
| 32 | 12 | return $this->state; |
|
| 33 | } |
||
| 34 | |||
| 35 | 6 | public function computeNextState(array $neighborStates) |
|
| 36 | { |
||
| 37 | 6 | $successorState = $this->getSuccessorState(); |
|
| 38 | 6 | $count = 0; |
|
| 39 | |||
| 40 | 6 | foreach ($neighborStates as $neighborState) { |
|
| 41 | 3 | if ($neighborState === $successorState) { |
|
| 42 | 3 | $count++; |
|
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | 6 | $this->nextState = $this->willCycle($count) ? $successorState : $this->state; |
|
| 47 | 6 | } |
|
| 48 | |||
| 49 | 6 | protected function getSuccessorState(): int |
|
| 50 | { |
||
| 51 | 6 | return ($this->state + 1) % $this->config->states(); |
|
| 52 | } |
||
| 53 | |||
| 54 | 6 | protected function willCycle(int $count): bool |
|
| 57 | } |
||
| 58 | |||
| 59 | 6 | public function setNextState() |
|
| 62 | 6 | } |
|
| 63 | |||
| 64 | 3 | public function __toString(): string |
|
| 67 | } |
||
| 68 | } |
||
| 69 |