[英]Uncaught TypeError: Cannot read property '1' of undefined in array
var grid = [];
for (var i = 0;i < 6;i++)
{
for (var j = 0; j < 6; j++)
grid[i] = [];
}
//Checking function(how many bombs near)
function check(cx,cy)
{
var numb = 0;
if (grid[cx][cy - 1] == "B") numb++;
if (grid[cx][cy + 1] == "B") numb++;
if (grid[cx - 1][cy] == "B") numb++;
if (grid[cx + 1][cy] == "B") numb++;
if (grid[cx - 1][cy - 1] == "B") numb++;
if (grid[cx + 1][cy - 1] == "B") numb++;
if (grid[cx - 1][cy + 1] == "B") numb++;
if (grid[cx + 1][cy + 1] == "B") numb++;
return numb;
}**
it gives me an error when i try to check every position that contains cx + or - 1, i tried to make the array in other ways but it didn't help. 当我尝试检查每个包含cx +或-1的位置时,它给了我一个错误,我尝试以其他方式制作数组,但没有帮助。 I try to make a minesweeper game, so here i check how many bombs are near each given idex
我尝试做一个扫雷游戏,所以在这里我检查每个给定的等离子点附近有多少枚炸弹
To prevent using indices of an array which have no array or value, you could use a function and hand over the array and indices and return true
, if the value is 'B'
, otherwise false
. 为了避免使用没有数组或值的数组索引,可以使用函数并将数组和索引移交,如果值是
'B'
则返回true
,否则返回false
。
The check takes plase with in
operator and a guard operator ( logical AND &&
) which breaks on false
and continues for true
and checks then the value. 检查使用
in
运算符和后卫运算符( 逻辑AND &&
),后者转为false
并继续为true
,然后检查值。
function checkB(array, i, j) {
return i in array && array[i][j] === 'B';
}
// call
if (checkB(grid, cx, cy - 1)) numb++;
check your function 检查你的功能
when call check(0,0) 通话检查时(0,0)
if (grid[cx][cy - 1] == "B") numb++;
grid[0][0-1]=
格[0] [0-1] =
0-1=?! 0-1 =?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.