Created
May 26, 2019 14:38
-
-
Save JosephRedfern/6d995be2a4edeba91a19aba770c50300 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
from matplotlib import pyplot as plt | |
from tqdm import tqdm | |
board = np.zeros((255, 255)) | |
board[127][127] = 1 | |
board[127][128] = 1 | |
board[128][127] = 1 | |
board[128][128] = 1 | |
def next_state(board): | |
if np.sum(board) == 0: | |
print("IZ DED") | |
raise Exception("Iz ded :( ") | |
new = np.zeros(board.shape) | |
for i in range(board.shape[0]): | |
for j in range(board.shape[1]): | |
tot = 0 | |
if i > 0: | |
tot += board[i-1][j] | |
if j > 0: | |
tot += board[i-1][j-1] | |
if j < board.shape[1] - 1: | |
tot += board[i-1][j+1] | |
if i < board.shape[0] - 1: | |
tot += board[i+1][j] | |
if j > 0: | |
tot += board[i+1][j-1] | |
if j < board.shape[1] - 1: | |
tot += board[i+1][j+1] | |
if j > 0: | |
tot += board[i][j-1] | |
if j < board.shape[1] - 1: | |
tot += board[i][j+1] | |
if tot > 0 and tot < 6: | |
new[i][j] = board[i][j] + 1 | |
return new | |
i = 0 | |
while True: | |
print(f"Iteration: {i}, living: {np.sum(board)}") | |
plt.clf() | |
plt.imshow(board) | |
plt.pause(0.01) | |
board = next_state(board) | |
i += 1 | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment