Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Pragyanur committed Aug 11, 2023
2 parents 1d66ce1 + 6187cdb commit 095c6c1
Showing 1 changed file with 167 additions and 20 deletions.
187 changes: 167 additions & 20 deletions scripts/sketch-03.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,136 @@
const cell_size = 15;
// const cell_size = 15;
// const cell_mid = cell_size / 2;


// let game_of_life = [];
// // game_of_life[x][y] = true / false
// let countX = 0;
// let countY = 0;

// // FUNCTION TO COUNT ALIVE NEIGHBOUR CELLS
// function count_alive(array, x, y) {
// let count = 0;
// // sides
// if (array[x][y - 1])
// count++;
// if (array[x][y + 1])
// count++;
// if (array[x - 1][y])
// count++;
// if (array[x + 1][y])
// count++;
// // diagonals
// if (array[x - 1][y + 1])
// count++;
// if (array[x - 1][y - 1])
// count++;
// if (array[x + 1][y + 1])
// count++;
// if (array[x + 1][y - 1])
// count++;

// return count;
// }

// // FUNCTION TO CHANGE STATE OF THE CELL
// function change_cell_state(array, x, y) {
// // live cell with less than 2 alive neighbours
// if (array[x][y] == true && count_alive(array, x, y) < 2)
// array[x][y] = false;
// // live cell with 2 or 3 alive neighbours
// if (array[x][y] == true && count_alive(array, x, y) >= 2 && count_alive(array, x, y) <= 3)
// array[x][y] = true;
// // live cell with more than 3 alive neighbours
// if (array[x][y] == true && count_alive(array, x, y) > 3)
// array[x][y] = false;
// // dead cell with exactly three alive neighbours
// if (array[x][y] == false && count_alive(array, x, y) == 3)
// array[x][y] = true;
// }

// // FUNCTION TO CHANGE THE COLOR OF THE CELL WRT ALIVE OR DEAD
// function color_cell(alive, x, y) {
// if (alive == true)
// fill(20, 100, 100);
// else fill(0);
// stroke(0);
// rect(x * cell_size, y * cell_size, cell_size, cell_size);
// }

// // RANDOMLY SET ALIVE CELLS
// function random_alive(array) {
// for (let x = 4; x < countX - 4; x++) {
// for (let y = 4; y < countY - 4; y++) {
// god = random(-1, 0.1);
// if (god > 0)
// array[x][y] = true;
// else array[x][y] = false;
// }
// }
// }

// // SETUP CANVAS
// function setup() {
// background(0);
// frameRate(15);
// createCanvas(windowWidth, windowHeight);
// // count the number of horizontal cells
// for (let x = cell_mid; x < width; x += cell_size) {
// countX++;
// }
// // count the number of vertical cells
// for (let y = cell_mid; y < height; y += cell_size) {
// countY++;
// }
// // initialize board to black
// for (let x = 0; x < countX; x++) {
// game_of_life[x] = [];
// for (let y = 0; y < countY; y++)
// game_of_life[x][y] = false;
// }
// for (let x = 0; x < countX; x++) {
// for (let y = 0; y < countY; y++) {
// let alive = game_of_life[x][y];
// color_cell(alive, x, y);
// }
// }
// // random_alive(game_of_life);
// }
// // ANIMATION
// function draw() {
// if (!mouseIsPressed) {
// for (let x = 1; x < countX - 1; x++) {
// for (let y = 1; y < countY - 1; y++) {
// change_cell_state(game_of_life, x, y);
// color_cell(game_of_life[x][y], x, y);
// }
// }
// }
// fill(255);
// textSize(20);
// text("Conway's Game of Life", width / 10, height / 5);
// fill(200);
// textSize(13);
// text("Touch and drag to create alive cells on the screen", width / 10, height / 5 + 20);

// }

// function mouseDragged() {
// let indexX = int(mouseX / cell_size);
// let indexY = int(mouseY / cell_size);
// color_cell(game_of_life[indexX][indexY], indexX, indexY);
// game_of_life[indexX][indexY] = true;
// }



