Random Walk Code

It’s been a while since I’ve coded anything. I’ve been thinking about my old word game “1,000 Word Quest” and wanted to make a daily puzzle out of it like Circuit Board Square. Unfortunately, due to the nature of the puzzle, doing data-driven graphics seems impossible as the path you take to solve the puzzle is different for each puzzle.

So the code I wrote, which is extremely inefficient and relies on randomness to achieve the goal, will randomly walk a 3x3 grid to produce a path that will touch each circle once and only once, moving in orthogonal directions only.

var puzzleWidth = 3;
var puzzleHeight = 3;
var walkLength = puzzleWidth * puzzleHeight;
var walkCount = 1;
var walkArr1 = [0, 0, 0];
var walkArr2 = [0, 0, 0];
var walkArr3 = [0, 0, 0];
var walkArr = [walkArr1, walkArr2, walkArr3];
var directionArr = ["UL", "U", "UR", "L", "R", "DL", "D", "DR"];
var walkedArr = [];

var solutionTotal = 1;
var solutionCount = 0;

var errorLimit = 10;
var errorCount = 0;
var errorLog = 0;

var startX = 0;
var startY = 0;

var currentX = 0;
var currentY = 0;

function getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
}

function setupArray() {

}

function updateText() {
    document.write("--- final walkArr #" + solutionCount + "---" + "<br>");
    document.write(walkArr[0] + "<br>");
    document.write(walkArr[1] + "<br>");
    document.write(walkArr[2] + "<br>");
    //document.write(walkArr[0][1]);
}

function createStartPoint() {
    // Create a start point for x and y
    // push that into the path array
    startX = getRandomInt(3);
    startY = getRandomInt(3);

    currentX = startX;
    currentY = startY;

    walkArr[currentY][currentX] = walkCount;

    walkCheck();
}

function walkCheck() {
    // walk a random direction:
    // 1. check if the next spot is open
    // 2. if it is, move to that spot. increase count by 1.
    var nextDirection = directionArr[getRandomInt(directionArr.length)];
    //console.log("nextDirection: " + nextDirection);
    switch (nextDirection) {
        case "UL":
            if ((currentY > 0) && (currentX > 0)) {
                walk(-1, -1);
            } else {
                walkCheck();
            }
            // check;
            break;
        case "U":
            if ((currentY > 0)) {
                walk(0, -1);
            } else {
                walkCheck();
            }
            // check;
            break;
        case "UR":
            if ((currentY > 0) && (currentX < 2)) {
                walk(1, -1);
            } else {
                walkCheck();
            }
            break;
        case "L":
            if ((currentX > 0)) {
                walk(-1, 0);
            } else {
                walkCheck();
            }
            break;
        case "R":
            if ((currentX < 2)) {
                walk(1, 0);
            } else {
                walkCheck();
            }
            break;
        case "DL":
            if ((currentY < 2) && (currentX > 0)) {
                walk(-1, 1);
            } else {
                walkCheck();
            }
            break;
        case "D":
            if ((currentY < 2)) {
                walk(0, 1);
            } else {
                walkCheck();
            }
            break;
        case "DR":
            if ((currentY < 2) && (currentX < 2)) {
                walk(1, 1);
            } else {
                walkCheck();
            }
            break;
    }
}

function walk(nextX, nextY) {
    /*
    console.log("------------");
    console.log("func: walk()");
    console.log("currentX: " + currentX);
    console.log("currentY: " + currentY);
    console.log("nextX: " + nextX);
    console.log("nextY: " + nextY);
    console.log("walkCount: " + walkCount);
    console.log("ErrorCount: " + errorCount);
    */

    if (walkCount <= walkLength) {
        if (walkArr[currentY + nextY][currentX + nextX] == 0) // if the next area is empty
        {
            walkCount++;
            walkArr[currentY + nextY][currentX + nextX] = walkCount;
            errorCount = 0;
            currentX = currentX + nextX;
            currentY = currentY + nextY;
            // updateText();

            walkCheck();
        } else {
            if (walkCount == walkLength) {
                console.log("-- complete #" + solutionCount + " --");
                solutionCount++;
                updateText();
                if (solutionCount < solutionTotal) {
                    resetPuzzle();
                }
            } else {
                if (errorCount < errorLimit) {
                    errorCount++;
                    walkCheck();
                } else {
                    console.log("-- dead end #" + errorLog + " steps #" + walkCount + "--");
                    errorLog++;
                    resetPuzzle();
                }
            }
        }
    }
}

function resetPuzzle() {
    walkCount = 1;
    walkArr1 = [0, 0, 0];
    walkArr2 = [0, 0, 0];
    walkArr3 = [0, 0, 0];
    walkArr = [walkArr1, walkArr2, walkArr3];
    createStartPoint();
}

//sayHello();
createStartPoint();