5

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?!

}
styke
  • 2,116
  • 2
  • 23
  • 51
  • which is it? `cellsX` or `cellsY`? – Joseph Apr 23 '13 at 17:04
  • You are trying to add `i` and `cellsY`. You are checking to see if `cellsX` is an integer, but you never check `cellsY` – Jeff Shaver Apr 23 '13 at 17:05
  • Well, for the last cellY, cellY + 1 will be out of the array bounds. – bfavaretto Apr 23 '13 at 17:06
  • @bfavaretto I will sort the bounding out later. For now, I just need to get the required index. – styke Apr 23 '13 at 17:07
  • 1
    what does `console.log(i + grid.cellsY);` show? What is `game.cells`? – epascarello Apr 23 '13 at 17:07
  • @epascarello it returns a number, although it's strange - say `i = 0` and `grid.cellsY = 50`, `console.log(i + grid.cellsY)` will return 050 – styke Apr 23 '13 at 17:09
  • 2
    @styke then they are strings, not ints. Either make them ints, or parse them using `parseInt(num, 10)` – Jeff Shaver Apr 23 '13 at 17:09
  • Just because it looks like a "number" does not mean it is a number. You have strings, not numbers. `console.log(typeof i, typeof grid.cellsY);` – epascarello Apr 23 '13 at 17:11
  • Bizzare, grid.cellsX and grid.cellsY are calculated with canvas.width / cellsize etc. i is an int from a loop function. How on earth did they become strings I have no clue – styke Apr 23 '13 at 17:13
  • 1
    @styke only one of them has to be a string. If one is a string, the other is automatically converted into a string and they are concatenated. – Jeff Shaver Apr 23 '13 at 17:15
  • @styke did you try the code which I posted in answer? – Zaheer Ahmed Apr 23 '13 at 17:17
  • @ZaheerAhmed Hi Zaheer, not yet, i shouldn't be a string in the first place so I am sorting that out! Thanks for your input though :) – styke Apr 23 '13 at 17:19

1 Answers1

5

There can be two reasons:

  1. In JavaScript variables are of loose-type that's why its good to parse int before arithmetic operation.

    try:

    console.log(game.cells[parseInt(i,10) + parseInt(grid.cellsY,10)]);
    
  2. You are trying to access an array, you need to check whether parseInt(i,10) + parseInt(grid.cellsY,10) index exist in your array or not.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • 1
    It's probably a typo or a wording issue but I can't understand what you mean with "In javascript variable is not by default an int". A JavaScript variable type is that of whatever you assign to it. – Álvaro González Apr 23 '13 at 17:17
  • 2
    JavaScript variables aren't typed at all. And since `+` is used for both adding numbers and concatenating strings, it's a good idea to explicitly convert possible strings to numbers before performing addition. – Blazemonger Apr 23 '13 at 17:21