作者:mobiledu2502905163 | 来源:互联网 | 2022-12-24 13:31
ImbuildingJohnConwaysgameoflifewithHTML5canvas.IthasagameOfLifeobjectwhichcontainsa
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:
我正在用HTML5 canvas构建John Conways的生活游戏。它有一个gameOfLife对象,它包含一个包含所有细胞及其状态(死/活)的数组。下面是构建单元格的代码:
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.
我想换一种方式。通过增加和减少细胞的数量(用小的变化+ 1和- 1)在Y轴(X)指数的细胞被评估,您应该能够想出任何左上角的指数,左,左下角,右上角,右下角单元格。
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?!
}
1 个解决方案