这是我的程序,我不明白为什么这个错误出现在第41行:"索引60超出了大小60的轴0和#34;它的游戏是生活和我试图自己做,因为我在python中的新事物很难理解互联网上的那些......
import numpy as np
rows=60
cols=60
size=rows*cols
nb_steps=300
array=np.random.randint(0,high=2, size=size,dtype=int).reshape(rows,cols)
next_state=np.zeros((rows,cols),dtype=int)
alive=1
dead=0
grid=np.int(size)
def nb_neighbors(grid):
neighbors=np.zeros((rows,cols),dtype=int)
for x in range (rows):
for y in range (cols):
if x==0 and y==0:
neighbors[x][y]=grid[x-1][y]+grid[x-1][y+1]+grid[x][y+1]
if x==0 and y==59:
neighbors[x][y]=grid[x+1][y]+grid[x+1][y-1]+grid[x][y-1]
if x==59 and y==0:
neighbors[x][y]=grid[x][y+1]+grid[x+1][y+1]+grid[x+1][y]
if x==59 and y==59:
neighbors[x][y]=grid[x][y-1]+grid[x-1][y-1]+grid[x-1][y]
if y-1==0:
neighbors[x][y]=grid[x-1][y]+grid[x+1][y]+grid[x-1][y+1]+grid[x][y+1]+grid[x+1][y+1]
if y+1==0:
neighbors[x][y]=grid[x-1][y-1]+grid[x][y-1]+grid[x+1][y-1]+grid[x-1][y]+grid[x+1][y]
if x-1==0:
neighbors[x][y]=grid[x][y-1]+grid[x+1][y-1]+grid[x+1][y]+grid[x][y+1]+grid[x+1][y+1]
if x+1==0:
neighbors[x][y]=grid[x-1][y-1]+grid[x][y-1]+grid[x-1][y]+grid[x-1][y+1]+grid[x][y+1]
else :
neighbors[x][y]=grid[x-1][y-1]+grid[x][y-1]+grid[x+1][y-1]+grid[x-1][y]+grid[x+1][y]+grid[x-1][y+1]+grid[x][y+1]+grid[x+1][y+1]
return neighbors
def next_state(old):
next_state=np.zeros((rows,cols),dtype=int)
neighbors=nb_neighbors(old)
for x in range (rows):
for y in range(cols):
if old[x][y] == alive and (neighbors[x][y] == 2 or neighbors[x][y] == 3):
next_state[x][y] = alive
if old[x][y] == dead and neighbors[x][y] == 3:
next_state[x][y] = alive
else :
next_state[x][y]= dead
return next
following=next_state(array)
for i in range (nb_steps):
following=next_state(following)
print (following)