- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是康威用 Python 制作的生命游戏模拟的代码片段。出于检查它是否有效的目的,它非常简单 - 但它没有。无法弄清楚为什么,但据我所知,它与更新有关。
感谢任何关于它为什么更新它的方式的输入:
pygame 中的 Gif(相同代码): http://imgur.com/6US3Nje未正确更新: http://imgur.com/9gubzAF
import pprint,random
#here we make the initial board, 6x6
board = []
for y in range (6):
row = []
for x in range (6):
row.append(random.randint(0,1))
board.append(row)
#and display it
pprint.pprint(board)
#this function counts the neighbours for each cell
def neighbours(x,y):
counter = 0
neighbours = \
[(x-1,y+1),(x,y+1),(x+1,y+1),\
(x-1,y), (x+1,y),\
(x-1,y-1),(x,y-1),(x+1,y-1)]
for n in neighbours:
a, b = n
try:
counter += board[a][b]
except:
pass
#printed out the counter to check if it counted correctly (it does, as far as I could tell)
if x == 4 and y == 4:
print(counter)
return counter
#function to make a new board based off the old one - basically, the updater.
#here's where the problem might lie - but for the life of me I cannot tell where and why.
def new(board):
new_board = []
for y in range(6):
new_row = []
for x in range(6):
n = neighbours(x,y)
oldcell = board[x][y]
#everything is set up to be according to the rules
#(if dead(0) can only come alive with 3 alive cells
#(if alive(1) can continue to live with exactly 2 or 3 live neighbours
if oldcell == 0:
newcell = 0
if n == 3:
newcell = 1
elif oldcell == 1:
newcell = 1
if n > 3 or n < 2:
newcell = 0
new_row.append(newcell)
new_board.append(new_row)
return new_board
#displaying the board for 6 instances
for i in range (6):
nboard = new(board)
board = nboard
pprint.pprint(board)
提前致谢!
最佳答案
您在循环中混合了行和列,有效地用每个新板转换了整个矩阵。循环应如下所示:
for x in range(6):
new_row = []
for y in range(6):
...
此外,循环中的大部分 if/else
逻辑都可以简化为一行。这是否更易于阅读和理解由您决定。
def new(board):
new_board = []
for x in range(6):
new_row = []
for y in range(6):
n = neighbours(x,y)
oldcell = board[x][y]
newcell = int(n in (2, 3)) if oldcell else int(n == 3)
new_row.append(newcell)
new_board.append(new_row)
return new_board
如前所述in comments ,还有第二个(虽然不太明显)问题:虽然您的 try/except
跳过所有场外索引是一个好主意,但它没有捕捉到例如x-1
for x
= 0
,因为[-1]
是Python中合法的索引(索引序列中的最后一个元素),使您的板“环绕”四个边缘中的两个。相反,您应该明确检查棋盘的边界。如果你愿意,你可以把它变成另一个“有趣”的单行:
def neighbours(x,y):
return sum(board[a][b] for a in (x-1,x,x+1) for b in (y-1,y,y+1)
if (a,b) != (x,y) and 0 <= a < len(board) and 0 <= b < len(board[a]))
当然,您也可以保留循环,只是将 try/except
替换为 if
检查。
关于python - (已关闭)Conway 的生命游戏无法正确更新(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332094/
我正在为一个学校项目开发康威的生命游戏。我不是直接寻找代码。我想找出我的代码有什么问题。 在康威的生命游戏中,如果一个细胞有 3 个存活的邻居,它就会从死亡状态变为存活状态。如果它有两个或三个活着的邻
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我构建了 Conways 生命游戏,但算法运行不正常。我为此混合使用了 Js 和 Jquery。我的程序所做的是逐个单元格地遍历整个棋盘,检查单元格的邻居,并通过检查它的邻居将游戏规则应用于每个单元格
这是一个有点理论性的问题。在编程作业中,约翰康威告诉我们要实现生命游戏。作为一项附加任务,我们被要求修改程序,以便它可以检测最多四代的模式重复。例如,给定这个特定的游戏“种子”,程序应该像这样: -
我目前正在使用 C++ 开发 Conway 的 Game of Life 的控制台版本。 Repo Link 问题是我的 Draw() 方法没有尽可能快地绘制下一代。 我记得在某处读到过,与获取控制台
我用 Java 编写了康威生命游戏的代码,现在我正在尝试将其转换为 C。代码非常简单,但是我在尝试创建适当的 结构时遇到了问题。 在 Java 中,如果我有实例变量: private boolean
我正在尝试制作康威生命游戏的简单版本,其中计算机生成矩形网格并填充代表“活”细胞的矩形。我遇到的问题是我无法在第一个图案之后清除网格,因此所有图案都在同一个网格上生成,它看起来像一大团彩色矩形。 这是
我的查询真的很难描述,所以我会尽量简洁地解释它。 在康威的生命游戏中,假设我有一张这样的 map : _ _ _ _ _ _ _ _ _ _ U _ R Y
我已经研究这段代码很长时间了,但我似乎无法弄清楚。我的代码的顶部部分用于预填充网格。但由于某种原因,我无法让我的形象按照预期移动和成长。请帮助我!! import java.util.Random;
所以我阅读了 Conways Game of Life 并尝试使用 PyGame 来实现它。 我试着让它面向对象。它的工作方式是我有一个细胞实例列表,然后检查它们有多少邻居,然后根据它们的邻居存活或死
这是康威用 Python 制作的生命游戏模拟的代码片段。出于检查它是否有效的目的,它非常简单 - 但它没有。无法弄清楚为什么,但据我所知,它与更新有关。 感谢任何关于它为什么更新它的方式的输入: py
我正在尝试完成 Hofstadter-Conway $10,000 sequence Scala 中的任务 Rosetta Code .我想尽可能地使用惯用的 Scala,但是我在不使用原始递归的情况
我在使用 C++ 为 Conway 的生命游戏编码时遇到了一些问题。我觉得我有一个好的开始,我只需要一些方向。首先,当我尝试运行该程序时,我总是得到一个空白的控制台。而且我也不太确定为板上没有完整八个
看上了康威的《生命游戏》,开始尝试用python写。目前我还没有为程序的边界编写任何代码,所以我只是在寻求帮助我现在拥有的东西。我似乎在初始化“信号灯”编队时遇到了麻烦。它没有像它应该的那样振荡,而是
我已经编写了 Conway's Game of Life 的简单实现使用 Store comonad(见下面的代码)。我的问题是网格生成从第五次迭代开始明显变慢。我的问题是否与我正在使用 Store
public void actionPerformed(ActionEvent e){ grid=new JButton[length+20][width+20];
我正在尝试用 Java 编写康威生活游戏,但它没有按预期运行。我的意思是,静物有效,但眼罩和轮船不有效。 这是我的算法的代码,播种由一个单独的鼠标监听器处理(按预期工作)。 int[][] s
I am trying to write a program that implements Conway's game of life on a 20x60 cell board. The grid
我正在使用在 involuntaryexercise.com 上找到的 reni2D 库构建这个程序我创建了一个由二维行列组成的网格,单击其中的每个方 block 即可打开和关闭。现在,我正在尝试制作
我正在尝试为 Conway 的人生游戏编写一个计算邻居的方法。如果一个死细胞与 2 或 3 个活细胞相邻,它应该会复活。但是,我的代码没有正确计算所有邻居。如果我给出输入坐标 (10, 10), (1
我是一名优秀的程序员,十分优秀!