« 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/

Game of Life textureIt 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?

At your humble service.

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(){
  //size the applet based on the input number of cells and their size
  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(){
    fill(255,20); //the motion blur trick again!
    for(int i=0;i<xcells;i++){
      for(int j=0;j<ycells;j++){
    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[8];
  neighbors[0] = cells[(_x-1+xcells)%xcells][(_y-1+ycells)%ycells];
  neighbors[1] = cells[(_x-1+xcells)%xcells][_y];
  neighbors[2] = cells[(_x-1+xcells)%xcells][(_y+1)%ycells];
  neighbors[3] = cells[_x][(_y-1+ycells)%ycells];
  neighbors[4] = cells[_x][(_y+1)%ycells];
  neighbors[5] = cells[(_x+1)%xcells][(_y-1+ycells)%ycells];
  neighbors[6] = cells[(_x+1)%xcells][_y];
  neighbors[7] = 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; }
  if(cells[_x][_y].dead&&n==2){ return false; }
  return true;

void toggle(int _x,int _y){
  cells[_x][_y].dead = !cells[_x][_y].dead;
  cells[_x][_y].dying = cells[_x][_y].dead;

class cell{
  int x,y,count;
  boolean dead = true;
  boolean dying = true;
  cell(int _x,int _y){
    x = _x;
    y = _y;
    cells[x][y] = this;
  void render(){
    dead = dying;
    if(!dead){ rect(x*cellw,y*cellh,cellw,cellh); }

void keyPressed(){
  paused = !paused;

void mousePressed(){

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.


Categories: Processing / Tutorials

Tags: / / / / / /

Leave a Response

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>