-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgrid.js
54 lines (48 loc) · 1.75 KB
/
grid.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
class Grid {
constructor(first_row) {
this.grid = [first_row.slice(0)];
}
/**
* @param {int} n - Number of rows to add onto grid
*/
addRows(n) {
for (let i = 0; i < n; i++) {
let row = [];
let row_above = this.grid[this.grid.length - 1];
row_above.forEach((tile, t) => {
let left = row_above[t - 1];
let center = tile;
let right = row_above[t + 1];
// `1` is a safe tile
if (left === undefined) left = 1;
if (right === undefined) right = 1;
/**
* A new tile is a _trap_ only in one of the following situations:
*
* - Its _left_ and _center_ tiles are traps, but its _right_ tile is not.
* - Its _center_ and _right_ tiles are traps, but its _left_ tile is not.
* - Only its _left_ tile is a trap.
* - Only its _right_ tile is a trap.
*/
if (
(left === 0 && center === 0 && right === 1) ||
(left === 1 && center === 0 && right === 0) ||
(left === 0 && center === 1 && right === 1) ||
(left === 1 && center === 1 && right === 0)
) {
// Trap tile
row.push(0);
} else {
// Safe tile
row.push(1);
}
});
this.grid.push(row);
}
}
// Assumes safe tiles are stored as a `1`
countSafeTiles() {
return this.grid.map(row => row.reduce((a, b) => a + b, 0)).reduce((a, b) => a + b, 0);
}
}
module.exports = Grid;