2018-12-01 14:14:22 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-12-09 10:20:16 +00:00
|
|
|
from cellular_automaton.ca_cell_state import CellState
|
2019-01-31 12:38:48 +00:00
|
|
|
from cellular_automaton.ca_rule import Rule
|
2018-12-01 14:14:22 +00:00
|
|
|
|
2019-02-03 17:26:31 +00:00
|
|
|
from cellular_automaton.cellular_automaton import CellularAutomaton, CellularAutomatonProcessor
|
|
|
|
from cellular_automaton.ca_factory import Factory
|
|
|
|
|
2018-12-01 14:14:22 +00:00
|
|
|
|
2018-12-02 16:45:08 +00:00
|
|
|
class TestRule(Rule):
|
2019-01-06 10:21:46 +00:00
|
|
|
@staticmethod
|
2019-01-31 12:38:48 +00:00
|
|
|
def evolve_cell(last_cell_state, last_neighbour_states):
|
2019-01-06 10:21:46 +00:00
|
|
|
try:
|
2019-01-31 12:38:48 +00:00
|
|
|
return last_neighbour_states[0]
|
2019-01-06 10:21:46 +00:00
|
|
|
except IndexError:
|
2019-02-02 08:11:48 +00:00
|
|
|
print("damn neighbours")
|
2019-01-31 12:38:48 +00:00
|
|
|
pass
|
|
|
|
return False
|
2019-01-06 10:21:46 +00:00
|
|
|
|
2018-12-01 19:37:18 +00:00
|
|
|
|
2019-01-31 12:38:48 +00:00
|
|
|
class MyState(CellState):
|
|
|
|
def __init__(self):
|
|
|
|
rand = random.randrange(0, 101, 1)
|
|
|
|
init = 0
|
|
|
|
if rand > 99:
|
|
|
|
init = 1
|
2018-12-01 19:37:18 +00:00
|
|
|
|
2019-02-02 08:11:48 +00:00
|
|
|
super().__init__((float(init),), draw_first_state=False)
|
2018-12-09 10:20:16 +00:00
|
|
|
|
|
|
|
def get_state_draw_color(self, iteration):
|
|
|
|
red = 0
|
2019-01-31 12:38:48 +00:00
|
|
|
if self.get_state_of_last_iteration(iteration)[0]:
|
2018-12-09 10:20:16 +00:00
|
|
|
red = 255
|
|
|
|
return [red, 0, 0]
|
|
|
|
|
|
|
|
|
2019-02-03 17:26:31 +00:00
|
|
|
def make_cellular_automaton(dimension, neighborhood, rule, state_class):
|
|
|
|
ca_factory = Factory()
|
|
|
|
cells = ca_factory.make_cellular_automaton(dimension=dimension, neighborhood=neighborhood, state_class=state_class)
|
|
|
|
return CellularAutomaton(cells, dimension, rule)
|
|
|
|
|
|
|
|
|
2018-12-01 14:14:22 +00:00
|
|
|
if __name__ == "__main__":
|
2019-01-31 12:38:48 +00:00
|
|
|
import random
|
|
|
|
from multiprocessing import freeze_support
|
|
|
|
from cellular_automaton.ca_neighborhood import MooreNeighborhood, EdgeRule
|
|
|
|
from cellular_automaton.ca_display import PyGameFor2D
|
2019-02-03 16:17:57 +00:00
|
|
|
|
2019-01-31 12:38:48 +00:00
|
|
|
freeze_support()
|
2019-02-03 17:26:31 +00:00
|
|
|
|
2018-12-09 14:25:12 +00:00
|
|
|
random.seed(1000)
|
2019-02-03 17:26:31 +00:00
|
|
|
# best is 400/400 with 0,2 ca speed and 0,09 redraw
|
|
|
|
neighborhood = MooreNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)
|
|
|
|
ca = make_cellular_automaton(dimension=[100, 100], neighborhood=neighborhood, rule=TestRule(), state_class=MyState)
|
2019-01-06 10:21:46 +00:00
|
|
|
ca_window = PyGameFor2D(window_size=[1000, 800], cellular_automaton=ca)
|
2019-02-03 16:17:57 +00:00
|
|
|
ca_processor = CellularAutomatonProcessor(process_count=4, cellular_automaton=ca)
|
2019-01-06 10:21:46 +00:00
|
|
|
ca_window.main_loop(cellular_automaton_processor=ca_processor,
|
|
|
|
ca_iterations_per_draw=5)
|