-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
93 lines (91 loc) · 2.8 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<title></title>
</head>
<body>
<pre id="out"></pre>
</body>
</html>
<script type="text/javascript">
function maze(x, y) {
var n = x * y - 1;
if (n < 0) {
alert("illegal maze dimensions");
return;
}
var horiz = [];
var verti = [];
for (var j = 0; j < x + 1; j++) (horiz[j] = []), (verti = []);
for (var j = 0; j < x + 1; j++)
(verti[j] = []),
(here = [
Math.floor(Math.random() * x),
Math.floor(Math.random() * y),
]),
(path = [here]),
(unvisited = []);
for (var j = 0; j < x + 2; j++) {
unvisited[j] = [];
for (var k = 0; k < y + 1; k++)
unvisited[j].push(
j > 0 &&
j < x + 1 &&
k > 0 &&
(j != here[0] + 1 || k != here[1] + 1)
);
}
while (0 < n) {
var potential = [
[here[0] + 1, here[1]],
[here[0], here[1] + 1],
[here[0] - 1, here[1]],
[here[0], here[1] - 1],
];
var neighbors = [];
for (var j = 0; j < 4; j++)
if (unvisited[potential[j][0] + 1][potential[j][1] + 1])
neighbors.push(potential[j]);
if (neighbors.length) {
n = n - 1;
next = neighbors[Math.floor(Math.random() * neighbors.length)];
unvisited[next[0] + 1][next[1] + 1] = false;
if (next[0] == here[0])
horiz[next[0]][(next[1] + here[1] - 1) / 2] = true;
else verti[(next[0] + here[0] - 1) / 2][next[1]] = true;
path.push((here = next));
} else here = path.pop();
}
console.log(x, y, horiz, verti);
return { x: x, y: y, horiz: horiz, verti: verti };
}
function display(m) {
var text = [];
for (var j = 0; j < m.x * 2 + 1; j++) {
var line = [];
if (0 == j % 2)
for (var k = 0; k < m.y * 4 + 1; k++)
if (0 == k % 4) line[k] = "x";
else if (j > 0 && m.verti[j / 2 - 1][Math.floor(k / 4)])
line[k] = " ";
else line[k] = "-";
else
for (var k = 0; k < m.y * 4 + 1; k++)
if (0 == k % 4)
if (k > 0 && m.horiz[(j - 1) / 2][k / 4 - 1]) line[k] = " ";
else line[k] = "|";
else line[k] = " ";
// for creating openings in the first and the last line
if (0 == j) line[1] = line[2] = line[3] = " ";
if (m.x * 2 - 1 == j) line[4 * m.y] = " ";
// console.log(line);
text.push(line.join("") + "\r\n");
// text.push(line);
}
console.log(text);
return text.join("");
}
document.getElementById("out").innerHTML = display(maze(10, 15));
</script>
</html>