/** * Brian Silverman's Brain Rule Cellular Automata * by Jonathan Pikalek * based partially off of code by Mike Davis. * * This program is a models Brian Silverman's * Brain Rule Cellular Automata. * cells have 3 discreet states 0-2, ready, firing, resetting
* cells update based on:
* a) the sum of their 8 neighbors that are in the 1 state
* b) their current state
* Cells in the ready state (0) move to the firing state (1) * if they have exactly two firing neigbors
* Cells in the firing state always changing to the resetting state (2)
* Cells in the restting state always move to the ready state
*
* controls:
l,L : randmoize cells
* k,K : kill all
* t,T : seed at center
* c,C : randomize colors
* -,_ : decrease delay
* =,+ : increase delay */ int sx, sy; float density = 0.01; int[][][] world; color[] palette; int[][] updateLogicLookup; int curMap; int refMap; int blockSize; int delayAmount; void setup() { size(640, 480, P3D); frameRate(12); blockSize = 4; sx = width/blockSize; sy = height/blockSize; world = new int[sx][sy][2]; palette = new color[3]; stroke(0); clearLife(); initLife(); reloadColor(); initUpdateLogicLookup(); curMap = 1; refMap = 0; delayAmount = 0; } void initUpdateLogicLookup() { // 0 1 2 3 4 5 6 7 8 updateLogicLookup = new int[][]{{0,0,1,0,0,0,0,0,0}, {2,2,2,2,2,2,2,2,2}, {0,0,0,0,0,0,0,0,0} }; } void clearLife() { for(int a=0; a= 100) delayAmount -= 100; if(key == '+' || key == '=') delayAmount += 100; } // Drawing and update cycle for (int x = 0; x < sx; x=x+1) { for (int y = 0; y < sy; y=y+1) { int prevState = world[x][y][refMap]; int neighborCount = getNeighborCount(x,y); int currState = updateLogicLookup[prevState][neighborCount]; world[x][y][curMap] = currState; fill(palette[currState]); rect(x*blockSize,y*blockSize,blockSize,blockSize); } // end for y } // end for x int temp = refMap; refMap = curMap; curMap = temp; } // Count the number of adjacent cells equal to one int getNeighborCount(int x, int y) { int count = 0; int u = (y + 1) % sy; int d = (y + sy - 1) % sy; int l = (x + sx - 1) % sx; int r = (x + 1) % sx; if(world[r][y][refMap] == 1) count++; if(world[x][u][refMap] == 1) count++; if(world[l][y][refMap] == 1) count++; if(world[x][d][refMap] == 1) count++; if(world[r][u][refMap] == 1) count++; if(world[l][u][refMap] == 1) count++; if(world[r][d][refMap] == 1) count++; if(world[l][d][refMap] == 1) count++; return count; } // returns a color st the sum of the components is // at least threshold color randomizedColor(int threshold) { // keep stupid things frm happening if(threshold > (3*255)) return color(255, 255, 255); int rComp = 0; int gComp = 0; int bComp = 0; while((rComp+gComp+bComp) < threshold) { rComp = int(random(0, 255)); gComp = int(random(0, 255)); bComp = int(random(0, 255)); } return color(rComp, gComp, bComp); }