neuropercolation/cellular_automaton/cell.py

55 lines
2.3 KiB
Python
Raw Normal View History

2019-02-23 15:20:48 +00:00
"""
Copyright 2019 Richard Feistenauer
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from . import cell_state
2019-01-31 12:38:48 +00:00
2018-12-01 19:37:18 +00:00
class Cell:
def __init__(self, state_class: cell_state.CellState, neighbors):
2019-02-23 15:20:48 +00:00
self._state = state_class
self._neighbor_states = neighbors
def is_set_for_redraw(self):
2019-02-24 11:37:26 +00:00
""" Flag indicating a change in the cells state since last call of 'was_redrawn'. """
2019-02-23 15:20:48 +00:00
return self._state.is_set_for_redraw()
def was_redrawn(self):
2019-02-24 11:37:26 +00:00
""" Should be called after this cell was drawn to prevent unnecessary redraws. """
2019-02-23 15:20:48 +00:00
self._state.was_redrawn()
def get_current_state(self, evolution_step):
return self._state.get_state_of_evolution_step(evolution_step)
2019-01-31 12:38:48 +00:00
2019-02-16 17:05:26 +00:00
def evolve_if_ready(self, rule, evolution_step):
2019-02-24 11:37:26 +00:00
""" When there was a change in this cell or one of its neighbours,
evolution rule is called and the new state and redraw flag gets set if necessary.
If there was a change neighbours will be notified.
"""
2019-02-23 15:20:48 +00:00
if self._state.is_active(evolution_step):
new_state = rule(list(self._state.get_state_of_last_evolution_step(evolution_step)),
[list(n.get_state_of_last_evolution_step(evolution_step)) for n in self._neighbor_states])
2019-02-24 11:37:26 +00:00
self.__set_new_state_and_consider_activation(new_state, evolution_step)
2019-01-31 12:38:48 +00:00
2019-02-24 11:37:26 +00:00
def __set_new_state_and_consider_activation(self, new_state: cell_state.CellState, evolution_step):
2019-02-23 15:20:48 +00:00
changed = self._state.set_state_of_evolution_step(new_state, evolution_step)
2019-02-24 11:37:26 +00:00
self.__activate_if_necessary(changed, evolution_step)
def __activate_if_necessary(self, changed, evolution_step):
2019-01-31 12:38:48 +00:00
if changed:
2019-02-23 15:20:48 +00:00
self._state.set_active_for_next_evolution_step(evolution_step)
for n in self._neighbor_states:
2019-02-16 17:05:26 +00:00
n.set_active_for_next_evolution_step(evolution_step)