Death Star Plans

Like Prime Directive but with separate rules for turning left/right and going forward.

Log in to post a comment.

Canvas.setpenopacity(0.5);
const size = 1;
const steps = 2;
const extent = 100;
const start = [0, 0];
const primes = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41];
const mode = -1+2*Math.round(Math.random());

Turtle.prototype.checkBounds = function() {
    this.penup();
    const x = this.x();
    const y = this.y();
    if      (x > extent)  this.setx(-extent+(x-extent));
    else if (x < -extent) this.setx(extent-(-1*(x+extent)));
    if      (y > extent)  this.sety(-extent+(y-extent));
    else if (y < -extent) this.sety(extent-(-1*(y+extent)));
    this.pendown();
}

Turtle.prototype.decideDirection = function(step) {
    const left = this.factors.reduce((a, f, idx) => !a && !(step%f), false);
    const right = this.factors.reduce((a, f, idx) => a || step%f==0, false);
    if (left && right) return 4*mode;
    if (left) return -1*mode;
    if (right) return mode;
    return false;
}

Turtle.prototype.chooseFactors = function(factors) {
    const n = 3+Math.floor(1*Math.random());
    this.factors = [];
    for (let i = 0; i < n; i++) {
        this.factors.push(factors.splice(Math.floor(Math.random()*factors.length), 1));
    }
}

const turtle = new Turtle();
turtle.penup();
turtle.goto(start[0], start[1]);
turtle.chooseFactors(primes);
turtle.end = turtle.factors.reduce((a, f) => a*f, 4)-1;
turtle.pendown();

function walk(i) {
    const dir = turtle.decideDirection(i);
    if (dir) turtle.circle(dir*size, 90, steps);
    else turtle.forward(size);
    turtle.checkBounds();
    return i < turtle.end;
}