gpt4 book ai didi

python - 在康威的生活游戏中计算活邻居不工作

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:59 25 4
gpt4 key购买 nike

所以我正在用 Python 创建康威的人生游戏。规则是

任何少于两个活邻居的活细胞都会死亡任何有两个或三个活邻居的活细胞都会存活到下一代。任何拥有三个以上活邻居的活细胞都会死亡。恰好有三个活邻居的任何死细胞都会成为活细胞。

我不明白为什么首先,我的 liveNeighbors 计数器有时会打印错误,而且面板永远不会改变。

def nextIteration(board):    
newBoard =[]
for i in board:
tempList = []
for j in i:
tempList.append(j)
newBoard.append(tempList)
print(newBoard)
for row in range(len(newBoard)):
for column in range(len(newBoard[row])):
liveNeighbors = 0
if (row - 1 >= 0) and (column - 1 >= 0):
if newBoard[row - 1][column - 1] == "X":
liveNeighbors += 1
if (row - 1 >= 0):
if newBoard[row - 1][column] == "X":
liveNeighbors += 1
if (row - 1 >= 0) and (column + 1 < len(newBoard[row])):
if newBoard[row - 1][column + 1] == "X":
liveNeighbors += 1
if (column - 1 >= 0):
if newBoard[row][column - 1] == "X":
liveNeighbors += 1
if (column + 1) < len(newBoard[row]):
if newBoard[row][column + 1] == "X":
liveNeighbors += 1
if (row + 1) < len(newBoard[row]):
if newBoard[row + 1][column - 1] == "X":
liveNeighbors += 1
if (row + 1 < len(newBoard[row])):
if newBoard[row + 1][column] == "X":
liveNeighbors += 1
if (row + 1 < len(newBoard[row]) and column + 1 < len(newBoard[row])):
if newBoard[row + 1][column + 1] == "X":
liveNeighbors += 1
if newBoard[row][column] == "X":
if liveNeighbors < 2 or liveNeighbors > 3:
board[row][column] == "0"
if newBoard[row][column] == "0":
if liveNeighbors == "3":
board[row][column] == "X"
print(liveNeighbors, end="")
print()
return board


def printBoard(board):
for row in board:
for item in row:
print(item, end="")
print()


def main():
rows = input("Please enter number of rows: ")
columns = input("Please enter number of columns: ")
print()
cellRow = 0
cellRows = []
cellColumns = []
total = 0
#the cellRow and cellColumn will contain all of the inputted rows
#and columns connected by the index value
while cellRow != "q":
cellRow = input("Please enter the row of a cell to turn on (or q to exit): ")
if cellRow != "q":
cellColumn = input("Please enter a column for that cell: ")
cellRows.append(cellRow)
cellColumns.append(cellColumn)
total = total + 1
print()
else:
print()
board = []
#boardTemp will hold a list that contains one row of the entire board
boardTemp = []
for i in range(int(rows)):
boardTemp.append("0")
for i in range(int(columns)):
board.append(boardTemp[:])
for i in range(total):
temp = i
board[int(cellRows[temp - 1])][int(cellColumns[temp - 1])] = "X"
iterations = input("How many iterations should I run? ")
print()
print("Starting Board:")
print()
printBoard(board)
iterationNumber = 1
for i in range(int(iterations)):
board = nextIteration(board)
print("Iteration", iterationNumber,":")
printBoard(board)
iterationNumber += 1
main()

打印:

Please enter number of rows: 5
Please enter number of columns: 5

Please enter the row of a cell to turn on (or q to exit): 3
Please enter a column for that cell: 3

Please enter the row of a cell to turn on (or q to exit): 2
Please enter a column for that cell: 3

Please enter the row of a cell to turn on (or q to exit): 4
Please enter a column for that cell: 3

Please enter the row of a cell to turn on (or q to exit): q

How many iterations should I run? 1

Starting Board:

00000
00000
000X0
000X0
000X0
[['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', 'X', '0'], ['0', '0', '0', 'X', '0'], ['0', '0', '0', 'X', '0']]
0
0
0
0
0
0
0
1
1
1
0
0
2
1
2
0
0
3
2
3
0
0
2
1
2
Iteration 1 :
00000
00000
000X0
000X0

最佳答案

您在 nextIteration 函数的这一部分检查相等性而不是赋值:

        if newBoard[row][column] == "X":
if liveNeighbors < 2 or liveNeighbors > 3:
board[row][column] == "0" # Here
if newBoard[row][column] == "0":
if liveNeighbors == "3":
board[row][column] == "X" # And here

board[row][column] == "0" 应该是 board[row][column] = "0" 等。目前该行计算为 True,然后立即丢弃该值,并且不会发生任何状态变化。

关于python - 在康威的生活游戏中计算活邻居不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29527161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com