I tried to make a Conway's Game of Life in Python. I want to test if the box isn't a border box, check the boxes that are all around, etc.
I've already tried to put this tests in comments and randomize the number of neighbors cells alive. The error disappears but another problem came and this is not the subject of this question.
newArr = arr
rows = 0
while rows < maxRows :
cols = 0
while cols < maxCols :
if cols != 0 and cols != maxCols and rows != 0 and rows != maxRows :
checks = 0
if arr[cols-1][rows-1] == '██' :
checks += 1
if arr[cols][rows-1] == '██' :
checks += 1
if arr[cols+1][rows-1] == '██' :
checks += 1
if arr[cols+1][rows] == '██' :
checks += 1
if arr[cols+1][rows+1] == '██' :
checks += 1
if arr[cols][rows+1] == '██' :
checks += 1
if arr[cols-1][rows+1] == '██' :
checks += 1
if arr[cols-1][rows] == '██':
checks += 1
if arr[rows][cols] == ' ' and checks == 3 :
newArr[rows][cols] == '██'
if arr[rows][cols] == '██' and checks > 2 and checks < 3 :
newArr[rows][cols] == '██'
else :
newArr[rows][cols] == ' '
cols += 1
rows += 1
arr = newArr
Here is the error
Traceback (most recent call last):
File "C:/Users/acer/AppData/Local/Programs/Python/Python37/Test.py", line 55, in <module>
if arr[cols+1][rows-1] == '██' :
IndexError: list index out of range
for row in range(1, maxRows - 1):
for col in range(1, maxCols - 1):
aliveNeighbours = 0
for i in range(-1, 2):
for j in range(-1, 2):
if arr[i + row][j + cols] = 'Your symbol' and (i != 0 or j != 0):
aliveNeighbours += 1
#Then check for various conditions for conways' game of life
This checks for alive neighbours around a cell.
We don't need the rows and columns at the edges.
>>> for i in range(-1, 2):
... for j in range(-1, 2):
... if i != 0 or j != 0:
... print(i, j)
...
-1 -1
-1 0
-1 1
0 -1
0 1
1 -1
1 0
1 1
>>>
This checks for every cell except it's own which is 0, 0.
Comment if anything can be improved.