-1

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.

domojz
  • 13
  • 6
  • At a glance, it looks like you're counting a cell as one of its own neighbors. – azurefrog Sep 01 '17 at 19:51
  • You're going to have to [debug your code](http://happycoding.io/tutorials/processing/debugging) to narrow it down to a line of code that's behaving differently from what you expected. – Kevin Workman Sep 01 '17 at 19:51
  • 2
    What's wrong? You tell us. What did you expect? What does the program actually do? What is the difference? – rossum Sep 01 '17 at 19:52
  • `However, it seems to be some other sort of automaton. Interesting, but not what I was going for.` What exactly does this mean? What are you expecting, and how is different from what you're observing? – stackoverflowuser2010 Sep 01 '17 at 19:53
  • @stackoverflowuser2010 That was just a comment on what actually happened. It's some other sort of automaton. Run the code, you'll see what I mean. – domojz Sep 01 '17 at 20:08
  • @domojz: To become a software professional, you need to better describe and quantify your bugs. Saying "it looks like something else" will not get you very far. – stackoverflowuser2010 Sep 01 '17 at 20:15

1 Answers1

3

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • The cell's counting of itself was the issue, thank you! The returning the dead cell is what I meant to do, just a way to handle the edge case instead of looping. – domojz Sep 01 '17 at 20:11
  • @domojz Why would you have to loop? Just ad an `if` statement that checks whether `i` and `j` are both in-bounds before checking. – Kevin Workman Sep 01 '17 at 20:16