« Back to Processing

# Another Game of Life

| No Comments | Published on May 5, 2008
The content that follows was originally published on the Don Havey website at http://donhavey.com/blog/tutorials/another-game-of-life/ It seems that everyone who has ever programmed in Java has, at some point, created a variation on John Conway’s Game of Life. Well I wouldn’t want to break with tradition. Here’s mine.

This is just a code dump, but the applet is so simple that it doesn’t really require a tutorial anyway. I’ll paste the code directly into the post below, but if you must download a zip file, you can click here.

Here’s the demo: With motion blur / Without motion blur
(Press spacebar to pause, toggle cells by clicking them with your mouse, then press spacebar again to animate.)

#### My take on it

Like I’ve mentioned before, the motion blur trick is a little tired. But the idea I had with this applet was to use the automaton to generate some interesting 8-bit-esque texture maps. Since we’re wrapping the cells around the edges of the applet, a screen capture of any frame can be tiled seamlessly. The example above was made from a 40 cell x 40 cell square, reduced to be a 40px x 40px tile, which was in turn defined as a pattern in Photoshop and used to fill the image.

Although the example above is made from a tiny tile, I think it looks pretty nice. Interesting wallpaper, if only conceptually. Certainly not meant to be used for realistic bump mapping or anything.

Want the code?

#### The code

``````int xcells = 40;
int ycells = 40;
int cellw = 10;
int cellh = 10;
cell[][] cells = new cell[xcells][ycells];
boolean paused = false;

void setup(){
background(255);
frameRate(10);
//size the applet based on the input number of cells and their size
size(xcells*cellw,ycells*cellh);
for(int i=0;i<xcells;i++){
for(int j=0;j<ycells;j++){
new cell(i,j);
//initialize roughly 1/3 of the cells to start in their "alive" state
//isn't my choice of variable names morbid? (not dying = alive)
if(random(3)<1){ cells[i][j].dying = false; }
}
}
}

void draw(){
if(!paused){
fill(255,20); //the motion blur trick again!
noStroke();
rect(0,0,width,height);
fill(0);
for(int i=0;i<xcells;i++){
for(int j=0;j<ycells;j++){
cells[i][j].render();
}
}
for(int i=0;i<xcells;i++){
for(int j=0;j<ycells;j++){
cells[i][j].dying = !live(i,j); //run the neighbor check on each cell
}
}
}
}

boolean live(int _x,int _y){
//get neighbors
cell[] neighbors = new cell;
neighbors = cells[(_x-1+xcells)%xcells][(_y-1+ycells)%ycells];
neighbors = cells[(_x-1+xcells)%xcells][_y];
neighbors = cells[(_x-1+xcells)%xcells][(_y+1)%ycells];
neighbors = cells[_x][(_y-1+ycells)%ycells];
neighbors = cells[_x][(_y+1)%ycells];
neighbors = cells[(_x+1)%xcells][(_y-1+ycells)%ycells];
neighbors = cells[(_x+1)%xcells][_y];
neighbors = cells[(_x+1)%xcells][(_y+1)%ycells];
int n = 0;
//obey the rules
for(int i=0;i<8;i++){ if(!neighbors[i].dead){ n++; } }
if(n<2){ return false; }
if(n>3){ return false; }
return true;
}

void toggle(int _x,int _y){
cells[_x][_y].render();
}

class cell{
int x,y,count;
boolean dying = true;
cell(int _x,int _y){
x = _x;
y = _y;
cells[x][y] = this;
}
void render(){
}
}

void keyPressed(){
paused = !paused;
}

void mousePressed(){
toggle(floor(xcells*mouseX/width),floor(ycells*mouseY/height));
}``````

Note that I slowed down the frame rate to allow the cell movement to be a little more understandable. You can slow it down even further if you’d like.

I’ve also programmed roughly one third of the cells to begin in the “alive” state. You can comment out that line of code if you’d rather start with a blank palette.

Enjoy!

Categories: Processing / Tutorials