2019-02-03 17:26:31 +00:00
|
|
|
from cellular_automaton.ca_cell import Cell, CellState
|
|
|
|
from cellular_automaton.ca_neighborhood import Neighborhood
|
|
|
|
from typing import Type
|
2019-02-09 17:16:19 +00:00
|
|
|
import itertools
|
2019-02-03 17:26:31 +00:00
|
|
|
|
|
|
|
|
2019-02-09 17:16:19 +00:00
|
|
|
class CAFactory:
|
|
|
|
@staticmethod
|
|
|
|
def make_cellular_automaton(dimension,
|
2019-02-03 17:26:31 +00:00
|
|
|
neighborhood: Type[Neighborhood],
|
|
|
|
state_class: Type[CellState]):
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
cells = CAFactory._make_cells(dimension, state_class)
|
|
|
|
CAFactory._apply_neighbourhood_to_cells(cells, neighborhood, dimension)
|
2019-02-15 19:02:57 +00:00
|
|
|
return cells
|
2019-02-09 17:16:19 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _make_cells(dimension, state_class):
|
|
|
|
cells = {}
|
|
|
|
for c in itertools.product(*[range(d) for d in dimension]):
|
2019-02-15 19:02:57 +00:00
|
|
|
cells[tuple(c)] = Cell(state_class)
|
2019-02-09 17:16:19 +00:00
|
|
|
return cells
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _apply_neighbourhood_to_cells(cells, neighborhood, dimension):
|
2019-02-15 19:02:57 +00:00
|
|
|
for coordinate, cell in cells.items():
|
|
|
|
n_coordinates = neighborhood.calculate_cell_neighbor_coordinates(coordinate, dimension)
|
|
|
|
cell.neighbours = [cells[tuple(nc)].state for nc in n_coordinates]
|
2019-02-03 17:26:31 +00:00
|
|
|
|