// changes and new code with next generation
const cell_size = 10;
const cell_mid = cell_size / 2;


let game_of_life = [];
let next_gen = [];
// game_of_life[x][y] = true / false
let countX = 0;
let countY = 0;
Expand Down Expand Up @@ -33,19 +161,35 @@ function count_alive(array, x, y) {
}

// FUNCTION TO CHANGE STATE OF THE CELL
function change_cell_state(array, x, y) {
// live cell with less than 2 alive neighbours
if (array[x][y] == true && count_alive(array, x, y) < 2)
array[x][y] = false;
// live cell with 2 or 3 alive neighbours
if (array[x][y] == true && count_alive(array, x, y) >= 2 && count_alive(array, x, y) <= 3)
array[x][y] = true;
// live cell with more than 3 alive neighbours
if (array[x][y] == true && count_alive(array, x, y) > 3)
function change_cell_state(next, array, x, y) {
// modifications
if (next[x][y] == false) {
array[x][y] = false;
// dead cell with exactly three alive neighbours
if (array[x][y] == false && count_alive(array, x, y) == 3)
array[x][y] = true;
next[x][y] = true;
}
else {
// live cell with less than 2 alive neighbours
if (array[x][y] == true && count_alive(array, x, y) < 2) {
array[x][y] = false;
next[x][y] = true;
}
// live cell with 2 or 3 alive neighbours
if (array[x][y] == true && count_alive(array, x, y) >= 2 && count_alive(array, x, y) <= 3) {
array[x][y] = true;
next[x][y] = false;
}
// live cell with more than 3 alive neighbours
if (array[x][y] == true && count_alive(array, x, y) > 3) {
array[x][y] = false;
next[x][y] = true;
}
// dead cell with exactly three alive neighbours
if (array[x][y] == false && count_alive(array, x, y) == 3) {
array[x][y] = true;
next[x][y] = true;
}
}

}

// FUNCTION TO CHANGE THE COLOR OF THE CELL WRT ALIVE OR DEAD
Expand All @@ -61,7 +205,7 @@ function color_cell(alive, x, y) {
function random_alive(array) {
for (let x = 4; x < countX - 4; x++) {
for (let y = 4; y < countY - 4; y++) {
god = random(-1, 0.1);
god = random(-1, 1);
if (god > 0)
array[x][y] = true;
else array[x][y] = false;
Expand All @@ -72,8 +216,8 @@ function random_alive(array) {
// SETUP CANVAS
function setup() {
background(0);
frameRate(15);
createCanvas(windowWidth, windowHeight);
frameRate(7);
createCanvas(500, 500);
// count the number of horizontal cells
for (let x = cell_mid; x < width; x += cell_size) {
countX++;
Expand All @@ -85,23 +229,26 @@ function setup() {
// initialize board to black
for (let x = 0; x < countX; x++) {
game_of_life[x] = [];
for (let y = 0; y < countY; y++)
next_gen[x] = [];
for (let y = 0; y < countY; y++) {
game_of_life[x][y] = false;
next_gen[x][y] = true;
}
}
for (let x = 0; x < countX; x++) {
for (let y = 0; y < countY; y++) {
let alive = game_of_life[x][y];
color_cell(alive, x, y);
}
}
// random_alive(game_of_life);
random_alive(game_of_life);
}
// ANIMATION
function draw() {
if (!mouseIsPressed) {
for (let x = 1; x < countX - 1; x++) {
for (let y = 1; y < countY - 1; y++) {
change_cell_state(game_of_life, x, y);
change_cell_state(next_gen, game_of_life, x, y);
color_cell(game_of_life[x][y], x, y);
}
}
Expand All @@ -120,4 +267,4 @@ function mouseDragged() {
let indexY = int(mouseY / cell_size);
color_cell(game_of_life[indexX][indexY], indexX, indexY);
game_of_life[indexX][indexY] = true;
}
}

0 comments on commit 095c6c1

Please sign in to comment.