Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added Maze creation example #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"etc": {
"list": {
"flood_fill": "Flood Fill",
"cellular_automata": "Cellular Automata"
"cellular_automata": "Cellular Automata",
"create_maze": "Create Maze"
},
"name": "Uncategorized"
}
Expand Down
247 changes: 247 additions & 0 deletions algorithm/etc/create_maze/create_maze/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
function buildMaze(){
var mySet = new disjoint_set();
var width = m;
var height = n;
var setSize = 0;
var graph = [];
var visitedMap = {};
var walls = {};
var rightWalls = [];
var downWalls = [];
var location = 0;

mySet.addElements(width*height);

// init 'graph'
for (var i = 0; i < width; i++) {
graph[i] = new Array(height);
for(var j = 0; j < height; j++){
graph[i][j] = location;

walls[location] = {down: true, right: true};
visitedMap[location] = false;

rightWalls.push({x:i, y:j});
downWalls.push({x:i, y:j});
location++;
}
};

var rightWalls = shuffle(rightWalls);
var downWalls = shuffle(downWalls);
while(setSize != mySet.elements - 1){
var randomWall = Math.floor((Math.random() * 2) + 1);
if(randomWall === 1 && downWalls.length > 0){
// Down wall
var currNode = downWalls.pop();
var i_x = currNode.x;
var i_y = currNode.y;
var i_y_down = i_y + 1;
if(i_y_down < height){
var u = graph[i_x][i_y];
var v = graph[i_x][i_y_down];
if(mySet.find(u) != mySet.find(v)){
mySet.setUnion(u,v);
setSize++;
// delete wall
walls[u].down = false;
}
}
}else if(randomWall === 2 && rightWalls.length > 0){
// Right Wall
var currNode = rightWalls.pop();
var i_x = currNode.x;
var i_y = currNode.y;
var i_x_right = i_x + 1;
if(i_x_right < width){
var u = graph[i_x][i_y];
var v = graph[i_x_right][i_y];
if(mySet.find(u) != mySet.find(v)){
mySet.setUnion(u,v);
setSize++;
// delete wall
walls[u].right = false;
}
}
}
}

//update deleted walls
for (var i = 0; i < width; i++) {
for(var j = 0; j < height; j++){
var current_wall = walls[graph[i][j]];

if(current_wall.down === false){
G[j*2 + 2][i*3 + 1] = ' ';
G[j*2 + 2][i*3 + 2] = ' ';
tracer._select(j*2 + 2, i*3 + 1)._wait();
tracer._select(j*2 + 2, i*3 + 2)._wait();
}
// tracer._notify(i, j, G[i][j])._wait();
if(current_wall.right === false){
G[j*2 + 1][i*3 + 3] = ' ';
tracer._select(j*2 +1 , i*3 + 3)._wait();
}
tracer._setData(G);
}
}

// Start location
G[0][0] = '│';
G[0][1] = ' ';
G[0][2] = ' ';
// End location
G[v_end-1][h_end-1] = '│';
G[v_end-1][h_end-2] = ' ';
G[v_end-1][h_end-3] = ' ';

// clean up grid for looks
for (var i = 0; i < v_end; i++) {
for(var j = 0; j < h_end; j++){
if(G[i][j] === '├'){
if(G[i][j+1] === ' '){
G[i][j] = '│';
}
}

if(G[i][j] === '┤'){
if(G[i][j-1] === ' '){
G[i][j] = '│';
}
}

if(G[i][j] === '┬'){
if(G[i+1][j] === ' '){
G[i][j] = '─';
}
}

if(G[i][j] === '┴'){
if(G[i-1][j] === ' '){
G[i][j] = '─';
}
}

if(G[i][j] === '┼'){
if(G[i][j+1] === ' '&& G[i-1][j] === ' ' && G[i][j-1] !== ' ' && G[i+1][j] !== ' '){
G[i][j] = '┐';
}
else if(G[i][j-1] === ' '&& G[i-1][j] === ' ' && G[i+1][j] !== ' ' && G[i][j+1] !== ' '){
G[i][j] = '┌';
}
else if(G[i][j-1] === ' '&& G[i+1][j] === ' ' && G[i-1][j] !== ' ' && G[i][j+1] !== ' '){
G[i][j] = '└';
}
else if(G[i][j+1] === ' '&& G[i+1][j] === ' ' && G[i-1][j] !== ' ' && G[i][j-1] !== ' '){
G[i][j] = '┘';
}

else if(G[i][j+1] === ' '&& G[i][j-1] === ' ' && (G[i+1][j] === ' ' || G[i-1][j] === ' ')){
G[i][j] = '│';
}
else if(G[i+1][j] === ' '&& G[i-1][j] === ' ' && (G[i][j-1] === ' ' || G[i][j+1] === ' ')){
G[i][j] = '─';
}

else if(G[i][j+1] === ' '&& G[i][j-1] === ' ' ){
G[i][j] = '│';
}
else if(G[i+1][j] === ' '&& G[i-1][j] === ' '){
G[i][j] = '─';
}
else if(G[i+1][j] === ' ' && G[i-1][j] !== ' ' && G[i][j-1] !== ' ' && G[i][j+1] !== ' '){
G[i][j] = '┴';
}

else if(G[i-1][j] === ' ' && G[i+1][j] !== ' ' && G[i][j+1] !== ' ' && G[i][j-1] !== ' '){
G[i][j] = '┬';
}

else if(G[i][j+1] === ' ' && G[i-1][j] !== ' ' && G[i+1][j] !== ' ' && G[i][j-1] !== ' '){
G[i][j] = '┤';
}

else if(G[i][j-1] === ' ' && G[i-1][j] !== ' ' && G[i+1][j] !== ' ' && G[i][j+1] !== ' '){
G[i][j] = '├';
}


}

}
}

G[1][1] = 'S';
G[v_end-1][h_end-2] = 'E';
// last wall clean up for start & end locations
if(G[0][3] === '┬'){
G[0][3] = '┌';
}
if(G[v_end-1][h_end-4] === '┴'){
G[v_end-1][h_end-4] = '┘';
}

// set the data
tracer._setData(G);
}

