如何在Java的控制台上更新输出? (右手规则迷宫求解器)

时间:2019-11-20 23:56:18

标签: java recursion maze

我正在尝试使用右手法则来解决迷宫,我的算法对我来说似乎很好。 我的代码有一点问题。 我想在控制台上更新输出,但是我真的不知道该怎么做。 我有一种必须使用Thread.sleep的感觉,但是我不知道将它放在哪里,以便它可以不断更新我的控制台,而不必每次移动都打印出新的迷宫。

我想让用户可以看到X的当前位置,并且我希望后面没有X。 仅凭单词很难解释,所以我制作了一张gif图片。

Moving of X

这是我的代码。感谢您能回答我的问题的任何人!祝你有美好的一天: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);
}

}

0 个答案:

没有答案