Conway的Python生活游戏问题

时间:2020-09-16 14:32:30

标签: python conways-game-of-life

我一直在尝试用python创建生命游戏,但是无论如何编写规则,我都在努力获得正确的结果,所以我不太确定我要去哪里哪里。 / p>

我尝试编写用于遍历单元格的规则,编写用于使用多个列表或使用类列表的基本代码,但是每次结果出错时,经过5次左右的迭代,仿真就有点高原。

这是我的源代码:

> array([[  0, 750],
>        [  0, 320]])

1 个答案:

答案 0 :(得分:0)

这里有一个问题,并且进行了类似的检查:

if Cell.state == 1 & Cell.neighbours <= 1

&按位 AND运算符,而不是逻辑 AND。看operator precedence rules,它将被解析为:

if Cell.state == (1 & Cell.neighbours) <= 1

由于comparison chaining而扩展为:

if Cell.state == (1 & Cell.neighbours) and (1 & Cell.neighbours) <= 1

相反,您需要使用and关键字:

if Cell.state == 1 and Cell.neighbours <= 1