2019-01-31 12:38:48 +00:00
|
|
|
from cellular_automaton.ca_cell_state import CellState
|
2019-02-03 16:17:57 +00:00
|
|
|
from typing import Type
|
2019-01-31 12:38:48 +00:00
|
|
|
|
|
|
|
|
2018-12-01 19:37:18 +00:00
|
|
|
class Cell:
|
2019-02-15 19:02:57 +00:00
|
|
|
def __init__(self, state_class: Type[CellState]):
|
2019-02-09 17:16:19 +00:00
|
|
|
self.state = state_class()
|
2019-02-16 17:05:26 +00:00
|
|
|
self.neighbor_states = []
|
2019-01-31 12:38:48 +00:00
|
|
|
|
2019-02-16 17:05:26 +00:00
|
|
|
def evolve_if_ready(self, rule, evolution_step):
|
|
|
|
if self.state.is_active(evolution_step):
|
|
|
|
new_state = rule(self.state.get_state_of_last_evolution_step(evolution_step),
|
|
|
|
[n.get_state_of_last_evolution_step(evolution_step) for n in self.neighbor_states])
|
|
|
|
self.set_new_state_and_activate(new_state, evolution_step)
|
2019-01-31 12:38:48 +00:00
|
|
|
|
2019-02-16 17:05:26 +00:00
|
|
|
def set_new_state_and_activate(self, new_state: CellState, evolution_step):
|
|
|
|
changed = self.state.set_state_of_evolution_step(new_state, evolution_step)
|
2019-01-31 12:38:48 +00:00
|
|
|
if changed:
|
2019-02-16 17:05:26 +00:00
|
|
|
self.state.set_active_for_next_evolution_step(evolution_step)
|
|
|
|
for n in self.neighbor_states:
|
|
|
|
n.set_active_for_next_evolution_step(evolution_step)
|