Skip to content


Automata Pattern

Based on Conway's Game of Life. After 300-something generations you get this:









and here's the code:

 
int cellsize=1;    //cellsize d'uh
int padding=0;
 
int docwidth=400;
int docheight=400;
 
int rows=docheight/cellsize;        //rows d'uh
int columns=docwidth/cellsize;     //columns d'uh
 
int generations=0;
 
Cell c;
 
Cell[][] cells=new Cell[columns][rows];
Cell[][] temp=new Cell[columns][rows];
 
int [][] universe=new int [columns][rows];
 
void setup(){
 
  docwidth+=columns*(padding)+padding;
  docheight+=rows*(padding)+padding;
 
  size(docwidth,docheight);
 
  for(int i=0;i<columns;i++){
   for(int j=0;j<columns;j++){
    universe[i][j]=int(random(0,10)%2);    //fill array with 1s and 0s
   } 
  }
 
  for(int i=0;i<columns;i++){
   for(int j=0;j<rows;j++){
    //cells[i][j]=i*j;
    c=new Cell(i*(cellsize+padding),j*(cellsize+padding),cellsize);
    cells[i][j]=c;
    temp[i][j]=c;
 
 
   }//endof for
  }//endof for
 
 
}//endof setup
 
void checkNeighbors(){
   for(int i=1;i<columns-1;i++){
     for(int j=1;j<rows-1;j++){
      if(cells[i][j-1]._life)   cells[i][j]._neighbors++;
      if(cells[i][j+1]._life)   cells[i][j]._neighbors++;
 
      if(cells[i-1][j-1]._life) cells[i][j]._neighbors++;
      if(cells[i-1][j+1]._life) cells[i][j]._neighbors++;
      if(cells[i-1][j]._life)   cells[i][j]._neighbors++;
 
      if(cells[i+1][j-1]._life) cells[i][j]._neighbors++;
      if(cells[i+1][j+1]._life) cells[i][j]._neighbors++;
      if(cells[i+1][j]._life)   cells[i][j]._neighbors++;
     } 
   }
 
 
}
 
void resetNeighbors(){
 for(int i=1;i<columns-1;i++){
     for(int j=1;j<rows-1;j++){
      cells[i][j]._neighbors=0; 
     }
    }
 
}
 
void conway(){
  for(int i=1;i<columns-1;i++){
     for(int j=1;j<rows-1;j++){
       if(cells[i][j]._life&&cells[i][j]._neighbors<2) {temp[i][j]._life=false; }
       else if(cells[i][j]._life&&cells[i][j]._neighbors>3){ temp[i][j]._life=false; }
       else if(!cells[i][j]._life&&cells[i][j]._neighbors==3){ temp[i][j]._life=true; }
       else{
        cells[i][j]=temp[i][j] ;
       }
     }
  }
 
 // Cell[][] buffer = cells;
 
  for(int i=0;i<columns;i++){
   for(int j=0;j<rows;j++){
 
    if(temp[i][j]._life) universe[i][j]=1;
    else universe[i][j]=0;
 
 
  if(universe[i][j]==1){
      cells[i][j]._life=true;
    }//endof if
    else{
       cells[i][j]._life=false;
    }
    }
  }
 
}
 
void draw(){
  delay(40);
  checkNeighbors();
  conway();
 
  generations++;
  save("pattern"+generations+".png");
 
  for(int i=0;i<columns;i++){
   for(int j=0;j<rows;j++){
 
    if(universe[i][j]==1){
      cells[i][j]._life=true;
    }//endof if
    else{
       cells[i][j]._life=false;
    }
 
    cells[i][j].display();
   }
  }
 
   resetNeighbors();
 
}//endof draw
 
 
 
 
class Cell{
 
  float  _xpos=0;
  float  _ypos=0;
  float  _cellsize=0;
  int    _alivefill;
  int    _deadfill;
  boolean _life;
  int _neighbors;
 
  Cell(float xpos, float ypos, float cellsize){
    _neighbors=0;
    _cellsize=cellsize;
    _xpos=xpos;
    _ypos=ypos;
    _deadfill=40;
    _alivefill=180;
    _life=true;
 
 
 
  }//endof constructor
 
  void display(){
   noStroke();
   if(_life)  fill(_alivefill);
   else fill(_deadfill);
   rect(_xpos+padding,_ypos+padding,_cellsize,_cellsize); 
  }
 
 
}//endof class
 
 

Posted in Cellular Automata, Processing, Programming, sketches.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.