java - 如何在 Java 中更新控制台上的输出?(右手规则迷宫求解器)
问题描述
我正在尝试使用右手法则来解决迷宫问题,我的算法对我来说似乎相当不错。不过,我的代码有点问题。我想在控制台上更新我的输出,但我真的不知道该怎么做。我有种我必须使用的感觉Thread.sleep
,但我不知道把它放在哪里,这样它就可以不断更新我的控制台,而无需每次移动都打印全新的迷宫。
我想让用户可以看到 X 的当前位置,并且我希望没有尾随的 X。这有点难以用文字来解释,所以我做了一个 gif 图片。
这是我的代码。感谢任何愿意回答我问题的人!祝你们有美好的一天: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);
}
}
解决方案
推荐阅读
- angular - 在 Angular 6 中使用 ChangeDetectionStrategy,需要在子元素(ng-class)上添加条件类
- javascript - 更改背景图像白色 html 和 css
- php - PHP正则表达式匹配数字范围,即17-21
- ruby-on-rails - 从特定的起始值排序 activerecord 关系
- html - white-space:前行在顶部添加不寻常的空间
- jquery - HoverIntent 配置,不拾取
- javascript - Safari 中的选框动画
- java - 从数据库获取数据时没有结果,为什么?java(安卓工作室
- java - 我们如何在 FXML 中定义上下文菜单?
- android - 启用 tls 1.2 Android 4.X - Xamarin Android -