You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.7 KiB
114 lines
3.7 KiB
#!/usr/bin/env python
|
|
|
|
import random
|
|
import copy
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
from anime import PltAnimation
|
|
from anime import PlotlyAnimation
|
|
|
|
|
|
class State(object):
|
|
def __init__(self, depth: int, width: int, coords=None):
|
|
# Coords: tuple of ints or list of tuple of ints
|
|
self.width = width
|
|
self.depth = depth
|
|
self.board = np.array([[0 for i in range(width)] for j in range(depth)])
|
|
if coords is None:
|
|
pass
|
|
elif isinstance(coords, tuple):
|
|
x, y = coords
|
|
self.board[x, y] = 1
|
|
elif isinstance(coords, list):
|
|
for pair in coords:
|
|
x, y = pair
|
|
self.board[x, y] = 1
|
|
elif coords == "random":
|
|
self.randomBoard()
|
|
else:
|
|
raise Exception(
|
|
"No proper coords provided. Try a tuple of ints or a list of tuple of ints"
|
|
)
|
|
|
|
def randomBoard(self):
|
|
for idx, i in enumerate(self.board):
|
|
for jdx, j in enumerate(i):
|
|
if random.randint(1, 2) == 1:
|
|
self.board[idx, jdx] = 1
|
|
else:
|
|
self.board[idx, jdx] = 0
|
|
|
|
def terminal_render(self):
|
|
print("-" * (self.width + 2))
|
|
for i in self.board:
|
|
print(
|
|
"|"
|
|
+ "".join(str(k) for k in ([" " if j == 0 else "*" for j in i]))
|
|
+ "|"
|
|
)
|
|
print("-" * (self.width + 2))
|
|
|
|
def plt_render(self):
|
|
plt.imshow(self.board)
|
|
plt.show()
|
|
|
|
def step(self):
|
|
boardcopy = copy.deepcopy(self.board)
|
|
for rowidx, row in enumerate(self.board):
|
|
for elemidx, elem in enumerate(row):
|
|
live_neighbours = self.checkNeighbours(boardcopy, rowidx, elemidx)
|
|
if self.board[rowidx, elemidx] == 1:
|
|
if live_neighbours == 2 or live_neighbours == 3:
|
|
pass
|
|
else:
|
|
self.board[rowidx, elemidx] = 0
|
|
else:
|
|
if live_neighbours == 3:
|
|
self.board[rowidx, elemidx] = 1
|
|
return self.board
|
|
|
|
def checkNeighbours(self, board, rowindex, columnindex, neighbourhood="moore"):
|
|
alive = 0
|
|
if rowindex != 0:
|
|
if board[rowindex - 1, columnindex] == 1:
|
|
alive += 1
|
|
if rowindex < self.depth - 1:
|
|
if board[rowindex + 1, columnindex] == 1:
|
|
alive += 1
|
|
if columnindex != 0:
|
|
if board[rowindex, columnindex - 1] == 1:
|
|
alive += 1
|
|
if columnindex < self.width - 1:
|
|
if board[rowindex, columnindex + 1] == 1:
|
|
alive += 1
|
|
if neighbourhood == "moore":
|
|
if rowindex != 0 and columnindex != 0:
|
|
if board[rowindex - 1, columnindex - 1] == 1:
|
|
alive += 1
|
|
if rowindex < self.depth - 1 and columnindex < self.width - 1:
|
|
if board[rowindex + 1, columnindex + 1] == 1:
|
|
alive += 1
|
|
if rowindex != 0 and columnindex < self.width - 1:
|
|
if board[rowindex - 1, columnindex + 1] == 1:
|
|
alive += 1
|
|
if rowindex < self.depth - 1 and columnindex != 0:
|
|
if board[rowindex + 1, columnindex - 1] == 1:
|
|
alive += 1
|
|
return alive
|
|
|
|
|
|
|
|
|
|
def main():
|
|
# state = State(20, 20, [(0, 1), (1, 2), (2, 0), (2, 1), (2, 2)])
|
|
|
|
state = State(100, 100, "random")
|
|
#animation = PltAnimation(state)
|
|
#ani = animation.animate()
|
|
#plt.show()
|
|
animation = PlotlyAnimation(state, 100)
|
|
animation.show()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|