-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.js
More file actions
148 lines (130 loc) · 2.56 KB
/
helper.js
File metadata and controls
148 lines (130 loc) · 2.56 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function showGoal() {
fill(theme.goal);
noStroke();
ellipse(goal.c * size + size / 2, goal.r * size + size / 2, size / 2);
}
function showCoins() {
for (const i in coins) {
// console.log('make it rain');
if (coins[i] === true) {
noStroke();
fill(theme.player);
ellipse(grid[i].c * size + size / 2, grid[i].r * size + size / 2, size / 6);
}
}
}
function randomMap() {
for (const cell of grid) {
let n = cell.checkNeighbors();
if (n)
removeWalls(cell, n);
}
}
function maze(instant) {
if (instant) {
while (true) {
current.visited = true;
let next = current.checkNeighbors();
if (next) {
stack.push(current);
removeWalls(current, next);
current = next;
} else if (stack.length > 0) {
current = stack.pop();
} else {
// noLoop();
for (const cell of grid) {
cell.visited = false;
}
break;
}
}
} else {
current.visited = true;
let next = current.checkNeighbors();
if (next) {
stack.push(current);
removeWalls(current, next);
current = next;
} else if (stack.length > 0) {
current = stack.pop();
} else {
for (const cell of grid) {
cell.visited = false;
}
// noLoop();
}
}
}
function getIndex(r, c) {
if (c > cols - 1 || r > rows - 1 || r < 0 || c < 0)
return -1;
return c + cols * r;
}
function removeWalls(a, b) {
let x = a.c - b.c;
let y = a.r - b.r;
if (x === 1) {
a.walls[3] = false;
b.walls[1] = false;
}
if (x === -1) {
a.walls[1] = false;
b.walls[3] = false;
}
if (y === 1) {
a.walls[0] = false;
b.walls[2] = false;
}
else if (y === -1) {
a.walls[2] = false;
b.walls[0] = false;
}
}
function manhattanDistance(c1, c2) {
return abs(c1.c - c2.c) + abs(c1.r - c2.r)
}
function showPath(path) {
strokeWeight(size / 8);
stroke(theme.path);
beginShape();
noFill();
for (const cell of path) {
// fill(0, 255, 0);
vertex(cell.c * size + size / 2, cell.r * size + size / 2);
}
endShape();
}
function getDirection(c1, c2) {
let x = c2.c - c1.c;
let y = c2.r - c1.r;
return { x, y };
}
function highlightCells(cells) {
for (const cell of cells) {
let x = cell.c * size;
let y = cell.r * size;
noStroke();
fill(0, 255, 255, 60);
rect(x, y, size, size);
}
}
function areNeighbors(c1, c2) {
if (c1 && c2) {
for (const n of c1.neighbors) {
if (n == c2)
return true;
}
}
return false;
}
function displayHeuristics() {
for (const cell of grid) {
let x = cell.c * size + size / 2;
let y = cell.r * size + size / 2;
textAlign(CENTER, CENTER);
color(255, 0, 0);
noFill();
text(cell.heuristic, x, y);
}
}