在康威的生活游戏中计算活着的邻居是不行的

时间:2015-04-08 23:34:32

标签: python

所以我在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

1 个答案:

答案 0 :(得分:2)

您正在检查相等性,而不是在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,然后该值立即被丢弃,并且不会发生状态更改。