【问题标题】:What's wrong with my (Conway's Game Of) life?我(康威的游戏)生活出了什么问题?
【发布时间】:2017-09-01 19:49:22
【问题描述】:

我试图在处理过程中编写康威生命游戏的 OOP 实现。然而,它似乎是某种其他类型的自动机。有趣,但不是我想要的。我找不到我的代码有任何问题,这就是我希望你能帮助解决的问题。

    class Cell {
    int state;
    int x;
    int y;

    Cell update()
  {
    try {

      int neighbors = 0;
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          neighbors += currentBoard[x+i][y+j].state;
        }
      }
      if      ((state == 1) && (neighbors <  2))return new Cell(x, y, 0);
      else if ((state == 1) && (neighbors >  3))return new Cell(x, y, 0); 
      else if ((state == 0) && (neighbors == 3))return new Cell(x, y, 1); 
      else return new Cell(x, y, state);
    }
    catch(ArrayIndexOutOfBoundsException exception) {
      return new Cell(x, y, 0);
    }
  }

  Cell( int _x, int _y, int _s)
  {
    state = _s;
    x = _x;
    y = _y;
  }
}

Cell[][] currentBoard;
Cell[][] newBoard;

void setup() {
  fullScreen();
  //frameRate(100);
  currentBoard = new Cell[width][height];
  newBoard = new Cell[width][height];
  for (int i = 0; i < width; i++)
  {
    for (int j = 0; j < height; j++)
    {
      currentBoard[i][j] = new Cell(i, j, int(random(2)));
    }
  }
}

void draw() {
  print(frameCount);
  for (int i = 0; i < width; i++)
  {
    for (int j = 0; j < height; j++)
    {
      try {
        newBoard[i][j] = currentBoard[i][j].update();
      }
      catch(ArrayIndexOutOfBoundsException exception) {
      }
    }
  }


  for (int i = 0; i < width; i++)
  {
    for (int j = 0; j < height; j++)
    {
      color setCol = color(255, 0, 0);
      if (newBoard[i][j].state == 1)
      {
        setCol = color(0, 0, 0);
      } else if (newBoard[i][j].state == 0)
      {
        setCol = color(255, 255, 255);
      }


      set(i, j, setCol);
    }
  }
  currentBoard = newBoard;
}

怎么了?另外,我不小心创建的自动机上的任何信息都会很酷,它会创建一些漂亮的图案。

【问题讨论】:

  • 一目了然,您似乎将一个单元格视为它自己的邻居之一。
  • 您将不得不debug your code 将其缩小到与您预期的行为不同的代码行。
  • 怎么了?你告诉我们。你期待什么?该程序实际上做了什么?有什么区别?
  • However, it seems to be some other sort of automaton. Interesting, but not what I was going for. 这到底是什么意思?您期待什么,与您观察到的有何不同?
  • @stackoverflowuser2010 这只是对实际发生的事情的评论。这是某种其他类型的自动机。运行代码,你就会明白我的意思了。

标签: java algorithm processing cellular-automata


【解决方案1】:

让我们看看这部分代码:

try {

  int neighbors = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      neighbors += currentBoard[x+i][y+j].state;
    }
  }
  if      ((state == 1) && (neighbors <  2))return new Cell(x, y, 0);
  else if ((state == 1) && (neighbors >  3))return new Cell(x, y, 0); 
  else if ((state == 0) && (neighbors == 3))return new Cell(x, y, 1); 
  else return new Cell(x, y, state);
}
catch(ArrayIndexOutOfBoundsException exception) {
  return new Cell(x, y, 0);
}

这里有几件事似乎是错误的。首先,当ij 都是0 时会发生什么?您要将每个单元格算作自己的邻居,这是不正确的。

其次,如果您处于优势地位,会发生什么?您将遇到ArrayIndexOutOfBoundsException,并返回一个死单元。同样,这可能不是您想要做的。像这样的静默 catch 块是导致您在看到无法解释的行为之前无法检测到的错误的秘诀。

退一步,你真的需要养成debugging your code的习惯。您可以通过创建一个较小的示例(例如,一个 3x3 的单元格网格)并用一张纸和一支铅笔或处理编辑器附带的调试器逐步完成以上所有操作。这应该永远是你做的第一件事。

【讨论】:

  • 细胞的计数是问题所在,谢谢!返回死单元是我的意思,只是一种处理边缘情况而不是循环的方法。
  • @domojz 你为什么要循环播放?只需添加一个 if 语句,在检查之前检查 ij 是否都在边界内。
猜你喜欢
  • 2017-03-15
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 2012-01-19
  • 2016-05-16
  • 2021-09-21
  • 2013-02-21
相关资源
最近更新 更多