首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python生命游戏生成错误的结构

Python生命游戏生成错误的结构
EN

Stack Overflow用户
提问于 2022-10-25 12:40:37
回答 1查看 21关注 0票数 0

我试图在python中编写生命的游戏程序。我不断得到错误的结构,产生后,并指出了我的错误的功能如下。

我想要程序做的事情:

它应该遍历一个数组,以确定该细胞在下一代中是活的还是死的,并将它们放入数组“cell”输入:(.-死单元x活着的单元格)。

..。

xxx

..。

输出

.x。

.x。

.x。

现在正在发生的事情:程序遍历单元格并修改它们。然后,它使用新值而不是原始值来确定下一个单元格是否还活着。

投入:

..。

xxx

..。

输出

.xx

x.x

..。

附加信息:

这两个数组的周围都有一个永久死亡的单元格框架。

代码语言:javascript
复制
    def nextGen(matrice):
cells = matrice
size = np.shape(matrice)
for i in range(1,size[0]-1):
    for j in range(1,size[1]-1):
        if matrice[i][j] > 0:
            alive = True
        else:
            alive = False    
        neighbours = 0    
        neighbours += matrice[i-1,j+1]
        neighbours += matrice[i-1,j] 
        neighbours += matrice[i-1,j -1]
        neighbours += matrice[i,j+1] 
        neighbours += matrice[i,j -1]
        neighbours += matrice[i+1,j+1]
        neighbours += matrice[i+1,j] 
        neighbours += matrice[i+1,j -1]
        if alive:
            if neighbours != 2 and neighbours !=3:
                cells[i,j] = 0
                #print("Cell has died", i, j, "Neighbours: ", neighbours)
        else:
            if neighbours == 3:
                cells[i,j] = 1
                #print("Cell was born", i, j,"Neighbours: ", neighbours)                    
        neighbours = 0
return cells
EN

回答 1

Stack Overflow用户

发布于 2022-10-25 12:48:33

问题在于@ copy 2357112建议cells = matrice不复制。要解决这个问题,我需要将其更改为cells = np.copy(matrice)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74194250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档