Skip to content

Instantly share code, notes, and snippets.

@extrajordanary
Created April 12, 2018 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save extrajordanary/567b4a2b0f3f9b27e148bedb4a770802 to your computer and use it in GitHub Desktop.
Save extrajordanary/567b4a2b0f3f9b27e148bedb4a770802 to your computer and use it in GitHub Desktop.
maximo-gol-review2
var grid;
function setup () {
createCanvas(400, 400);
grid = new Grid(100);
var array = [0, 1, 2];
for (var i = -1; i < array.length; i ++) {
if (i >= 0) {
print(array[i]);
}
}
}
function draw () {
background(0);
grid.draw();
}
class Grid {
constructor (cellSize) {
// update the contructor to take cellSize as a parameter
// use cellSize to calculate and assign values for numberOfColumns and numberOfRows
this.cellSize = cellSize;
this.numberOfRows = floor(height / this.cellSize);
this.numberOfColumns = floor(width / this.cellSize);
this.cells = makeSchoolArray(this.numberOfColumns, this.numberOfRows)
for (var column = 0; column < this.numberOfColumns; column ++) {
for (var row = 0; row < this.numberOfRows; row++) {
this.cells[column][row] = new Cell(column, row, cellSize);
}
}
// print(this.cells);
}
draw () {
for (var column = 0; column < this.numberOfColumns; column ++) {
for (var row = 0; row < this.numberOfRows; row++) {
var cell = this.cells[column][row];
cell.draw();
}
}
}
randomize() {
}
}
class Cell {
constructor (column, row, size) {
this.column = column;
this.row = row;
this.size = size;
this.isAlive = false;
var isAlive = floor(random(2));
if (isAlive == 0){
// this.isAlive = false;
this.setIsAlive(false);
} else {
// this.isAlive = true;
this.setIsAlive(true);
}
}
draw () {
if (this.isAlive) {
fill(255);
} else {
fill(255,0,0);
}
noStroke();
rect(this.column * this.size + 1, this.row * this.size + 1, this.size - 1, this.size - 1);
}
setIsAlive (isAlive) {
this.isAlive = isAlive;
}
}
function makeSchoolArray(cols, rows){
var arr = new Array(cols);
for (var i = 0; i < arr.length; i++){
arr[i] = new Array(rows);
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment