Skip to content

Instantly share code, notes, and snippets.

@shioken
Created December 9, 2016 14:15
Show Gist options
  • Save shioken/78146e55fafd70c6dac725ab48eb67a2 to your computer and use it in GitHub Desktop.
Save shioken/78146e55fafd70c6dac725ab48eb67a2 to your computer and use it in GitHub Desktop.
LifeGame with Processing
void setup() {
size(480, 480);
init();
frameRate(12);
unit = width / SIZE;
}
final int SIZE = 96;
int map[][] = new int[SIZE * SIZE][2];
int current;
int unit;
void init() {
current = 0;
for (int i = 0; i < SIZE * SIZE; i++) {
map[i][current] = (int)random(2);
}
}
void draw() {
background(0);
for (int i = 0; i < SIZE * SIZE; i++) {
int x = i % SIZE;
int y = i / SIZE;
fill(map[i][current] * 255);
rect(x * unit, y * unit, unit, unit);
}
next();
}
void next() {
for (int i = 0; i < SIZE * SIZE; i++) {
int x = i % SIZE;
int y = i / SIZE;
int total = 0;
for (int x1 = -1; x1 < 2; x1++) {
for (int y1 = -1; y1 < 2; y1++) {
if (x1 == 0 && y1 == 0) {
continue;
}
int tx = (x + x1 + SIZE) % SIZE;
int ty = (y + y1 + SIZE) % SIZE;
total += map[tx + ty * SIZE][current];
}
}
map[i][1 - current] = 0;
if (map[i][current] > 0) {
if (total == 2 || total == 3) {
map[i][1 - current] = 1;
}
}
else {
if (total == 3) {
map[i][1 - current] = 1;
}
}
}
current = 1 - current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment