code+mod._4

code format="java' World w; int maxcells =25000; //number of cells int numcells; Cell[] cells = new Cell[maxcells]; color spore1, spore2, spore3, spore4, spore5, spore6, spore7; //add spore color black = color(0, 0, 0); // set lower for smoother animation, higher for faster simulation int runs_per_loop = 10000; void setup { size(500, 1000, P3D); frameRate(4000); //**cell speed moving clearscr; w = new World; spore1 = color(222, 111, 55); //RGB color spore2 = color(222, 111, 111); //RGB color spore3 = color(55, 111, 55); //RGB color spore4 = color(55, 111, 222); //RGB color spore5 = color(255, 255, 255); //add spore RGB color spore6 = color(0, 0, 255); spore7 = color(222, 32, 98); numcells = 0; seed; } void seed { // Add cells at random places for (int i = 0; i < maxcells; i++) {

float r = random(1); float r2 = random(1); int cX, cY; int c;

if(r2 < 0.5) { cX = width/3 + int(random(width/3)); cY = height/5 + int(random(height/3));

if (r < 0.2) c = spore1; else if (r < 0.4) c = spore2; else if (r < 0.6) c = spore3; else c = spore4;

} else { cX = width/2 + int(random(width/3)); cY = height/2 + int(random(height/3));

if (r < 0.6) c = spore3; else if (r < 0.8) c = spore4; else if (r < 0.8) c = spore5; else if (r < 0.8) c = spore6; else c = spore7; }

if (w.getpix(cX, cY) == black) { w.setpix(cX, cY, c); cells[numcells] = new Cell(cX, cY); numcells++; } } } void draw { // Draw your white areas //fill(222, 111, 55); //rect(width/2, 0, 200, 100);

// Run cells in random order for (int i = 0; i < runs_per_loop; i++) { int selected = min((int)random(numcells), numcells - 1); cells[selected].run; } } void clearscr { for (int y = 0; y < height; y++) { //**change the direction for (int x = 0; x < width; x++) { set(x, y, color(0)); } } } class Cell { int x, y; Cell(int xin, int yin) { x = xin; y = yin; } // Perform action based on surroundings void run { // Fix cell coordinates while(x < 0) { x+=width; } while(x > width - 1) { x-=width; } while(y < 0) { y+=height; } while(y > height - 1) { y-=height; } // Cell instructions int myColor = w.getpix(x, y); if (myColor == spore1) { if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1); else move((int)random(3) - 1, 0);

} else if (myColor == spore2) { if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1); else move((int)random(3) - 1, 0); } else if (myColor == spore3) { if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1); else move((int)random(3) - 1, 0); } else if (myColor == spore4) { if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1); else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1); else move((int)random(3) - 1, 0); } else if (myColor == spore5) { if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -5); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 5); else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(5, 5); else if (w.getpix(x - 1, y) == spore5 && w.getpix(x - 1, y + 1) != black) move(0, 5); else if (w.getpix(x - 1, y) == spore5 && w.getpix(x - 1, y + 1) == black) move(-5, 5); else move((int)random(5) - 1, 5); } else if (myColor == spore6) { if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -5); else if (w.getpix(x + 1, y) == spore4 && w.getpix(x + 1, y + 1) != black) move(0, 5); else if (w.getpix(x + 1, y) == spore4 && w.getpix(x + 1, y + 1) == black) move(5, 5); else if (w.getpix(x - 1, y) == spore6 && w.getpix(x - 1, y + 1) != black) move(0, 5); else if (w.getpix(x - 1, y) == spore6 && w.getpix(x - 1, y + 1) == black) move(-5, 5); else move((int)random(5) - 1, 5); } else if (myColor == spore7) { if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1); else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1); else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1); else move((int)random(3) - 1, 0); } }

// Will move the cell (dx, dy) units if that space is empty void move(int dx, int dy) { if (w.getpix(x + dx, y + dy) == black) { w.setpix(x + dx, y + dy, w.getpix(x, y)); w.setpix(x, y, color(0)); x += dx; y += dy; } } } // The World class simply provides two functions, get and set, which access the // display in the same way as getPixel and setPixel. The only difference is that // the World class's get and set do screen wraparound ("toroidal coordinates"). class World { void setpix(int x, int y, int c) { while(x < 0) x+=width; while(x > width - 1) x-=width; while(y < 0) y+=height; while(y > height - 1) y-=height; set(x, y, c); }

color getpix(int x, int y) { while(x < 0) x+=width; while(x > width - 1) x-=width; while(y < 0) y+=height; while(y > height - 1) y-=height; return get(x, y); } } void mousePressed { setup; }

code