//import processing.opengl.*; //Conway Game of Life //This version by Robert Carlsen, 8-2008 /* 1. Any live cell with fewer than two live neighbours dies, as if by loneliness. 2. Any live cell with more than three live neighbours dies, as if by overcrowding. 3. Any live cell with two or three live neighbours lives, unchanged, to the next generation. 4. Any dead cell with exactly three live neighbours comes to life. //looking to move into 3d */ //some globals //array for the pixels int[][] world; //going to use a one-dimentional array sx*sy*sz long int sx,sy,sz; float density = .10; boolean mutateFlag = true; //ranges for rules of life int low = 3; int high = 6; int[][] matrix = {{-1,-1,0},{0,-1,0},{1,-1,0},{-1,0,0},{1,0,0},{-1,1,0},{0,1,0},{1,1,0}, {-1,-1,-1},{0,-1,-1},{1,-1,-1},{-1,0,-1},{1,0,-1},{-1,1,-1},{0,1,-1},{1,1,-1},{0,0,-1}, {-1,-1,1},{0,-1,1},{1,-1,1},{-1,0,1},{1,0,1},{-1,1,1},{0,1,1},{1,1,1},{0,0,1} }; void setup() { size(500,500,P3D); // smooth(); frameRate(15); sx = 30; sy = 30; sz = 30; world = new int[sx*sy*sz][3]; //the properties array is [current,next,age] //populate the world // Set random cells to 'on' for (int i = 0; i < sx * sy * sz * density; i++) { world[(int)random(sx*sy*sz)][1] = 1; } colorMode(HSB,1.5,1,1); } void draw() { background(0); lights(); fill(200,75); //noFill(); //stroke(225,50); noStroke(); //draw the array for(int i=0;ihigh) { world[j][1] = 0; world[j][2] = -1; //-1 is dead } else { //2 or 3 remain unchanged } } else { //exactly three will spawn life if(count==high) { world[j][1] = 1; world[j][2] = millis(); //record the birth date } } } } int coordsToIndex(int x, int y, int z){ //convert from coordinates to the array index //use the plane first (z), then the row (y), then the column (x) return z*sx*sy+y*sx+x; } int[] indexToCoords(int index) { //convert the index to x,y,z values int z = index/(sx*sy); int y = (index%(sx*sy))/sx; int x = (index%(sx*sy))%sx; int[] coords = {x,y,z}; return coords; } void mutate() { if(countCells() > 0) { // mess with the rules if there are still alive cells if(round(random(1))==0) {if(high>low+1) high--;}else{high++;} println("mutation, high: "+high); } else { //otherwise spawn a new "culture" float density = 0.10; for (int i = 0; i < sx * sy * sz * density; i++) { world[(int)random(sx*sy*sz)][1] = 1; } } } int countCells(){ //returns the number of "live" cells int liveCells = 0; for(int i=0;i0) low--; break; case RIGHT: high++; break; case LEFT: if(high>1 && high>low) high--; break; } println("low: "+low+", high: "+high); } else { switch(key){ case 'm': mutateFlag = !mutateFlag; break; } } } int startX,startY; float rotY,rotX,sRotX,sRotY; void mousePressed() { startX = mouseX; startY = mouseY; sRotX = rotX; sRotY = rotY; } void mouseDragged() { rotY = map(mouseX-startX,0,width,sRotY,sRotY+TWO_PI); rotX = map(startY-mouseY,0,height,sRotX,sRotX+TWO_PI); }