xxxxxxxxxx
77
let COLS, ROWS;
let SIZE = 10;
let fr = 3;
let i,j,k,l,count,x,y;
let GRID, NEXT;
function setup() {
frameRate(fr);
createCanvas(windowWidth,windowHeight);
COLS = floor(windowWidth / SIZE) + 2;
ROWS = floor(windowHeight / SIZE) + 2;
// COLS = 50; ROWS = 50;
// print(COLS); print(ROWS);
// Create GRIDS
GRID = new Array(COLS,ROWS);
NEXT = GRID;
// Fill with random noise
for (i=0;i<COLS;i++){
GRID[i] = new Array(10);
for (j=0;j<ROWS;j++){
GRID[i][j] = floor(random(2));
}
}
// Set outer ROWS / COLS to 0
for (i=0;i<COLS;i++){
GRID[i][0] = 0;
GRID[i][ROWS-1] = 0;
}
for (j=0;j<ROWS;j++){
GRID[0][j] = 0;
GRID[COLS-1][j] = 0;
}
}
function draw() {
// Count surroundings of each cell
for (i=1;i<COLS-1;i++){
for (j=1;j<ROWS-1;j++){
count = 0;
for (k=i-1;k<i+2;k++){
for (l=j-1;l<j+2;l++){
count += GRID[k][l];
}
}
count -= GRID[i][j];
// print(count);
// evaluate
if (GRID[i][j] == 0 && count == 3) {
NEXT[i][j] = 1;
} else if (GRID[i][j] == 1 && count == 2){
NEXT[i][j] = 1;
} else if (GRID[i][j] == 1 && count == 3){
NEXT[i][j] = 1;
} else {
NEXT[i][j] = 0;
}
}
}
// Display all except borders
for (i=1;i<COLS;i++){
for (j=1;j<ROWS;j++){
x = i * SIZE;
y = j * SIZE;
fill (NEXT[i][j] * -255 + 255);
stroke (NEXT[i][j] * -255 + 255);
square(x,y,SIZE - 2, SIZE / 2)
}
}
// Prepair next cycle
GRID = NEXT;
}