Estoy codificando la función de revelación tratando de usar la recursividad pero tengo un problema, cuando el ciclo se ejecuta en la esquina no encuentra ninguna celda y devuelve un error, he visto que este operador '?' devuelve el valor o indefinido incluso si no hay valor, pero no puedo entender cómo podría incluirse este operador en mi ciclo
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); } } } }Compruebe los límites en los bucles:
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); } } } }