首页 > 解决方案 > 康威的生命游戏 - 邻居计数问题

问题描述

我正在尝试在 python 中运行“生活游戏”,但我似乎无法让程序的邻居计数方面正常工作。我正在尝试使用多个 if 语句来避免数组中的任何“越界”错误,这一直有效。然而,该程序正在寻找不存在的“邻居”,导致该程序在真正不应该的地方产生活细胞。任何帮助将不胜感激,我真的坚持这一点。

额外信息:列表为 10x10,单元格检查所有八个方向。

谢谢

def checkNeighbours(newWorld, row, column):
   neighboursCount = 0
   if(column > 0):
       if(newWorld[row][column-1] == ALIVE): #checks cell to left
           neighboursCount = neighboursCount + 1
   if(row < 9):
       if(newWorld[row+1][column] == ALIVE): #checks cell below
           neighboursCount = neighboursCount + 1
   if(row < 9 and column < 9):
       if(newWorld[row+1][column+1] == ALIVE): #checks cell to bottom right
           neighboursCount = neighboursCount + 1
   if(row < 9 and column > 0):
       if(newWorld[row+1][column-1] == ALIVE): #checks cell to bottom left
           neighboursCount = neighboursCount + 1
   if(column < 9):
       if(newWorld[row][column+1] == ALIVE): #checks cell to right
           neighboursCount = neighboursCount + 1
   if(row > 0 and column > 0):
       if(newWorld[row-1][column-1] == ALIVE): #checks cell to top left
           neighboursCount= neighboursCount + 1
   if(row > 0):
       if(newWorld[row-1][column] == ALIVE): #checks cell above
           neighboursCount = neighboursCount + 1
   if(row != 0 and column != 9):
       if(newWorld[row-1][column+1] == ALIVE): #checks cell to top right
           neighboursCount = neighboursCount + 1
   return(neighboursCount)


def birthsAndDeaths(newWorld, row, column, neighboursCount):
   if(neighboursCount == 3 and newWorld[row][column] == DEAD):
       return(ALIVE)
   elif(neighboursCount == 2 or neighboursCount == 3 and newWorld[row][column] == ALIVE):
       return(ALIVE)
   elif(neighboursCount <= 1):
       return(DEAD)
   elif(neighboursCount >= 4):
       return(DEAD) 

def turnChanger(turn, newWorld):
   turn = turn + 1
   row = 0
   while (row < SIZE): # Each iteration accesses a single row 
       column = 0
       while (column < SIZE):  # Each iteration accesses a single column in the given row
           neighboursCount = checkNeighbours(newWorld, row, column)
           print(neighboursCount, "for cell", row, column)
           newWorld[row][column] = birthsAndDeaths(newWorld, row, column, neighboursCount)
           column = column + 1
       row = row + 1  
   return(turn, newWorld)

def start():
   choice = 0
   choice = selection()
   world = worldChooser(choice)

   oldWorld = []
   newWorld = []
   turn = 0

   oldWorld = world
   newWorld = world
   
   turn, newWorld = turnChanger(turn, newWorld)
   display(turn, oldWorld, newWorld)   
   
   world = newWorld
start()

标签: python

解决方案


问题出在birthsAndDeaths函数中,在这一行:

elif(neighboursCount == 2 or neighboursCount == 3 and newWorld[row][column] == ALIVE):

由于操作顺序,您必须确保or首先评估 ,因此请在该部分周围使用括号。

elif((neighboursCount == 2 or neighboursCount == 3) and newWorld[row][column] == ALIVE):

任何and运算符都在 之前进行评估or,因此在您的代码中,只要任何单元格旁边有 2 个邻居,它就会最终将单元格设置为活动的。

使用您当前的代码,假设我们在一个死单元旁边有 2 个邻居,它会去

elif(neighboursCount == 2 or neighboursCount == 3 and newWorld[row][column] == ALIVE):

然后

elif(true or false and false):

然后

elif(true or false):

然后

elif(true):

并且主体elif返回一个ALIVE结果。

在括号周围使用括号or会首先评估这些括号的内容。在此内容是一项or操作。使用括号意味着我们将得到

elif((neighboursCount == 2 or neighboursCount == 3) and newWorld[row][column] == ALIVE):

然后

elif((true or false) and false):

然后

elif(true and false):

然后

elif(false):

这是您在此处询问的特定问题的解决方案。然而,Carcigenicate 指出了另一个主要问题。如果您尝试解决该问题但仍然遇到问题,您可以发布一个新问题。


推荐阅读