// Wolfram Cellular Automata // Daniel Shiffman // A class to manage the CA // Created 2 May 2005 class CA { int[] cells; // An array of 0s and 1s int generation; // How many generations? int scl; // How many pixels wide/high is each cell? int[] rules; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} CA(int[] r) { rules = r; scl = 1; cells = new int[width/scl]; restart(); } CA() { scl = 1; cells = new int[width/scl]; randomize(); restart(); } // Set the rules of the CA void setRules(int[] r) { rules = r; } // Make a random ruleset void randomize() { //long rulenum = 0; for (int i = 0; i < 27; i++) { rules[i] = int(random(3)); //rulenum += (long)(rules[i]*pow(3,i)); print(rules[i]); } println(" "); //println(rulenum); } // Reset to generation 0 void restart() { for (int i = 0; i < cells.length; i++) { cells[i] = 0; } cells[int(cells.length/3)] = 1; // We arbitrarily start with just the middle cell having a state of "1" cells[int((cells.length*2)/3)] = 2; // We arbitrarily start with just the middle cell having a state of "1" //cells[cells.length/2] = 2; // We arbitrarily start with just the middle cell having a state of "1" generation = 0; } // The process of creating the new generation void generate() { // First we create an empty array for the new values int[] nextgen = new int[cells.length]; // For every spot, determine new state by examing current state, and neighbor states // Ignore edges that only have one neighor for (int i = 1; i < cells.length-1; i++) { int left = cells[i-1]; // Left neighbor state int me = cells[i]; // Current state int right = cells[i+1]; // Right neighbor state nextgen[i] = rules(left,me,right); // Compute next generation state based on ruleset } nextgen[0] = rules(cells[cells.length-1],cells[0],cells[1]); nextgen[cells.length-1] = rules(cells[cells.length-2],cells[cells.length-1],cells[0]); // Copy the array into current value cells = (int[]) nextgen.clone(); generation++; } // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' void render() { for (int i = 0; i < cells.length; i++) { if (cells[i] == 1) fill(255); else if (cells[i] == 2) fill(255,0,0);//(255,0,0); else fill(0); noStroke(); rect(i*scl,(generation%height)*scl, scl,scl); } } // Implementing the Wolfram rules // Could be improved and made more concise, but here we can explicitly see what is going on for each case int rules (int a, int b, int c) { if (a == 1 && b == 1 && c == 1) return rules[0]; if (a == 1 && b == 1 && c == 0) return rules[1]; if (a == 1 && b == 0 && c == 1) return rules[2]; if (a == 1 && b == 0 && c == 0) return rules[3]; if (a == 0 && b == 1 && c == 1) return rules[4]; if (a == 0 && b == 1 && c == 0) return rules[5]; if (a == 0 && b == 0 && c == 1) return rules[6]; if (a == 0 && b == 0 && c == 0) return rules[7]; if (a == 2 && b == 2 && c == 2) return rules[8]; if (a == 2 && b == 2 && c == 1) return rules[9]; if (a == 2 && b == 2 && c == 0) return rules[10]; if (a == 2 && b == 1 && c == 2) return rules[11]; if (a == 2 && b == 1 && c == 1) return rules[12]; if (a == 2 && b == 1 && c == 0) return rules[13]; if (a == 2 && b == 0 && c == 2) return rules[14]; if (a == 2 && b == 0 && c == 1) return rules[15]; if (a == 2 && b == 0 && c == 0) return rules[16]; if (a == 1 && b == 2 && c == 2) return rules[17]; if (a == 1 && b == 2 && c == 1) return rules[18]; if (a == 1 && b == 2 && c == 0) return rules[19]; if (a == 1 && b == 1 && c == 2) return rules[20]; if (a == 0 && b == 2 && c == 2) return rules[21]; if (a == 0 && b == 2 && c == 1) return rules[22]; if (a == 0 && b == 2 && c == 0) return rules[23]; if (a == 0 && b == 1 && c == 2) return rules[24]; if (a == 0 && b == 0 && c == 2) return rules[25]; if (a == 1 && b == 0 && c == 2) return rules[26]; return 0; } /* // The CA is done if it reaches the bottom of the screen boolean finished() { if (generation > height/scl) { return true; } else { return false; } } */ }