forked from AllenDowney/ComplexityScience
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLife.py
87 lines (64 loc) · 1.88 KB
/
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
""" 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
"""
from __future__ import print_function, division
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, Cell2DViewer
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]
class LifeViewer(Cell2DViewer):
"""Viewer for Game of Life."""
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)
viewer = LifeViewer(life)
anim = viewer.animate(frames=100, interval=1)
plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99)
plt.show()
if __name__ == '__main__':
main(*sys.argv)