-
Notifications
You must be signed in to change notification settings - Fork 0
/
cellular_automata.pyde
143 lines (97 loc) · 3.77 KB
/
cellular_automata.pyde
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
STOP = False
DEAD_CELL_COLOR = (243, 243, 197)
ALIVE_CELL_COLOR = (79, 189, 112)
DEAD_CELL_HOVER_COLOR = (227, 227, 118)
ALIVE_CELL_HOVER_COLOR = (0, 144, 46)
class Cell:
def __init__(self, pos, size_, is_alive, generation, alive_neighbs=0):
self.pos = pos
self.size_ = size_
self.alive_neighbs = alive_neighbs
self.is_alive = is_alive
self.generation = generation
def count_neighbors(self, cell_list):
rows = len(cell_list)
cols = len(cell_list[0])
rows_range = (self.pos[0] - 1, self.pos[0], self.pos[0] + 1)
cols_range = (self.pos[1] - 1, self.pos[1], self.pos[1] + 1)
for i in rows_range:
for j in cols_range:
if i == self.pos[0] and j == self.pos[1]:
continue
if cell_list[i % rows][j % cols].is_alive:
self.alive_neighbs += 1
def update(self):
if self.is_alive:
if self.alive_neighbs < 2 or self.alive_neighbs > 3:
self.is_alive = False
self.generation = 0
else:
self.generation += 1
else:
if self.alive_neighbs == 3:
self.is_alive = True
self.generation = 1
def show(self):
if self.is_alive:
fill(*ALIVE_CELL_COLOR)
else:
fill(*DEAD_CELL_COLOR)
if int(mouseX / CELL_SIZE) == self.pos[0] and int(mouseY / CELL_SIZE) == self.pos[1]:
if self.is_alive:
fill(*ALIVE_CELL_HOVER_COLOR)
else:
fill(*DEAD_CELL_HOVER_COLOR)
rect(self.pos[0] * self.size_, self.pos[1] * self.size_, self.size_, self.size_)
noStroke()
def setup():
global CELL_LIST, NEW_CELL_LIST, CELL_SIZE
CELL_LIST = []
NEW_CELL_LIST = []
CELL_SIZE = 10
size(1000, 1000)
noStroke()
for i in range(height/CELL_SIZE):
tmp_list = []
for j in range(width/CELL_SIZE):
is_alive=True if random(0, 1) > 0.9 else False
generation = 1 if is_alive else 0
cell = Cell((i, j), CELL_SIZE, is_alive, generation, alive_neighbs=0)
tmp_list.append(cell)
CELL_LIST.append(tmp_list)
background(*DEAD_CELL_COLOR)
for i in CELL_LIST:
for cell in i:
cell.show()
def draw():
global CELL_LIST, NEW_CELL_LIST, STOP
background(*DEAD_CELL_COLOR)
frameRate(10)
if not STOP:
for lst in CELL_LIST:
tmp_lst = []
for cell in lst:
new_cell = Cell(cell.pos, cell.size_, cell.is_alive, cell.generation, 0)
new_cell.count_neighbors(CELL_LIST)
new_cell.update()
tmp_lst.append(new_cell)
NEW_CELL_LIST.append(tmp_lst)
CELL_LIST, NEW_CELL_LIST = NEW_CELL_LIST, []
for lst in CELL_LIST:
for cell in lst:
cell.show()
def keyReleased():
global STOP, CELL_LIST
if keyCode == 32: # space
STOP = True if not STOP else False
elif keyCode == 8: # backspace
# kill all cells
for row in CELL_LIST:
for cell in row:
cell.is_alive = False
def mousePressed():
global CELL_LIST, CELL_SIZE
if mouseButton == LEFT:
j = mouseY // CELL_SIZE
i = mouseX // CELL_SIZE
CELL_LIST[i][j].is_alive = True if not CELL_LIST[i][j].is_alive else False