Skip to content

Instantly share code, notes, and snippets.

@omaraflak
Last active June 2, 2021 12:37
Show Gist options
  • Save omaraflak/6cee2468c2f6f593b4b7a832219eb9ab to your computer and use it in GitHub Desktop.
Save omaraflak/6cee2468c2f6f593b4b7a832219eb9ab to your computer and use it in GitHub Desktop.
Game Of Life - Python Efficient Implementation
import numpy as np
from scipy import signal
from matplotlib import animation, pyplot as plt
def animate(i):
global board
neighbors = signal.correlate2d(board, kernel, mode="same")
board = ((board == 1) & ((neighbors == 2) | (neighbors == 3))) | ((board == 0) & (neighbors == 3))
image.set_data(board)
return image
kernel = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
board = np.random.randint(0, 2, size=(150, 150), dtype=np.uint8)
fig = plt.figure()
image = plt.imshow(board, cmap="gray")
anim = animation.FuncAnimation(fig, animate, init_func=None, interval=50)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment