2019-02-23 15:20:10 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import random
|
|
|
|
from cellular_automaton import *
|
|
|
|
|
|
|
|
|
2019-02-23 15:20:48 +00:00
|
|
|
ALIVE = [1.0]
|
|
|
|
DEAD = [0]
|
|
|
|
|
|
|
|
|
2019-02-23 15:20:10 +00:00
|
|
|
class TestRule(Rule):
|
2019-02-23 15:20:48 +00:00
|
|
|
random_seed = random.seed(13)
|
2019-02-23 15:20:10 +00:00
|
|
|
|
|
|
|
def init_state(self, cell_coordinate):
|
2019-02-23 15:20:48 +00:00
|
|
|
rand = random.randrange(0, 16, 1)
|
|
|
|
init = max(.0, float(rand - 14))
|
2019-02-23 16:37:18 +00:00
|
|
|
return [init]
|
2019-02-23 15:20:10 +00:00
|
|
|
|
|
|
|
def evolve_cell(self, last_cell_state, neighbors_last_states):
|
2019-02-23 15:20:48 +00:00
|
|
|
new_cell_state = last_cell_state
|
|
|
|
alive_neighbours = self.__count_alive_neighbours(neighbors_last_states)
|
|
|
|
if last_cell_state == DEAD and alive_neighbours == 3:
|
|
|
|
new_cell_state = ALIVE
|
|
|
|
if last_cell_state == ALIVE and alive_neighbours < 2:
|
|
|
|
new_cell_state = DEAD
|
|
|
|
if last_cell_state == ALIVE and 1 < alive_neighbours < 4:
|
|
|
|
new_cell_state = ALIVE
|
|
|
|
if last_cell_state == ALIVE and alive_neighbours > 3:
|
|
|
|
new_cell_state = DEAD
|
|
|
|
return new_cell_state
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __count_alive_neighbours(neighbours):
|
|
|
|
an = []
|
|
|
|
for n in neighbours:
|
|
|
|
if n == ALIVE:
|
|
|
|
an.append(1)
|
|
|
|
return len(an)
|
2019-02-23 15:20:10 +00:00
|
|
|
|
|
|
|
def get_state_draw_color(self, current_state):
|
|
|
|
return [255 if current_state[0] else 0, 0, 0]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
neighborhood = MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)
|
2019-02-23 15:20:48 +00:00
|
|
|
ca = CAFactory.make_multi_process_cellular_automaton(dimension=[100, 100],
|
|
|
|
neighborhood=neighborhood,
|
|
|
|
rule=TestRule,
|
|
|
|
processes=4)
|
2019-02-23 15:20:10 +00:00
|
|
|
ca_window = CAWindow(cellular_automaton=ca, evolution_steps_per_draw=1)
|