Last active
December 15, 2015 15:49
-
-
Save disser2/5284240 to your computer and use it in GitHub Desktop.
Precedurale version of Conway's Game of Life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Settings | |
int size = 400; | |
float startCellsPercentage = 20; | |
int framrate = 60; | |
//Rules for living cells from 0 to 8 neighbours, true = lives on, false = dies | |
boolean[] aliveCellRules = { | |
false, false, true, true, false, false, false, false | |
}; | |
//Rules for dead cells from 0 to 8 neighbours, true = rebirth, false = still dead | |
boolean[] deadCellRules = { | |
false, false, false, true, false, false, false, false | |
}; | |
//Game-Canvas | |
boolean[][] canvas = new boolean[size][size]; | |
void setup() { | |
size(size, size); | |
frameRate(framrate); | |
noLoop(); | |
//Initialize the canvas with randomly living and dead cells | |
for (int i = 0; i < width; i = i + 1) { | |
for (int j = 0; j < height; j = j + 1) { | |
float rnd = random(0, 99); | |
if (rnd < startCellsPercentage) { | |
//Alive | |
canvas[i][j] = true; | |
} | |
else { | |
//Dead | |
canvas[i][j] = false; | |
} | |
} | |
} | |
} | |
void draw() { | |
//Background | |
rect(0, 0, width, height); | |
//Dead or alive? | |
for (int i = 1; i < width-1; i = i + 1) { | |
for (int j = 1; j < height-1; j = j + 1) { | |
//Cell is alive | |
if (canvas[i][j] == true) { | |
int aliveCounter = 0; | |
//Surrounding cells are checked (including itself) | |
for (int k = -1; k <= 1; k++) { | |
for (int l = -1; l <= 1; l++) { | |
if (canvas[i+k][j+l] == true) { | |
aliveCounter++; | |
} | |
} | |
} | |
//minus itself | |
aliveCounter--; | |
//Applying rules | |
for (int p = 0; p <= 8; p++) { | |
if (aliveCounter == p) { | |
canvas[i][j] = aliveCellRules[p]; | |
} | |
} | |
} | |
//Cell is dead | |
else { | |
int aliveCounter = 0; | |
//Surrounding cells are checked | |
for (int k = -1; k < 2; k++) { | |
for (int l = -1; l < 2; l++) { | |
if (canvas[i+k][j+l] == true) { | |
aliveCounter++; | |
} | |
} | |
} | |
//Applying rules | |
for (int p = 0; p <= 8; p++) { | |
if (aliveCounter == p) { | |
canvas[i][j] = deadCellRules[p]; | |
} | |
} | |
} | |
} | |
} | |
//Painting the canvas | |
for (int i = 0; i < width; i = i + 1) { | |
for (int j = 0; j < height; j = j + 1) { | |
if (canvas[i][j] == true) { | |
point(i, j); | |
} | |
} | |
} | |
} | |
void mousePressed(){ | |
redraw(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment