首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算康威3D生命游戏的下一次迭代?

如何计算康威3D生命游戏的下一次迭代?
EN

Stack Overflow用户
提问于 2021-10-22 03:22:03
回答 1查看 171关注 0票数 0

我正在编写一个脚本,它生成一个包含三维空间中立方体位置的字典,计算每个立方体的邻居,然后根据康威的生命游戏中的规则计算新的迭代。我的网格是偶数元组的字典,如下所示:

代码语言:javascript
复制
grid = {(0, 0, 0): True, (0, 0, 2): True, (2, 0, 0): True, (2, 0, 2): True, (0, 2, 0): True, (0, 2, 2): True, (2, 2, 0): True, (2, 2, 2): True}

TrueFalse表示单元格是alive还是dead

为了计算每个立方体的摩尔邻居,我编写了以下函数:

代码语言:javascript
复制
# Calculates the neighbors of a cell
def get_neighbors(grid, x,y,z):
    count = 0
    for pos in (
        # List out all possible neighbours of cell
        (x-2,y-2,z-2),
        (x-2,y-2,z),
        (x-2,y-2,z+2),
        (x-2,y,z-2),
        (x-2,y,z),
        (x-2,y,z+2),
        (x-2,y+2,z-2),
        (x-2,y+2,z),
        (x-2,y+2,z+2),
        (x,y-2,z-2),
        (x,y-2,z),
        (x,y-2,z+2),
        (x,y,z-2),
        (x,y,z+2),
        (x,y+2,z-2),
        (x,y+2,z),
        (x,y+2,z+2),
        (x+2,y-2,z-2),
        (x+2,y-2,z),
        (x+2,y-2,z+2),
        (x+2,y,z-2),
        (x+2,y,z),
        (x+2,y,z+2),
        (x+2,y+2,z-2),
        (x+2,y+2,z),
        (x+2,y+2,z+2)):
        if pos in grid:
            # If the neighbour is alive, add 1 to the count
            if grid[pos] == True:
                count += 1
    return count

# Checks if a cell is alive
def is_alive(grid, x,y,z):
    if (x,y,z) in grid:
        if grid[(x,y,z)] == True:
            return True
    return False

这将正确地输出邻居数,并返回给定的元组是否有TrueFalse。为了计算下一次迭代并包含生命游戏规则,我写道:

代码语言:javascript
复制
# Calculates the next iteration of the game
def next_iteration(grid):
    new_grid = {}
    length = len(grid)
    # Iterate through the grid with range step size of 2, 
    # since we only have even numbers in the tuples.
    for x in range(0,length, 2):
        for y in range(0,length, 2):
            for z in range(0,length, 2):
                # Get the number of neighbors of the cell
                neighbors = get_neighbors(grid, x,y,z)
                if is_alive(grid, x,y,z):
                    # If the cell is alive, check if it should die
                    if neighbors < 2:
                        new_grid[(x,y,z)] = False
                    elif neighbors > 3:
                        new_grid[(x,y,z)] = False
                    else:
                        new_grid[(x,y,z)] = True
                else:
                    # If the cell is dead, check if it should be alive
                    if neighbors == 3:
                        new_grid[(x,y,z)] = True
                    else:
                        new_grid[(x,y,z)] = False
    return new_grid

但是,如果我生成一个2x2x2网格并尝试计算三次迭代,就会得到 (仅附加输出的一部分),其中everythingFalse。这似乎不对。

我的代码做错了什么?我是否没有正确计算邻居,还是规则没有得到正确的检查?

如何正确地实现这个三维生命游戏?

我的完整脚本是这里

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-22 04:20:59

实际上,您的代码有时会产生一些True值--取决于初始配置。但是,几乎所有的值都是False,因为在每次迭代时,您都在扩展网格,包括许多远离任何活单元的单元格,因此它们的值必须是False。具体情况如下:

  • 从一个由8个单元格组成的网格开始,坐标为0或2。
  • 在第一次迭代之后,您将得到一个网格,其坐标为4**3 = 64单元,坐标为0、2、4和6。只有坐标为0、2和4的单元才能存活。
  • 经过2次迭代,网格由由0到63之间的所有偶数给出的坐标单元组成。只有坐标为0、2、4和6的细胞才有可能存活。在一个由32**3 = 32768个单元组成的网格中有64个这样的单元。
  • 经过3次迭代后,网格将有16384**3 = 4398046511104个单元,并且由于内存不足,代码将崩溃。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69671042

复制
相关文章

相似问题

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