Game of Life Midterm

float R=125;
float centerR=125;
float a=PI/2;
float a1=PI;
float a2=3*PI/2;
float pathR=125;
float pathG=125;
float G=125;
float centerG=125;
float pathB=125;
float B=125;
float centerB=125;
int sx, sy, zoom;
int[][][] world;
boolean run;
int[][] pattern = {
{0,0,0,1,0,0,0,1,1,1,1},
{0,0,0,1,0,0,0,1,0,0,1},
{0,0,0,1,0,0,0,1,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,0,0},
{1,0,0,1,0,0,0,1,0,0,0},
{1,1,1,1,0,0,0,1,0,0,0}};

void setup()
{
  zoom = 3;
  sx = int(640/zoom);
  sy = int(480/zoom);
  world = new int[sx][sy][2];
  run = false;
  size(sx*zoom, sy*zoom, P2D);
  frameRate(50);
  int placeX = int(sx/2)-3;
  int placeY = int(sy/2)-5;
  for ( int x = 0; x < pattern[0].length; x++ ) {
    for ( int y = 0; y < pattern.length; y++ ) {
        world[placeX+x][placeY+y][1] = pattern[y][x];
    }
  }
  noStroke();
  fill(200);
}

void draw()
{
  //when running: black background, if not: dark gray
  background(pathR,pathG,pathB);
  pathR=centerR+R*sin(a);
  a=a+.03;

  pathG=centerG+G*sin(a1);
  a1=a1+.03;

  pathB=centerB+B*sin(a2);
  a2=a2+.03;
  scale(zoom);

  // Drawing and update cycle
  for (int x = 0; x < sx; x++) {
    for (int y = 0; y < sy; y++) {
      if (world[x][y][1] == 1) {
        world[x][y][0] = 1;
        rect(x,y,1,1);
      }
      else {
        world[x][y][0] = 0;
      }
    }
  }
  if ( run ) {
    for (int x = 0; x < sx; x=x+1) {
      for (int y = 0; y < sy; y=y+1) {
        int count = neighbors(x, y);
        if (world[x][y][0] == 0) {
          if (count == 3) {
            world[x][y][1] = 1;
          }
        }
        else if (count < 2 || count > 3) {
          world[x][y][1] = 0;
        }
      }
    }
  }
}

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];
}
void keyPressed() {
  if(key == 'm'){
    background(0);
  }
  if(key == 'n'){
    noStroke();
    fill(random(30,500), random(30,500), random(30,500));

  }
  if (key == 'b' ){
    stroke( random(30,500), random(30,500), random(30,500));
  }
  if ( key == 'c' ) {
    //clear the world
    world = new int[sx][sy][2];
    run = false;
  }
  if (key == 'a') {
     run = !run;
  }
   if ( run ) { fill(255); } else { fill(200); }
}
void mousePressed() {
  world[int(mouseX/zoom)][int(mouseY/zoom)][1] ^= 1;
}