康威的生命游戏规则在python中无法正常运行

时间:2016-09-02 02:32:30

标签: python list conways-game-of-life

我试图在不使用python 3.5.2中的任何附加组件或列表列表的情况下进行基于列表的Conway生命游戏的实现。我现在遇到的问题是每个点的“邻居”数量是不正确的。我在下面的代码中留下了我的print语句。在这个例子中,被占用的单元用“o”表示,而未被占用的单元用“。”表示。

#Determine how many neighbors are surrounding the provided point    
def getNeighbors(board, index, columns):
    neighbors = 0
    try:
        if(board[index + 1] == "o"):
            neighbors += 1
    except:
        pass
    try:
        if(board[index - 1] == "o"):
            neighbors += 1
    except:
        pass
    try:
        if(board[index + columns] == "o"):
            neighbors += 1
    except:
        pass
    try:
        if(board[index - columns] == "o"):
            neighbors += 1
    except:
        pass
    try:
        if(board[index - columns + 1] == "o"):
            neighbors += 1
    except:
        pass
    try:
        if(board[index - columns - 1] == "o"):
            neighbors += 1
    except:
        pass
    try:
        if(board[index + columns + 1] == "o"):
            neighbors += 1
    except:
        pass
    try:
        if(board[index + columns - 1] == "o"):
            neighbors += 1
    except:
        pass
    return neighbors

#Creates the game board in a list of lists
def mkBoard(rows,columns):
    board = ["."] * rows * columns
    return board

#Used to edit a point on the game board
def editPoint(x,y,board,columns):
    i = 0
    i = x + ((y - 1) * columns) - 1
    if( board[i] == "o"):
        board[i] = "."
    elif( board[i] == "."):
        board[i] = "o"
    return board


#Simulates the next step in the game
def nextTurn(board, columns):
    prevBoard = board
    i = 0
    for index in prevBoard:
        neighbors = 0
        if( index == 'o'):
            neighbors = getNeighbors(prevBoard, i, columns)
            print(neighbors)
            if(neighbors == 0 or neighbors == 1):
                board[i] = "."
            elif(neighbors >= 4):
                board[i] = "."
        elif(index == "."):
            neighbors = getNeighbors(prevBoard, i, columns)
            print(neighbors)
            if(neighbors == 3):
                board[i] = "o"
        i += 1
    return board

#Prints the board to the screen to show the user what is happening
def printBoard(board, columns):
    counter = 0
    for cell in board:
        print(cell,end="  ")
        counter += 1
        if( counter == columns):
            print('\n')
            counter = 0

print("======Conway's Game of Life======")

#Take user input for number of rows and columns for the board and converts them into integers
rows = input('Enter the number of rows:')
rows = int(rows)
columns = input('Enter the number of columns:')
columns = int(columns)

#Create the board and show it to the user
board = mkBoard(rows,columns)
printBoard(board,columns)

choice = 0

#If the user wants to add points to the board they can, otherwise they can begin the game
while( 1 != 3):
    choice = input('Make a choice:\n1) Change a point\n2) Continue the game\n3) Quit\n')
    if(choice =='1'):
        x = input('Enter the x coordinate of the point to negate: ')
        x = int(x)
        y = input('Enter the y coordinate of the point to negate: ')
        y = int(y)
        board = editPoint(x,y,board, rows)
        printBoard(board,columns)
    elif(choice =='2'):
        board = nextTurn(board, columns)
        printBoard(board,columns)
    elif(choice == '3'):
        break

1 个答案:

答案 0 :(得分:0)

我发现了两个错误:

  1. board = editPoint(x,y,board, rows),您应该通过columns而不是rows
  2. nextTurn prevBoard = board中,>>> a= [0,1]没有按照您的想法行事。赋值不会创建列表的副本。两个变量都引用相同的列表对象。见这个例子:

    >>> b= a

    >>> a[0]= 9

    >>> b # output: [9, 1]

    prevBoard= board[:]

    要创建列表副本,请使用textarea

相关问题