【发布时间】:2016-11-02 05:45:48
【问题描述】:
我正在使用 Python 执行标准的康威生命游戏程序。我在遍历数组时尝试计算邻居时遇到问题。我创建了打印语句来打印 if 语句的连续性,以及每个语句的计数值。
这是我的代码:(我在整个代码的 # 中都有问题)
import random
numrows = 10
numcols = 10
def rnd():
rn = random.randint(0,1)
return rn
def initial():
grid = []
count = 0
for x in range(numrows):
grid.append([])
for y in range(numcols):
rand=random.randrange(1,3)
if(rand == 1):
grid[x].append('O')
else:
grid[x].append('-')
for x in grid:
print(*x, sep=' ',end="\n") #this prints the random 2d array
print("")
print("")
answer = 'y'
newgrid = []
count = 0
while(answer == 'y'): # I believe I am going through, checking neighbors
# and moving onto the next index inside these for
#loops below
for r in range(0,numrows):
grid.append([])
for c in range(0,numcols):
if(r-1 > -1 and c-1 > -1): #I use this to check out of bound
if(newgrid[r-1][c-1] == 'O'):#if top left location is O
count = count + 1 #should get count += 1
else:
count = count
print("top left check complete")
print(count)
if(r-1 > -1):
if(newgrid[r-1][c] == 'O'):
count = count + 1
else:
count = count
print("top mid check complete")
print(count)
if(r-1 > -1 and c+1 < numcols):
if(newgrid[r-1][c+1] == 'O'):
count = count + 1
else:
count = count
print("top right check complete")
print(count)
if(c-1 > -1 and r-1 > -1):
if(newgrid[r][c-1] == 'O'):
count = count + 1
else:
count = count
print("mid left check complete")
print(count)
if(r-1 > -1 and c+1 < numcols):
if(newgrid[r][c+1] == 'O'):
count = count + 1
else:
count = count
print("mid right check complete")
print(count)
if(r+1 < numrows and c-1 > -1):
if(newgrid[r+1][c-1] == 'O'):
count = count + 1
else:
count = count
print("bot left check complete")
print(count)
if(r+1 < numrows and c-1 > -1):
if(newgrid[r+1][c] == 'O'):
count = count + 1
else:
count = count
print("bot mid check complete")
print(count)
if(r+1 < numrows and c+1 < numcols):
if(newgrid[r+1][c+1] == 'O'):
count = count + 1
else:
count = count
print("bot right check complete")
print(count)
# I am not sure about the formatting of the code below, how do I know that
# the newgrid[r][c] location is changing? should it be according to the for-
# loop above? Or should it get it's own? If so, how could I construct it as
# to not interfere with the other loops and items of them?
if(newgrid[r][c] == '-' and count == 3):
newgrid[r][c] ='O'
elif(newgrid[r][c] == 'O' and count < 2):
newgrid[r][c] = '-'
elif(newgrid[r][c] == 'O' and (count == 2 or count == 3)):
newgrid[r][c] = 'O'
elif(newgrid[r][c] == 'O' and count > 3):
newgrid[r][c] = '-'
# I'm also confused how to go about printing out the 'new' grid after each
# element has been evaluated and changed. I do however know that after the
# new grid prints, that I need to assign it to the old grid, so that it can
# be the 'new' default grid. How do I do this?
for z in newgrid:
print(*z, sep=' ',end="\n")
answer = input("Continue? y or n( lower case only): ")
newgrid = grid
if(answer != 'y'):
print(" Hope you had a great life! Goodbye!")
initial()
这是当前的输出和错误信息:
>>> initial() - O - - O - - O - - - O - - O - - - O O - O - - O - O O - O O - - O - - O O O O O - O O - - - O O - O - O - O - O - O - O - O O O O - - O - - - - - O O O - - O O O - O - - O - - - - - O O O - O - - - top left check complete 0 top mid check complete 0 top right check complete 0 mid left check complete 0 mid right check complete 0 bot left check complete 0 bot mid check complete 0 Traceback (most recent call last): File "<pyshell#68>", line 1, in <module> initial() File "C:\Users\Ted\Desktop\combined.py", line 86, in initial if(newgrid[r+1][c+1] == 'O'): IndexError: list index out of range
当我遍历随机数组以查看邻居是什么时,它似乎没问题,直到它移动到 [0][1] 同时检查机器人右邻居。
另外,中间的右边邻居应该 +1 才能算作是活着的。但是,即使 if 语句连续,count 仍然是 0?
问题 1:我怎么可能知道我的 if 条件足以满足数组所有边的 [r][c] 的每个实例?
问题 2:我目前的检查越界方法是否最适合我的情况?有没有办法在我检查值之前“检查所有是否越界”?
此时我已经束手无策了。提前感谢您花时间帮助回答我的问题
【问题讨论】:
-
一个建议是使用调试器并单步执行代码。
-
看起来错误发生位置附近的很多代码都有
grid和newgrid切换。newgrid是空的,但是您正在向grid添加行,并且您正在newgrid中查找内容(导致异常)。 -
@Blckknght 好点!我原本打算在回答中提到这一点,但我被其他问题分心了。我现在已经添加了该信息,所以谢谢你提醒我。
标签: python arrays conways-game-of-life