final int SQTOPLEFT = 0; final int SQTOPCOL = 1; final int SQTOPRIGHT = 2; final int SQROWLEFT = 3; final int SQROWCOL = 4; final int SQROWRIGHT = 5; final int SQBOTLEFT = 6; final int SQBOTCOL = 7; final int SQBOTRIGHT = 8; final int SQUARESIZE = 9; int GRIDWIDTH = 5; int GRIDHEIGHT = 5; int GRIDSQUARESIZE = 25; int GRIDLEFT = 100; int GRIDTOP = 50; char grid[][] = new char[GRIDWIDTH][GRIDHEIGHT]; char tempgrid[][] = new char[GRIDWIDTH][GRIDHEIGHT]; int genctr; //oops magic #, arbitray limit of 1000 rules. For shame! Rule rules[] = new Rule[1000]; int ruleCount = 0; //FEATURES NEEDED: //apply rules //display grid //wildcard //neightbor counting rule //speed / timing //key-conditional rules //round ending conditionals //variables //THOUGHT ISSUES //so goes from 9 squares to 1, eh? would 9 to 9 work, or too many conflicts? //wrap around is default void setup(){ size(400,800); for(int x = 0; x < GRIDWIDTH; x++){ for(int y = 0; y < GRIDHEIGHT; y++){ grid[x][y] = ' '; } } grid[2][1] = '.'; grid[0][1] = 'O'; frameRate(1); rules[ruleCount++] = new SituationRule(" . ",' '); rules[ruleCount++] = new SituationRule(" . ",'.'); rules[ruleCount++] = new SituationRule(" O ",' '); rules[ruleCount++] = new SituationRule(" O ",'O'); textAlign(CENTER); textFont(loadFont("CourierNewPS-BoldMT-48.vlw"),14); } void draw(){ // // print current grid and clear tempgrid // background(128); int y,x; //drawlines for(y = 0; y <= GRIDHEIGHT; y++){ line(GRIDLEFT, GRIDTOP+(GRIDSQUARESIZE*y), GRIDLEFT+(GRIDSQUARESIZE*GRIDHEIGHT), GRIDTOP+(GRIDSQUARESIZE*y) ); } for(x = 0; x <= GRIDWIDTH; x++){ line(GRIDLEFT+(GRIDSQUARESIZE*x), GRIDTOP, GRIDLEFT+(GRIDSQUARESIZE*x), GRIDTOP+(GRIDSQUARESIZE*GRIDHEIGHT) ); } println((genctr++)+"---"); for(y = 0; y < GRIDHEIGHT; y++){ for(x = 0; x < GRIDWIDTH; x++){ text(grid[x][y], GRIDLEFT+(GRIDSQUARESIZE*(x+.5)), GRIDTOP+(GRIDSQUARESIZE*(y+.7)) ); print(grid[x][y]); tempgrid[x][y] = ' '; } println(); } // // apply rules to every square // updating tempgrid w/ new values for( y = 0; y < GRIDHEIGHT; y++){ for( x = 0; x < GRIDWIDTH; x++){ for(int k = 0; k < ruleCount; k++){ rules[k].applyRule(x,y,grid,tempgrid); } } } // // copy tempgrid to grid // for( y = 0; y < GRIDHEIGHT; y++){ for( x = 0; x < GRIDWIDTH; x++){ grid[x][y] = tempgrid[x][y]; } } } //basic interface for all types of rules... abstract class Rule{ abstract boolean applyRule(int x, int y, char[][] grid, char [][]tempgrid); abstract String toRuleString(); } class SituationRule extends Rule{ char[] ttt = new char[9]; char newVal; //take rules public SituationRule(String startString, char startNewValue){ if(startString.length() != 9) { throw new RuntimeException("SituationRule string must be 9"); } for(int i = 0; i < SQUARESIZE; i++){ ttt[i] = startString.charAt(i); } newVal = startNewValue; } boolean applyRule(int x, int y, char[][] grid, char [][]tempgrid){ //println("XY:"+x+" "+y); //println("my center is '"+ttt[CENTERSQUARE]+"'"); //println("grid is '"+grid[x][y]+"'"); if(testneighbors(x,y,grid)){ tempgrid[x][y] = newVal; return true; } return false; } //figure out the neighbors, wrapping around w/ modulo... //return as soon as there's a failure... boolean testneighbors(int x, int y, char[][] grid){ int left = (x - 1 + GRIDWIDTH) % GRIDWIDTH; int right = (x + 1 + GRIDWIDTH) % GRIDWIDTH; int col = x; int top = (y - 1 + GRIDHEIGHT) % GRIDHEIGHT; int bot = (y + 1 + GRIDHEIGHT) % GRIDHEIGHT; int row = y; if(ttt[SQTOPLEFT] != grid[left][top]) return false; if(ttt[SQTOPCOL] != grid[col][top]) return false; if(ttt[SQTOPRIGHT] != grid[right][top]) return false; if(ttt[SQROWLEFT] != grid[left][row]) return false; if(ttt[SQROWCOL] != grid[col][row]) return false; if(ttt[SQROWRIGHT] != grid[right][row]) return false; if(ttt[SQBOTLEFT] != grid[left][bot]) return false; if(ttt[SQBOTCOL] != grid[col][bot]) return false; if(ttt[SQBOTRIGHT] != grid[right][bot]) return false; return true; } String toRuleString(){ StringBuffer sb = new StringBuffer(); for(int i = 0; i< SQUARESIZE;i++){ sb.append(ttt[i]); } sb.append(newVal); return sb.toString(); } }