|
| 1 | + |
| 2 | + |
| 3 | +# Day 27 - Array Series Part 10: The Minesweeper Problem |
| 4 | + |
| 5 | +**Question** -- Given the position of bombs, number of rows and number of columns, prepare the mine field for the minesweeper game. |
| 6 | + |
| 7 | +**Example** |
| 8 | + |
| 9 | +``` |
| 10 | +input: |
| 11 | + position of bombs - [[0, 0], [0, 1]] |
| 12 | + number of columns in mine field = 4 |
| 13 | + number of rows in mine field = 4 |
| 14 | +output: |
| 15 | +[ |
| 16 | + [-1, -1, 1, 0], |
| 17 | + [ 2, 2, 1, 0], |
| 18 | + [ 0, 0, 0, 0], |
| 19 | + [ 0, 0, 0, 0] |
| 20 | +] |
| 21 | +``` |
| 22 | + |
| 23 | +**Please Note that** |
| 24 | + |
| 25 | +- Positions having -1 represents a bomb |
| 26 | +- 0 indicates **no bomb in vicinity** |
| 27 | +- any other positive number n represents **n bombs in the vicinity** |
| 28 | + |
| 29 | +Vicinity includes direct horizontal/vertical/diagonal neighboring position |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +# Solution |
| 34 | + |
| 35 | +## JavaScript Implementation |
| 36 | + |
| 37 | +### [Solution 1 (First Assign then place values)](./JavaScript/minesweeper.js) |
| 38 | + |
| 39 | +```js |
| 40 | +/** |
| 41 | + * @author MadhavBahlMD |
| 42 | + * @date 25/01/2019 |
| 43 | + * Method - First Assign then place values |
| 44 | + * Complexity - O(row*col) // row and col are the number of rows and number of columns |
| 45 | + */ |
| 46 | + |
| 47 | +function makeMineField (posArr, row, col) { |
| 48 | + let mineField = []; |
| 49 | + |
| 50 | + // initialize the mineField with zeros |
| 51 | + for (let i=0; i<row; i++) { |
| 52 | + let thisRow = []; |
| 53 | + for (let j=0; j<col; j++) |
| 54 | + thisRow.push (0); |
| 55 | + mineField.push (thisRow); |
| 56 | + } |
| 57 | + |
| 58 | + // Iterate over position array and put -1 at those positions in the minefield |
| 59 | + for (let pos of posArr) { |
| 60 | + mineField [pos[0]][pos[1]] = -1; |
| 61 | + } |
| 62 | + |
| 63 | + // Iterate over each element and complete the mine field |
| 64 | + for (let i=0; i<row; i++) { |
| 65 | + for (let j=0; j<col; j++) { |
| 66 | + if (mineField [i][j] !== -1) |
| 67 | + mineField[i][j] = assignValue (mineField, i, j, row, col); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + console.log (mineField); |
| 72 | +} |
| 73 | + |
| 74 | +function assignValue (mineField, thisRow, thisCol, row, col) { |
| 75 | + let count = 0; |
| 76 | + |
| 77 | + // Check for bombs in all 3 rows |
| 78 | + for (let i=-1; i<=1; i++) |
| 79 | + for (let j=-1; j<=1; j++) |
| 80 | + if ((thisRow+i >= 0 && thisRow+i < row) && (thisCol+j >= 0 && thisCol+j < col)) |
| 81 | + if (mineField [thisRow+i][thisCol+j] === -1) count++; |
| 82 | + |
| 83 | + return count; |
| 84 | +} |
| 85 | + |
| 86 | +makeMineField ([[0, 0], [0, 1]], 4, 4); |
| 87 | +``` |
| 88 | + |
| 89 | +### [Solution 2 (Assign and place values simultaneously)](./JavaScript/minesweeper2.js) |
| 90 | + |
| 91 | +```js |
| 92 | +/** |
| 93 | + * @author MadhavBahlMD |
| 94 | + * @date 25/01/2019 |
| 95 | + * Method - First Assign then place values |
| 96 | + * Complexity - O(num_bombs) // num_bombs = number of bombs |
| 97 | + */ |
| 98 | + |
| 99 | +function makeMineField (posArr, row, col) { |
| 100 | + let mineField = []; |
| 101 | + |
| 102 | + // initialize the mineField with zeros |
| 103 | + for (let i=0; i<row; i++) { |
| 104 | + let thisRow = []; |
| 105 | + for (let j=0; j<col; j++) |
| 106 | + thisRow.push (0); |
| 107 | + mineField.push (thisRow); |
| 108 | + } |
| 109 | + |
| 110 | + // Iterate over position array and assign values |
| 111 | + for (let pos of posArr) { |
| 112 | + mineField [pos[0]][pos[1]] = -1; |
| 113 | + |
| 114 | + for (let i=-1; i<=1; i++) |
| 115 | + for (let j=-1; j<=1; j++) |
| 116 | + if ((pos[0]+i >= 0 && pos[0]+i < row) && (pos[1]+j >= 0 && pos[1]+j < col)) |
| 117 | + if (mineField [pos[0]+i][pos[1]+j] !== -1) mineField [pos[0]+i][pos[1]+j]++; |
| 118 | + } |
| 119 | + |
| 120 | + console.log (mineField); |
| 121 | +} |
| 122 | + |
| 123 | +makeMineField ([[0, 0], [0, 1]], 4, 4); |
| 124 | +``` |
0 commit comments