Python - Conways生命游戏

时间:2015-11-17 20:26:57

标签: python conways-game-of-life

似乎我的过程不适用于下一次迭代。我在主要版本中有很深的副本,我认为问题在于它们并且在它们被调用时。

间距有点偏,我道歉。

问题出在我的结果中,它使用活细胞打印出第一次迭代。然后,下一次迭代是空白的,并且单元格中没有变化。

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()

2 个答案:

答案 0 :(得分:1)

您的代码正在崩溃,因为您正在printBoard()的列表末尾之外编制索引。这可以通过更改范围来修复:

def printBoard(cols, rows, boardList):
    for i in range(rows):
        for j in range(cols):
        ...

通过清理缩进可能会修复其他错误,它似乎至少会在每次迭代时应用这些更改。

您可能应该对rows, cols参数传递的顺序保持一致,以便以后节省您的头痛。

getNeighbours()中的Conway算法看起来不正确。活细胞应该死亡,除非他们有2或3个邻居,并且有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 0

答案 1 :(得分:1)

由于您似乎在应用其他答案的多个更改时遇到问题,因此这是我测试过的简化版本,并且在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()

我也提高了打印代码的性能,大板的速度非常慢。

我通过firing a Glider at a Blinker on a 20x20 board进行了测试,似乎工作正常。