forked from ggregg42/GameOfLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.py
230 lines (187 loc) · 5.27 KB
/
GameOfLife.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
# -*- coding: utf-8 -*-
import numpy as np
from GameOfLife_utils import readRLE_New, readPattern, plotcells, get_history, makeMovie, plt
import argparse
import os
''' Initial board '''
## Random board 60x60 with 20% alive
#B = np.random.choice(2,(60,60),p=[0.8,0.2]).astype(bool)
################################
def do_it(pattern,filename,output_dir,shape,pos,T,trim=False,rH=False, rV=False, tp=False):
''' Load a pattern from an RLE file, run evolution and make a movie
Options :
* pos = where to position the pattern on the board
* rH, rV and tp to reverse horizontal, vertical or transpose the dimensions
* trim : trim edges for nicer plot
'''
# Read RLE file in the rle folder
B = readPattern(pattern,shape,pos,rH=rH,rV=rV,tp=tp)
history = get_history(B,T)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
plotcells(history[0,:,:],os.path.join(output_dir,filename+"_init.png"))
plotcells(history[-1,:,:],os.path.join(output_dir,filename+"_end.png"))
makeMovie(history,os.path.join(output_dir,filename+".mp4"),trim=trim)
################################
#setting command line arguments parser
parser = argparse.ArgumentParser()
parser.add_argument('--rlefile_path', type=str, help="path of the RLE file to read")
parser.add_argument('--output_dir', type=str, help="output directory for frames and movies", default="output")
parser.add_argument('--ffmpeg_path', type=str, help="path to ffmpeg")
args = parser.parse_args()
# Path of ffmpeg executable for animation
if args.ffmpeg_path:
plt.rcParams['animation.ffmpeg_path'] = args.ffmpeg_path
if not args.rlefile_path:
raise ValueError("missing argument.")
Cshape, pos, T, rH, rV, trim, tp, pattern = readRLE_New(args.rlefile_path)
filename = os.path.split(args.rlefile_path)[1]
filename = filename.split('.')[0]
do_it(pattern=pattern,filename=filename,shape=Cshape,pos=pos,T=T,trim=trim,rH=rH,rV=rV,tp=tp,output_dir=args.output_dir)
#pattern = "example1"
#shapeY = 10
#pos = (6,4)
#T = 15
#do_it(pattern,shapeY,pos,T)
#pattern = "blinker"
#shapeY = 9
#pos = (6,4)
#T = 30
#do_it(pattern,shapeY,pos,T)
#pattern = "four"
#shapeY = 9
#pos = (6,4)
#T = 10
#do_it(pattern,shapeY,pos,T)
#pattern = "five"
#shapeY = 18
#pos = (13,8)
#T = 20
#do_it(pattern,shapeY,pos,T)
#pattern = "example2"
#shapeY = 10
#pos = (7,4)
#T = 15
#do_it(pattern,shapeY,pos,T)
#pattern = "stills"
#shapeY = 24
#pos = (8,2)
#T = 4
#do_it(pattern,shapeY,pos,T)
#pattern = "oscillos"
#shapeY = 16
#pos = (4,2)
#T = 30
#do_it(pattern,shapeY,pos,T)
#pattern = "beehiveplus"
#shapeY = 20
#pos = (15,7)
#T = 30
#do_it(pattern,shapeY,pos,T)
#pattern = "stairs6"
#shapeY = 28
#pos = (22,13)
#T = 100
#do_it(pattern,shapeY,pos,T)
#pattern = "Rpento"
#shapeY = 100
#pos = (70,55)
#T = 1500
#do_it(pattern,shapeY,pos,T,trim=True)
#pattern = "cthulhu"
#shapeY = 17
#pos = (9,2)
#T = 10
#do_it(pattern,shapeY,pos,T,rV=True)
#pattern = "exploder"
#shapeY = 20
#pos = (15,7)
#T = 100
#do_it(pattern,shapeY,pos,T)
#pattern = "ten"
#shapeY = 15
#pos = (8,7)
#T = 100
#do_it(pattern,shapeY,pos,T)
#pattern = "104P177"
#shapeY = 70
#pos = (40,12)
#T = 1000
#do_it(pattern,shapeY,pos,T)
#pattern = "spaceships"
#shapeY = 40
#pos = (60,9)
#T = 130
#do_it(pattern,shapeY,pos,T,rH=True)
#pattern = "canadagoose"
#shapeY = 50
#pos = (50,35)
#T = 150
#do_it(pattern,shapeY,pos,T,rV=True,rH=True)
#pattern = "60P5H2V0"
#shapeY = 50
#pos = (3,16)
#T = 200
#do_it(pattern,shapeY,pos,T,rV=True,rH=True, tp=True)
#pattern = "puffer1"
#shapeY = 150
#pos = (5,65)
#T = 600
#do_it(pattern,shapeY,pos,T,rV=True,tp=True)
#pattern = "backrake3"
#shapeY = 200
#pos = (5,90)
#T = 500
#do_it(pattern,shapeY,pos,T,rV=True,tp=True, trim=True)
#pattern = "linepuffer"
#shapeY = 300
#pos = (50,65)
#T = 800
#do_it(pattern,shapeY,pos,T,rV=True,tp=True)
#pattern = "gosperglidergun"
#shapeY = 60
#pos = (5,5)
#T = 200
#do_it(pattern,shapeY,pos,T)
#pattern = "10cellinfinitegrowth"
#shapeY = 360
#pos = (400,300)
#T = 4000
#do_it(pattern,shapeY,pos,T,trim=True)
#pattern = "breeder1"
#shapeY = 1080
#pos = (10,400)
#T = 2500
#do_it(pattern,shapeY,pos,T,rV=True, trim=True )
#pattern = "max"
#shapeY = 720
#pos = (640,360)
#T = 1000 # 1000
#do_it(pattern,shapeY,pos,T, trim=True)
#pattern = "p41660p5h2v0gun"
#shapeY = 1200
#pos = (500,100)
#T = 5000 # 1000
#do_it(pattern,shapeY,pos,T, trim=True)
#pattern = "otcametapixeloff"
#shapeY = 2200
#pos = (800,50)
#T = 1000 # 1000
#do_it(pattern,shapeY,pos,T)
## Random 20% alive
#for s in range(100):
# np.random.seed(s)
# B = np.random.choice(2,(3*16,3*9),p=[0.85,0.15]).astype(bool)
# T = 300
# history = get_history(B,T)
# #plotcells(history[0,:,:],"output/random"+str(s)+"_init.png")
# plotcells(history[-1,:,:],"output/random"+str(s)+"_end.png")
# #makeMovie(history,"output/random"+str(s)+".mp4")
#s = 1
#np.random.seed(s)
#B = np.random.choice(2,(3*16,3*9),p=[0.85,0.15]).astype(bool)
#T = 100
#history = get_history(B,T)
#plotcells(history[0,:,:],"output/random"+str(s)+"_init.png")
#plotcells(history[-1,:,:],"output/random"+str(s)+"_end.png")
#makeMovie(history,"output/random"+str(s)+".mp4")