Skip to content

Commit 401098e

Browse files
author
Kevin Nadro
committed
updating maze viz
- cleaned up some code - added more visuals to the array2Dtracer - added logger information to see what the code is doing
1 parent 03760c3 commit 401098e

File tree

2 files changed

+125
-41
lines changed

2 files changed

+125
-41
lines changed

algorithm/etc/create_maze/create_maze/code.js

Lines changed: 117 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ function buildMaze(){
1212

1313
mySet.addElements(width*height);
1414

15+
logger._print("initializing grid (all walls are up)");
1516
// init 'graph'
17+
// each room has two walls, a down and right wall.
1618
for (var i = 0; i < width; i++) {
1719
graph[i] = new Array(height);
1820
for(var j = 0; j < height; j++){
@@ -21,51 +23,90 @@ function buildMaze(){
2123
walls[location] = {down: true, right: true};
2224
visitedMap[location] = false;
2325

26+
// If you can label the rooms with just 2 digits
27+
if (width*height < 100) {
28+
var location_string = location.toString();
29+
30+
G[j*2 + 1][i*3 + 1] = location_string[0];
31+
G[j*2 + 1][i*3 + 2] = location_string[1];
32+
33+
tracer._setData(G);
34+
}
35+
2436
rightWalls.push({x:i, y:j});
2537
downWalls.push({x:i, y:j});
2638
location++;
2739
}
28-
};
40+
}
2941

42+
logger._print("shuffled the walls for random selection");
43+
// Randomly shuffle the walls
3044
var rightWalls = shuffle(rightWalls);
3145
var downWalls = shuffle(downWalls);
46+
47+
// Picking random walls to remove
3248
while(setSize != mySet.elements - 1){
3349
var randomWall = Math.floor((Math.random() * 2) + 1);
3450
if(randomWall === 1 && downWalls.length > 0){
3551
// Down wall
36-
var currNode = downWalls.pop();
37-
var i_x = currNode.x;
38-
var i_y = currNode.y;
52+
var current_room = downWalls.pop();
53+
var i_x = current_room.x;
54+
var i_y = current_room.y;
3955
var i_y_down = i_y + 1;
4056
if(i_y_down < height){
4157
var u = graph[i_x][i_y];
4258
var v = graph[i_x][i_y_down];
59+
tracer._notify(i_y*2 + 1, i_x*3 + 1);
60+
tracer._notify(i_y*2 + 1, i_x*3 + 2);
61+
tracer._notify(i_y_down*2 + 1, i_x*3 + 1);
62+
tracer._notify(i_y_down*2 + 1, i_x*3 + 2);
4363
if(mySet.find(u) != mySet.find(v)){
64+
logger._print('Rooms: ' + u + ' & ' + v + ' now belong to the same set, delete wall between them');
65+
66+
logger._wait();
4467
mySet.setUnion(u,v);
4568
setSize++;
4669
// delete wall
4770
walls[u].down = false;
48-
}
71+
}else{
72+
logger._print('Rooms: ' + u + ' & ' + v + ' would create a cycle! This is not good!');
73+
logger._wait();
74+
}
4975
}
76+
tracer._clear();
5077
}else if(randomWall === 2 && rightWalls.length > 0){
5178
// Right Wall
52-
var currNode = rightWalls.pop();
53-
var i_x = currNode.x;
54-
var i_y = currNode.y;
79+
var current_room = rightWalls.pop();
80+
var i_x = current_room.x;
81+
var i_y = current_room.y;
5582
var i_x_right = i_x + 1;
5683
if(i_x_right < width){
5784
var u = graph[i_x][i_y];
5885
var v = graph[i_x_right][i_y];
59-
if(mySet.find(u) != mySet.find(v)){
86+
tracer._notify(i_y*2 + 1, i_x*3 + 1);
87+
tracer._notify(i_y*2 + 1, i_x*3 + 2);
88+
tracer._notify(i_y*2 + 1, i_x_right*3 + 1);
89+
tracer._notify(i_y*2 + 1, i_x_right*3 + 2);
90+
if(mySet.find(u) != mySet.find(v)){
91+
logger._print('Rooms: ' + u + ' & ' + v + ' now belong to the same set, delete wall between them');
92+
93+
logger._wait();
6094
mySet.setUnion(u,v);
6195
setSize++;
6296
// delete wall
6397
walls[u].right = false;
64-
}
98+
}else{
99+
logger._print('Rooms: ' + u + ' & ' + v + ' would create a cycle! This is not good!');
100+
logger._wait();
101+
}
65102
}
103+
tracer._clear();
66104
}
67105
}
68106

