game of life unexpected result

edited October 2015 in Library Questions

hello all

im trying to write Conwey's game of life, but for some reason i am getting unexpected results. If you'll run the code bellow, you'll see that cross (+) shaped forms remain while this type should not exist. you would notice that these crosses flicker while the other static forms don't.

could anyone see where am I wrong?

Thanks

nn

import peasy.*;
import toxi.geom.*;

PeasyCam cam;

int cols = 100;
int rows = 100;


CA grid [][] = new CA[cols][rows];

void setup () {
  size (800, 600, P3D);
 // frameRate (10);

  cam = new PeasyCam (this, 100);
  for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {
      Vec3D ptLoc = new Vec3D (i*15, j*15, 0);
      grid[i][j] = new CA(ptLoc, i, j);  
    }
  }

}

void draw() {
  background(0);

  stroke (255);
  fill (255,20);
  //rect (0,0, 600,600);

  for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {
      grid[i][j].run();  
    }
  }

  for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {
      grid[i][j].updateType();  
    }
  }  
}

class CA {

  Vec3D loc;
  int x,y;
  int type = 0;
  int futType = 0;

  CA(Vec3D _loc, int _x, int _y) {
    loc = _loc;
    x = _x;
    y = _y;

    float rnd = random (100);
    if (rnd<50) {
      type = 1;
    }
  }

  void run () {
    display();
    evalN();
  }

  void updateType() {
    type = futType;
  }

  void evalN() {
    int count = 0;
    if (grid[(x+cols-1)%cols][(y+rows-1)%rows].type == 1) count++;
    if (grid[(x+cols)%cols][(y+rows-1)%rows].type == 1) count++;
    if (grid[(x+cols+1)%cols][(y+rows-1)%rows].type == 1) count++;
    if (grid[(x+cols-1)%cols][(y+rows)%rows].type == 1) count++;
    if (grid[(x+cols+1)%cols][(y+rows)%rows].type == 1) count++;
    if (grid[(x+cols-1)%cols][(y+rows+1)%rows].type == 1) count++;
    if (grid[(x+cols)%cols][(y+rows+1)%rows].type == 1) count++;
    if (grid[(x+cols+1)%cols][(y+rows+1)%rows].type == 1) count++;

    if (type==1 && count < 2){  //starvation
      futType = 0;
    }

    if (type==1 && count > 3) {  //over population
      futType = 0;
    }

    if (type==0 && count==3) {//revive
      futType = 1; 
    }

    if (type==1 && ((count ==3) || (count ==2))){ //perfect
      futType = 1;
    }    
  }

  void display () {
    if (type == 1) {      
      stroke (255);
      strokeWeight(5);
      point (loc.x, loc.y);
    }
  } 


}

Answers

  • Answer ✓

    that looks fine. commenting in line 14 makes it a bit more obvious - the crosses are oscillating too fast to see.

Sign In or Register to comment.