class disjoint_set {
constructor(){
this.set = [];
this.elements = 0;
}
addElements(numberOfElements){
for(var i = 0; i < numberOfElements; i++){
this.elements++;
this.set.push(-1);
}
}
find(element){
if(this.set[element] < 0){
return element;
}else {
return this.set[element] = this.find(this.set[element]);
}
}
setUnion(_a,_b){
var a = this.find(_a);
var b = this.find(_b);

if(a != b){
var newSize = (this.set[a] + this.set[b]);
if(this.compareSize(a,b)){
this.set[b] = a;
this.set[a] = newSize;
}else{
this.set[a] = b;
this.set[b] = newSize;
}
}
}
compareSize(a,b){
if(this.set[a] === this.set[b]){
return true;
}else if(this.set[a] < this.set[b]){
return true;
}else{
return false;
}
}
}

// http://bost.ocks.org/mike/shuffle/
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}

buildMaze();
58 changes: 58 additions & 0 deletions algorithm/etc/create_maze/create_maze/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var tracer = new Array2DTracer ();
var n = 6; // rows (change these!)
var m = 6; // columns (change these!)

var h_end = m * 4 - (m-1);
var v_end = n * 3 - (n-1);

var G = [];

for (var i = 0; i < v_end; i++) {
G[i] = new Array(h_end);
for(var j = 0; j < h_end; j++){

G[i][j] = ' ';

if(i === 0 && j === 0){ // top-left corner
G[i][j] = '┌';
}else if( i === 0 && j === h_end-1){ // top-right corner
G[i][j] = '┐';
}else if(i === v_end - 1 && j === 0){ // bottom-left corner
G[i][j] = '└';
}else if(i === v_end - 1 && j === h_end - 1){ // bottom-right corner
G[i][j] = '┘';
}
else if( (j % 3 === 0) && (i % v_end !== 0 && i !== v_end-1 && i % 2 == 1)){
G[i][j] = '│';
}else if (i % 2 === 0){
G[i][j] = '─';
}

if(m > 1){
if( j % 3 === 0 && j !== 0 && j !== h_end - 1 && i === 0){
G[i][j] = '┬';
}
if( j % 3 === 0 && j !== 0 && j !== h_end - 1 && i === v_end-1){
G[i][j] = '┴';
}
}

if(n > 1){
if(i % 2 === 0 && i !== 0 && i !==v_end-1 && j === 0){
G[i][j] = '├';
}
if(i % 2 === 0 && i !== 0 && i !==v_end-1 && j === h_end-1){
G[i][j] = '┤';
}
}

if(n > 1 && m > 1){
if(i % 2 === 0 && j % 3 === 0 && i !== 0 && j !== 0 && i !== v_end-1 && j !== h_end-1){
G[i][j] = '┼';
}
}

}
}

tracer._setData(G);
9 changes: 9 additions & 0 deletions algorithm/etc/create_maze/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Create Maze": "Building a maze can be done with using disjoint sets.",
"References": [
"<a href='https://en.wikipedia.org/wiki/Disjoint-set_data_structure'>Disjoint Sets Wikipedia</a>"
],
"files": {
"create_maze": ""
}
}
2 changes: 2 additions & 0 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var Promise = require('es6-promise').Promise;

import path from 'path';
import gulp from 'gulp';
import uglify from 'gulp-uglify';
Expand Down