-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell.js
More file actions
74 lines (61 loc) · 1.51 KB
/
cell.js
File metadata and controls
74 lines (61 loc) · 1.51 KB
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
function Cell(state, i, j) {
this.row = i
this.col = j
this.f = 0
this.g = 0
this.h = 0
this.depth = 0
this.cost = 0
this.state = state
this.neighbors = []
this.previous = undefined
this.treeX = 0
this.treeY = 0
this.wall = false;
if (this.state == "w") {
this.wall = true;
}
this.show = function(color) {
if (this.wall) {
fill(wallColor)
noStroke();
square(this.col * cellSize, this.row * cellSize, cellSize);
} else if (color) {
fill(color);
square(this.col * cellSize, this.row * cellSize, cellSize);
}
}
this.addNeighbors = function(grid) {
var i = this.row
var j = this.col
if (i < grid[0].length - 1) {
this.neighbors.push(grid[i + 1][j])
}
if (i > 0) {
this.neighbors.push(grid[i - 1][j])
}
if (j < grid.length - 1) {
this.neighbors.push(grid[i][j + 1])
}
if (j > 0) {
this.neighbors.push(grid[i][j - 1])
}
}
}
Cell.prototype.visit = function(parent) {
if (this.neighbors != null) {
this.left.visit(this);
}
console.log(this.row, this.col);
fill(255);
noStroke();
textAlign(CENTER);
text(this.x, this.treeX, this.treeY);
stroke(255);
noFill();
rect(this.treeX, this.treeY, 20, 20);
line(parent.x, parent.y, this.x, this.y);
if (this.right != null) {
this.right.visit(this);
}
}