-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgame_of_life.py
310 lines (223 loc) · 8.17 KB
/
game_of_life.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
A collection of models derived from Conway's Game Of Life.
Experiment classes included.
"""
import importlib
from xentica import core
from xentica import seeds
from xentica.core import color_effects
from xentica.tools.rules import LifeLike
class GameOfLife(core.CellularAutomaton):
"""
The classic CA built with Xentica framework.
It has only one property called ``state``, which is positive
integer with max value of 1.
"""
state = core.IntegerProperty(max_val=1)
class Topology:
"""
Mandatory class for all ``CellularAutomaton`` instances.
All class variables below are also mandatory.
Here, we declare the topology as a 2-dimensional orthogonal
lattice with Moore neighborhood, wrapped to a 3-torus.
"""
dimensions = 2
lattice = core.OrthogonalLattice()
neighborhood = core.MooreNeighborhood()
border = core.TorusBorder()
def emit(self):
"""
Implement the logic of emit phase.
Statements below will be translated into C code as emit kernel
at the moment of class creation.
Here, we just copy main state to surrounding buffers.
"""
for buf in self.buffers:
buf.state = self.main.state
def absorb(self):
"""
Implement the logic of absorb phase.
Statements below will be translated into C code as well.
Here, we sum all neigbors buffered states and apply Conway
rule to modify cell's own state.
"""
neighbors_alive = core.IntegerVariable()
for i in range(len(self.buffers)):
neighbors_alive += self.neighbors[i].buffer.state
is_born = (8 >> neighbors_alive) & 1
is_sustain = (12 >> neighbors_alive) & 1
self.main.state = is_born | is_sustain & self.main.state
@color_effects.MovingAverage
def color(self):
"""
Implement the logic of cell's color calculation.
Must return a tuple of RGB values computed from ``self.main``
properties.
Also, must be decorated by a class from ``color_effects``
module.
Here, we simply define 0 state as pure black, and 1 state as
pure white.
"""
red = self.main.state * 255
green = self.main.state * 255
blue = self.main.state * 255
return (red, green, blue)
class GameOfLifeStatic(GameOfLife):
"""
Game of Life variant with static border made of live cells.
This is an example of how easy you can inherit other models.
"""
class Topology(GameOfLife.Topology):
"""
You can inherit parent class ``Topology``.
Then, override only necessary variables.
"""
border = core.StaticBorder(1)
class GameOfLifeColor(GameOfLife):
"""
Game Of Life variant with RGB color.
This is an example of how to use multiple properties per cell.
"""
state = core.IntegerProperty(max_val=1)
red = core.IntegerProperty(max_val=255)
green = core.IntegerProperty(max_val=255)
blue = core.IntegerProperty(max_val=255)
def emit(self):
"""Copy all properties to surrounding buffers."""
for buf in self.buffers:
buf.state = self.main.state
buf.red = self.main.red
buf.green = self.main.green
buf.blue = self.main.blue
def absorb(self):
"""
Calculate RGB as neighbors sum for living cell only.
Note, parent ``absorb`` method should be called using direct
class access, not via ``super``.
"""
GameOfLife.absorb(self)
red_sum = core.IntegerVariable()
green_sum = core.IntegerVariable()
blue_sum = core.IntegerVariable()
for i in range(len(self.buffers)):
red_sum += self.neighbors[i].buffer.red + 1
green_sum += self.neighbors[i].buffer.green + 1
blue_sum += self.neighbors[i].buffer.blue + 1
self.main.red = red_sum * self.main.state
self.main.green = green_sum * self.main.state
self.main.blue = blue_sum * self.main.state
@color_effects.MovingAverage
def color(self):
"""Calculate color as usual."""
red = self.main.state * self.main.red
green = self.main.state * self.main.green
blue = self.main.state * self.main.blue
return (red, green, blue)
class GameOfLife6D(GameOfLife):
"""
Game of Life variant in 6D.
Nothing interesting, just to prove you can do it with ease.
"""
class Topology(GameOfLife.Topology):
"""
Hyper-spacewalk, is as easy as increase ``dimensions`` value.
However, we are also changing neighborhood to Von Neumann
here, to prevent neighbors number exponential grow.
"""
dimensions = 6
neighborhood = core.VonNeumannNeighborhood()
class LifelikeCA(GameOfLife):
"""Lifelike CA with a flexible rule that could be changed at runtime."""
rule = core.Parameter(
default=LifeLike.golly2int("B3/S23"),
interactive=True,
)
def absorb(self):
"""Implement parent's clone with a rule as a parameter."""
neighbors_alive = core.IntegerVariable()
for i in range(len(self.buffers)):
neighbors_alive += self.neighbors[i].buffer.state
is_born = (self.rule >> neighbors_alive) & 1
is_sustain = (self.rule >> 9 >> neighbors_alive) & 1
self.main.state = is_born | is_sustain & self.main.state
def step(self):
"""Change the rule interactively after some time passed."""
if self.timestep == 23:
self.rule = LifeLike.golly2int("B3/S23")
super().step()
class GOLExperiment(core.Experiment):
"""
Particular experiment for the vanilla Game of Life.
Here, we define constants and initial conditions from which the
world's seed will be generated.
The ``word`` is an RNG seed string. The ``size``, ``zoom`` and
``pos`` are board contstants. The ``seed`` is a pattern used in
the initial board state generation.
``BigBang`` is a pattern when small area initialized with a
high-density random values.
"""
word = "OBEY XENTICA"
size = (640, 360, )
zoom = 3
pos = [0, 0]
seed = seeds.patterns.BigBang(
pos=(320, 180),
size=(100, 100),
vals={
"state": seeds.random.RandInt(0, 1),
}
)
class GOLExperiment2(GOLExperiment):
"""
Another experiment for the vanilla GoL.
Since it is inherited from ``GOLExperiment``, we can redefine only
values we need.
``PrimordialSoup`` is a pattern when the whole board is
initialized with low-density random values.
"""
word = "XENTICA IS YOUR GODDESS"
seed = seeds.patterns.PrimordialSoup(
vals={
"state": seeds.random.RandInt(0, 1),
}
)
class GOLExperimentColor(GOLExperiment):
"""
The experiment for ``GameOfLifeColor``.
Here, we introduce ``fade_out`` constant, which is used in
rendering and slowly fading out the color of cells.
Note, it is only an aestetic effect, and does not affect the real
cell state.
"""
fade_in = 255
fade_out = 10
smooth_factor = 1
seed = seeds.patterns.PrimordialSoup(
vals={
"state": seeds.random.RandInt(0, 1),
"red": seeds.random.RandInt(0, 255),
"green": seeds.random.RandInt(0, 255),
"blue": seeds.random.RandInt(0, 255),
}
)
class GOLExperiment6D(GOLExperiment2):
"""
Special experiment for 6D Life.
Here, we define the world with 2 spatial and 4 looped
micro-dimensions, 3 cells per micro-dimension.
As a result, we get large quasi-stable oscillators, looping over
micro-dimensions. Strangely formed, but nothing interesting,
really.
"""
size = (640, 360, 3, 3, 3, 3)
class DiamoebaExperiment(GOLExperiment):
"""Experiment with the interactive rule."""
rule = LifeLike.golly2int("B35678/S5678")
def main():
"""Run model/experiment interactively."""
moire = importlib.import_module("moire")
model = GameOfLifeColor(GOLExperimentColor)
gui = moire.GUI(runnable=model)
gui.run()
if __name__ == "__main__":
main()