So I have an input 2D array with already placed mines in it:
const input = [
[0, 0, '*'],
['*', 0, 0],
[0, '*', 0]
];
What I need to do it's to output changed 2D array with added number to neighbors, but I don't know how can I elegantly access them.
const mineSweeper = (input) => {
for (let row = 0; row < input.length; row++) {
for (let col = 0; col < input[row].length; col++) {
if (input[row][col] === '*') {
// How can I access neighbors from here elegantly?
}
}
}
}
The output should look like that:
const output = [
[1, 2, '*'],
['*', 3, 2],
[2, '*', 1]
];
Any tip? Thanks.