使用蟒蛇计算康威生命游戏中的邻居



大家好,作为一个初学者,我正在尝试做康威的生活游戏,但我一直得到IndexError:列表索引超出范围,我不知道为什么会发生这种情况,你们能帮忙吗?非常感谢。

def count_neighbors(x, y, G):
count= 0
n=len(G)
if x>0:
if y>0:
if G[x-1][y-1]=='x':
count +=1
if y<n-1:
if G[x-1][y+1]=='x':
count +=1
if G[x-1][y]=='x':
count +=1
if y>0:
if G[x][y-1]=='x':
count +=1
if y<n-1:
if G[x][y+1] =='x':
count +=1
if x<n-1:
if y>0:
if G[x+1][y-1] =='x':
count +=1
if y<n-1:
if G[x+1][y+1] =='x':
count +=1
if G[x+1][y]=='x':
count +=1
return count

您没有检查x<n-1,因此G[x+1]可能越界:

if y<n-1:
if G[x+1][y+1] =='x':
count +=1
if G[x+1][y]=='x':
count +=1

经过进一步的检查,您似乎应该只缩进这个块一次,这样它就属于您上面写的if x<n-1:检查。

您的代码还假设G是一个正方形矩阵(高度==宽度(;如果不是这样,那么您也会遇到问题。

一个更通用的答案是使用循环:

def count(x,y,G):
counts = 0
for dy in (-1,0,1):
for dx in (-1,0,1):
if not (dx and dy) and x+dx in range(n) and y+dy in range(n) and G[y+dy][x+dx] == 'x':
counts += 1
return counts

我使用的另一个技巧是列出可能的移动:

dirs = (
(-1,-1), ( 0,-1), ( 1,-1),
(-1, 0),          ( 1, 0),
(-1, 1), ( 0, 1), ( 1, 1)
)
def count(x,y,G):
counts = 0
for dx,dy in dirs:
if x+dx in range(n) and y+dy in range(n) and G[y+dy][x+dx] == 'x':
counts += 1
return counts

更新:我刚刚意识到我对4个邻居有错误的规则!正在修复。。。

更新#2:修复了2条规则。

我用多种语言(包括assebly语言(编写了许多程序来玩康威的人生游戏。这些程序都不简单也不短!

这是Conway生活游戏的一个纯numpy解决方案,它不仅在几行代码中计算所有邻居计数,而且在总共只有9个numpy语句的情况下计算下一代。

import numpy as np
# A 5x5 numpy array represents a 3x3 "world" where the outer edge cells
# will always remain equal to 0.
G = np.array([[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0]])
print('G ---------- Original generation')
print(G)
# Create array S (sum) where each element is the sum of the 3x3 cell surrounding
# the same element in the G array.
S = np.zeros_like(G)
S[1:-1, 1:-1] = sum(G[i:G.shape[0]-2+i, j:G.shape[1]-2+j]
for i in range(0, 3) for j in range(0, 3))
print('S ---------- Provisional neighbor counts')
print(S)
# Since the sums in S include the center cell we subtract the extra values by
# subtracting G from S!
X = np.subtract(S, G)
print('X ---------- Adjusted neighbor counts')
print(X)
# Apply Conway's rules of life
X[((X == 1) | (X > 3)) & (G == 1)] = -1  # Death
X[((X == 1) | (X > 3)) & (G == 0)] = 0 # No change if G is already equal to 0
X[X == 2] = 0  # Survival
X[(X == 3) & (G == 1)] = 0  # No change if G is already equal to 1
X[(X == 3) & (G == 0)] = +1 # Birth otherwise!
print('X ---------- Changes for the next generation')
print(X)
# Apply the calculated changes from X to G
G = np.add(G, X)
print('G ---------- Tada!!! The next generation')
print(G)

输出:

G ---------- Original generation
[[0 0 0 0 0]
[0 1 1 1 0]
[0 0 1 0 0]
[0 1 0 1 0]
[0 0 0 0 0]]
S ---------- Provisional neighbor counts
[[0 0 0 0 0]
[0 3 4 3 0]
[0 4 6 4 0]
[0 2 3 2 0]
[0 0 0 0 0]]
X ---------- Adjusted neighbor counts
[[0 0 0 0 0]
[0 2 3 2 0]
[0 4 5 4 0]
[0 1 3 1 0]
[0 0 0 0 0]]
X ---------- Changes for the next generation
[[ 0  0  0  0  0]
[ 0  0  0  0  0]
[ 0  0 -1  0  0]
[ 0 -1  1 -1  0]
[ 0  0  0  0  0]]
G ---------- Tada!!! The next generation
[[0 0 0 0 0]
[0 1 1 1 0]
[0 0 0 0 0]
[0 0 1 0 0]
[0 0 0 0 0]]

最新更新