Conway Game of Life

edited June 2015 in Questions about Code

Guys, i have a university work that i need to make the this game i found a code somewhere on google, but there's a part that i dont understand

// Birth and death cycle 
for (int x = 0; x < sx; x=x+1) { 
  for (int y = 0; y < sy; y=y+1) { 
    int count = neighbors(x, y); 
    if (count == 3 && world[x][y][0] == 0) 
    { 
      world[x][y][1] = 1; 
    } 
    if ((count < 2 || count > 3) && world[x][y][0] == 1) 
    { 
      world[x][y][1] = -1; 
    } 
  } 
} 

// Count the number of adjacent cells 'on' 
int neighbors(int x, int y) 
{ 
  return world[(x + 1) % sx][y][0] + 
  world[x][(y + 1) % sy][0] + 
  world[(x + sx - 1) % sx][y][0] + 
  world[x][(y + sy - 1) % sy][0] + 
  world[(x + 1) % sx][(y + 1) % sy][0] + 
  world[(x + sx - 1) % sx][(y + 1) % sy][0] + 
  world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] + 
  world[(x + 1) % sx][(y + sy - 1) % sy][0]; 
}

I dont know principaly the "count the number of adjacent cells 'on'. I dont know what that lots of world do, i already tried to comment the parts, i know that it makes the "game" finish early, but i dont know exactly what they do. Could you guys help me? My teacher wants me to comment all the code, but i cant comment that part because i dont understand.

Tagged:

Answers

  • Answer ✓
    +-+-+-+
    |0|0|1|
    +-+-+-+
    |0|1|1|
    +-+-+-+
    |1|0|0|
    +-+-+-+
    

    Right, here we see one world cell... the one in the middle. We also see the surrounding cells.

    What will happen to this center cell according to the rules of the Game of Life?

    How do you know that?

    What did you need to do to determine that?

    That's what this code does.

  • One friend of mine told me that world[x][y][0] = its the current place and the world[x][y][1] = its the state that birth and death cycle sets, its corrects? I really dont got very well the code, and my teacher wants it all commentated...

  • Well, start by posting it formatted. Then break it down into smaller bits and see if you can understand them little by little.

  • // All Examples Written by Casey Reas and Ben Fry
    
    // unless otherwise stated.
    
    int sx, sy; 
    float density = 0.5; 
    int[][][] world;
    
    
    
    void setup() 
    
    { size(100, 100);
      frameRate(12);
      sx = width;
      sy = height;
      world = new int[sx][sy][2]; 
      stroke(255); 
      // Set random cells to 'on' 
      for (int i = 0; i < sx * sy * density; i++) { 
        world[(int)random(sx)][(int)random(sy)][1] = 1; 
      } 
    } 
    
    void draw() 
    { 
      background(0); 
      // Drawing and update cycle 
      for (int x = 0; x < sx; x=x+1) { 
        for (int y = 0; y < sy; y=y+1) { 
          //if (world[x][y][1] == 1) 
          // Change recommended by The.Lucky.Mutt
          if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1)) 
          { 
            world[x][y][0] = 1; 
            point(x, y); 
          } 
          if (world[x][y][1] == -1) 
          { 
            world[x][y][0] = 0; 
          } 
          world[x][y][1] = 0; 
        } 
      } 
      // Birth and death cycle 
      for (int x = 0; x < sx; x=x+1) { 
        for (int y = 0; y < sy; y=y+1) { 
          int count = neighbors(x, y); 
          if (count == 3 && world[x][y][0] == 0) 
          { 
            world[x][y][1] = 1; 
          } 
          if ((count < 2 || count > 3) && world[x][y][0] == 1) 
          { 
            world[x][y][1] = -1; 
          } 
        } 
      } 
    } 
    // Count the number of adjacent cells 'on' 
    int neighbors(int x, int y) 
    { 
      return world[(x + 1) % sx][y][0] + 
      world[x][(y + 1) % sy][0] + 
      world[(x + sx - 1) % sx][y][0] + 
      world[x][(y + sy - 1) % sy][0] + 
      world[(x + 1) % sx][(y + 1) % sy][0] + 
      world[(x + sx - 1) % sx][(y + 1) % sy][0] + 
      world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] + 
      world[(x + 1) % sx][(y + sy - 1) % sy][0]; 
    }
    
  • Answer ✓

    Shiffman has a whole chapter about it in his book. That worth reading.

Sign In or Register to comment.