From 03601c2060b801825b755cd6ee4105fe2dc3b792 Mon Sep 17 00:00:00 2001 From: rfeistenauer Date: Thu, 22 Oct 2020 15:33:13 +0200 Subject: [PATCH] Adds setter for automaton cells --- cellular_automaton/automaton.py | 9 ++++++++- tests/test_automaton.py | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cellular_automaton/automaton.py b/cellular_automaton/automaton.py index d649ccc..2bc8eac 100644 --- a/cellular_automaton/automaton.py +++ b/cellular_automaton/automaton.py @@ -89,7 +89,14 @@ class CellularAutomaton(CellularAutomatonCreator, abc.ABC): def get_cells(self): return self._current_state - cells = property(get_cells) + def set_cells(self, cells): + """ Sets the cell states both as current and next states """ + for (coordinate, c_cell), n_cell in zip(self._current_state.items(), self._next_state.values()): + new_cell_state = cells[coordinate].state + c_cell.state = new_cell_state + n_cell.state = new_cell_state + + cells = property(get_cells, set_cells) def get_evolution_step(self): return self._evolution_step diff --git a/tests/test_automaton.py b/tests/test_automaton.py index 7b243d5..98d0f42 100644 --- a/tests/test_automaton.py +++ b/tests/test_automaton.py @@ -34,17 +34,14 @@ class TAutomaton(ca.CellularAutomaton): NEIGHBORHOOD = ca.MooreNeighborhood(ca.EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS) -@pytest.fixture -def automaton(): - return TAutomaton(NEIGHBORHOOD, [3, 3]) - - -def test_process_evolution_steps(automaton): +def test_process_evolution_steps(): + automaton = TAutomaton(NEIGHBORHOOD, [3, 3]) automaton.evolve(5) assert automaton.evolution_step == 5 -def test_process_evolution_calls(automaton): +def test_process_evolution_calls(): + automaton = TAutomaton(NEIGHBORHOOD, [3, 3]) automaton.evolve(5) assert automaton.cells[(1, 1)].state[0] == 5 @@ -54,3 +51,11 @@ def test_dimensions(dimensions): automaton = TAutomaton(ca.MooreNeighborhood(), dimension=[3] * dimensions) automaton.evolve() assert automaton.cells[(1, ) * dimensions].state[0] == 1 + + +def test_process_evolution_calls(): + automaton = TAutomaton(NEIGHBORHOOD, [3, 3]) + automaton.evolve(5) + automaton2 = TAutomaton(NEIGHBORHOOD, [3, 3]) + automaton2.cells = automaton.cells + assert automaton2.cells[(1, 1)].state[0] == 5