Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.