107+
tracer._clear();
108+
109+
logger._print("deleting the walls");
69110
//update deleted walls
70111
for (var i = 0; i < width; i++) {
71112
for(var j = 0; j < height; j++){
@@ -77,27 +118,84 @@ function buildMaze(){
77118
tracer._select(j*2 + 2, i*3 + 1)._wait();
78119
tracer._select(j*2 + 2, i*3 + 2)._wait();
79120
}
80-
// tracer._notify(i, j, G[i][j])._wait();
121+
81122
if(current_wall.right === false){
82123
G[j*2 + 1][i*3 + 3] = ' ';
83124
tracer._select(j*2 +1 , i*3 + 3)._wait();
84125
}
85126
tracer._setData(G);
86127
}
87128
}
129+
logger._print('cleaning up the grid!');
130+
cleanUpGrid(width,height);
131+
132+
// Clear out walls for the start and end locations.
133+
var random_start = Math.floor(Math.random()*width);
134+
var random_end = Math.floor(Math.random()*width);
135+
136+
logger._print('setting the Start (S) & End (E) locations');
137+
138+
// Start Location
139+
G[0][random_start*3 + 1] = ' ';
140+
G[0][random_start*3 + 2] = ' ';
141+
G[1][random_start*3 + 1] = 'S';
142+
143+
// End Location
144+
G[v_end - 1][random_end*3 + 1] = ' ';
145+
G[v_end - 1][random_end*3 + 2] = ' ';
146+
G[v_end - 2][random_end*3 + 1] = 'E';
147+
148+
cleanUpStartLocation(random_start);
149+
cleanUpEndLocation(random_end);
88150

