Brians Brain
https://gyazo.com/b5113805473432ee140dd60210d02b67
code:java
Cell[][] _cellArray;
int _cellSize = 10;
int _numx, _numy;
int picnum = 0;
void setup() {
size(500, 300);
_numx = floor(width/_cellSize);
_numy = floor(height/_cellSize);
restart();
}
void restart() {
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
Cell newCell = new Cell(x, y);
}
}
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
int above = y-1;
int below = y+1;
int left = x-1;
int right = x+1;
if (above < 0) { above = _numy-1; }
if (below == _numy) { below = 0; }
if (left < 0) { left = _numx-1; }
if (right == _numx) { right = 0; }
_cellArrayxy.addNeighbour(_cellArraylefty); _cellArrayxy.addNeighbour(_cellArrayxabove); _cellArrayxy.addNeighbour(_cellArrayxbelow); _cellArrayxy.addNeighbour(_cellArrayrighty); }
}
}
void draw() {
background(200);
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
_cellArrayxy.calcNextState(); }
}
translate(_cellSize/2, _cellSize/2);
for (int x = 0; x < _numx; x++) {
for (int y = 0; y < _numy; y++) {
}
}
if (picnum++ < 100) {
saveFrame("###.jpg");
}
}
void mousePressed() {
restart();
}
class Cell {
float x, y;
int state;
int nextState;
Cell[] neighbors;
Cell(float ex, float why) {
x = ex * _cellSize;
y = why * _cellSize;
nextState = int(random(2));
state = nextState;
}
void addNeighbour(Cell cell) {
neighbors = (Cell[])append(neighbors, cell);
}
void calcNextState() {
if (state == 0) {
int firingCount = 0;
for (int i = 0; i < neighbors.length; i++) {
if (neighborsi.state == 1) { firingCount++;
}
}
if (firingCount == 2) {
nextState = 1;
} else {
nextState = state;
}
} else if (state == 1) {
nextState = 2;
} else if (state == 2) {
nextState = 0;
}
}
void drawMe() {
state = nextState;
stroke(0);
if (state == 1) {
fill(0);
} else if (state == 2) {
fill(150); // gray
} else {
fill(255);
}
ellipse(x, y, _cellSize, _cellSize);
}
}