-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (51 loc) · 1.61 KB
/
main.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
#Conway's Game of Life
import matplotlib.pyplot as plt
import numpy as np
#Init the Board
nBoardSize = 50 + 2 #2 Borders
Board = np.zeros([nBoardSize,nBoardSize],int)
nFrames = 100
#Random Board
np.random.seed(1307)
RBoard = np.array([[0 if i==0 or j==0 or i==nBoardSize-1 or j==nBoardSize-1 else np.random.choice([0,1]) for i in range(nBoardSize)] for j in range(nBoardSize)])
#Shape Definer
def DefineShape(sFileName):
return np.array([[1 if i=='#' else 0 for i in j.strip()] for j in open(sFileName,'r').read().strip().split('\n')])
#Some Initial Shapes
#Blinker
sSpaceShip = DefineShape("Shapes/Explode.txt")
#Shape Loading Func
def LoadShape(b, s, x, y):
for i in range(s.shape[0]):
for j in range(s.shape[1]):
b[x+i+1,y+j+1]=s[i,j] #Ignoring Borders
#Update Board
def Update(b):
b2 = np.copy(b)
nbrs=[(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for i in range(1,b.shape[0]-1):
for j in range(1,b.shape[1]-1):
s=0
for k in nbrs:
s+=b[i+k[0],j+k[1]]
birth = (b[i,j]==0 and s==3)
alive = b[i,j]==1 and (s==2 or s==3)
b2[i,j] = 1 if (birth or alive) else 0
return b2
#Load a shape
LoadShape(Board, sSpaceShip, 10, 10)
#Display Image
nFrameNo=0
plt.imshow(Board)
plt.axis("off")
plt.savefig("Images/{:06d}.png".format(nFrameNo), bbox_inches='tight')
plt.close()
#Animation Loop
while(nFrameNo<nFrames):
nFrameNo+=1
print(nFrameNo)
Board=Update(Board)
plt.imshow(Board)
plt.axis("off")
plt.savefig("Images/{:06d}.png".format(nFrameNo), bbox_inches='tight')
plt.close()