【问题标题】:Minesweeper : Problem to recover the mines adjacent to a tileMinesweeper : 恢复与瓷砖相邻的地雷的问题
【发布时间】: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


    【解决方案1】:

    欢迎来到堆栈溢出!

    你似乎在这部分有错字

    for (row = minRow; row <= maxRow; row++) {
        for (col = minCol; col <= maxCol; row++) {
            if (board[row][col].boom === true) count++;
        }
    }
    

    必须是:

    //                                 v here
    for (col = minCol; col <= maxCol; col++) {
    

    不是:

     for (col = minCol; col <= maxCol; row++) {
    

    这意味着“行”将不断增加,直到达到索引 10,然后它会尝试读取第 10 行(未定义)的“col”(将是 0)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多