在 Java 中从文本文件执行多个迷宫



你好,我是stackoverflow和Java的新手。我已经在这个项目上工作了很长一段时间(大约一周),我需要创建一个迷宫求解器,它从每个文本字段的 [0,0] 运行到 [9,9](如果我使用正确的术语,idk)。每个迷宫由一条空线分隔。我有程序可以正确显示迷宫,但现在出现了问题,我无法让它解决每个迷宫,只有文本文件中的最后一个迷宫,即使我更改迷宫的顺序,它也只是解决最后一个。该程序运行迷宫集中的最后一个迷宫,但我无法让它从第一个迷宫开始并连续执行每个迷宫,这是我处理的代码:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class Maze
{
private static char[][] maze;
private static ArrayList<String> mazegenerator;
public static void initializeMaze(String fileName)
{
try
{
mazegenerator = new ArrayList<>();
int numcols = 0;
int r = 0;
Scanner file = new Scanner(new File(fileName));
while(file.hasNextLine())
{
String nextLine = file.nextLine(); // sets the String nextLine a value
mazegenerator.add(nextLine); // inserts the string into the array
if (nextLine.length() > numcols)
numcols = nextLine.length(); //sets numcols to the length of the Line
if(nextLine.length() == 0)
clearthismaze(); // clears the input from array
} // loop to collect the cols and set the row size
int numrows = mazegenerator.size();
maze = new char[numrows][numcols];
for (int i ; r < numrows; r ++)
{
String row = mazegenerator.get(r);
for (int c = 0; c < numcols; c++)
{
if(row.length() >= c)
maze[r][c]= row.charAt(c);
}
}
if(r == 10 && numrows == 10 && numcols == 10)
System.out.println();
printMaze();
solveMaze(0,0);
System.out.println();
printMaze();
numrows = 0;
numcols = 0;
}
catch(Exception e){System.out.println(fileName + " has an issue");} 

} // this method generates the maze and sets it up which is later printed
public static void printMaze()
{
for (char[] row: maze)
{
for (char c: row)
System.out.print(c);
System.out.println();
}
System.out.println();
} //method to print maze accordingly
public static void main (String[] args)
{       
System.out.println("This program prints the original maze before solving and then prints the maze solution, n"
+ "solving from the upper left to the lower right. X's represent routes taken, 2's represent the true path, n"
+ "if no 2's are present then that means the maze is unsolvable n");
initializeMaze("multiplemazes.txt");
} // main method , driver method
public static boolean solveMaze(int r, int c)
{
boolean finished = false;
if(PossibleToMove(r,c)){
maze[r][c] = 'X'; // coordinate has been visited mark it so you don't return to it                                                  
if(c == maze[0].length - 1 && r == maze.length-1)// base case: did I reach destination?
finished = true;  // maze is solved   
else{              
if(!finished)
finished = solveMaze(r,c-2); // look left
if(!finished)
finished = solveMaze(r-1,c); // look up
if(!finished)
finished = solveMaze(r,c+2); // look right
if(!finished)
finished = solveMaze(r+1,c); // look down
}
if(finished) // part of the true path
maze[r][c] = '2';
if(!finished){
System.out.println(">>>" + " Backtracking from " + "( " + r + ", " + c/2 + ")");               
}
}
// if I know that I reached a dead end
// this can't be part of the solution
return finished;         
} //method solveMaze
private static boolean PossibleToMove(int r, int c){
boolean canmove  = false;
if(r >=0 && r < maze.length && c >=0 && c < maze[0].length)
if(maze[r][c] == '0')
canmove = true;
return canmove;
}  // method PossibleToMove
public static void clearthismaze(){
mazegenerator.clear();
}
} // class Maze

以下是 4 个迷宫的文本文件示例:

0 1 0 0 0 0 0 0 0 1
0 0 0 1 0 1 0 1 0 1
0 1 0 1 0 1 1 1 1 1
1 0 0 1 0 0 0 1 0 0
1 0 1 1 0 1 0 1 0 1
1 0 0 0 1 1 0 1 0 0
0 0 1 0 0 0 0 1 1 0
0 1 1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 1 0 1
1 0 0 0 0 0 0 1 0 0
0 0 1 0 0 0 1 1 0 1
1 0 0 1 0 1 0 0 0 1
0 1 0 0 0 1 1 1 0 1
0 0 1 1 0 0 0 1 0 0
1 0 0 1 1 1 1 1 0 1
1 0 0 1 0 1 1 0 0 1
0 0 1 0 1 0 0 1 1 1
0 1 1 0 0 0 0 0 1 1
0 1 0 1 1 1 0 0 0 0
0 0 0 1 1 0 0 1 1 0
0 0 0 0 1 1 0 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 1 0 1
1 0 0 0 0 0 0 1 0 0
0 1 0 1 0 1 0 0 0 1
0 0 0 1 0 1 0 1 0 1
0 1 0 1 0 1 1 1 1 1
1 0 1 1 0 1 0 0 0 1
1 0 0 1 0 0 0 1 0 0
0 0 0 0 1 1 0 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 1 0 1 1 0 0 0 0
1 1 0 1 1 1 0 1 0 1
1 0 0 0 1 0 0 1 0 0
0 1 0 1 0 1 0 0 0 1
0 0 0 1 0 1 0 1 0 1
0 1 0 1 0 1 1 1 1 1
1 0 1 1 0 1 0 0 0 1
1 0 0 1 0 0 0 1 0 0

任何数量的帮助都会很有用,是的,这是一个家庭作业,我想我只是卡在一个循环问题上,或者我的 clearthismaze() 方法有问题。我的目的是打印出数组,解决数组,清除数组,跳过空行,然后移动到下一个数组。如果有人能指出这个问题,我将不胜感激,谢谢。

如果得到一个空行,则正在清除mazegenerator。因此,如果您开始解决迷宫,则只有最后一个可用。

您可以将类型更改为List<List<String>> mazegenerator。所以mazegenerator.get(0)拥有第一个迷宫,mazegenerator.get(0).get(0)拥有第一个迷宫的第一行。 您还需要更改文件读取循环:

List<String> singleMaze = new ArrayList<>();
while(file.hasNextLine()) {
// ...
if(nextLine.length() == 0) {
mazegenerator.add(singleMaze);
singleMaze = new ArrayList<>();
}
}

最新更新