-
Notifications
You must be signed in to change notification settings - Fork 0
/
mazemaker.js
154 lines (137 loc) · 3.86 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
150
151
152
153
154
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.toGrid = function () {
var h = this.height * 2 + 1;
var w = this.width * 2 + 1;
// Fill out grid and put in walls
var grid = new Array(h);
for (var i = 0; i < h; i++) {
grid[i] = new Array(w);
grid[i][0] = 1;
grid[i][w - 1] = 1;
}
for (var i = 1; i < w - 1; i++) {
grid[0][i] = 1;
grid[h - 1][i] = 1;
}
// Fill in the map
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var item = this.sets[y * this.width + x];
var itemr = this.sets[y * this.width + (x + 1)];
var itemb = this.sets[(y + 1) * this.width + x];
grid[y*2+1][x*2+1] = 0;
grid[y*2+1][x*2+2] = item.right ? 1 : 0;
grid[y*2+2][x*2+1] = item.bottom ? 1 : 0;
grid[y*2+2][x*2+2] = item.right || item.bottom || itemr.bottom || itemb.right ? 1 : 0;
}
}
return grid;
}
}());
var sizes = process.stdout.getWindowSize();
var width = (sizes[0] - 2) >> 2;
var height = (sizes[1] - 1) >> 1;
//width = 10;
//height = 10;
var maze = new Maze(width, height);
var grid = maze.toGrid();
var ansi = require('ansi');
var cursor = ansi(process.stdout);
grid.forEach(function (line, y) {
line.forEach(function (cell, x) {
if (cell) {
var color = (Math.random() * 0x1000000 << 0) | 0x808080
cursor.bg.hex(color.toString(16));
} else {
cursor.bg.reset();
}
process.stdout.write(" ");
});
cursor.bg.reset();
process.stdout.write("\n");
});
// Sample output for a 5x5 maze
// ██████████████████████
// ██ ██ ██
// ██ ██████ ██ ██████
// ██ ██ ██
// ██ ██████ ██ ██████
// ██ ██ ██ ██
// ██ ██████████████ ██
// ██ ██ ██
// ██ ██████ ██████ ██
// ██ ██ ██ ██
// ██████████████████████