Created
October 22, 2018 02:39
-
-
Save ti-nspire/838c5e6be8c7f3079496fbe8b1b24c08 to your computer and use it in GitHub Desktop.
a Conway's Game of Life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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