Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
Created October 22, 2018 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ti-nspire/838c5e6be8c7f3079496fbe8b1b24c08 to your computer and use it in GitHub Desktop.
Save ti-nspire/838c5e6be8c7f3079496fbe8b1b24c08 to your computer and use it in GitHub Desktop.
a Conway's Game of Life
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import convolve2d
from matplotlib.animation import FuncAnimation
class LifeGame: # 引数は左から(2次元排列, (表示領域の幅, 高さ), 更新ミリ秒)
def __init__(self, mat, figsize=None, interval=1):
self.mat = np.asarray(mat, dtype=np.uint8)
self.Rules = np.array([[0,0,0,1,0,0,0,0,0], [0,0,1,1,0,0,0,0,0]], dtype=np.uint8)
self.Weight = np.array([[1,1,1], [1,0,1], [1,1,1]], dtype=np.uint8)
self.anim = FuncAnimation(plt.figure(figsize=figsize), self.__update, interval=interval)
plt.show()
def __update(self, i):
plt.clf()
plt.imshow(self.mat, cmap="binary")
self.mat = self.Rules[self.mat, convolve2d(self.mat, self.Weight, mode="same", boundary="wrap")]
if __name__ == "__main__": LifeGame(np.loadtxt("life_demo.csv", delimiter=",", dtype=np.uint8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment