//One dimentional Cellular Automata // // Coded by Victor-charles Scafati // scafativ@execpc.com // www.execpc.com/~scafativ // September 2003 // //This program implements a one dimensional cellular automata that is a subject of study in //Steven Wolfram's book "A new kind of science." // // Usage: //The program starts with a rule-set and initial colony that I choose rather arbitrarily. //The user can change the rule set by clicking the bottom row boxes on the left. Each //of the eight groups represent one of the possible input conditions for a particular cell- //the leftmost group is the condition in which all three of the previous generation "neighbors" //are dark, the second is with the upper right "neighbor" is lit, etc. //The boxes on the right determine the first (topmost) generation. // //This is considered "one dimensional" in the cellular automata world because the Y axis (from top //to bottom) is the "time" axis. // //I know that my implementation is painfully procedural. Any suggestion of a book on object design //would be greatly appreciated. color cellColor; color boxColor; color boxBlack; int[] cellRule = { 0,1,1,0,1,0,0,0 }; // Initial colony rule- seems to be the most interesting rule int[] seedColony = {0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0}; // Initial colony seeding (top row) int xSize = 640; int ySize = 400; //not including the control bar at the bottom void setup() { size(xSize, ySize + 20); noBackground(); clearscr(); cellColor = color(172, 255, 128); boxColor = color(172, 255, 128); boxBlack = color(0,0,0); for (int initialCell = 0; initialCell < 23; initialCell++){ if(seedColony[initialCell] == 1){ setPixel((xSize / 2) - 12 + initialCell,0,boxColor); drawBox(xSize - ((24 - initialCell) * 10),ySize,boxColor); } else { drawBox(xSize - ((24 - initialCell) * 10),ySize,boxBlack); } } //Set up the 8 input groups in the lower left corner drawBox(10,ySize,boxBlack); drawBox(20,ySize,boxBlack); drawBox(30,ySize,boxBlack); drawBox(50,ySize,boxBlack); drawBox(60,ySize,boxBlack); drawBox(70,ySize,boxColor); drawBox(90,ySize,boxBlack); drawBox(100,ySize,boxColor); drawBox(110,ySize,boxBlack); drawBox(130,ySize,boxBlack); drawBox(140,ySize,boxColor); drawBox(150,ySize,boxColor); drawBox(170,ySize,boxColor); drawBox(180,ySize,boxBlack); drawBox(190,ySize,boxBlack); drawBox(210,ySize,boxColor); drawBox(220,ySize,boxBlack); drawBox(230,ySize,boxColor); drawBox(250,ySize,boxColor); drawBox(260,ySize,boxColor); drawBox(270,ySize,boxBlack); drawBox(290,ySize,boxColor); drawBox(300,ySize,boxColor); drawBox(310,ySize,boxColor); // Fill in the variable rule cells. for (int grp = 0; grp < 8; grp++){ if (cellRule[grp] == 0){ drawBox((grp * 40) + 20,ySize + 10,boxBlack); } else { drawBox((grp * 40) + 20,ySize + 10,boxColor); } } } void loop() { //Execute the multiple generations for (int y = 1; y < ySize; y++){ for (int x = 1; x < xSize; x++){ int group = 0; //check the neighbors and determine which of 8 situations exist if (getPixel(x-1, y-1) != 0){ group = group + 4; } if (getPixel(x, y-1) != 0){ group = group + 2; } if (getPixel(x+1, y-1) != 0){ group = group + 1; } if (cellRule[group] == 0){ setPixel(x, y, 0); } else { setPixel(x, y, cellColor); } } } } void clearscr() { for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) setPixel(x, y, 0); } void drawBox(int xpos, int ypos, color boxColor) { stroke(cellColor); fill (boxColor); int boxsize = 10; rect(xpos, ypos, boxsize, boxsize); } void mousePressed() { int freezeMouseX = mouseX; int freezeMouseY = mouseY; if(freezeMouseY < ySize){ setup(); } else { //Mouse is at the control bar if (freezeMouseY < ySize + 10){ //Mouse is at the initial colony level if ((freezeMouseX > (xSize - 240)) && (freezeMouseX < xSize - 10)){ //Mouse is in one of the boxes int initCell = (23 - (xSize - freezeMouseX) / 10); if (seedColony[initCell] == 1){ seedColony[initCell] = 0; } else { seedColony[initCell] = 1; } } } else { //Mouse is at the rule level int ruleCell = ((freezeMouseX - 20) / 40); //Check that click is in the box if ((freezeMouseX > (20 + (ruleCell * 40))) && (freezeMouseX < (30 + (ruleCell * 40)))){ println(((20 + (ruleCell * 40)))); if (cellRule[ruleCell] == 1){ cellRule[ruleCell] = 0; } else { cellRule[ruleCell] = 1; } } } } setup(); }