-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsmaze6b.html
597 lines (511 loc) · 17.7 KB
/
jsmaze6b.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
<?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: 90vw; height: 90vh; min-height: 90vh; padding: 10px; margin: auto auto; background: white; position: relative; }
table.maze { width: 100%; height: 100%; border: none;}
table.maze td { border-width: 1px; border-style: none; border-color: black; }
table.maze td { padding: 0; font-size: 12px; min-height: 1em; min-width: 1em; background-repeat: no-repeat; background-position: center; background-origin: padding-box; background-clip: padding-box; background-size: 50%;}
table.maze td.wall_0 { border-top-style: solid; }
table.maze td.wall_1 { border-right-style: solid; }
table.maze td.wall_2 { border-bottom-style: solid; }
table.maze td.wall_3 { border-left-style: solid; }
table.maze td.current_serial { background-color: #f8f; }
table.maze td.current { background-color: cyan; }
table.maze td.backtrack_0 { background-image: url(images/arrow-up.svg); }
table.maze td.backtrack_1 { background-image: url(images/arrow-right.svg); }
table.maze td.backtrack_2 { background-image: url(images/arrow-down.svg); }
table.maze td.backtrack_3 { background-image: url(images/arrow-left.svg); }
table.maze td.testing { background-image: url(images/question-lg.svg); }
table.maze td.prior_serials { background-color: #ff8; }
</style>
<script type="text/javascript">
// <![CDATA[
// This function will add an onload handler to the body. It's fine to
// add a bunch of handlers, this keeps track of all of them.
function add_body_onload(func) {
var old_body_onload=window.onload;
window.onload=function() {
if (old_body_onload) { old_body_onload(); }
func();
}
}
// 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];
var reverse_dirs=[2,3,0,1];
// walls are horizontal or vertical
var wall_offsets={
h: { x: [0,0,0,0], y: [0,0,1,0] },
v: { x: [0,1,0,0], y: [0,0,0,0] }
};
var wall_types=['h','v','h','v'];
// 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 full_perms =
[[0,1,2,3],[0,1,3,2],[0,2,1,3],[0,2,3,1],[0,3,1,2],[0,3,2,1],
[1,0,2,3],[1,0,3,2],[1,2,0,3],[1,2,3,0],[1,3,0,2],[1,3,2,0],
[2,0,1,3],[2,0,3,1],[2,1,0,3],[2,1,3,0],[2,3,0,1],[2,3,1,0],
[3,0,1,2],[3,0,2,1],[3,1,0,2],[3,1,2,0],[3,2,0,1],[3,2,1,0]];
var xsize=parseInt(args['xsize']);
var ysize=parseInt(args['ysize']);
if (!xsize) xsize=10;
if (!ysize) ysize=10;
// We'll add some empty cells around the outside for bounding
xsize += 2;
ysize += 2;
var right_preference=args['r'];
if (right_preference) {
right_preference = parseFloat(right_preference);
} else {
right_preference = 0.5;
}
if (right_preference<0.0) {
right_preference=0.0;
} else if (right_preference>1.0) {
right_preference=1.0;
}
var straight_preference=args['s'];
if (straight_preference) {
straight_preference = parseFloat(straight_preference);
} else {
straight_preference = 0.0;
}
if (straight_preference<-1.0) {
straight_preference=-1.0;
} else if (straight_preference>1.0) {
straight_preference=1.0;
}
// For each cell, we need to keep state. Walls are booleans,
// and consist of horizontal walls and vertical walls. We can
// determine which walls belong to a cell using wall_offsets
// and wall_types.
// For each cell position we'll keep the following information:
// 1. entry direction
// 2. permutation
// 3. current sequence
// 4. depth
// Only depth needs to be kept in an array for later use
var depths = new Array(xsize);
var serials = new Array(xsize);
var walls = { h: new Array(xsize), v: new Array(xsize+1) }
var maxdepth=0;
function* make_maze() {
var pieces_left= (xsize-2) * (ysize-2);
var serial = 0;
// Make the seed segment
yield* make_maze_helper(
Math.floor(Math.random()*(xsize-3))+2,
Math.floor(Math.random()*(ysize-3))+2,
null, serial++, Math.max(xsize,ysize));
while (pieces_left) {
var next_start=Math.floor(Math.random()*pieces_left);
var empty_found=0, x, y;
for (x=1 ; x<xsize-1 ; x++) {
for (y=1 ; y<ysize-1 ; y++) {
if (serials[x][y]===null) empty_found++;
if (empty_found>next_start) break;
}
if (empty_found>next_start) break;
}
console.log("Pieces left: " + pieces_left + ", next start: " + next_start + ", starting: " + x + "," + y + ", serial: " + serial);
yield { action: 'new_segment', serial: serial };
yield* make_maze_helper(x,y,null,serial++,pieces_left);
}
function* make_maze_helper(x,y,dir,serial,depth) {
pieces_left--;
if (serials[x][y]!=null) {
throw "Serial at " + x + ',' + y + ' is ' + serials[x][y] + ', would set to ' + serial;
}
serials[x][y] = serial;
yield { action: 'move', x: x, y: y, dir: dir };
// remove the wall where we entered
if (dir!==null) {
remove_wall(x,y,reverse_dirs[dir]);
}
if (depth==0) return false;
var i,j,k,new_dir,straightness,these_perms;
if (dir!==null) {
var lr=(Math.random()>=right_preference ? 0 : 1);
// straightness is a number from 0 to 2, with "0" being
// straight first, "1" straight second, and "2" straight
// last. straight_preference is between -1 and 1, with
// -1 being very straight, 0 being neutral, and 1 being
// very curvy. We'll use the beta function to come up
// with some numbers for this.
if (straight_preference < 0.0) {
straightness=get_straightness(-straight_preference);
} else if (straight_preference > 0.0) {
straightness=2-get_straightness(straight_preference);
} else {
straightness=Math.floor(Math.random()*2.999999);
}
these_perms = perms[dir][straightness][lr];
} else {
these_perms = full_perms[Math.floor(Math.random()*24)];
}
for (new_dir of these_perms) {
i=x+deltax[new_dir];
j=y+deltay[new_dir];
// it should always be in bounds, this is a sanity check
if (in_bounds(i,j)) {
if (unvisited(i,j)) {
var ret = yield* make_maze_helper(i,j,new_dir,serial,depth-1);
if (!ret) return false;
yield { action: 'backtrack', x: x, y: y };
} else if (serials[i][j]!==null && serials[i][j]!==serial) {
remove_wall(x,y,new_dir);
yield { action: 'connect', x: i, y: j, dir: new_dir };
return false;
} else {
yield { action: 'try', x: i, y: j };
}
}
}
return true;
}
}
function in_bounds(x,y) {
return (x>=0 && x<xsize && y>=0 && y<ysize);
}
function four_walls(x,y) {
var ret = new Array(4);
for (var dir=0; dir<4; dir++) {
ret[dir] = get_wall(x,y,dir);
}
return ret;
}
function four_walls_as_integer(x,y) {
var ret = 0;
var fw_arr = four_walls(x,y);
for (var dir=0; dir<4; dir++) {
if (fw_arr[dir]) ret |= 1<<dir;
}
return ret;
}
function unvisited(x,y) {
return four_walls(x,y).every(t => t);
}
function get_wall(x,y,dir) {
wall_type = wall_types[dir];
return walls[wall_type][x+wall_offsets[wall_type]['x'][dir]][y+wall_offsets[wall_type]['y'][dir]];
}
function set_wall(x,y,dir,val) {
wall_type = wall_types[dir];
walls[wall_type][x+wall_offsets[wall_type]['x'][dir]][y+wall_offsets[wall_type]['y'][dir]] = val;
}
function remove_wall(x,y,dir) {
set_wall(x,y,dir,false);
}
// seed is 0 -> 1. At 0, we have a regular distribution and at 1
// we have a distribution completely weighted toward 0. Simply, at
// 0 the cutoffs are at 1/3 and 2/3, since 2/3 is half way between
// 1/3 and 1. At .5, the first cutoff is 2/3 and the second is
// 11/12.
// cutoff2 is .5 of remaining space when seed is 0 and 0 of
// remaining space when seed is 1.
function get_straightness(seed) {
var cutoff1 = .333333333 + seed*.666666666
var rnd = Math.random();
if (rnd<cutoff1) {
return 0;
} else {
var cutoff2 = 1.0 - (1.0 - cutoff1) * (.5-seed*.5);
if (rnd<cutoff2) {
return 1;
} else {
return 2;
}
}
}
function test_get_straightness() {
var counts = new Array(11);
for (var seed=0; seed<=10; seed++) {
these_counts = [0,0,0];
for (var x=0; x<50000; x++) {
var straightness = get_straightness(seed/10.0);
these_counts[straightness] += 1;
}
counts[Math.floor(seed)] = these_counts;
}
console.log(counts);
}
function create_table_cells(table, xsize, ysize) {
for (var y=0; y<ysize; y++) {
var tr = document.createElement('tr');
tr.id = 'row_' + y;
tr.className = 'row_' + y;
for (var x=0; x<xsize; x++) {
var td = document.createElement('td');
td.id = 'cell_' + x + '_' + y;
td.className = 'row_' + y + ' col_' + x;
for (var dir=0; dir<4; dir++) {
if (get_wall(x,y,dir)) {
td.classList.add('wall_' + dir);
}
}
tr.appendChild(td);
}
table.appendChild(tr);
}
}
function td_at(x,y) {
return document.getElementById('cell_' + x + '_' + y);
}
function clear_walls() {
// Create the horizontal walls, all set to "true" except
// first and last
walls = { 'h': false, 'v': false };
walls['h'] = new Array(ysize+1);
for (var x=0 ; x<xsize ; x++) {
walls['h'][x] = new Array(ysize+1).fill(false);
}
// Create the vertical walls, all set to "true" except first
walls['v'] = new Array(xsize+1);
// and last
for (var x=0 ; x<xsize+1 ; x++) {
walls['v'][x] = new Array(ysize).fill(false);
}
}
function set_initial_walls() {
// horizontal walls
for (var x=1 ; x<xsize-1 ; x++) {
for (var y=1 ; y<ysize ; y++) {
walls['h'][x][y] = true;
}
}
// vertical walls
for (var x=1 ; x<xsize ; x++) {
for (var y=1 ; y<ysize-1 ; y++) {
walls['v'][x][y] = true;
}
}
}
function init() {
maxdepth=0;
for (var x=0 ; x<xsize ; x++) {
depths[x] = new Array(ysize);
}
for (var x=0 ; x<xsize ; x++) {
serials[x] = new Array(ysize).fill(null);
}
clear_walls();
set_initial_walls();
var table = document.getElementById('maze');
create_table_cells(table, xsize, ysize);
var last_x=null, last_y=null;
var last_try_x=null, last_try_y=null;
var completely_done = false;
var maze_generator = make_maze();
document.addEventListener('keydown', function(ev) {
if (ev.keyCode == 32) {
var next_item = maze_generator.next();
if (completely_done) {
alert("Done");
} else if (next_item.done) {
// open walls for start/end
handle_action({action: 'open_start', x: 1, y: 0, dir: 2});
handle_action({action: 'open_end', x: xsize-2, y: ysize-2, dir: 2});
completely_done = true;
} else if (next_item.value===true || next_item.value===false) {
// ignore this
console.log("Ignoring " + next_item.value);
} else {
var action = next_item.value;
handle_action(action);
}
}
});
function handle_action(action) {
console.log(action);
switch(action.action) {
case 'move':
if (action.dir!==null) {
remove_outbound_wall(last_x, last_y, action.dir);
remove_inbound_wall(action.x, action.y, action.dir);
set_backtrack(action.x, action.y, action.dir);
}
update_current(action.x, action.y);
break;
case 'connect':
remove_outbound_wall(last_x, last_y, action.dir);
remove_inbound_wall(action.x, action.y, action.dir);
break;
case 'new_segment':
clear_backtracks(action.serial-1);
set_prior_serial(action.serial-1);
remove_try();
remove_current();
break;
case 'try':
update_try(action.x, action.y);
break;
case 'backtrack':
update_current(action.x, action.y);
break;
case 'open_start':
remove_outbound_wall(action.x, action.y, action.dir);
remove_inbound_wall(action.x+deltax[action.dir], action.y+deltay[action.dir], action.dir);
break;
case 'open_end':
remove_try();
remove_current();
clear_backtracks();
set_prior_serial();
remove_outbound_wall(action.x, action.y, action.dir);
remove_inbound_wall(action.x+deltax[action.dir], action.y+deltay[action.dir], action.dir);
break;
}
function remove_outbound_wall(x,y,dir) {
if (x!==null && y!==null) {
var td = td_at(x,y);
td.classList.remove('wall_' + dir);
}
}
function remove_inbound_wall(x,y,dir) {
remove_outbound_wall(x,y,reverse_dirs[dir]);
}
function set_prior_serial() {
// we'll add the class prior_serials and remove class
// current_serial
var tds = table.querySelectorAll('td.current_serial');
for (const td of Array.from(tds)) {
td.classList.remove('current_serial');
td.classList.add('prior_serials');
}
}
function set_backtrack(x,y,dir) {
var td = td_at(x,y);
td.classList.add('backtrack_' + reverse_dirs[dir]);
}
function clear_backtracks() {
var tds = table.querySelectorAll('td.backtrack_0,td.backtrack_1,td.backtrack_2,td.backtrack_3');
for (const td of Array.from(tds)) {
td.classList.remove('backtrack_0');
td.classList.remove('backtrack_1');
td.classList.remove('backtrack_2');
td.classList.remove('backtrack_3');
}
}
function remove_try() {
if (last_try_x!==null && last_try_y!==null) {
var last_try_td = td_at(last_try_x, last_try_y);
last_try_td.classList.remove('testing');
last_try_x=null;
last_try_y=null;
}
}
function update_try(x,y) {
remove_try();
last_try_x=x;
last_try_y=y;
var td = td_at(x,y);
td.classList.add('testing');
}
function remove_current() {
if (last_x!==null && last_y!==null) {
var last_td = td_at(last_x, last_y);
last_td.classList.remove('current');
last_x = null;
last_y = null;
}
}
function update_current(x,y) {
remove_try();
remove_current();
last_x=x;
last_y=y;
var td = td_at(x,y);
td.classList.add('current');
td.classList.add('current_serial');
}
}
}
add_body_onload(init);
//]]>
</script>
</head>
<body>
<div id="wrap">
<p>
This is a drunk walk demonstration. Press the
space bar to advance to the next action in building the
maze. The maze is set in a square grid using an HTML table
for display. Parameters are xsize, ysize, l for left/right
preference (0 left, 1 right), and s for straightness preference
(-1 really straight, 0 normal, 1 really curvy).
</p>
<table id="maze" class="maze" cellspacing="0">
</table>
</div>
</body>
</html>