关于右手规则迷宫求解器的问题

时间:2019-11-15 03:18:42

标签: java maze

我正在尝试制作一个使用右手规则解决任何12 * 12迷宫的程序。 我对此程序有一些疑问。 “ X”代表当前位置,“#”代表墙壁,“。”代表X可以走的路。

  1. 根据结果继续更新X的位置。

我希望用户看到X如何沿右壁移动(在我的代码中以#表示)。我认为我必须延迟输出并更新输出,但是我不知道如何编写这些情况。我想这样更新输出:随着X的进行,它将删除先前的“ X”并将其更改回“。”。有什么方法可以做到这一点?

  1. 我的代码有错误。

如果我将我的算法用于 maze1 ,则X会替换我不想要的#。对于 maze2 ,它甚至不起作用。我认为当X周围有3个#时,我的算法会出错。是否有解决此问题的可能方法?

这是我的“迷宫遍历程序”代码。

public class MazeTraversalProgram {
    private static int row, column;
    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();
}

//starts the maze - for loop look at first column go down by each row

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

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

private static void turnCounterC () {
    if (direction == "right") {
        direction = "up";
    } else if (direction == "up") {
        direction = "left";
    } else if (direction == "left") {
        direction = "down";
    } else if (direction == "down") {
        direction = "right";
    }
}

private static void turnC () {
    if (direction == "right") {
        direction = "down";
    } else if (direction == "up") {
        direction = "right";
    } else if (direction == "left") {
        direction = "up";
    } else if (direction == "down") {
        direction = "left";
    }
}


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

    int count = 0;

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

    if (wallOnRight(maze) && wallOnFront(maze)){
        turnCounterC();
    } else if (!wallOnRight(maze)) {
        turnC();
    }
    goForward(maze);
    if (maze[11][row] == 'X') {
        maze[11][row] = 'X';
    } else {
        traverseMaze(maze);
    }
}

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

}

对不起,如果您听不懂我的英语,我不是母语。 也感谢您谁能回答我的问题!

祝您有美好的一天:D:D

0 个答案:

没有答案