-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsmaze1.html
245 lines (210 loc) · 8.25 KB
/
jsmaze1.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?xml-stylesheet href="#maze-style" type="text/css"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Maze</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css" id="maze-style">
body, div, table, tr, th, td { margin: 0; padding: 0; border: 0; }
div#wrap { width: 90%; height: 90%; margin: auto auto; }
table.maze { width: 100%; height: 100%; border: 1px solid blue;}
table.maze td { border-width: 0px; border-style: solid; border-color: red; }
table.maze td { padding: 0; font-size: 1px; height: 10px; }
table.maze td.b0000 { border: none; }
table.maze td.b0001 { border-top-width: 1px; }
table.maze td.b0010 { border-right-width: 1px; }
table.maze td.b0011 { border-top-width: 1px; border-right-width: 1px; }
table.maze td.b0100 { border-bottom-width: 1px; }
table.maze td.b0101 { border-top-width: 1px; border-bottom-width: 1px; }
table.maze td.b0110 { border-right-width: 1px; border-bottom-width: 1px; }
table.maze td.b0111 { border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; }
table.maze td.b1000 { border-left-width: 1px; }
table.maze td.b1001 { border-top-width: 1px; border-left-width: 1px; }
table.maze td.b1010 { border-right-width: 1px; border-left-width: 1px; }
table.maze td.b1011 { border-top-width: 1px; border-right-width: 1px; border-left-width: 1px; }
table.maze td.b1100 { border-bottom-width: 1px; border-left-width: 1px; }
table.maze td.b1101 { border-top-width: 1px; border-bottom-width: 1px; border-left-width: 1px; }
table.maze td.b1110 { border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; }
table.maze td.b1111 { border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; }
</style>
<script type="text/javascript">
// <![CDATA[
// Get query parameters from the url. This returns an object like this:
// ?param => args['param']=true
// ?param=value => args['param']=value
// ?param[]=value => args['param']=[value,...]
function get_args() {
var args = new Object();
var query_string=location.search.slice(1);
if (!query_string) return args;
var query_pairs = query_string.split('&');
var pname, pvalue;
for (var i=0 ; i<query_pairs.length ; i++) {
var equal_position=query_pairs[i].indexOf('=');
if (equal_position<0) {
args[my_uri_decoder(query_pairs[i])]=true;
} else {
pname=my_uri_decoder(query_pairs[i].slice(0,equal_position));
pvalue=my_uri_decoder(query_pairs[i].slice(equal_position+1));
// If a name is followed by [], then we'll create an array of
// values. This is good for a multiple-select box
if (pname.match(/\[\]$/)) {
pname=pname.slice(0,-2);
if (!args[pname]) args[pname]=new Array();
args[pname].push(pvalue);
} else {
args[pname]=pvalue;
}
}
}
return args;
}
function my_uri_decoder(v) {
return decodeURIComponent(v.replace(/\+/g,'%20'));
}
var args=get_args();
// Basically, this array "converts" any number 0-15 to its
// binary equivalent as a string, which is useful for the use
// with the above stylesheet.
var to_bin=['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111'];
// Dir: 0 1 2 3
// N E S W
var deltax=[0,1,0,-1];
var deltay=[-1,0,1,0];
// perms array has these indices:
// 1. direction of entering square
// 2. a perm #
// 3. a direction #
// 4. sequence #
//
// Note that the perm# is broken down as follows:
// 0 - go straight first
// 1 - go straight second
// 2 - go straight last
//
// Direction # is:
// 0 - left first
// 1 - right first
//
// Therefore:
//
// 0,0 - straight,right,left
// 0,1 - straight,left,right
// 1,0 - right,straight,left
// 1,1 - left,straight,right
// 2,0 - right,left,straight
// 2,1 - left,right,straight
var perms = [[[[0,1,3],[0,3,1]],[[1,0,3],[3,0,1]],[[1,3,0],[3,1,0]]],
[[[1,2,0],[1,0,2]],[[2,1,0],[0,1,2]],[[2,0,1],[0,2,1]]],
[[[2,3,1],[2,1,3]],[[3,2,1],[1,2,3]],[[3,1,2],[1,3,2]]],
[[[3,0,2],[3,2,0]],[[0,3,2],[2,3,0]],[[0,2,3],[2,0,3]]]];
var xsize=parseInt(args['xsize'])+1;
var ysize=parseInt(args['ysize'])+1;
if (!xsize) xsize=10;
if (!ysize) ysize=10;
// For each cell, we need to keep state. The algorithm is
// recursive, but allowing the stack to deal with the recursion
// will quickly lead to an overflow. Here are the variables
// that we have to keep track of for each cell:
// 1. walls (maze array)
// 2. entry direction
// 3. permutation
// 4. start direction
// 5. current sequence
// 6. depth
// 7. is this outside of the maze
function MazePiece(walls, side) {
this.walls=walls===undefined ? 15 : walls;
this.entry=0;
this.perm=0;
this.start=0;
this.current=0;
this.depth=0;
this.side=side;
}
// Start out with all cells at 15, which is the first four bits
// set. Note that x==0 and y==0 are walls, as are x==xsize
// and y=ysize. Note also the the origin is upper left.
var maze=new Array(xsize+1);
for (var x=0 ; x<=xsize ; x++) {
maze[x]=new Array(ysize+1);
for (var y=0 ; y<=ysize ; y++) {
maze[x][y]=new MazePiece();
}
maze[x][0]=new MazePiece(4, true);
maze[x][ysize]=new MazePiece(1, true);
}
for (var y=1 ; y<ysize ; y++) {
maze[0][y]=new MazePiece(2, true);
maze[xsize][y]=new MazePiece(8, true);
}
// make all corners 0
maze[0][0]=new MazePiece(0, true);
maze[xsize][0]=new MazePiece(0, true);
maze[0][ysize]=new MazePiece(0, true);
maze[xsize][ysize]=new MazePiece(0, true);
var startx=1;
var starty=ysize;
var depth=0;
var maxdepth=0;
// If performed recursively, this algorithm is simple...
function make_maze(x,y,depth,dir) {
// remove the wall where we entered
maze[x][y].walls &= ~(1<<(dir^2));
// update max depth
if (depth>maxdepth) maxdepth=depth;
maze[x][y].depth=depth;
var i,j,k,new_dir;
var lr=(Math.random()>=.5 ? 0 : 1);
var straightness=Math.floor(Math.random()*3);
// Use something like this to make a straighter maze, or
// remove the "2-" to make a really curvy maze.
//var straightness=2-Math.floor(Math.log(Math.random()*19+1));
for (k=0 ; k<3 ; k++) {
new_dir = perms[dir][straightness][lr][k];
i=x+deltax[new_dir];
j=y+deltay[new_dir];
if (maze[i][j].walls==15) {
maze[x][y].walls &= ~(1<<new_dir);
make_maze(i,j,depth+1,new_dir);
}
}
}
make_maze(startx,starty,0,0);
maze[xsize-1][0].walls &= ~(1<<2);
maze[xsize-1][1].walls &= ~(1<<0);
// Set the depth of the side pieces to something interesting
var total_length=xsize+ysize+1;
for (var x=1 ; x<xsize ; x++) {
maze[x][0].depth = Math.floor((x+ysize)/total_length*maxdepth);
maze[x][ysize].depth = Math.floor((x-1)/total_length*maxdepth);
}
for (var y=0 ; y<=ysize ; y++) {
maze[0][y].depth = Math.floor((ysize-y+1)/total_length*maxdepth);
maze[xsize][y].depth = Math.floor((xsize+ysize-y+1)/total_length*maxdepth);
}
//]]>
</script>
</head>
<body>
<div id="wrap">
<h1>JavaScript Maze</h1>
<table class="maze" cellspacing="0">
<script type="text/javascript">
// <![CDATA[
for (var y=0 ; y<=ysize ; y++) {
document.write('<tr>');
for (var x=0 ; x<=xsize ; x++) {
document.write('<td style="background: rgb(0,0,' + (255-Math.floor((maze[x][y].depth/maxdepth)*255)) + ');" class="b' + to_bin[maze[x][y].walls] + '"> </td>');
// document.write('<td class="b' + to_bin[maze[x][y].walls] + '"> </td>');
}
document.write('</tr>');
}
//]]>
</script>
</table>
<p><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></p>
</div>
</body>
</html>