Jun-14-2019, 09:06 AM
Using above neighbor calculation a simple Game of Life
can be done like this:
can be done like this:
import numpy as np from scipy.signal import convolve2d import os import time SIZE = 30, 30 def show(table): star = table * ord('*') blank = (table != 1) * ord(' ') field = star + blank for row in field: print(''.join(chr(char) for char in row)) kernel = np.array([[1,1,1],[1,0,1],[1,1,1]]).astype(np.uint8) table = np.random.randint(0, 2, size=SIZE).astype(np.uint8) os.system('cls') print(table) for i in range(100): next_table = table.copy() neighbors = convolve2d(table, kernel, 'same') next_table[neighbors < 2] = 0 next_table[neighbors > 3] = 0 next_table[(neighbors == 3) & (table == 0)] = 1 table = next_table os.system('cls') show(table) time.sleep(.050)