1

I'm trying to solve maze by using right hand rule, and my algorithm seems pretty good to me. I have a little problem with my codes though. I want to update my output on console, but I really don't know how to do it. I kind of got the sense that I have to use Thread.sleep, but I don't know where to put it so that it can update my console constantly without printing whole new maze each move.

I want to make user can see the X's current position, and I want there's no trailing X. It's a bit hard to explain with just words, so I made an gif picture.

Moving of X

And here are my codes. Thank you for anyone who will answer my question! Have a nice day y'all :D

public class MazeTraversalGradedProgram {
    private static int row, column, count;
    private static String direction = "right";

private static char maze1 [] [] = {
    {'#','#','#','#','#','#','#','#','#','#','#','#'},
    {'#','.','.','.','#','.','.','.','.','.','.','#'},
    {'.','.','#','.','#','.','#','#','#','#','.','#'},
    {'#','#','#','.','#','.','.','.','.','#','.','#'},
    {'#','.','.','.','.','#','#','#','.','#','.','.'},
    {'#','#','#','#','.','#','.','#','.','#','.','#'},
    {'#','.','.','#','.','#','.','#','.','#','.','#'},
    {'#','#','.','#','.','#','.','#','.','#','.','#'},
    {'#','.','.','.','.','.','.','.','.','#','.','#'},
    {'#','#','#','#','#','#','.','#','#','#','.','#'},
    {'#','.','.','.','.','.','.','#','.','.','.','#'},
    {'#','#','#','#','#','#','#','#','#','#','#','#'}};

private static char maze2 [] [] = {
    {'#','#','#','#','#','#','#','#','#','#','#','#'},
    {'#','.','.','.','.','.','#','.','.','.','.','#'},
    {'#','.','#','.','#','.','#','#','#','#','.','#'},
    {'#','#','#','.','#','.','.','.','.','#','.','#'},
    {'#','.','.','.','.','#','#','#','.','#','.','#'},
    {'#','#','#','#','.','#','.','#','.','#','.','#'},
    {'#','.','.','#','.','#','.','#','.','#','.','.'},
    {'#','#','#','#','.','#','.','#','.','#','.','#'},
    {'#','.','.','.','.','#','.','.','.','#','.','#'},
    {'#','.','#','#','#','#','#','#','.','#','.','#'},
    {'.','.','#','.','.','.','.','#','.','.','.','#'},
    {'#','#','#','#','#','#','#','#','#','#','#','#'}};

private static void printMaze (char [] [] maze) {
    for (int i = 0; i<maze.length; i++) {
        for(int j = 0; j<maze[i].length; j++) {
            System.out.print(maze [i][j] + " ");
        }
        System.out.println();
    }
    System.out.println();
    System.out.println();
}

private static void goForward (char [] [] maze){
    if (direction == "right") {
        column++;
        maze[row][column] = 'X';
    } else if (direction == "up") { // array[xnd row --- ][ynd column | ]
        row--;
        maze[row][column] = 'X';
    } else if (direction == "left") {
        column--;
        maze[row][column] = 'X';
    } else if (direction == "down") {
        row++;
        maze[row][column] = 'X';
    }
}
private static boolean wallOnRight (char [] [] maze) {
    if (direction == "right" && maze[row+1][column] == '#') {
        return true;
    }
    else if (direction == "up" && maze[row][column+1] == '#') {
        return true;
    }
    else if (direction == "left" && maze[row-1][column] == '#') {
        return true;
    }
    else if (direction == "down" && maze[row][column-1] == '#') {
        return true;
    }
    return false;
}

private static boolean wallOnFront (char [] [] maze) {
    if (direction == "right" && maze[row][column+1] == '#') {
        return true;
    }
    else if (direction == "up" && maze[row-1][column] == '#') {
        return true;
    }
    else if (direction == "left" && maze[row][column-1] == '#') {
        return true;
    }
    else if (direction == "down" && maze[row+1][column] == '#') {
        return true;
    }
    return false;
}

private static void traverseMaze (char [] [] maze){

    if (count == 0) {
        for (int i = 0; i < 12; i++) {
            if (maze[i][0] == '.'){                 // array[xnd row --- ][ynd column | ]
                maze[i][0] = 'X';
                row = i; column = 0;
            }
        }
        count++;
    }

    if (wallOnRight(maze) && !wallOnFront(maze)) {
        goForward(maze);
    } else if (wallOnRight(maze) && wallOnFront(maze)) {
        if (direction == "right") {
            direction = "up";
        } else if (direction == "up") {
            direction = "left";
        } else if (direction == "left") {
            direction = "down";
        } else if (direction == "down") {
            direction = "right";
        }
    } else if (!wallOnRight(maze)) {
        if (direction == "right") {
            direction = "down";
        } else if (direction == "up") {
            direction = "right";
        } else if (direction == "left") {
            direction = "up";
        } else if (direction == "down") {
            direction = "left";
        }
        goForward(maze);
    }

    if (column == 11) {
        count = 0 ;
    } else {
        traverseMaze(maze);
    }


}

public static void main (String [] args){
    printMaze(maze1);
    traverseMaze(maze1);
    printMaze(maze2);
    traverseMaze(maze2);
}

}
  • 3
    You cannot update console. You will need some type of GUI for that. – PM 77-1 Nov 20 '19 at 23:59
  • @PM77-1 What I mean by that, is constantly updating outputs like my GIF picture. I could find some cases that updating outputs with using ```Thread.sleep``` command and while loops or for loops, but I really couldn't find any with 2D array. Thank you for your comment! – Joshua Chang Nov 21 '19 at 00:24
  • 1
    You can't **reliably** do this on every platform that Java is supported on. It is completely dependent upon what is interpreting the code and producing your **output**. There are some libraries and commands you can use to move your cursor around in the console...but again, these are dependent upon the environment in which Java is being run. – Idle_Mind Nov 21 '19 at 00:45
  • As mentioned from @JoshuaChang you cannot **clean** the console in Java, BUT you can redraw the whole screen after every input, see https://stackoverflow.com/questions/58987624/java-text-based-maze-game/59261042#59261042 – Martin Frank Dec 10 '19 at 05:57

0 Answers0