Spent some time implementing Conways game of life within the HTML canvas using Javascript.
My cells are not acting as expecting. It seems to be picking up more neighboring cells than are actually there, and removing cells which should not be removed. Any help would be appreciated!
//Globals
var currentCells = [
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
];
var currentGrid = currentCells;
var lastGrid = currentCells;
var cellsX = currentCells[1].length;
var cellsY = currentCells.length;
var canvas_id = "gameGrid";
var gridWidth = 100;
var gridHeight = 50;
var c = document.getElementById("gameGrid");
var ctx = c.getContext("2d");
var cellWidth = gridWidth / cellsX;
var cellHeight = gridHeight / cellsY;
ctx.fillStyle = "yellow";
window.setInterval(step, 1000);
function move(){
for(var a=0;a<cellsX;a++) {
for(var b=0; b<cellsY;b++) {
if (a > 0 && b > 0 && a < cellsY - 1 && b < cellsX){ //centre cells only
var currentNeighbours = neighbourCount(a, b);
if (lastGrid[a][b] == 0){
if(currentNeighbours == 3){
currentGrid[a][b] = 1;
}
}else if (lastGrid[a][b] == 1){
if(currentNeighbours > 3 || currentNeighbours < 2){
currentGrid[a][b] = 0;
//console.log("triggered " + currentNeighbours);
}
}
}
}
}
lastGrid = currentGrid;
}
function neighbourCount(a, b){
var currentNeighbours = 0;
if(lastGrid[a-1][b] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a+1][b] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a][b-1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a][b+1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a-1][b-1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a+1][b+1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a-1][b+1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
if(lastGrid[a+1][b-1] == 1){ //
currentNeighbours = currentNeighbours + 1;
}
//console.log(currentNeighbours);
return currentNeighbours;
}
function Draw(){
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
for(var a=0;a<cellsY;a++) {
var startX = 0;
for(var b=0; b<cellsX;b++) {
startX = cellWidth * b;
if (currentGrid[a][b] == 1){
ctx.fillRect(startX,(a*cellWidth) ,cellWidth,cellWidth);
}
ctx.strokeRect(startX,(a*cellWidth) ,cellWidth,cellWidth);
}
}
}
function step(){
move();
Draw();
}
<canvas id="gameGrid">
</canvas>