Game of Life – Tre colori

int   sx, sy;          // dimensioni tabella
int   sx_u, sy_u;      // ultima cella
float densita = 0.2;   // densità iniziale della popolazione
int   MONDO[][][];     // la tabella a due livelli, due matrici...
int   quanti;

void setup() 
{ 
  size(500, 350);
  frameRate(30);
  sx = width;
  sy = height;
  quanti=(int)(sx*sy*densita);
  MONDO = new int[sx][sy][2];   
  sx_u=sx-1;
  sy_u=sy-1; 

  occupa();    
} 

void occupa()
{ 
  background(0); 
  // svuota
  for (int i = 0; i < sx; i++)
  for (int j = 0; j < sy; j++)
  {
	  MONDO[i][j][1]=0;
	  MONDO[i][j][0]=0;
  }  
  // occupa
  for (int i=1; i <= quanti; i++)
	  MONDO [(int)random(sx)] [(int)random(sy)] [1] = 2; 
}

void mousePressed()
{
   occupa();
}

void draw() 
{ 
  for(int x=0; x < sx; x++)
  for(int y=0; y < sy; y++)
  { 
	 if (MONDO[x][y][1] == 2)          // cella occupata
	 { 
		 MONDO[x][y][0] = 1;           // copia
	 stroke(255); 
	 point(x, y);                  // disegna
	 } 
	 else if (MONDO[x][y][1] == 1)     // cella occupata
	 { 
		 MONDO[x][y][0] = 1;           // copia
	 stroke(0, 255, 255); 
	 point(x, y);                  // disegna
	 }  
	 else if (MONDO[x][y][1] == -1)    // cella da liberare
	 { 
		 MONDO[x][y][0] = 0; 
	 stroke(0, 50, 50);         
	 point(x, y);                  // disegna        
	 } 	 
	 MONDO[x][y][1] = 0;               // pulito...
  } 

  for (int x=0; x < sx; x++)
  for (int y=0; y < sy; y++)
  { 
	 int conta = vicini(x, y); 

	 if (conta == 3 && MONDO[x][y][0] == 0) 
	 { 
		MONDO[x][y][1] = 2;                         // nuovo nato...
	 } 
	 else if ((conta < 2 || conta > 3) && MONDO[x][y][0] == 1) 
	 { 
		MONDO[x][y][1] = -1;                        // morto...
	 } 
	 else                                          
	 { 
		MONDO[x][y][1] = MONDO[x][y][0];            // copia...
	 }
  } 
} 

// Conta il numero di vicini per la cella (x, y)

int vicini(int x, int y) 
{ 
  int numero=0;

  numero += (x > 0)                  ? MONDO[x-1][y  ][0] : MONDO[sx_u][y   ][0]; // !Ovest
  numero += (x < sx_u)               ? MONDO[x+1][y  ][0] : MONDO[0   ][y   ][0]; // !Est
  numero += (y > 0)                  ? MONDO[x  ][y-1][0] : MONDO[x   ][sy_u][0]; // !Nord
  numero += (y < sy_u)               ? MONDO[x  ][y+1][0] : MONDO[x   ][0   ][0]; // !Sud
  numero += (x != 0    && y != 0)    ? MONDO[x-1][y-1][0] : MONDO[sx_u][sy_u][0]; // !NO
  numero += (x != 0    && y != sy_u) ? MONDO[x-1][y+1][0] : MONDO[sx_u][0   ][0]; // !SO
  numero += (x != sx_u && y != 0)    ? MONDO[x+1][y-1][0] : MONDO[0   ][sy_u][0]; // !NE
  numero += (x != sx_u && y != sy_u) ? MONDO[x+1][y+1][0] : MONDO[0   ][0   ][0]; // !SE

  return numero;  
}