【发布时间】:2021-03-25 20:13:47
【问题描述】:
我遇到了这个问题。我的功能似乎检查了我的 10x10 板的边界之外。它用于返回 Tile[row, column] 周围的地雷数量,但我得到:
minesweeper.js:152 Uncaught TypeError: Cannot read property '0' of undefined
at getAdjacentMines (minesweeper.js:152)
这是我的功能,谢谢大家的时间。
function getAdjacentMines(row, col) {
let count = 0;
let minRow = row - 1;
let maxRow = row + 1;
let minCol = col - 1;
let maxCol = col + 1;
if (minRow < 0) minRow = 0;
if (maxRow > board.length) maxRow = board.length - 1;
if (minCol < 0) minCol = 0;
if (maxCol > board.length) maxCol = board.length - 1;
for (row = minRow; row <= maxRow; row++) {
for (col = minCol; col <= maxCol; row++) {
if (board[row][col].boom === true) count++;
}
}
return count;
}
【问题讨论】:
标签: javascript minesweeper uncaught-exception