-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcell.js
64 lines (57 loc) · 1.22 KB
/
cell.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
const posDiff = {
top: [0, -1],
left: [-1, 0],
right: [1, 0],
bottom: [0, 1]
};
const posFrom = {
top: 'bottom',
left: 'right',
right: 'left',
bottom: 'top'
};
export default class Cell {
constructor (maze, x, y) {
this.maze = maze;
this.x = x;
this.y = y;
this.visited = false;
this.walls = {
top: true,
left: true,
right: true,
bottom: true
};
}
visit (pos) {
this.walls[pos] = false;
this.neighbor(pos).walls[posFrom[pos]] = false;
this.updateBorders();
this.neighbor(pos).updateBorders();
}
updateBorders () {
for (const pos in this.walls) {
if (this.walls[pos]) {
this.$td.classList.add(pos);
} else {
this.$td.classList.remove(pos);
}
}
}
get neighbors () {
return ['top', 'left', 'right', 'bottom'].map(pos => ({
pos,
cell: this.neighbor(pos)
})).filter(neighbor => neighbor.cell);
}
get unvisitedNeighbors () {
return this.neighbors.filter(neighbor => !neighbor.cell.visited);
}
neighbor (pos) {
const { maze } = this;
const [xDiff, yDiff] = posDiff[pos];
const x = this.x + xDiff;
const y = this.y + yDiff;
return maze[y] && maze[y][x];
}
}