-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (171 loc) · 5.27 KB
/
index.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var extend = function(obj, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) obj[key] = src[key];
}
return obj;
}
/**
* Create a maze.
* @param {Number} columnCount - number of columns in the maze
* @param {Number} rowCount - number of rows in the maze
* @return {Object} Object representing a maze
*/
var mazeMaker = function(xDimension, yDimension, config) {
const defaults = {
loops: 0,
verbose: false
}
config = extend(defaults, config);
// Enumerate the movement options and the associated borders to remove
const directions = [
{dx: 0, dy: 1, direction: "up" },
{dx: 1, dy: 0, direction: "right" },
{dx: 0, dy: -1, direction: "down" },
{dx: -1, dy: 0, direction: "left" }
];
const oppositeDirections = {
up: "down",
down: "up",
left: "right",
right: "left"
};
// Mazes with negative dimensions or one cell are not valid
if (xDimension < 1) {
throw new Error("xDimension too small");
}
if (xDimension < 1) {
throw new Error("yDimension too small");
}
if ((xDimension * yDimension) <= 1) {
throw new Error("combined dimensions too small");
}
// Simple maze data structure. All borders start out set and are cleared
// below.
var maze = {};
for (var x = 0; x < xDimension; x++) {
maze[x] = {};
for (var y = 0; y < yDimension; y++) {
maze[x][y] = {
x: x,
y: y,
visited: false,
borders: {
up: true,
right: true,
down: true,
left: true
},
neighbors: {}
};
}
}
// Set neighbors as a convenience for several functions below
for (var x = 0; x < xDimension; x++) {
for (var y = 0; y < yDimension; y++) {
var cell = maze[x][y];
for (var i = 0; i < directions.length; i++) {
var direction = directions[i];
try {
var neighbor =
maze[cell.x + direction.dx][cell.y + direction.dy];
if (neighbor) {
cell.neighbors[direction.direction] = neighbor;
}
} catch (err) { };
}
}
}
// Pick a random starting point and store it to the path
var currentCell = maze[Math.floor(Math.random() * xDimension)][Math.floor(Math.random() * yDimension)]
var path = [ currentCell ];
var unvisitedCount = xDimension * yDimension - 1;
while (unvisitedCount > 0) {
currentCell = path.slice(-1)[0];
if (typeof(currentCell) === 'undefined') {
throw new Error("current cell should not be undefined");
}
currentCell.visited = true;
if (config.verbose) {
console.log("At [" + currentCell.x + ", " + currentCell.y + "]");
}
var nextCells = [];
for (direction in currentCell.neighbors) {
var cell = currentCell.neighbors[direction];
if (cell.visited === false) {
cell.direction = direction;
nextCells.push(cell);
}
}
// If there are no neighboring cells to visit, backtrack
if (nextCells.length === 0) {
if (config.verbose) {
console.log('backtracking');
}
path.pop();
continue;
}
// Otherwise, choose a cell at random, take out the borders, mark it as
// visited, and add it to the path
var nextCell = nextCells[Math.floor(Math.random() * nextCells.length)];
if (config.verbose) {
console.log("removing " + nextCell.direction + " border from [" + currentCell.x + ", " + currentCell.y + "]");
}
if (currentCell.borders[nextCell.direction] !== true) {
throw new Error("current cell should have this border");
}
currentCell.borders[nextCell.direction] = false;
var oppositeBorder = oppositeDirections[nextCell.direction];
if (config.verbose) {
console.log("removing " + oppositeBorder + " border from [" + nextCell.x + ", " + nextCell.y + "]");
}
if (nextCell.borders[oppositeBorder] !== true) {
throw new Error("next cell should have his border");
}
nextCell.borders[oppositeBorder] = false;
unvisitedCount--;
path.push(nextCell);
}
// Validate that the maze is correct by checking if borders match
for (var x = 0; x < xDimension; x++) {
for (var y = 0; y < yDimension; y++) {
var cell = maze[x][y];
if (x === 0) {
if (cell.borders.left !== true) {
throw new Error("cell should have this border");
}
}
if (x === xDimension - 1) {
if (cell.borders.right !== true) {
throw new Error("cell should have this border");
}
}
if (y === 0) {
if (cell.borders.down !== true) {
throw new Error("cell should have this border");
}
}
if (y === yDimension - 1) {
if (cell.borders.up !== true) {
throw new Error("cell should have this border");
}
}
for (direction in cell.neighbors) {
var neighbor = cell.neighbors[direction];
if (cell.borders[direction] !== neighbor.borders[oppositeDirections[direction]]) {
throw new Error("mismatched borders");
}
}
}
}
// Clean up the maze structure
for (var x = 0; x < xDimension; x++) {
for (var y = 0; y < yDimension; y++) {
var cell = maze[x][y];
delete(cell.neighbors);
delete(cell.visited);
}
}
return maze;
}
exports = module.exports = mazeMaker
// vim: ts=2:sw=2:et:ft=javascript