/* Simulation of a parallel computation, automaton-ish. Each cell updates based upon the immediate (not diagonal) neighbors of its immediate neighbors, resulting in implicit communication to 12 total cells, weighted. */ /* 25 possible cells filled. Cells stays (or becomes) alive between min and max */ int minimum = 8; int maximum = 18; /* Display variables */ int cellSize = 2; int cellsX = 300; int cellsY = 300; /* The world */ boolean[][] grid = new boolean[cellsX][cellsY]; /* Our groups */ color black = #BBBBBB; color white = #44CC44; /* Click to start */ void setup() { background(white); noStroke(); size(600, 600, P2D); seedGrid(); colorGrid(); noLoop(); } void draw() { shuffle(); colorGrid(); } void mousePressed() { setup(); } void keyPressed() { loop(); } void seedGrid() { for(int x = 0; x < cellsX; x++) { for(int y = 0; y < cellsY; y++) { int gridseed = (int)random(-1,2); if(gridseed == 0) { grid[x][y] = false; } else { grid[x][y] = true; } } } } void colorGrid() { for(int x = 0; x < cellsX; x++) { for(int y = 0; y < cellsY; y++) { if(grid[x][y] == false) { fill(black); } else { fill(white); } rect(x*cellSize,y*cellSize,cellSize,cellSize); } } } void shuffle() { for(int x = 0; x < cellsX; x++) { for(int y = 0; y < cellsY; y++) { x = (int)random(0,cellsX); y = (int)random(0,cellsY); int trueCells = trueCells(x,y); if( trueCells > minimum && trueCells < maximum) { grid[x][y] = true; } else { grid[x][y] = false; } } } } int trueCells(int x, int y) { /* some short-cuts in this method */ int trueCells = 0; if(grid[(x+cellsX-2)%cellsX][y]) { trueCells += 1; } if(grid[(x+cellsX-1)%cellsX][y]) { trueCells += 2; } if(grid[(x+1)%cellsX][y]) { trueCells += 2; } if(grid[(x+2)%cellsX][y]) { trueCells += 1; } if(grid[x][(y+cellsY-2)%cellsY]) { trueCells += 1; } if(grid[x][(y+cellsY-1)%cellsY]) { trueCells += 2; } if(grid[x][(y+1)%cellsY]) { trueCells += 2; } if(grid[x][(y+2)%cellsY]) { trueCells += 1; } if(grid[(x+cellsX-1)%cellsX][(y+cellsY-1)%cellsY]) { trueCells += 2; } if(grid[(x+1)%cellsX][(y+1)%cellsY]) { trueCells += 2; } if(grid[(x+1)%cellsX][(y+cellsY-1)%cellsY]) { trueCells += 2; } if(grid[(x+cellsX-1)%cellsX][(y+1)%cellsY]) { trueCells += 2; } return trueCells; }