// Cellular Automata // an exploration of the 8-neighbour rule space // Chris Wallace Feb 2005 // based on a Game of Life program by // Mike Davis int sx, sy; float density = 0.5; int[][][] world; int[] live2dead, dead2live, ilive2dead, idead2live; int mode; BFont metaBold; String sdensity, slive2dead, sdead2live,sframeRate; int frameRate, frameno; int web=1; void setup() { metaBold= loadFont("Meta-Bold.vlw.gz"); textFont(metaBold,20); live2dead= new int[9]; dead2live= new int[9]; if (web==1) { slive2dead=param("live"); sdead2live=param("dead"); sdensity=param("density"); sframeRate=param("frameRate"); } else { slive2dead="1,4,5,8"; sdead2live="1,3"; sdensity="0.5"; sframeRate="4"; } density = Float.parseFloat(sdensity); frameRate=Integer.parseInt(sframeRate); ilive2dead=splitInts(slive2dead,','); idead2live=splitInts(sdead2live,','); for (int i=0;i<=8;i++) { live2dead[i]=0; dead2live[i]=0; } for (int i=0; i alive if (world[x][y][0] == 0 && dead2live[count]==1 ) world[x][y][1] = 1; // if alive but alive -> die else if ( world[x][y][0] == 1 && live2dead[count] ==1) world[x][y][1] = 0; else // no change world[x][y][1]=world[x][y][0]; } } } else { // mode = 1 text("Generation no.",20,20); text(frameno,30,40); int cellCount=0; for (int x = 0; x < sx; x=x+1) { for (int y = 0; y < sy; y=y+1) { if (world[x][y][1] == 1) cellCount++; } } text("% populated",20,100); text((float)cellCount/(sx*sy),30,120); } } void mousePressed() { if (mode==0) mode=1; else mode=0; } // Count the number of adjacent cells 'on' int neighbors(int x, int y) { return world[(x + 1) % sx][y][0] + world[x][(y + 1) % sy][0] + world[(x + sx - 1) % sx][y][0] + world[x][(y + sy - 1) % sy][0] + world[(x + 1) % sx][(y + 1) % sy][0] + world[(x + sx - 1) % sx][(y + 1) % sy][0] + world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] + world[(x + 1) % sx][(y + sy - 1) % sy][0]; }