-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathminesweeper2.js
More file actions
32 lines (26 loc) · 899 Bytes
/
minesweeper2.js
File metadata and controls
32 lines (26 loc) · 899 Bytes
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
/**
* @author MadhavBahl
* @date 25/01/2019
* Method - First Assign then place values
* Complexity - O(num_bombs) // num_bombs = number of bombs
*/
function makeMineField (posArr, row, col) {
let mineField = [];
// initialize the mineField with zeros
for (let i=0; i<row; i++) {
let thisRow = [];
for (let j=0; j<col; j++)
thisRow.push (0);
mineField.push (thisRow);
}
// Iterate over position array and assign values
for (let pos of posArr) {
mineField [pos[0]][pos[1]] = -1;
for (let i=-1; i<=1; i++)
for (let j=-1; j<=1; j++)
if ((pos[0]+i >= 0 && pos[0]+i < row) && (pos[1]+j >= 0 && pos[1]+j < col))
if (mineField [pos[0]+i][pos[1]+j] !== -1) mineField [pos[0]+i][pos[1]+j]++;
}
console.log (mineField);
}
makeMineField ([[0, 0], [0, 1]], 4, 4);