如何解决扫雷艇的边角问题?
how to fix the corner problem in a minesweeper?
我正在尝试使用递归编写显示函数,但我遇到了一个问题,当循环在拐角处运行时,它找不到任何单元格并且 returns 出现错误,我已经看到这个运算符 '?.' returns 值或未定义,即使没有值,但我不知道如何将此运算符包含在我的循环中
export function reveal(boardWithMines: CellEnum[][], boardWithOutMines: CellEnum[][], x: number, y: number) {
if (boardWithOutMines[x][y] === CellEnum.Hidden || boardWithMines[x][y] === CellEnum.Cero) {
boardWithOutMines[x][y] = boardWithMines[x][y];
for (let xOffset = -1; xOffset <= 1; xOffset++) {
for (let yOffset = -1; yOffset <= 1; yOffset++) {
reveal(boardWithMines, boardWithOutMines, x + xOffset, y + yOffset);
}
}
}
}
This is the error that shows up in the console
检查循环中的边界:
export function reveal(boardWithMines: CellEnum[][], boardWithOutMines: CellEnum[][], x: number, y: number) {
if (boardWithOutMines[x][y] === CellEnum.Hidden || boardWithMines[x][y] === CellEnum.Cero) {
boardWithOutMines[x][y] = boardWithMines[x][y];
for (let i = Math.max(0, x-1), iMax = Math.min(boardWithMines.length, x+2); i < iMax; ++i) {
for (let j = Math.max(0, y-1), jMax = Math.min(boardWithMines[i].length, y+2); j < jMax; ++j) {
reveal(boardWithMines, boardWithOutMines, i, j);
}
}
}
}
我正在尝试使用递归编写显示函数,但我遇到了一个问题,当循环在拐角处运行时,它找不到任何单元格并且 returns 出现错误,我已经看到这个运算符 '?.' returns 值或未定义,即使没有值,但我不知道如何将此运算符包含在我的循环中
export function reveal(boardWithMines: CellEnum[][], boardWithOutMines: CellEnum[][], x: number, y: number) {
if (boardWithOutMines[x][y] === CellEnum.Hidden || boardWithMines[x][y] === CellEnum.Cero) {
boardWithOutMines[x][y] = boardWithMines[x][y];
for (let xOffset = -1; xOffset <= 1; xOffset++) {
for (let yOffset = -1; yOffset <= 1; yOffset++) {
reveal(boardWithMines, boardWithOutMines, x + xOffset, y + yOffset);
}
}
}
}
This is the error that shows up in the console
检查循环中的边界:
export function reveal(boardWithMines: CellEnum[][], boardWithOutMines: CellEnum[][], x: number, y: number) {
if (boardWithOutMines[x][y] === CellEnum.Hidden || boardWithMines[x][y] === CellEnum.Cero) {
boardWithOutMines[x][y] = boardWithMines[x][y];
for (let i = Math.max(0, x-1), iMax = Math.min(boardWithMines.length, x+2); i < iMax; ++i) {
for (let j = Math.max(0, y-1), jMax = Math.min(boardWithMines[i].length, y+2); j < jMax; ++j) {
reveal(boardWithMines, boardWithOutMines, i, j);
}
}
}
}