Mazey Xmas

c-c-c-combo!

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(-1);

// Global code will be evaluated once.
const turtle = new Turtle();


// adjustments
var lim = 100; // dimensional (100 = full)
var extent = 90; // (100 best) less frame, more is off screen
var interations = 6*lim*lim; // (maze fullness)
var darken = false; // redraw lines to darken them
var staying = .92 // 0 (curvy) to 1 (straight)


var Letter = new Array();
Letter['A'] = new Array(0,0,0.5,1,0.75,0.5,0.25,0.5,0.75,0.5,1,0);
Letter['B'] = new Array(0,0,0,1,0.625,1,0.75,0.875,0.75,0.625,0.625,0.5,0,0.5,0.625,0.5,0.75,0.375,0.75,0.125,0.625,0,0,0);
Letter['C'] = new Array(0.75,0.125,0.625,0,0.125,0,0,0.125,0,0.875,0.125,1,0.625,1,0.75,0.875);
Letter['D'] = new Array(0,0,0,1,0.625,1,0.75,0.875,0.75,0.125,0.625,0,0,0);
Letter['E'] = new Array(0.75,0,0,0,0,0.5,0.75,0.5,0,0.5,0,1,0.75,1);
Letter['F'] = new Array(0,0,0,0.5,0.75,0.5,0,0.5,0,1,0.75,1);
Letter['G'] = new Array(0.75,0.5,0.625,0.5,0.75,0.5,0.75,0.125,0.625,0,0.125,0,0,0.125,0,0.875,0.125,1,0.625,1,0.75,0.875);
Letter['H'] = new Array(0,0,0,1,0,0.5,0.75,0.5,0.75,1,0.75,0);
Letter['I'] = new Array(0,0,0.25,0,0.125,0,0.125,1,0,1,0.25,1);
Letter['J'] = new Array(0,0.125,0.125,0,0.375,0,0.5,0.125,0.5,1);
Letter['K'] = new Array(0,0,0,1,0,0.5,0.75,1,0,0.5,0.75,0);
Letter['L'] = new Array(0,0,0,1,0,0,0.75,0);
Letter['M'] = new Array(0,0,0,1,0.5,0,1,1,1,0);
Letter['N'] = new Array(0,0,0,1,0.75,0,0.75,1);
Letter['O'] = new Array(0.75,0.125,0.625,0,0.125,0,0,0.125,0,0.875,0.125,1,0.625,1,0.75,0.875,0.75,0.125);
Letter['P'] = new Array(0,0,0,1,0.625,1,0.75,0.875,0.75,0.625,0.625,0.5,0,0.5);
Letter['Q'] = new Array(0.75,0.125,0.625,0,0.125,0,0,0.125,0,0.875,0.125,1,0.625,1,0.75,0.875,0.75,0.125,0.875,0);
Letter['R'] = new Array(0,0,0,1,0.625,1,0.75,0.875,0.75,0.625,0.625,0.5,0,0.5,0.625,0.5,0.875,0);
Letter['S'] = new Array(0,0.125,0.125,0,0.625,0,0.75,0.125,0.75,0.375,0.675,0.5,0.125,0.5,0,0.625,0,0.875,0.125,1,0.625,1,0.75,0.875);
Letter['T'] = new Array(0,1,0.5,1,0.5,0,0.5,1,1,1);
Letter['U'] = new Array(0,1,0,0.125,0.125,0,0.625,0,0.75,0.125,0.75,1);
Letter['V'] = new Array(0,1,0.375,0,0.75,1);
Letter['W'] = new Array(0,1,0.25,0,0.5,1,0.75,0,1,1);
Letter['X'] = new Array(0,0,0.375,0.5,0,1,0.375,0.5,0.75,1,0.375,0.5,0.75,0);
Letter['Y'] = new Array(0,1,0.375,0.5,0.375,0,0.375,0.5,0.75,1);
Letter['Z'] = new Array(0,1,0.75,1,0,0,0.75,0);
Letter[' '] = new Array();

function drawLetter(let, x, y, size) {
    var points = Letter[let];
    if (typeof points == 'undefined' || points.length < 1) return;
    turtle.penup();
    for (var i = 0; i < points.length - 1; i = i+2) {
        turtle.goto(x+points[i]*size,y-points[i+1]*size);
        if (i == 0) turtle.pendown();
    }
}

function drawWord(word, x, y, size) {
    word = word.toUpperCase();
    for (var i = 0; i < word.length; i++) {
        // repeat the letter shifted to darken/shadow
        [0,0.1,0.2,0.3,0.4,0.5,-0.1,-0.2].forEach(function(shift) {
            drawLetter(word[i], x+shift, y+shift, size);
        });
        // letter spacing
        if (word[i] == 'I') { x += size-3 } else 
        if (word[i] == 'M' || word[i] == 'T') { x += size+3 } else 
            x += size+1;
    }
}



// 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) {
    console.log('is '+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()+1,turtle.y())) okdirs.push(1);
    if (isValidDestination(turtle.x()-1,turtle.y()  )) okdirs.push(2);
    if (isValidDestination(turtle.x()  ,turtle.y()+1)) okdirs.push(3);
    if (isValidDestination(turtle.x()  ,turtle.y()-1)) okdirs.push(4);

    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 = .2;
        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 += 1;
            break;
        case 2:
            newx -= 1;
            break;
        case 3:
            newy += 1;
            break;
        case 4:
            newy -= 1;
            break;
        default:
            console.log('move switch default ('+newdir+') hit oops!');
    }
    drawTo(newx, newy);
    logHit();
}

function restartTurtle() {
    turtle.penup();
    turtle.setx(randInt(-lim, lim));
    turtle.sety(randInt(-lim, lim));
    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();
    turtle.goto(extent,-extent);
    turtle.goto(extent,extent);
    turtle.goto(-extent,extent);
    turtle.goto(-extent,-extent);
}

// init
outline();
drawWord("Merry", -40, -20, 17);
drawWord("Christmas", -78, 20, 17);
restartTurtle();

function walk(i) {

    move();
    return i < interations;
}