//Daniel Shiffman, Nature of Code, 3/1/2005 //http://stage.nyu.edu/nature //a basic implementation of John Conway's Game of Life CA //how could this be improved to use object oriented programming? //think of it as similar to our particle system, with a "cell" class //to describe each individual cell and a "cellular automata" class //to describe a collection of cells //int cellsize = 1; //int COLS, ROWS; //game of life board int[] old_board, new_board; void setup() { size(200, 200); //smooth(); //initialize rows, columns and set-up arrays //COLS = width/cellsize; //ROWS = height/cellsize; old_board = new color[width*height]; new_board = new color[width*height]; colorMode(RGB,255); background(0); //call function to fill array with random values 0 or 1 initBoard(); frameRate(3); } void draw() { //background(100); //loadPixels(); //loop through every spot in our 2D array and check spots neighbors for (int x = 0; x < width;x++) { for (int y = 0; y < height;y++) { int nb = color(0); //Note the use of mod ("%") below to ensure that cells on the edges have "wrap-around" neighbors //above row int above = (y+height-1) % height; int below = (y+1) % height; int left = (x+width-1) % width; int right = (x+1) % width; // Rug rules with wrap nb = color(old_board[(left*width)+above]) + color(old_board[ (x*width) +above]) + color(old_board[(right*width)+ above]) + //middle row color(old_board[(left*width)+ y ]) + color(old_board[(right*width)+ y ]) + //bottom row color(old_board[(left*width)+below]) + color(old_board[(x*width)+below]) + color(old_board[(right*width)+below]); nb = color(nb/8) + color(1); //println(nb); /* float r = red(nb); float g = green(nb); float b = blue(nb); r = red(nb)%256; g = green(nb)%256; b = blue(nb)%256; */ new_board[(y*width)+x] = color(nb);//color(r,g,b); } } loadPixels(); //RENDER game of life based on "new_board" values for ( int i = 0; i < width;i++) { for ( int j = 0; j < height;j++) { //stroke(color(new_board[(j*width)+i])); //stroke(255,0,0); //fill(255,0,0); //noStroke(); pixels[j*width+i] = new_board[(j*width)+i]; //point(i,j); //rect(i,j,10,10); } } updatePixels(); //filter(BLUR,8); //swap old and new game of life boards int[] tmp = old_board; old_board = new_board; new_board = tmp; //updatePixels(); } //init board with random "alive" squares void initBoard() { // background(0); // loadPixels(); // old_board[2000] = color(255); old_board[((width*(height/2))+(width/2))] = color(255); //updatePixels(); } //re-set board when mouse is pressed void mousePressed() { initBoard(); }