我正在撰寫顯示函式以嘗試使用遞回,但我遇到了一個問題,當回圈在拐角處運行時,它找不到任何單元格并回傳錯誤,我已經看到這個運算子“?。” 即使沒有值也回傳值或未定義,但我不知道如何將這個運算子包含在我的回圈中
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);
}
}
}
}
這是控制臺中顯示的錯誤
uj5u.com熱心網友回復:
檢查回圈中的邊界:
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);
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475128.html
標籤:javascript 打字稿 递归
