-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathLife.py
More file actions
67 lines (47 loc) · 1.59 KB
/
Life.py
File metadata and controls
67 lines (47 loc) · 1.59 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
63
64
65
66
67
""" Code example from Complexity and Computation, a book about
exploring complexity science with Python. Available free from
http://greenteapress.com/complexity
Copyright 2016 Allen Downey
MIT License: http://opensource.org/licenses/MIT
"""
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
"""
For animation to work in the notebook, you might have to install
ffmpeg. On Ubuntu and Linux Mint, the following should work.
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install ffmpeg
"""
from Cell2D import Cell2D
from scipy.signal import correlate2d
class Life(Cell2D):
"""Implementation of Conway's Game of Life."""
kernel = np.array([[1, 1, 1], [1, 10, 1], [1, 1, 1]])
table = np.zeros(20, dtype=np.uint8)
table[[3, 12, 13]] = 1
def step(self):
"""Executes one time step."""
c = correlate2d(self.array, self.kernel, mode="same")
self.array = self.table[c]
def main(script, *args):
"""Constructs a puffer train.
Uses the entities in this file:
http://www.radicaleye.com/lifepage/patterns/puftrain.lif
"""
lwss = ["0001", "00001", "10001", "01111"]
bhep = ["1", "011", "001", "001", "01"]
n = 400
m = 600
life = Life(n, m)
col = 120
life.add_cells(n // 2 + 12, col, *lwss)
life.add_cells(n // 2 + 26, col, *lwss)
life.add_cells(n // 2 + 19, col, *bhep)
life.animate(frames=1000)
plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99)
plt.show()
if __name__ == "__main__":
main(*sys.argv)