我有这个程序试图将它周围的网格空间的数量相加为某个值,并且它不断给出错误“IndexError:list out of range”。我尝试将其设置为晚开始列和行,并提前结束一列和行以达到相同的效果。错误点特别指向 [x+1][y+1]。
for l in range(loops):
for x in range(self.width):
for y in range(self.height):
neighbors = 0
if tiles_copy[x-1][y-1] == 1:
neighbors += 1
if tiles_copy[x][y-1] == 1:
neighbors += 1
if tiles_copy[x+1][y-1] == 1:
neighbors += 1
if tiles_copy[x+1][y] == 1:
neighbors += 1
if tiles_copy[x+1][y+1] == 1:
neighbors += 1
if tiles_copy[x][y+1] == 1:
neighbors += 1
if tiles_copy[x-1][y+1] == 1:
neighbors += 1
if tiles_copy[x-1][y] == 1:
neighbors += 1