/**
* Original Conway's Game of Life by Mike Davis
* I changed the resolution & hacked in some color code
*
* This program is a simple version of Conway's
* game of Life. A lit point turns off if there
* are fewer than two or more than three surrounding
* lit points. An unlit point turns on if there
* are exactly three lit neighbors. The 'density'
* parameter determines how much of the board will
* start out lit.
*
* When new cells are spawned, their color is determined
* in part by that of their 'parents'.
*/
int sx, sy;
float density = 0.5;
int[][][] world;
color[][] palette;
void setup()
{
size(640, 480, P3D);
frameRate(12);
sx = width/5;
sy = height/5;
world = new int[sx][sy][2];
palette = new color[sx][sy];
stroke(0);
// Set random cells to 'on'
for (int i = 0; i < sx * sy * density; i++) {
world[(int)random(sx)][(int)random(sy)][1] = 1;
}
for(int a=0; a 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];
}
// returns a color st the sum of the components is
// at least threshold
color randomizedColor(int threshold)
{
// keep stupid things frm happening
if(threshold > (3*255))
return color(255, 255, 255);
int rComp = 0;
int gComp = 0;
int bComp = 0;
while((rComp+gComp+bComp) < threshold)
{
rComp = int(random(0, 255));
gComp = int(random(0, 255));
bComp = int(random(0, 255));
}
return color(rComp, gComp, bComp);
}
color swapGenes(color c, float chance)
{
if(random(0,1) > chance)
return c;
if(random(0,1) > .5)
return color(green(c), blue(c), red(c));
return color(blue(c), red(c), green(c));
}
color breedColor(int x, int y)
{
color[] parent = new color[3];
int index = 0;
if(world[(x + 1) % sx][y][0]==1)
parent[index++] = palette[(x + 1) % sx][y];
if(world[x][(y + 1) % sy][0]==1)
parent[index++] = palette[x][(y + 1) % sy];
if(world[(x + sx - 1) % sx][y][0]==1)
parent[index++] = palette[(x + sx - 1) % sx][y];
if(world[x][(y + sy - 1) % sy][0]==1)
parent[index++] = palette[x][(y + sy - 1) % sy];
if(world[(x + 1) % sx][(y + 1) % sy][0]==1)
parent[index++] = palette[(x + 1) % sx][(y + 1) % sy];
if(world[(x + sx - 1) % sx][(y + 1) % sy][0]==1)
parent[index++] = palette[(x + sx - 1) % sx][(y + 1) % sy];
if(world[(x + sx - 1) % sx][(y + sy - 1) % sy][0]==1)
parent[index++] = palette[(x + sx - 1) % sx][(y + sy - 1) % sy];
if(world[(x + 1) % sx][(y + sy - 1) % sy][0]==1)
parent[index++] = palette[(x + 1) % sx][(y + sy - 1) % sy];
int rGene = int(random(0,3));
int gGene = int(random(0,3));
int bGene = int(random(0,3));
return color(red(parent[rGene]), green(parent[gGene]), blue(parent[bGene]));
}