-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathivy.js
39 lines (36 loc) · 1.26 KB
/
ivy.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
function IvyGenerator(topology, sizeX, sizeY) {
this.maze = new Maze(topology, sizeX, sizeY);
this.vertexHasBeenVisited = [];
this.availableBranches = [];
// start around the borders
Array.prototype.push.apply(this.availableBranches, this.maze.getBorderBranches());
if (this.availableBranches.length === 0) {
// some topologies don't have borders
// start in a random spot
var vertexCount = this.maze.getVertexCount();
if (vertexCount > 0) {
var startingVertex = util.randomInt(vertexCount);
this.visitVertex(startingVertex);
}
}
}
IvyGenerator.prototype.visitVertex = function(vertex) {
this.vertexHasBeenVisited[vertex] = true;
this.maze.vertexColors[vertex] = Maze.FILLED;
var branches = this.maze.vertexToBranches(vertex);
Array.prototype.push.apply(this.availableBranches, branches);
};
IvyGenerator.prototype.getOptions = function() {
if (this.availableBranches.length === 0) return null;
return {
type: "branch",
values: this.availableBranches,
};
};
IvyGenerator.prototype.doOption = function(index) {
var branch = util.popIndex(this.availableBranches, index);
if (!this.vertexHasBeenVisited[branch.vertex]) {
this.maze.edgeColors[branch.edge] = Maze.FILLED;
this.visitVertex(branch.vertex);
}
};