xxxxxxxxxx
148
let tiles;
let timer = 0;
const ns = { 1: [0, -1], 2: [1, 0], 4: [0, 1], 8: [-1, 0] };
const opps = { 1: 4, 2: 8, 4: 1, 8: 2 };
function setup() {
createCanvas(600, 600);
tiles = Array(225).fill(null);
tiles[7 * 15 + 7] = 0;
}
function drawTile(n) {
/* stroke( 0 );
strokeWeight( 0.01 );
rect(0, 0, 1, 1); */
/*
noStroke();
fill( 0 );
rect( 0.125, 0.125, 0.75, 0.75 );
fill( 255 );
rect( 0.375, 0.375, 0.25 );
*/
noStroke();
fill(234, 64, 0);
rect(0.125, 0.125, 0.25, 0.25);
rect(0.625, 0.125, 0.25, 0.25);
rect(0.125, 0.625, 0.25, 0.25);
rect(0.625, 0.625, 0.25, 0.25);
if (n & 1) {
// Top
rect(0.125, 0, 0.25, 0.125);
rect(0.625, 0, 0.25, 0.125);
} else {
rect(0.375, 0.125, 0.25, 0.25);
}
if (n & 2) {
// Right
rect(0.875, 0.125, 0.125, 0.25);
rect(0.875, 0.625, 0.125, 0.25);
} else {
rect(0.625, 0.375, 0.25, 0.25);
}
if (n & 4) {
// Bottom
rect(0.125, 0.875, 0.25, 0.125);
rect(0.625, 0.875, 0.25, 0.125);
} else {
rect(0.375, 0.625, 0.25, 0.25);
}
if (n & 8) {
// Left
rect(0, 0.125, 0.125, 0.25);
rect(0, 0.625, 0.125, 0.25);
} else {
rect(0.125, 0.375, 0.25, 0.25);
}
/*
fill( 255, 0, 0 );
textAlign( CENTER );
push();
translate( 0.5, 0.75 );
scale( 0.05 );
text( n, 0, 0 );
pop();
*/
}
function draw() {
background(236, 113, 103);
const ops = [];
for (let y = 0; y < 15; ++y) {
for (let x = 0; x < 15; ++x) {
const n = tiles[y * 15 + x];
if (n != null) {
push();
translate((x * width) / 15, (y * height) / 15);
scale(width / 15);
drawTile(n);
pop();
for (let dir of [1, 2, 4, 8]) {
if (!(n & dir)) {
const v = ns[dir];
const nx = x + v[0];
const ny = y + v[1];
if (nx >= 0 && nx < 15 && ny >= 0 && ny < 15) {
if (tiles[ny * 15 + nx] === null) {
ops.push([x, y, dir]);
}
}
}
}
}
}
}
if (ops.length == 0) {
if (timer < 30) {
++timer;
return;
}
timer = 0;
// Open up a spot. Find a leaf node and prune it.
let leaves = [];
for (let y = 0; y < 15; ++y) {
for (let x = 0; x < 15; ++x) {
const n = tiles[y * 15 + x];
if ([1, 2, 4, 8].includes(n)) {
leaves.push([x, y]);
}
}
}
const [x, y] = leaves[int(random(leaves.length))];
const n = tiles[y * 15 + x];
const v = ns[n];
tiles[(y + v[1]) * 15 + (x + v[0])] &= ~opps[n];
tiles[y * 15 + x] = null;
for (let d of [1, 2, 4, 8]) {
const v = ns[d];
const ox = x + v[0];
const oy = y + v[1];
if (ox >= 0 && ox < 15 && oy >= 0 && oy < 15) {
ops.push([ox, oy, opps[d]]);
}
}
}
if (ops.length > 0) {
const [x, y, dir] = ops[int(random(ops.length))];
tiles[y * 15 + x] |= dir;
tiles[(y + ns[dir][1]) * 15 + (x + ns[dir][0])] = opps[dir];
}
// noLoop();
}
function keyPressed() {
if (key === "s") {
save("jan11.png");
} else {
loop();
}
}