Skip to main content
 首页 » 编程设计

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

2024年10月25日10cyq1162

所以我正在用 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,然后立即丢弃该值,并且不会发生任何状态变化。