-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.js
163 lines (131 loc) · 4.39 KB
/
maze.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// maze.js
//
// Coded by Alexandru Istrate
//
// Maze generation logic by Daniel Shiffman
// https://github.com/CodingTrain/website/tree/main/CodingChallenges/CC_010_Maze_DFS/P5
//
// Depth-first search
// Recursive backtracker
// https://en.wikipedia.org/wiki/Maze_generation_algorithm
function Maze(width, height, mazeWidth) {
this.mazeWidth = mazeWidth;
this.cols = floor(width / this.mazeWidth);
this.rows = floor(height / this.mazeWidth);
this.grid = [];
this.current = null;
this.stack = [];
this.isDone = false;
this.isGameOver = false;
this.won = false;
this.lastVisited = [];
this.setup = function () {
for (let j = 0; j < this.rows; j++) {
for (let i = 0; i < this.cols; i++) {
var cell = new Cell(i, j);
this.grid.push(cell);
var goldChance = random(0, 100);
if (goldChance <= 8) {
cell.hasGold = true;
}
}
}
this.current = this.grid[0];
this.lastVisited = this.grid;
// Set the start
this.grid[0].isStart = true;
};
this.draw = function () {
background(51);
// Display the cells
for (let i = 0; i < this.grid.length; i++) {
this.grid[i].show();
}
this.current.visited = true;
this.current.highlight();
if (this.isDone) {
return;
}
// STEP 1: Check the node's neighbours
let next = this.current.checkNeighbors(this.grid, this.rows, this.cols);
if (next) {
// STEP 1.5: Mark the node as visited
next.visited = true;
// STEP 2: Store the current node so that we can return to it
this.stack.push(this.current);
// STEP 3: Clear the walls to connect with the other parts of the maze
removeWalls(this.current, next);
if (this.lastVisited.length > 1) {
this.lastVisited = this.lastVisited.filter(w => !w.visited);
} else {
this.lastVisited[0].isEnd = true;
// Make sure this cell doesn't get gold on it
this.lastVisited[0].hasGold = false;
}
// STEP 4: Advance the iterator
this.current = next;
} else if (this.stack.length > 0) {
this.current = this.stack.pop();
} else {
this.isDone = true
}
};
this.moveCursor = function (direction) {
switch (direction) {
case LEFT_ARROW:
if (!this.current.walls[3]) {
// console.log("Left");
this.current = this.grid[index(this.current.i - 1, this.current.j, this.rows, this.cols)]
}
break;
case RIGHT_ARROW:
if (!this.current.walls[1]) {
// console.log("Right");
this.current = this.grid[index(this.current.i + 1, this.current.j, this.rows, this.cols)]
}
break;
case UP_ARROW:
if (!this.current.walls[0]) {
// console.log("Up");
this.current = this.grid[index(this.current.i, this.current.j - 1, this.rows, this.cols)]
}
break;
case DOWN_ARROW:
if (!this.current.walls[2]) {
// console.log("Down");
this.current = this.grid[index(this.current.i, this.current.j + 1, this.rows, this.cols)]
}
break;
default:
return false;
}
if (this.current.isEnd) {
this.isGameOver = true;
this.won = true;
}
if (this.current.hasGold) {
// Remove the old gold
this.current.hasGold = false;
gotGold();
}
return true;
}
function removeWalls(a, b) {
let x = a.i - b.i;
if (x === 1) {
a.walls[3] = false;
b.walls[1] = false;
} else if (x === -1) {
a.walls[1] = false;
b.walls[3] = false;
}
let y = a.j - b.j;
if (y === 1) {
a.walls[0] = false;
b.walls[2] = false;
} else if (y === -1) {
a.walls[2] = false;
b.walls[0] = false;
}
};
}