-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsolution.js
102 lines (93 loc) · 3.02 KB
/
solution.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Tests whether a value is valid, which is defined as being
* within range of zero to max value, inclusive.
* @param {Number} value The value to be tested for validity
* @param {Number} maxValue The maximum value allowed
* @returns {Boolean} The validity of the value
*/
function isValidValue(value, maxValue) {
return value > -1 && value < maxValue;
}
/**
* Tests whether a particular cell inside a 2-dimensional array is
* *not* the given value.
* @param {Number} row The row coordinate
* @param {Number} col The column coordinate
* @param {Number[][]} mineField The 2-dimensional array of values
* @param {Number} value The value that is not allowed
* @returns {Boolean}
*/
function isNotValue(row, col, mineField, value) {
return mineField[row][col] !== value;
}
/**
*
* @param {Number[][]} mineCoords An array containing coordinate pairs for bomb locations
* @param {Number} numRows Number of rows in the minefield.
* @param {Number} numCols Number of columns in the minefield
* @returns {Number[][]} Returns a 2-dimensional array with bombs places and
* danger values placed in adjacent spaces.
*/
function mineSweeper(mineCoords, numRows, numCols) {
const mineField = new Array(numRows);
for (let i = 0; i < mineField.length; i++) {
const row = new Array(numCols);
for (let j = 0; j < row.length; j++) {
row[j] = 0;
}
mineField[i] = row;
}
// Curried utility functions to reduce verbosity.
function isValidCol(col) {
return isValidValue(col, numCols);
}
function isValidRow(row) {
return isValidValue(row, numRows);
}
function isNotBomb(row, col) {
return isNotValue(row, col, mineField, -1);
}
mineCoords.forEach(([x, y]) => {
// Mark current bomb location
mineField[x][y] = -1;
// Increase bomb risk values for all adjacent cells to the current bomb,
// unless the adjacent cell already contains another bomb.
const rowAbove = x - 1;
const rowBelow = x + 1;
const colBefore = y - 1;
const colAfter = y + 1;
// Row above bomb,
if (isValidRow(rowAbove)) {
if (isValidCol(colBefore) && isNotBomb(rowAbove, colBefore)) {
mineField[rowAbove][colBefore] += 1;
}
if (isNotBomb(rowAbove, y)) {
mineField[rowAbove][y] += 1;
}
if (isValidCol(colAfter) && isNotBomb(rowAbove, colAfter)) {
mineField[rowAbove][colAfter] += 1;
}
}
// Same row as bomb
if (isValidCol(colBefore) && isNotBomb(x, colBefore)) {
mineField[x][colBefore] += 1;
}
if (isValidCol(colAfter) && isNotBomb(x, colAfter)) {
mineField[x][colAfter] += 1;
}
// Row below bomb
if (isValidRow(rowBelow)) {
if (isValidCol(colBefore) && isNotBomb(rowBelow, colBefore)) {
mineField[rowBelow][colBefore] += 1;
}
if (isNotBomb(rowBelow, y)) {
mineField[rowBelow][y] += 1;
}
if (isValidCol(colAfter) && isNotBomb(rowBelow, colAfter)) {
mineField[rowBelow][colAfter] += 1;
}
}
});
return mineField;
}
export default mineSweeper;