xxxxxxxxxx
220
let grid = [[]];
let x, y, px, py;
let direction;
let UNIT;
let Radius;
const gridSize = 8;
const startx = 0;
const starty = 0;
const offset = 25;
const Direction = {
U: 0,
R: 1,
D: 2,
L: 3,
None: -1,
};
const Directions = [Direction.U, Direction.R, Direction.D, Direction.L];
function setup() {
createCanvas(400, 400);
background(220);
console.log("start time " + hour() + " : " + minute() + " : " + second())
UNIT = width/gridSize;
Radius = UNIT/3
direction = Direction.U;
x = startx;
y = starty;
px = -1;
py = -1;
ellipseMode(RADIUS);
for (let ly = 1; ly < height / UNIT; ly++) {
grid.push(new Array(width / UNIT));
}
fill(0);
for (let ly = 0; ly < height / UNIT; ly++) {
for (let lx = 0; lx < width / UNIT; lx++) {
grid[ly][lx] = 0;
ellipse(
lx * UNIT + offset,
ly * UNIT + offset,
(width / (width / UNIT)) * 0.25,
(height / (height / UNIT)) * 0.25
);
}
}
fill(255);
grid[x][y] = 1;
frameRate(30)
}
function draw() {
// DRAW Grid
// for (let y = 0; y < height/UNIT; y++) {
// for (let x = 0; x < width/UNIT; x++) {
// rect(x*UNIT, y*UNIT, width/(width/UNIT), height/(height/UNIT
// }
// }
for (let i = 0; i < 1; i++) {
// DRAW Walker
if (px != -1 && py != -1) {
strokeWeight(8);
// stroke(255, 0, 0);
stroke(255)
line(
px * UNIT + offset,
py * UNIT + offset,
x * UNIT + offset,
y * UNIT + offset
);
// console.log(px, x, direction)
}
strokeWeight(1);
// stroke(0);
ellipse(x * UNIT + offset, y * UNIT + offset, Radius, Radius);
// UPDATE Walker
px = x;
py = y;
randomDirection();
direction = checkNewDirection();
// MOVE Walker
switch (direction) {
case Direction.U:
y -= 1;
break;
case Direction.R:
x += 1;
break;
case Direction.D:
y += 1;
break;
case Direction.L:
x -= 1;
break;
case Direction.None:
for (let ly = 0; ly < height / UNIT; ly++) {
for (let lx = 0; lx < width / UNIT; lx++) {
if (grid[ly][lx] == 0) {
// weve failed so retry
background(220);
for (let ly = 0; ly < height / UNIT; ly++) {
for (let lx = 0; lx < width / UNIT; lx++) {
grid[ly][lx] = 0;
fill(0);
ellipse(
lx * UNIT + offset,
ly * UNIT + offset,
(width / (width / UNIT)) * 0.25,
(height / (height / UNIT)) * 0.25
);
fill(255);
}
}
px = -1;
py = -1;
x = startx;
y = starty;
grid[y][x] = 1;
// console.log("FAIL");
return;
}
}
}
console.log("CONQUERED");
console.log("conquer time " + hour() + " : " + minute() + " : " + second())
noLoop();
return;
}
grid[x][y] = 1;
// console.table(grid)
}
}
function checkNewDirection() {
// Check if we are stuck
if (y == 0 || grid[x][y - 1] == 1) {
if (x == grid.length - 1 || grid[x + 1][y] == 1) {
if (y == grid[0].length - 1 || grid[x][y + 1] == 1) {
if (x == 0 || grid[x - 1][y] == 1) {
return Direction.None;
}
}
}
}
switch (direction) {
case Direction.U:
// check edges
if (y == 0) {
randomDirection();
return checkNewDirection();
}
// avoid self
if (grid[x][y - 1] == 1) {
randomDirection();
return checkNewDirection();
}
break;
case Direction.R:
// check edges
if (x == grid.length - 1) {
randomDirection();
return checkNewDirection();
}
// avoid self
if (grid[x + 1][y] == 1) {
randomDirection();
return checkNewDirection();
}
break;
case Direction.D:
// check edges
if (y == grid[0].length - 1) {
randomDirection();
return checkNewDirection();
}
// avoid self
if (grid[x][y + 1] == 1) {
randomDirection();
return checkNewDirection();
}
break;
case Direction.L:
// check edges
if (x == 0) {
randomDirection();
return checkNewDirection();
}
// avoid self
if (grid[x - 1][y] == 1) {
randomDirection();
return checkNewDirection();
}
break;
}
return direction;
}
function randomDirection() {
direction = random(Directions);
}