Maze 2.0

added 4 diagonals to maze generator
spaced it out a bit

Log in to post a comment.

Canvas.setpenopacity(-1);
const turtle = new Turtle();

// adjustments
var lim = 99; // dimensional (100 = full)
var extent = 99; // (100 best) less frame, more is off screen
var interations = 2*lim*lim; // (maze fullness)
var darken = true; // redraw lines to darken them
var staying = .7; // 0 (curvy) to 1 (straight)
var step = 3; 

// keeping track of where it has been
var hit = new Array();
var max = Math.max(lim, extent);
for (var x = -max; x <= max; x++) {
    hit[x] = new Array();
    for (var y = -max; y <= max; y++) {
        hit[x][y] = false;
    }
}

function logHit() {
    hit[turtle.x()][turtle.y()] = true;
}

function isValidDestination(x,y) {
    if (x < -extent || x > extent || 
        y < -extent || y > extent ||
        hit[x][y]) return false;
    return true;
}

var lastdir;

function pickDirection() {
    var okdirs = new Array();
    if (isValidDestination(turtle.x()+step,turtle.y())) okdirs.push(1);
    if (isValidDestination(turtle.x()-step,turtle.y())) okdirs.push(2);
    if (isValidDestination(turtle.x(),turtle.y()+step)) okdirs.push(3);
    if (isValidDestination(turtle.x(),turtle.y()-step)) okdirs.push(4);
    
    if (isValidDestination(turtle.x()+step,turtle.y()+step)) okdirs.push(5);
    if (isValidDestination(turtle.x()-step,turtle.y()-step)) okdirs.push(6);
    if (isValidDestination(turtle.x()-step,turtle.y()+step)) okdirs.push(7);
    if (isValidDestination(turtle.x()+step,turtle.y()-step)) okdirs.push(8);

    if (okdirs.length == 0) return 0;
    if (Math.random() < staying && okdirs.indexOf(lastdir) != -1) {
        return lastdir;
    } else {
        return okdirs[Math.floor(Math.random()*okdirs.length)];
    }
}

function drawTo(x,y) {
    var fromx = turtle.x();
    var fromy = turtle.y();
    turtle.goto(x,y);
    if (darken) { //redraw at slight offset to darken lines
        var offset = .1;
        turtle.goto(fromx+offset,fromy+offset);
        turtle.goto(x+offset,y+offset);
        turtle.goto(x,y);
    }
}

function move() {
    var newdir = pickDirection();
    if (newdir == 0) { restartTurtle(); return; }
    lastdir = newdir;
    var newx = turtle.x();
    var newy = turtle.y();
    switch(newdir) {
        case 1:
            newx += step;
            break;
        case 2:
            newx -= step;
            break;
        case 3:
            newy += step;
            break;
        case 4:
            newy -= step;
            break;
        case 5:
            newx += step;
            newy += step;
            break;
        case 6:
            newx -= step;
            newy -= step;
            break;
        case 7:
            newx -= step;
            newy += step;
            break;
        case 8:
            newx += step;
            newy -= step;
            break;            
        default:
            console.log('move switch default ('+newdir+') hit oops!');
    }
    drawTo(newx, newy);
    logHit();
}

function restartTurtle() {
    turtle.penup();
    turtle.setx(randInt(-lim/step, lim/step)*step);
    turtle.sety(randInt(-lim/step, lim/step)*step);
    logHit();
    turtle.pendown();
}

// utils
function randInt(min, max) {
    var pick = Math.floor(Math.random() * (max - min + 1) + min);
    return pick;
}

function outline() {
    turtle.penup();
    turtle.goto(-extent,-extent);
    turtle.pendown();
    drawTo(extent,-extent);
    drawTo(extent,extent);
    drawTo(-extent,extent);
    drawTo(-extent,-extent);
}

// init
outline();
restartTurtle();

function walk(i) {
    move();
    return i < interations;
}