generated from 202309-EKTA-JO-FSW/minesweeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (28 loc) · 872 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function minesweeperBoard(board) {
const numRows = board.length;
const numCols = board[0].length;
const resultBoard = [];
for (let i = 0; i < numRows; i++) {
const row = [];
for (let j = 0; j < numCols; j++) {
if (board[i][j] === 1) {
row.push(9); // 9 represents a mine
} else {
let count = 0;
// Check the 8 adjacent cells for mines
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
const newRow = i + x;
const newCol = j + y;
if (newRow >= 0 && newRow < numRows && newCol >= 0 && newCol < numCols && board[newRow][newCol] === 1) {
count++;
}
}
}
row.push(count);
}
}
resultBoard.push(row);
}
return resultBoard;
}