neuropercolation/cellular_automaton/cell.py

46 lines
1.8 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):
return self._state.is_set_for_redraw()
def was_redrawn(self):
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-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-16 17:05:26 +00:00
self.set_new_state_and_activate(new_state, evolution_step)
2019-01-31 12:38:48 +00:00
def set_new_state_and_activate(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-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)