好吧,我完成了一個迷宮程序,但問題是它只需要一個大小爲20x20的矩陣,我將它編程爲一個大小爲30x20的矩陣,但它總是給我一個這樣的錯誤:在java中的迷宮遍歷
Exception in thread "main" java.lang.Exception: Invalid map.
at MazeSolver.loadMaze(MazeSolver.java:73)
at MazeSolver.main(MazeSolver.java:233)
有人可以看看它,並給我一些反饋,我在哪裏搞砸了嗎?
我的猜測是,問題就出在這裏的方法
private static char[][] loadMaze(String filename) throws Exception
MazeSolver
類
在線49:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MazeSolver {
private static Scanner keyboard = new Scanner(System.in);
/**
* Forces the user to enter a string
*/
private static String readString(String prompt) {
while (true) {
System.out.print(prompt);
String value = keyboard.nextLine().trim();
if (!value.isEmpty()) {
return value;
}
System.out.println("Error: Please enter a value.");
}
}
/**
* Forces the user to enter an integer
*/
private static int readInt(String prompt) {
while (true) {
try {
System.out.print(prompt);
return Integer.parseInt(keyboard.nextLine());
} catch (Exception e) {
System.out.println("Error: Please enter a numeric value.");
}
}
}
/**
* Load the maze from file
*/
private static char[][] loadMaze(String filename) throws Exception {
// Load the maze file, we assume the file is 30 x 20
char[][] maze = new char[30][20];
try {
Scanner inFile = new Scanner(new File(filename));
for (int row = 0; row < maze.length; row++) {
String line = inFile.nextLine();
for (int column = 0; column < maze.length; column++) {
maze[row][column] = line.charAt(column);
if (maze[row][column] != PATH && maze[row][column] != WALL && maze[row][column] != EXIT) {
throw new Exception("Invalid map.");
}
}
}
inFile.close();
} catch(FileNotFoundException e) {
System.out.println("The file " + filename + " does not exist.");
System.exit(0);
} catch (Exception e) {
throw new Exception("Invalid map.");
}
return maze;
}
/**
* Forces the user to provide a starting coordinate
*/
private static Coordinate readStartingCoordinate(char[][] maze) {
int startingRow = 0;
int startingColumn = 0;
while (true) {
// Read a valid row
startingRow = readInt("Enter starting row: ");
while (startingRow < 0 || startingRow >= 30) {
System.out.println("Error: Please enter a value from 0 to 29");
startingRow = readInt("Enter starting row: ");
}
// Read a valid column
startingColumn = readInt("Enter starting column: ");
while (startingColumn < 0 || startingColumn >= 20) {
System.out.println("Error: Please enter a value from 0 to 19");
startingColumn = readInt("Enter starting column: ");
}
// Make sure that the starting coordinate isn't a '1' (wall)
if (maze[startingRow][startingColumn] == WALL) {
System.out.println("Error: The starting point you selected is a wall");
} else {
break;
}
}
return new Coordinate(startingRow, startingColumn);
}
/**
* Solve the maze recursively
*/
// Go down
if (row + 1 < maze.length && (maze[row + 1][column] == PATH || maze[row + 1][column] == EXIT)) {
path.push(new Coordinate(row + 1, column));
if (solveMaze(maze, path)) {
return true;
}
}
// Go left
if (column - 1 >= 0 && (maze[row][column - 1] == PATH || maze[row][column - 1] == EXIT)) {
path.push(new Coordinate(row, column - 1));
if (solveMaze(maze, path)) {
return true;
}
}
// Go right
if (column + 1 < maze.length && (maze[row][column + 1] == PATH || maze[row][column + 1] == EXIT)) {
path.push(new Coordinate(row, column + 1));
if (solveMaze(maze, path)) {
return true;
}
}
// If none of the directions worked, we reached a dead end
path.pop();
return false;
}
/**
* Remove all visited marks from the maze and put the correct path the
* computer to solve it
*/
private static void cleanMaze(char[][] maze, LinkedList path, Coordinate start) {
for (int row = 0; row < maze.length; row++) {
for (int column = 0; column < maze.length; column++) {
if (maze[row][column] == VISITED) {
maze[row][column] = PATH;
}
}
}
Coordinate end = path.pop();
maze[end.getRow()][end.getColumn()] = EXIT;
while(!path.isEmpty()) {
Coordinate coordinate = path.pop();
maze[coordinate.getRow()][coordinate.getColumn()] = ANSWER;
}
maze[start.getRow()][start.getColumn()] = START;
}
/**
* Print the maze with numbered row and columns
*/
private static void printMaze(char[][] maze) {
System.out.printf("%3s", " ");
for (int i = 0; i < maze.length; i++) {
System.out.printf("%3d", i);
}
System.out.println();
for (int row = 0; row < maze.length; row++) {
System.out.printf("%-3d", row);
for (int column = 0; column < maze.length; column++) {
System.out.printf("%3c", maze[row][column]);
}
System.out.println();
}
System.out.println();
}
/**
* Entry point of the program
*/
public static void main(String[] args) throws Exception {
// Ask for the file to solve and load the maze
String filename ="C:\\Users\\TriZam\\Desktop\\MazeFile.txt"; // readString("Enter maze filename: ");
char[][] maze = loadMaze(filename);
// Ask for starting coordinates
printMaze(maze);
Coordinate start = readStartingCoordinate(maze);
LinkedList path = new LinkedList();
// Solve the maze
path.push(start);
solveMaze(maze, path);
// If map is solved print out results
if (solveMaze(maze, path)) {
cleanMaze(maze, path, start);
printMaze(maze);
System.out.println("I am free");
} else {
printMaze(maze);
System.out.println("Help, I am trapped");
}
}
}
你知道這是***你正在拋出異常嗎?如果你的代碼假設一個30x20的迷宮,但輸入只有20x20,你是否期望它能夠魔術般地適應?您應該打印原始異常的堆棧跟蹤以找出真正的問題,這可能是'IndexOutOfBoundsException' –
'C:\ Users \ TriZam \ Desktop \ MazeFile.txt'文件包含什麼? – anacron
我的迷宮txt文件 – user3500147