-
Notifications
You must be signed in to change notification settings - Fork 1
/
mazemaker.js
executable file
·150 lines (136 loc) · 3.98 KB
/
mazemaker.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
#!/usr/local/bin/node
Array.prototype.shuffle = function () {
var i, j, tempi, tempj;
i = this.length;
if ( i === 0 ) {
return false;
}
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
tempi = this[i];
tempj = this[j];
this[i] = tempj;
this[j] = tempi;
}
};
Number.prototype.times = function (fn) {
for (var i = 0; i < this; i += 1) {
fn(i);
}
};
var Maze;
(function () {
function get_root(node) {
if (node.parent) {
return get_root(node.parent);
}
return node;
}
function check_wall(first, second) {
var first_root, second_root;
first_root = get_root(first);
second_root = get_root(second);
if (first_root === second_root) {
return true;
}
first_root.parent = second_root;
return false;
}
Maze = function (width, height) {
var sets = [];
var order = [];
for (var i = 0; i < width * height; i += 1) {
sets.push({});
order.push(i * 2);
order.push(i * 2 + 1);
}
order.shuffle();
order.forEach(function (i) {
var first, second;
if (i >= sets.length) {
// Right Wall
first = sets[i - sets.length];
if (i % width === width - 1) {
first.right = true;
// Skip right wall for last column of cells
return;
}
second = sets[i - sets.length + 1];
first.right = check_wall(first, second);
} else {
// Bottom Wall
first = sets[i];
if (Math.floor(i / width) === height - 1) {
first.bottom = true;
// Skip bottom wall for last row of cells
return;
}
second = sets[i + width];
first.bottom = check_wall(first, second);
}
});
this.width = width;
this.height = height;
this.sets = sets;
}
Maze.prototype.display = function () {
var x, y, map, item, grid, line;
grid = [];
map = "██";
line = [1];
for (x = 0; x < this.width; x += 1) {
map += "████";
line.push(1);
line.push(1);
}
grid.push(line);
for (y = 0; y < this.height; y += 1) {
map += "\n██";
line = [1];
for (x = 0; x < this.width; x += 1) {
item = this.sets[y * this.width + x];
map += item.right ? " ██" : " ";
line.push(0);
line.push(item.right ? 1 : 0);
}
grid.push(line);
map += "\n██";
line = [1];
for (x = 0; x < this.width; x += 1) {
item = this.sets[y * this.width + x];
var itemr = this.sets[y * this.width + x + 1];
var itemb = this.sets[y * this.width + x + this.width];
map += item.bottom ? "██" : " ";
line.push(item.bottom ? 1 : 0);
map += (item.right || item.bottom || itemr.bottom || itemb.right) ? "██" : " ";
line.push((item.right || item.bottom || itemr.bottom || itemb.right) ? 1 : 0);
}
grid.push(line);
}
map += "\n";
// sys.p(grid);
return map;
};
}());
if (__filename === process.ARGV[1]) {
var sys = require('sys');
var width, height;
// Check that there are two extra arguments and that they are both non-zero numbers
if (process.ARGV.length == 4 && (width = parseInt(process.ARGV[2])) && (height = parseInt(process.ARGV[3]))) {
sys.print((new Maze(width, height)).display());
} else {
sys.puts("USAGE:\n\t" + process.ARGV[1] + " width height")
}
}
// Sample output for a 5x5 maze
// ██████████████████████
// ██ ██ ██
// ██ ██████ ██ ██████
// ██ ██ ██
// ██ ██████ ██ ██████
// ██ ██ ██ ██
// ██ ██████████████ ██
// ██ ██ ██
// ██ ██████ ██████ ██
// ██ ██ ██ ██
// ██████████████████████