我的流程似乎不适用于下一次迭代。我有深刻的副本在主要,我认为问题在于他们和何时被调用。
间隔有点不对,我很抱歉。
问题是,在我的结果,它打印第一次迭代很好与活的细胞。然后,下一次迭代是空白的,并显示单元格中没有变化。
LIVING_CELL = 'A'
DEAD_CELL = '.'
#Get the alive cells from the user
def getBoard(rows, cols, boardList):
myList = [[0]*cols for i in range(rows)]
while True:
aliveRows = input("Please enter a row of a cell to turn on (or q to exit): ")
if aliveRows == 'q':
break
aliveCols = input("Please enter a column for that cell: ")
print()
myList[int(aliveRows)][int(aliveCols)] = 1
return myList
#next board cells
def getNxtIter(cols, rows, cur, nxt):
for i in range(1,rows-1):
for j in range(1,cols-1):
nxt[i][j] = getNeighbors(i, j, cur)
#Processing the neighbor cells
def getNeighbors(x, y, boardList):
nCount = 0
for j in range(y-1,y+2):
for i in range(x-1,x+2):
if not(i == x and j == y):
if boardList[i][j] != -1:
nCount += boardList[i][j]
if boardList[x][y] == 1 and nCount < 2:
return 0
if boardList[x][y] == 1 and nCount > 3:
return 1
else:
return boardList[x][y]
#Printing and forming the actual board
def printBoard(cols, rows, boardList):
for i in range(rows+2):
for j in range(cols+2):
if boardList[i][j] == -1:
print(DEAD_CELL, end=" ")
elif boardList[i][j] == 1:
print(LIVING_CELL, end=" ")
else:
print(DEAD_CELL, end=" ")
print()
def main():
#Getting and validating the number of rows and columns
x = 1
while x ==1:
rows = int(input("Please enter the number of rows: "))
if rows < 0:
x = 1
elif rows> 50:
x = 1
else:
x =0
n = 1
while n == 1:
cols = int(input("Please enter the number of columns: "))
if cols < 0:
n = 1
elif cols > 50:
n = 1
else:
n = 0
boardList = []
newBoardList = []
boardList = getBoard(rows, cols, boardList)
newBoardList = [x[:] for x in boardList]
print()
#Getting iterations to run, and validating if <= 0
a = 1
while a == 1:
iterations = int(input("How many iterations should I run? "))+1
if iterations <= 0:
a = 1
else:
a = 0
for count in range(iterations):
print()
print("Iteration:", count)
print()
printBoard(cols, rows, boardList)
getNxtIter(cols, rows, boardList, newBoardList)
boardList = [x[:] for x in newBoardList]
newBoardList = [x[:] for x in boardList]
main()发布于 2015-11-18 03:45:40
由于您似乎在应用来自另一个答案的多个更改时遇到了问题,下面是我测试过的简化和清理过的版本,这是Python 3中的一些工作:
LIVING_CELL = 'A'
DEAD_CELL = '.'
#Get the alive cells from the user
def getBoard(rows, cols):
myList = [[0]*cols for i in range(rows)]
while True:
raw = input('Please enter "row <space> column", of a cell to turn on (RETURN to exit): ')
if raw == '':
break
splitted = raw.split()
if len(splitted) != 2:
print("Invalid input")
else:
row, col = int(splitted[0]), int(splitted[1])
if row >= rows or col >= cols:
print("Invalid row/column value")
else:
myList[row][col] = 1
printBoard(rows, cols, myList)
return myList
#next board cells
def getNxtIter(rows, cols, cur, nxt):
for i in range(rows):
for j in range(cols):
nxt[i][j] = getNeighbors(rows, cols, i, j, cur)
#Processing the neighbor cells
def getNeighbors(rows, cols, x, y, boardList):
nCount = 0
for j in range(y-1,y+2):
for i in range(x-1,x+2):
if not(i == x and j == y):
itest, jtest = i, j
if itest == rows:
itest = 0
if jtest == cols:
jtest = 0
nCount += boardList[itest][jtest]
if nCount == 3 or (boardList[x][y] == 1 and nCount == 2):
return 1
return 0
#Printing and forming the actual board
def printBoard(rows, cols, boardList):
for i in range(rows):
line = []
for j in range(cols):
if boardList[i][j] == 1:
line.append(LIVING_CELL)
else:
line.append(DEAD_CELL)
print("".join(line))
def main():
#Getting and validating the number of rows and columns
while True:
rows = int(input("Please enter the number of rows: "))
if rows > 0 and rows <= 50:
break
while True:
cols = int(input("Please enter the number of columns: "))
if rows > 0 and rows <= 50:
break
boardList = getBoard(rows, cols)
newBoardList = [x[:] for x in boardList]
print()
#Getting iterations to run, and validating if <= 0
while True:
iterations = int(input("How many iterations should I run? "))+1
if iterations > 0:
break
for count in range(iterations):
print()
print("Iteration:", count)
print()
printBoard(rows, cols, boardList)
getNxtIter(rows, cols, boardList, newBoardList)
boardList, newBoardList = newBoardList, boardList
main()我也改进了打印代码的性能,它和大板一起变得非常慢。
我用在20x20板上向盲人射击滑翔机测试了它,看上去很好。
发布于 2015-11-17 22:24:15
您的代码对我来说是崩溃的,因为您是在printBoard()列表的末尾进行索引。可以通过更改范围来解决这个问题:
def printBoard(cols, rows, boardList):
for i in range(rows):
for j in range(cols):
...其他错误可能是通过清理缩进来修复的,现在似乎至少每次迭代都会应用这些更改。
您可能应该对传递rows, cols参数的顺序保持一致,以便以后省去麻烦。
在我看来,康威在getNeighbours()中的算法是不正确的。活细胞应该死亡,除非他们有两个或三个邻居,而死亡的细胞与3个邻居应该活过来。以下内容更简单,工作正常:
if nCount == 3 or (boardList[x][y] == 1 and nCount == 2):
return 1
return 0另一个问题是,由于getNxtIter()中的范围不同,您不处理沿板边缘的单元格。以下是使董事会“被包围”所需的改变:
#next board cells
def getNxtIter(cols, rows, cur, nxt):
for i in range(rows):
for j in range(cols):
nxt[i][j] = getNeighbors(i, j, cur)
#Processing the neighbor cells
def getNeighbors(x, y, boardList):
rows, cols = len(boardList), len(boardList[0])
nCount = 0
for j in range(y-1,y+2):
for i in range(x-1,x+2):
if not(i == x and j == y):
itest, jtest = i, j
if itest == rows:
itest = 0
if jtest == cols:
jtest = 0
nCount += boardList[itest][jtest]
if nCount == 3 or (boardList[x][y] == 1 and nCount == 2):
return 1
return 0https://stackoverflow.com/questions/33766340
复制相似问题