neuropercolation/src/cellular_automaton/ca_cell.py

22 lines
964 B
Python
Raw Normal View History

2019-01-31 12:38:48 +00:00
from cellular_automaton.ca_cell_state import CellState
from typing import Type
2019-01-31 12:38:48 +00:00
2018-12-01 19:37:18 +00:00
class Cell:
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)