Search code examples
javaalgorithmprocessingcellular-automata

What's wrong with my (Conway's Game Of) life?


I tried to write an OOP implementation of Conway's Game of Life in processing. However, it seems to be some other sort of automaton. Interesting, but not what I was going for. I can't find any problems with my code, that's what I'm hoping you can help with.

    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;
}

What's wrong? Also, any information on the automaton I accidentally created would be cool, it creates some beautiful patterns.


Solution

  • Let's look at this section of your code:

    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);
    }
    

    There are a couple things that seem wrong here. First, what happens when both i and j are 0? You're going to count each cell as its own neighbor, which is not correct.

    Secondly, what happens if you're on an edge? You're going to encounter an ArrayIndexOutOfBoundsException, and return a dead cell. Again, this is probably not what you want to do. Silent catch blocks like this are a recipe for bugs that you don't detect until you see unexplained behavior.

    Taking a step back, you really need to get into the habit of debugging your code. You could have figured out all of the above by creating a smaller example (say, a 3x3 grid of cells) and stepping through it with a piece of paper and a pencil, or the debugger that comes with the Processing editor. This should always be the first thing you do.