xxxxxxxxxx
165
let size = 10;
let rows = 60;
let cols = 60;
let generation;
let input;
let runGame;
let runAnt;
var cells = [];
class Cell {
constructor(x, y) {
this.alive = false;
this.x = x;
this.y = y;
}
}
class Ant {
// direction increases "clockwise": left --, right ++
// 0 1 2 3
// Never Eat Sea Weed
// North East South West
constructor(x, y, direction) {
this.direction = direction;
this.x = x;
this.y = y;
}
step(wasAlive) {
if (wasAlive) {
// black
this.direction--;
} else {
this.direction++;
}
this.direction = (4 + this.direction) % 4; // handles negatives as well.
switch (this.direction) {
case 0:
this.y--;
break;
case 1:
this.x++;
break;
case 2:
this.y++;
break;
case 3:
this.x--;
break;
}
this.x = (rows + this.x) % rows;
this.y = (cols + this.y) % cols;
}
}
let ANT;
function setup() {
cols = int(windowHeight / size);
rows = int(windowWidth / size);
var myCanvas = createCanvas(rows * size + 1, cols * size + 1);
myCanvas.parent("canvas");
myCanvas.mouseClicked(editBoard);
reset();
}
function draw() {
document.getElementById("stats").innerHTML = "Generations: "+generation+"<br>Frame Rate:"+((int)(frameRate()));
runGame = document.getElementById("lifeCheckBox").checked;
runAnt = document.getElementById("antCheckBox").checked;
input = !document.getElementById("editCheckBox").checked;
background(255);
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
stroke(0, 0, 0, 7);
push();
if (cells[i][j].alive) fill(0);
else noFill();
rect(i * size, j * size, size, size);
pop();
}
}
push();
fill(112, 255, 112);
rect(ANT.x * size, ANT.y * size, size, size);
pop();
if (!input) {
if (runGame) {
cells = gol();
}
if (runAnt) {
ant();
}
generation++;
}
}
function gol() {
let thisGen = [];
for (var x = 0; x < rows; x++) {
thisGen[x] = [];
for (var y = 0; y < cols; y++) {
thisGen[x][y] = new Cell(x, y);
var neighbors = 0;
for (var xoff = -1; xoff <= 1; xoff++) {
for (var yoff = -1; yoff <= 1; yoff++) {
let xIndex = x + xoff;
let yIndex = y + yoff;
if (xoff == 0 && yoff == 0) continue;
xIndex = (rows + xIndex) % rows;
yIndex = (cols + yIndex) % cols;
if (cells[xIndex][yIndex].alive) {
neighbors++;
}
}
}
/*
Any live cell with fewer than two live neighbors dies, as if by underpopulation.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by overpopulation.
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
*/
thisGen[x][y].alive =
neighbors == 3 || (cells[x][y].alive && neighbors == 2);
}
}
return thisGen;
}
function ant() {
ANT.step(cells[ANT.x][ANT.y].alive);
cells[ANT.x][ANT.y].alive = !cells[ANT.x][ANT.y].alive;
}
function editBoard() {
if (input) {
let xIndex = floor(mouseX / size);
let yIndex = floor(mouseY / size);
cells[xIndex][yIndex].alive = !cells[xIndex][yIndex].alive;
}
}
function reset() {
generation = 0;
ANT = new Ant(int(rows / 2), int(cols / 2), 0);
for (var i = 0; i < rows; i++) {
cells[i] = [];
for (var j = 0; j < cols; j++) {
cells[i][j] = new Cell(i, j);
}
}
}