) #change pattern here.
import numpy as np
import pygame,time,sys
from pygame.locals import *
##+++==sample pattern =====
S1 = [[0,0,0,0,0,0],
[0,0,0,1,0,0],
[0,1,0,1,0,0],
[0,0,1,1,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0]]
glider_gun =[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
diehard = [[0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 1, 1]]
glider = [[1, 0, 0],
[0, 1, 1],
[1, 1, 0]]
r_pentomino = [[0, 1, 1],
[1, 1, 0],
[0, 1, 0]]
beacon = [[0, 0, 1, 1],
[0, 0, 1, 1],
[1, 1, 0, 0],
[1, 1, 0, 0]]
acorn = [[0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[1, 1, 0, 0, 1, 1, 1]]
spaceship = [[0, 0, 1, 1, 0],
[1, 1, 0, 1, 1],
[1, 1, 1, 1, 0],
[0, 1, 1, 0, 0]]
block_switch_engine = [[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0]]
Pulsar= [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0],
[0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0],
[0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0],
[0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
##---==sample pattern =====
def compute_neighbours(Z):
rows,cols = len(Z), len(Z[0])
N = [[0,]*(cols) for i in range(rows)]
for x in range(1,cols-1):
for y in range(1,rows-1):
N[y][x] = Z[y-1][x-1]+Z[y][x-1]+Z[y+1][x-1] \
+ Z[y-1][x] +Z[y+1][x] \
+ Z[y-1][x+1]+Z[y][x+1]+Z[y+1][x+1]
return N
def show(Z):
for l in Z[1:-1]:
print (l[1:-1])
def iterate(Z):
rows,cols = len(Z), len(Z[0])
N = compute_neighbours(Z)
for x in range(1,cols-1):
for y in range(1,rows-1):
if Z[y][x] == 1 and (N[y][x] < 2 or N[y][x] > 3):
Z[y][x] = 0
elif Z[y][x] == 0 and N[y][x] == 3:
Z[y][x] = 1
return Z
##test for S1 pattern
for i in range(10):
print("iterate=",i)
iterate(S1)
show(S1)
Randomflag=True
W=100
H=100
##Select pattern sample===========
if Randomflag==True:
##==test for Random pattern ==
Z=np.zeros((W,H))
S = np.random.randint(0,2,(W,H))
pattern=np.array(S) #random pattern sample
Z[:pattern.shape[0], :pattern.shape[1]] = pattern
else:
##==test sample pattern =====
Z=np.zeros((W,H))
pattern=np.array(Pulsar) #change pattern here.
Z[30:pattern.shape[0]+30, 30:pattern.shape[1]+30] = pattern
pygame.init()
screen = pygame.display.set_mode((W*6,H*6))
pygame.display.set_caption("Game of Life")
screen.fill((255,255,255))
black = 0,0,0
white=255,255,255
radius = 3
width = 1
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
iterate(Z)
for j in range(W):
for k in range(H):
position = j*6,k*6
if Z[j][k]==1:
pygame.draw.circle(screen, black, position, radius, width)
else:
pygame.draw.circle(screen, white, position, radius, width)
pygame.display.update()
time.sleep(0.2)