Question

So, the code is compiling and running perfectly. However, usually, Conway's Game of life stabilizes after some time, while this code keeps running indefinitely without ever getting a dead branch, and I'd love if anybody could help me figuring out why.

int[][] world;
int SCALE_FACTOR = 4;
float DENSITY = 0.1;

void setup() {
  size(500,500);
  world = new int[width/SCALE_FACTOR][height/SCALE_FACTOR];
  for(int i = 0; i < width/SCALE_FACTOR; i++) {
    for (int j = 0; j < height/SCALE_FACTOR; j++) {
      int r = (int)random(100);
      world[i][j] = r % (int)(1/DENSITY) == 0 ? 1 : 0;
    }
  }
  frameRate(1);
  stroke(0, 255, 0);
}

void draw() {
  scale(SCALE_FACTOR);
  background(0);
  for(int i = 0; i < width/SCALE_FACTOR; i++) {
    for (int j = 0; j < height/SCALE_FACTOR; j++) {
      if (world[i][j] == 1) point(i, j);
    }
  }
  lifeCycle();    
}

void lifeCycle() {
  int[][] newworld = new int[width/SCALE_FACTOR][height/SCALE_FACTOR];
  for(int i = 0; i < width/SCALE_FACTOR; i++) {
    for (int j = 0; j < width/SCALE_FACTOR; j++) {
      if (world[i][j] == 1) {
        if (neighbours(i, j) == 3) {
          newworld[i][j] = 1;
        } else {
          newworld[i][j] = 0;
        }
      } else {
        if (neighbours(i, j) == 2 || neighbours(i, j) == 3) {
          newworld[i][j] = 1;
        } else {
          newworld[i][j] = 0;
        }
      }
    }
  }
  for (int i = 0; i < width/SCALE_FACTOR; i++) {
    for (int j = 0; j < height/SCALE_FACTOR; j++) {
      world[i][j] = newworld[i][j];
    }
  }
}

int neighbours(int x, int y) {
  int px = (x == width/SCALE_FACTOR - 1) ? 0 : (x+1);
  int py = (y == height/SCALE_FACTOR - 1) ? 0 : (y+1);
  int mx = (x == 0) ? (width/SCALE_FACTOR - 1) : (x-1);
  int my = (y == 0) ? (height/SCALE_FACTOR - 1) : (y-1);
  return world[mx][my] + world[mx][y] + world[mx][py]
    + world[x][my] + world[x][py]
    + world[px][my] + world[px][y] + world[px][py];
}
Was it helpful?

Solution

Well this looks wrong to start with:

for (int j = 0; j < width/SCALE_FACTOR; j++) {

Surely that should be height.

Next up: currently your logic says, "If the square was alive before, it's only alive in the next round if it's got three neighbours; if it was dead before, it's alive in the next round if it's got two or three neighbours." That's the wrong way round. It takes three live neighbours to create life, but only two or three neighbours to sustain life.

That's all I can immediately see that's wrong with it, but tips on diagnosing the problem:

  • I'd strongly suggest keeping width and height in game terms rather than pixel sizes, which I assume is what SCALE_FACTOR is meant to be. Scale the units up when drawing, but keep all your logic in terms of the actual grid elements.
  • If you isolate the UI from the "business logic" you can easily unit test it.
  • When you're using a UI to display the board, you might want to give it a "manual advance" mode so you can see exactly what's going on more easily.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top