Repository navigation
Expand file tree
/
Copy pathCA.py
More file actions
62 lines (49 loc) · 2.04 KB
/
Copy pathCA.py
File metadata and controls
62 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 5 16:35:21 2020
@author: esteban
"""
# =============================================================================
# Usando la libreria de automata celular
# =============================================================================
import random
from cellular_automaton import Rule, MooreNeighborhood, EdgeRule, CAFactory, CAWindow
import pygame
ALIVE = [1.0]
DEAD = [0]
class ConwaysRule(Rule):
random_seed = random.seed(13)
def init_state(self, cell_coordinate):
rand = random.randrange(0, 16, 1)
init = max(.0, float(rand - 14))
return [init]
def evolve_cell(self, last_cell_state, neighbors_last_states):
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)
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)
ca = CAFactory.make_multi_process_cellular_automaton(dimension=[100, 100],
neighborhood=neighborhood,
rule=ConwaysRule,
processes=4)
ca_window = CAWindow(cellular_automaton=ca, evolution_steps_per_draw=1)
pygame.quit()