//Maze generation: http://weblog.jamisbuck.org/2011/1/12/maze-generation-recursive-division-algorithm //Pathfinding: https://repl.it/@kaldisberzins/pathfinder-II var size = { s: 8, w: 31, h: 31 }; size.w *= size.s; size.h *= size.s; size.lw = size.w – size.s; size.lh = size.h – size.s; var canvas = document.querySelector('.maze'); var ctx = canvas.getContext('2d'); var get = function (x, y){ return ctx.getImageData(x, y, 1, 1).data; }; var set = function (x, y, data){ ctx.fillStyle = data; ctx.fillRect(x, y, size.s, size.s); }; var copy = function (obj) { return Object.create(obj); }; var random = function (start, end) { var range = (end – start) / size.s; var random = Math.floor(Math.random()*range/2)*2; return ((start/size.s) + random) * size.s; }; var stack, start, end; var generateMaze = function () { canvas.width = size.w; canvas.height = size.h; ctx.fillStyle = 'white'; ctx.fillRect(0, 0, size.w, size.h); start = {x: random(size.s,size.lw), y: 0}; end = {x: random(size.s,size.lw), y: size.lh}; set(start.x,start.y,'green'); set(end.x,end.y,'yellow'); for (var x=0; x size.s && region1.h > size.s){ stack.push(region1); } if (region2.w > size.s && region2.h > size.s) { stack.push(region2); } for (var i=cutStart; i=chosen.x) { newPath.paths.push({ x: chosen.x-size.s, y: chosen.y, dir: 'l' }); } var right = get(chosen.x+size.s, chosen.y); if (right[0] === 255 && last.x<=chosen.x) { newPath.paths.push({ x: chosen.x+size.s, y: chosen.y, dir: 'r' }); } var top = get(chosen.x, chosen.y-size.s); if (top[0] === 255 && last.y>=chosen.y) { newPath.paths.push({ x: chosen.x, y: chosen.y-size.s, dir: 't' }); } if (newPath.paths.length>=2) { newPath.node = true; path.push(newPath); } else if (newPath.paths.length === 0){ //console.log("backtracking…"); ctx.fillStyle = 'blue'; ctx.fillRect(newPath.x, newPath.y, size.s, size.s); for(let i = path.length-1; i>-1; i–){ if(path[i].node && path[i].paths.length !== 0){ break; } else { ctx.fillStyle = 'blue'; ctx.fillRect(path[i].x, path[i].y, size.s, size.s); path.splice(i, 1); } } //console.log("path after manipulation", path); } else { path.push(newPath); } if ((end.x === chosen.x && end.y === chosen.y)){ setTimeout(generateMaze, 1000); } else { requestAnimationFrame(find); } }; addEventListener('load', generateMaze);
//Maze generation: http://weblog.jamisbuck.org/2011/1/12/maze-generation-recursive-division-algorithm
//Pathfinding: https://repl.it/@kaldisberzins/pathfinder-II
var size = {
s: 8,
w: 31,
h: 31
};
size.w *= size.s;
size.h *= size.s;

size.lw = size.w – size.s;
size.lh = size.h – size.s;

var canvas = document.querySelector(‘.maze’);

var ctx = canvas.getContext(‘2d’);

var get = function (x, y){
return ctx.getImageData(x, y, 1, 1).data;
};
var set = function (x, y, data){
ctx.fillStyle = data;
ctx.fillRect(x, y, size.s, size.s);
};
var copy = function (obj) {
return Object.create(obj);
};
var random = function (start, end) {
var range = (end – start) / size.s;
var random = Math.floor(Math.random()*range/2)*2;
return ((start/size.s) + random) * size.s;
};

var stack, start, end;

var generateMaze = function () {
canvas.width = size.w;
canvas.height = size.h;

ctx.fillStyle = ‘white’;
ctx.fillRect(0, 0, size.w, size.h);

start = {x: random(size.s,size.lw), y: 0};
end = {x: random(size.s,size.lw), y: size.lh};

set(start.x,start.y,’green’);
set(end.x,end.y,’yellow’);
for (var x=0; x size.s && region1.h > size.s){
stack.push(region1);
}
if (region2.w > size.s && region2.h > size.s) {
stack.push(region2);
}

for (var i=cutStart; i=chosen.x) {
newPath.paths.push({
x: chosen.x-size.s,
y: chosen.y,
dir: ‘l’
});
}
var right = get(chosen.x+size.s, chosen.y);
if (right[0] === 255 && last.x<=chosen.x) { newPath.paths.push({ x: chosen.x+size.s, y: chosen.y, dir: 'r' }); } var top = get(chosen.x, chosen.y-size.s); if (top[0] === 255 && last.y>=chosen.y) {
newPath.paths.push({
x: chosen.x,
y: chosen.y-size.s,
dir: ‘t’
});
}
if (newPath.paths.length>=2) {
newPath.node = true;
path.push(newPath);
} else if (newPath.paths.length === 0){
//console.log(“backtracking…”);
ctx.fillStyle = ‘blue’;
ctx.fillRect(newPath.x, newPath.y, size.s, size.s);
for(let i = path.length-1; i>-1; i–){
if(path[i].node && path[i].paths.length !== 0){
break;
} else {
ctx.fillStyle = ‘blue’;
ctx.fillRect(path[i].x, path[i].y, size.s, size.s);
path.splice(i, 1);
}
}
//console.log(“path after manipulation”, path);
} else {
path.push(newPath);
}
if ((end.x === chosen.x && end.y === chosen.y)){
setTimeout(generateMaze, 1000);
} else {
requestAnimationFrame(find);
}
};

addEventListener(‘load’, generateMaze);

SPREAD THE LOVE