2016-09-02 30 views
0

我試圖做一個基於列表的實施康威的生活遊戲,而不使用python 3.5.2中的任何附件或列表列表。我現在遇到的問題是,每個點所具有的「鄰居」數量都不正確。我在下面的代碼中留下了我的打印聲明。在這個例子中,佔用的單元用「○」表示,而未佔用的單元用「。」表示。康威的生命遊戲規則在python中無法正常運行

#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

如果'index'位於行的開始或結尾處,'getNeighbors'會發生什麼? – jwodder

+1

你可以創建[mcve]嗎?如果'getNeighbors'是您所問的代碼,請儘可能多地刪除其餘的代碼。 –

+0

@jwoddler看起來問題在於我的代碼不知道行的開始和結束之間的區別。我試圖測試你的建議,並且鄰居值對於應該有鄰居的空間是正確的,但是任何位於遠端的空間也都註冊爲顯示的同一端。 – dff

回答

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[:]