Yesterday’s ugliness in implemented in Processing:
class Cell { int x; int y; int size; int live; Cell(int tx, int ty, int ts, int tl) { x = tx; y = ty; size = ts; live = tl; } void display() { stroke(0); if (live == 0) { fill(209); } else { fill(255); } rect(x, y, size, size); } void display(int c) { stroke(0); fill(c); rect(x, y, size, size); } } Cell[][] grid; int[][] next; int maxI; int maxJ; int size = 10; int start = 0; void setup() { size(900, 500); background(255); maxI = floor(width / size) - 1; maxJ = floor(height / size ) -1; grid = new Cell[maxI][maxJ]; next = new int[maxI][maxJ]; noLife(); } void noLife() { for (int i = 0; i < maxI; i++) { for (int j = 0; j < maxJ; j++) { grid[i][j] = new Cell(i * size, j * size, size, 0); next[i][j] = 0; grid[i][j].display(); } } } void draw() { if (start == 1) { runLife(); delay(200); } } void mouseClicked() { if (start == 1) { noLife(); start = 0; } if ((mouseX < maxI * size) && (mouseY < maxJ * size)) { int i = floor(mouseX / size); int j = floor(mouseY / size); if (grid[i][j].live == 1) { grid[i][j].live = 0; } else { grid[i][j].live = 1; } grid[i][j].display(); } else { start = 1; } } void runLife() { int i0; int i1; int j0; int j1; int alive; for (int i = 0; i < maxI; i++) { for (int j = 0; j < maxJ; j++) { i0 = i - 1; i1 = i + 1; j0 = j - 1; j1 = j + 1; if (i0 < 0) i0 = maxI - 1; if (j0 < 0) j0 = maxJ - 1; if (i1 == maxI) i1 = 0; if (j1 == maxJ) j1 = 0; alive = 0; if (grid[i][j0].live == 1) alive++; if (grid[i1][j0].live == 1) alive++; if (grid[i1][j].live == 1) alive++; if (grid[i1][j1].live == 1) alive++; if (grid[i][j1].live == 1) alive++; if (grid[i0][j1].live == 1) alive++; if (grid[i0][j].live == 1) alive++; if (grid[i0][j0].live == 1) alive++; if ((grid[i][j].live == 1) && ((alive == 2) || (alive == 3))) { next[i][j] = 1; } else { next[i][j] = 0; } if ((grid[i][j].live == 0) && (alive == 3)) { next[i][j] = 1; } } } for (int i = 0; i < maxI; i++) { for (int j = 0; j < maxJ; j++) { grid[i][j].live = next[i][j]; grid[i][j].display(); next[i][j] = 0; } } }
Two things that I find cool about Processing:
- You can export your application as a Java applet.
- Processing.js