-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
98 lines (88 loc) · 2 KB
/
functions.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
function index(x,y) {
if (x<0 || y<0 || x>cols-1 || y>rows-1) {
return -1;
}
return x + y * cols;
}
function AroundClear(x,y) { // Shorten this function
if(grid[index(x,y-1)] &&
grid[index(x+1,y-1)] &&
grid[index(x+1,y)] &&
grid[index(x+1,y+1)] &&
grid[index(x,y+1)] &&
grid[index(x-1,y+1)] &&
grid[index(x-1,y)] &&
grid[index(x-1,y-1)] &&
!grid[index(x,y-1)].visited &&
!grid[index(x+1,y-1)].visited &&
!grid[index(x+1,y)].visited &&
!grid[index(x+1,y+1)].visited &&
!grid[index(x,y+1)].visited &&
!grid[index(x-1,y+1)].visited &&
!grid[index(x-1,y)].visited &&
!grid[index(x-1,y-1)].visited) {
return true;
}
}
function Cell(x,y) {
this.x = x;
this.y = y;
this.visited = false;
this.checkNeighbors = function() {
var neighbors = [];
if (AroundClear(x,y-2) == true) {
neighbors.push([grid[index(x,y-2)],"UP"]);
}
if (AroundClear(x+2,y) == true) {
neighbors.push([grid[index(x+2,y)],"RIGHT"]);
}
if (AroundClear(x,y+2) == true) {
neighbors.push([grid[index(x,y+2)],"DOWN"]);
}
if (AroundClear(x-2,y) == true) {
neighbors.push([grid[index(x-2,y)],"LEFT"]);
}
if (neighbors.length > 0) {
var r = floor(random(0, neighbors.length));
return neighbors[r];
} else {
return undefined;
}
}
this.show = function() {
noStroke();
if (this == current) {
fill('#ffccdd');
} else if (!this.visited) {
fill(51);
} else {
fill('#aec6cf');
}
if (stack.length == 0) {
if (this.visited) {
fill(255);
}
if (this == start) {
fill('#77dd77');
}
if (this == finish) {
fill('#ff6961');
}
}
rect(this.x * w, this.y * w, w, w);
}
this.bridge = function(a) {
if(a[1] == "UP") {
grid[index(x,y-1)].visited = true;
}
if(a[1] == "RIGHT") {
grid[index(x+1,y)].visited = true;
}
if(a[1] == "DOWN") {
grid[index(x,y+1)].visited = true;
}
if(a[1] == "LEFT") {
grid[index(x-1,y)].visited = true;
}
}
}