89-
// Start location
90-
G[0][0] = '│';
91-
G[0][1] = ' ';
92-
G[0][2] = ' ';
93-
// End location
94-
G[v_end-1][h_end-1] = '│';
95-
G[v_end-1][h_end-2] = ' ';
96-
G[v_end-1][h_end-3] = ' ';
151+
logger._print('maze is completed!');
152+
153+
// set the data
154+
tracer._setData(G);
155+
}
156+
function cleanUpStartLocation(start){
157+
if(G[0][start*3] === "┬" && G[1][start*3] === '│'){
158+
G[0][start*3] = '┐';
159+
}
160+
if(G[0][start*3 + 3] === "┬" && G[1][start * 3 + 3] === '│'){
161+
G[0][start*3 + 3] = '┌';
162+
}
163+
if(G[0][start*3] === '┌'){
164+
G[0][start*3] = '│';
165+
}
166+
if(G[0][start*3 + 3] === '┐'){
167+
G[0][start*3 + 3] = '│';
168+
}
169+
}
170+
171+
function cleanUpEndLocation(end){
172+
if(G[v_end - 1][end*3] === '┴' && G[v_end - 2][end*3] === '│'){
173+
G[v_end - 1][end*3] = '┘';
174+
}
175+
if(G[v_end - 1][end*3+3] === '┴' && G[v_end - 2][end*3+3] === '│'){
176+
G[v_end - 1][end*3+3] = '└';
177+
}
178+
if(G[v_end - 1][end*3] === '└'){
179+
G[v_end - 1][end*3] = '│';
180+
}
181+
if(G[v_end - 1][end*3 + 3] === '┘'){
182+
G[v_end - 1][end*3 + 3] = '│';
183+
}
184+
}
185+
186+
function cleanUpGrid(width,height){
187+
// Remove room numbers
188+
for (var i = 0; i < width; i++) {
189+
for(var j = 0; j < height; j++){
190+
G[j*2 + 1][i*3 + 1] = ' ';
191+
G[j*2 + 1][i*3 + 2] = ' ';
192+
}
193+
}
97194

98195
// clean up grid for looks
99196
for (var i = 0; i < v_end; i++) {
100197
for(var j = 0; j < h_end; j++){
198+
101199
if(G[i][j] === '├'){
102200
if(G[i][j+1] === ' '){
103201
G[i][j] = '│';
@@ -170,19 +268,6 @@ function buildMaze(){
170268

171269
}
172270
}
173-
174-
G[1][1] = 'S';
175-
G[v_end-1][h_end-2] = 'E';
176-
// last wall clean up for start & end locations
177-
if(G[0][3] === '┬'){
178-
G[0][3] = '┌';
179-
}
180-
if(G[v_end-1][h_end-4] === '┴'){
181-
G[v_end-1][h_end-4] = '┘';
182-
}
183-
184-
// set the data
185-
tracer._setData(G);
186271
}
187272

188273
class disjoint_set {

algorithm/etc/create_maze/create_maze/data.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var tracer = new Array2DTracer ();
1+
var tracer = new Array2DTracer();
2+
var logger = new LogTracer();
23
var n = 6; // rows (change these!)
34
var m = 6; // columns (change these!)
45

@@ -7,9 +8,9 @@ var v_end = n * 3 - (n-1);
78

89
var G = [];
910

10-
for (var i = 0; i < v_end; i++) {
11+
for (var i = 0; i < v_end; i++) { // by row
1112
G[i] = new Array(h_end);
12-
for(var j = 0; j < h_end; j++){
13+
for(var j = 0; j < h_end; j++){ // by column
1314

1415
G[i][j] = ' ';
1516

@@ -21,14 +22,13 @@ for (var i = 0; i < v_end; i++) {
2122
G[i][j] = '└';
2223
}else if(i === v_end - 1 && j === h_end - 1){ // bottom-right corner
2324
G[i][j] = '┘';
24-
}
25-
else if( (j % 3 === 0) && (i % v_end !== 0 && i !== v_end-1 && i % 2 == 1)){
25+
}else if( (j % 3 === 0) && (i % v_end !== 0 && i !== v_end-1 && i % 2 == 1)){
2626
G[i][j] = '│';
2727
}else if (i % 2 === 0){
2828
G[i][j] = '─';
2929
}
3030

31-
if(m > 1){
31+
if(m > 1){ // More than one column
3232
if( j % 3 === 0 && j !== 0 && j !== h_end - 1 && i === 0){
3333
G[i][j] = '┬';
3434
}
@@ -37,7 +37,7 @@ for (var i = 0; i < v_end; i++) {
3737
}
3838
}
3939

40-
if(n > 1){
40+
if(n > 1){ // More than one row
4141
if(i % 2 === 0 && i !== 0 && i !==v_end-1 && j === 0){
4242
G[i][j] = '├';
4343
}
@@ -46,12 +46,11 @@ for (var i = 0; i < v_end; i++) {
4646
}
4747
}
4848

49-
if(n > 1 && m > 1){
49+
if(n > 1 && m > 1){ // More than one row and column
5050
if(i % 2 === 0 && j % 3 === 0 && i !== 0 && j !== 0 && i !== v_end-1 && j !== h_end-1){
5151
G[i][j] = '┼';
5252
}
5353
}
54-
5554
}
5655
}
5756

0 commit comments

Comments
 (0)