所以我正在用python创建conway的生活游戏。规则是

少于两个活邻居的任何活细胞死亡
任何有两个或三个活邻居的活细胞都可以存活到下一代。
任何具有三个以上活邻居的活细胞都会死亡。
任何具有恰好三个活邻居的死小区都将变为活小区。

我不知道为什么,首先,我的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 - 在Conway的生活游戏中计算活着的邻居不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29527161/

10-12 20:57