Im building John Conways game of life with HTML5 canvas. It has a gameOfLife object which contains an array with all the cells and their states (dead/alive). Here is the code that builds a cell:
function cell(x,y){
this.x = x;
this.y = y;
this.isAlive = false;
}
I am exploring ways of checking a cells surrounding cells states. As I understand, one way is to iterate through the array and find a cell with coordinates that match as being around the currently checked cell.
I was thinking of going about a different way. By adding and subtracting the number of cells (with small variations of +1 and -1) on the Y (and the X) axis from the index of the cell being evaluated, you should be able to come up with the index of any top left, left, bottom left, top right, right, bottom right cell.
I haven't been able to test this idea though, as it doesn't let me get the desired index:
So, in my update loop:
//I know that there is a cell at the index of exampleIndex + cellsY
exampleIndex = 200;
game.getLivingNeighbours(exampleIndex);
function getLivingNeighbours(i){
console.log(i) //Logs an integer
console.log(grid.cellsY) //Logs an integer
console.log(game.cells[i + grid.cellsY]); //Undefined?!
}