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.
106 lines
3.4 KiB
106 lines
3.4 KiB
import random
|
|
import copy
|
|
import time
|
|
import os
|
|
|
|
os.system("cls")
|
|
|
|
|
|
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 = [[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 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 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
|
|
|
|
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, 50, "random")
|
|
state.render()
|
|
for i in range(100):
|
|
time.sleep(0.2)
|
|
os.system("clear")
|
|
state.step()
|
|
state.render()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|