康威的生命游戏 - 如何伪造“无限”2d飞机

时间:2021-11-21 13:35:31

I'm using a 2d array to represent the grid of cells. When a cell on an edge or in a corner checks it's neighbors that would be out of bounds, it treats them as permanently dead.

我正在使用二维数组来表示细胞网格。当边缘或角落中的单元格检查它的邻居是否会超出界限时,它会将它们视为永久死亡。

function getCell(row, column) {
  if(row === -1 || row === cellMatrix.length || column === -1 || column === cellMatrix[0].length)
  {
    return 0;
  }
  else return cellMatrixCopy[row][column];
}

I'd just like to get rid of behavior like gliders halting and turning into blocks when they reach the edge of the grid. How would you "get rid" of the edges of the array?

我只是想摆脱滑翔机停止并在到达网格边缘时变成块的行为。你将如何“摆脱”阵列的边缘?

You can checkout the full implementation here. Thanks in advance.

您可以在此处查看完整实施。提前致谢。

3 个解决方案

#1


4  

How to fake an “infinite” 2d plane?

如何伪造“无限”2d飞机?

To create an infinite dimension game board use a sparse matrix representation where the row and column indicies are arbitrary-precision integers. Roughly as follows:

要创建无限维度游戏板,请使用稀疏矩阵表示,其中行和列指示是任意精度整数。大致如下:

map<pair<BigInt,BigInt>, Cell> matrix;

Cell& get_cell(BigInt x, BigInt y)
{
    return matrix[make_pair[x,y]];
}

#2


0  

The game of life is impossible to fake being infinite. Say the structure you create tries to expand past the current bounds, what is to stop it from expanding infinitely. Like MitchWheat said you could wrap the edges which is your best bet (for the more natural looking behavior), but since it's turing complete you can't fake it being infinite in every situation without having infinite memory.

生命的游戏是不可能伪造无限的。假设您创建的结构尝试扩展超出当前边界,什么是阻止它无限扩展。就像MitchWheat所说的那样,你可以把边缘包裹起来,这是你最好的选择(对于更自然的行为),但是因为它的图灵完成你无法在没有无限记忆的情况下假装它在每种情况下都是无限的。

Since the game of life is turing complete that means if a something "goes off the edge" it is impossible to tell if it will come back for a general case (related to the halting problem), meaning any heuristic you use to decide when something goes off the edge will have a degree of inaccuracy, if this is acceptable then you should consider that approach to, though IMO simulating the game of life incorrectly kinda defeats the purpose.

由于生命游戏是图灵完成的,这意味着如果一个东西“脱离边缘”,就无法判断它是否会回归一般情况(与停止问题有关),这意味着你用什么启发式来决定什么东西离开边缘将有一定程度的不准确,如果这是可以接受的那么你应该考虑这种方法,虽然IMO模拟生活游戏错误有点失败的目的。

Another way to go would be to purposely simulate a larger area than you show so objects appear to go off the edge

另一种方法是有目的地模拟比你显示的更大的区域,这样物体就会偏离边缘

#3


0  

This question is also here with some good answers.

这个问题也在这里提供了一些很好的答案。

The simplest modification to your simulation (and not mentioned on the other question) would be to triple the area's length and width, but only show what you currently show. The only catch would be if something went off-screen, hit the wall, and then came back. You could try playing around with the game rules at the boundary to make it more likely for a cell to die (so that a glider could get a little further in before becoming a block that could mess things up). Maybe that a live cell with three neighbors would die if it were within two steps of the side.

对模拟进行最简单的修改(在另一个问题上没有提到)将是区域长度和宽度的三倍,但只显示您当前显示的内容。唯一的问题是,如果有什么东西离开屏幕,撞墙,然后又回来了。您可以尝试在边界处玩游戏规则,以使其更有可能让一个单元死亡(这样滑翔机可以在变成一个可以搞砸的块之前再进一步)。也许有三个邻居的活细胞如果在两侧的两个步骤内就会死亡。

One of the other answers (and our answer) can be summarized as keeping track of the live nodes, not the entire screen. Then you would be able to do cool stuff like zoom and pan as if it were Google Maps.

其中一个答案(和我们的答案)可以概括为跟踪实时节点,而不是整个屏幕。然后你就可以像缩放和平移一样做酷炫的东西,就好像它是谷歌地图一样。

#1


4  

How to fake an “infinite” 2d plane?

如何伪造“无限”2d飞机?

To create an infinite dimension game board use a sparse matrix representation where the row and column indicies are arbitrary-precision integers. Roughly as follows:

要创建无限维度游戏板,请使用稀疏矩阵表示,其中行和列指示是任意精度整数。大致如下:

map<pair<BigInt,BigInt>, Cell> matrix;

Cell& get_cell(BigInt x, BigInt y)
{
    return matrix[make_pair[x,y]];
}

#2


0  

The game of life is impossible to fake being infinite. Say the structure you create tries to expand past the current bounds, what is to stop it from expanding infinitely. Like MitchWheat said you could wrap the edges which is your best bet (for the more natural looking behavior), but since it's turing complete you can't fake it being infinite in every situation without having infinite memory.

生命的游戏是不可能伪造无限的。假设您创建的结构尝试扩展超出当前边界,什么是阻止它无限扩展。就像MitchWheat所说的那样,你可以把边缘包裹起来,这是你最好的选择(对于更自然的行为),但是因为它的图灵完成你无法在没有无限记忆的情况下假装它在每种情况下都是无限的。

Since the game of life is turing complete that means if a something "goes off the edge" it is impossible to tell if it will come back for a general case (related to the halting problem), meaning any heuristic you use to decide when something goes off the edge will have a degree of inaccuracy, if this is acceptable then you should consider that approach to, though IMO simulating the game of life incorrectly kinda defeats the purpose.

由于生命游戏是图灵完成的,这意味着如果一个东西“脱离边缘”,就无法判断它是否会回归一般情况(与停止问题有关),这意味着你用什么启发式来决定什么东西离开边缘将有一定程度的不准确,如果这是可以接受的那么你应该考虑这种方法,虽然IMO模拟生活游戏错误有点失败的目的。

Another way to go would be to purposely simulate a larger area than you show so objects appear to go off the edge

另一种方法是有目的地模拟比你显示的更大的区域,这样物体就会偏离边缘

#3


0  

This question is also here with some good answers.

这个问题也在这里提供了一些很好的答案。

The simplest modification to your simulation (and not mentioned on the other question) would be to triple the area's length and width, but only show what you currently show. The only catch would be if something went off-screen, hit the wall, and then came back. You could try playing around with the game rules at the boundary to make it more likely for a cell to die (so that a glider could get a little further in before becoming a block that could mess things up). Maybe that a live cell with three neighbors would die if it were within two steps of the side.

对模拟进行最简单的修改(在另一个问题上没有提到)将是区域长度和宽度的三倍,但只显示您当前显示的内容。唯一的问题是,如果有什么东西离开屏幕,撞墙,然后又回来了。您可以尝试在边界处玩游戏规则,以使其更有可能让一个单元死亡(这样滑翔机可以在变成一个可以搞砸的块之前再进一步)。也许有三个邻居的活细胞如果在两侧的两个步骤内就会死亡。

One of the other answers (and our answer) can be summarized as keeping track of the live nodes, not the entire screen. Then you would be able to do cool stuff like zoom and pan as if it were Google Maps.

其中一个答案(和我们的答案)可以概括为跟踪实时节点,而不是整个屏幕。然后你就可以像缩放和平移一样做酷炫的东西,就好像它是谷歌地图一样。