用 JavaScript 和 HTML5 Canvas 实现的元胞自动机

发布于 2024-12-22 10:17:21 字数 263 浏览 6 评论 0原文

我用 JavaScript 实现了康威的生命游戏,但我没有看到相同的模式,例如高斯珀的滑翔机枪。我按照维基百科文章中描述的方式播种网格,但是枪从未发生过。

有人会看一下我的代码,看看它是否有什么问题,对实现有什么建议吗?

https://github.com/DiegoSalazar/ConwaysGameOfLife

I implemented a Conway's Game of Life in JavaScript but I'm not seeing the same patterns such as Gosper's Glider Gun. I seed the grid the ways it's depicted in the Wikipedia article but, the gun never happens.

Will someone look at my code and see if there's anything wrong with it, any suggestions to the implementation?

https://github.com/DiegoSalazar/ConwaysGameOfLife

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-12-29 10:17:21

您不是同时更新所有单元格,而是按顺序更新。在第一代中诞生的细胞在第一代其他细胞的计算中不会显得活着(它仍然算作死亡)。

创建一个名为 willBeAlive 的新属性,并使用它来保存单元格新计算的活动状态。一旦该代的所有计算完成,将每个单元格的alive属性设置为其willBeAlive属性并重绘。

以下是更改:

Automaton.prototype.update = function() {
  for (var x = 0; x < this.w; x++) {
    for (var y = 0; y < this.h; y++) {
      this.grid[x][y].killYourselfMaybe();
    }
  }
  // set the alive property to willBeAlive
  for (var x = 0; x < this.w; x++) {
    for (var y = 0; y < this.h; y++) {
            this.grid[x][y].alive = this.grid[x][y].willBeAlive;
        }
    }  
}


Cell.prototype.killYourselfMaybe = function(grid) {
  var num = this.numLiveNeighbors();

  this.willBeAlive = false;

  if (this.alive) {
    // 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
    if (num < 2) this.willBeAlive = false;
    // 2. Any live cell with two or three live neighbours lives on to the next generation.
    if (num == 2 || num == 3) { this.willBeAlive = true}
    // 3. Any live cell with more than three live neighbours dies, as if by overcrowding.
    if (num > 3) this.willBeAlive = false;
  } else {
    // 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
    if (num == 3) this.willBeAlive = true;
  }
}

这是“Gosper's Glider Gun”的种子数组:

[[2,6],[2,7],[3,6],[3,7],[12,6],[12,7],[12,8],[13,5],[13,9],[14,4],[14,10],[15,4],[15,10],[16,7],[17,5],[17,9],[18,6],[18,7],[18,8],[19,7],[22,4],[22,5],[22,6],[23,4],[23,5],[23,6],[24,3],[24,7],[26,2],[26,3],[26,7],[26,8],[36,4],[36,5],[37,4],[37,5]]

You are not updating all of the cells simultaneously, rather sequentially. A cell that is born in the first generation will not appear alive to the calculation of other cells of the first generation (it still counts as dead).

Create a new property called willBeAlive and use that to hold the cell's new calculated alive state. Once all the calculations for that generation are done, set each cell's alive property to its willBeAlive property and redraw.

Here are the changes:

Automaton.prototype.update = function() {
  for (var x = 0; x < this.w; x++) {
    for (var y = 0; y < this.h; y++) {
      this.grid[x][y].killYourselfMaybe();
    }
  }
  // set the alive property to willBeAlive
  for (var x = 0; x < this.w; x++) {
    for (var y = 0; y < this.h; y++) {
            this.grid[x][y].alive = this.grid[x][y].willBeAlive;
        }
    }  
}


Cell.prototype.killYourselfMaybe = function(grid) {
  var num = this.numLiveNeighbors();

  this.willBeAlive = false;

  if (this.alive) {
    // 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
    if (num < 2) this.willBeAlive = false;
    // 2. Any live cell with two or three live neighbours lives on to the next generation.
    if (num == 2 || num == 3) { this.willBeAlive = true}
    // 3. Any live cell with more than three live neighbours dies, as if by overcrowding.
    if (num > 3) this.willBeAlive = false;
  } else {
    // 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
    if (num == 3) this.willBeAlive = true;
  }
}

and here is a seed array for "Gosper's Glider Gun":

[[2,6],[2,7],[3,6],[3,7],[12,6],[12,7],[12,8],[13,5],[13,9],[14,4],[14,10],[15,4],[15,10],[16,7],[17,5],[17,9],[18,6],[18,7],[18,8],[19,7],[22,4],[22,5],[22,6],[23,4],[23,5],[23,6],[24,3],[24,7],[26,2],[26,3],[26,7],[26,8],[36,4],[36,5],[37,4],[37,5]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文