使用Stack的二维迷宫求解器:ArrayIndexOutOfBoundsException

时间:2017-09-28 02:55:48

标签: java arrays indexoutofboundsexception maze

我正在开发一个程序来解决在迷宫中寻找路径的问题。迷宫由0&1,s&1表示,E代表退出。迷宫由20x30表示(0' s代表路径,1代表墙壁)。我正在使用堆栈来跟踪以前可用的位置。

我想我已经找到了大部分代码,但每当我尝试运行它时,我都会得到一个ArrayIndexOutOfBoundsException。我认为主要问题是边界周围没有明确的围墙。也许围绕迷宫的边界为1?

我的代码如下:

import java.util.*;
import java.io.*;
public class MazeGenerator {

//main
 public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(System.in);
        int userRow, userCol;

        MazeGenerator maze = new MazeGenerator();
        maze.fillArray();
        maze.print();

        System.out.println();

        //asking for user starting position
        System.out.print("What row would you like to start in?: " );
        userRow = sc.nextInt();
        while(userRow > 29 || userRow < 0) {
            System.out.print("INVALID INPUT! PLEASE ENTER VALUES BETWEEN 0 - 29 INCLUSIVE: " );
            userRow = sc.nextInt();
        }
        System.out.println();

        System.out.print("What column would you like to start in? ");
        userCol = sc.nextInt();
        while(userCol > 19 || userCol < 0) {
            System.out.print("INVALID INPUT! PLEASE ENTER VALUES BETWEEN 0 - 19 INCLUSIVE: " );
            userCol= sc.nextInt();
        }


        System.out.println("\n\nFind a path using a stack: ");
        //maze.userStart(userRow,userCol);

        maze.setUserRow(userRow);
        maze.setUserColumn(userCol);

        maze.solveStack();
 }

//methods for creating maze
public static final int ROW = 30;
public static final int COLUMN = 20;
public int userRow = 0;
public int userColumn = 0;
private static String[][] maze = new String[ROW][COLUMN];

public void fillArray() throws IOException {
    File file = new File("maze.txt");
    FileReader reader = new FileReader(file);
    BufferedReader buff = new BufferedReader(reader);

    for(int counter1 = 0; counter1 < ROW; counter1++) {

        String l = buff.readLine();

        for(int counter2 = 0; counter2 < COLUMN; counter2++) {

            maze[counter1][counter2] = String.valueOf(l.charAt(counter2));
        }
    }

    buff.close();
}

public void print() throws IOException {

    System.out.printf("%-4s", ""); //spaces column
    for (int counter = 0; counter < COLUMN; counter++){

         System.out.printf("%-4d",counter); //print the column number

    }
    System.out.println();

    for(int counter1 = 0; counter1 < maze.length; counter1++) { //loop for printing rows

         System.out.printf("%-4d",counter1); //print row number

        for(int counter2 = 0; counter2 < maze[counter1].length; counter2++) { //loop for printing columns

            System.out.printf("%-4s", maze[counter1][counter2]); //printing values of maze
        }
        System.out.println(); // new line
    }
}

public int size() {
    return maze.length;
}

public void setUserRow (int userRow) {
    this.userRow = userRow;
}

public void setUserColumn (int userColumn) {
    this.userColumn = userColumn;
}

public int getUserRow() {
    return userRow;
}

public int getUserColumn() {
    return userColumn;
}

public String mark(int row, int col, String value) {
    assert(inMaze(row,col)); 
    String temp = maze[row][col];
    maze[row][col] = value;
    return temp;
    }

public String mark (MazePosition pos, String value) {
    return mark(pos.row(), pos.col(), value); 
}

public boolean isMarked(int row, int col) {
    assert(inMaze(row,col)); 
    return (maze[row][col].equals("+"));
}

public boolean isMarked(MazePosition pos) {
    return isMarked(pos.row(), pos.col());
}

public boolean Clear(int row, int col) {
    assert(inMaze(row,col)); 
    return (maze[row][col] != "1" && maze[row][col] != "+");
 }

public boolean Clear(MazePosition pos) {
     return Clear(pos.row(), pos.col());
 }

//true if cell is within maze 
public boolean inMaze(int row, int col) {
    if (row >= 0 && col<size() && row>= 0 && col<size()) {
        return true; 
    }
    else if (row < 0 && col<size() && row >= 0 && col<size()) {
        return false;
    }
    else return false;
}

//true if cell is within maze 
public boolean inMaze(MazePosition pos) {
    return inMaze(pos.row(), pos.col());
} 

public boolean Done( int row, int col) {
    return (maze[row][col].equals("E"));
}

public boolean Done(MazePosition pos) {
    return Done(pos.row(), pos.col());
}

public String[][] clone() {

    String[][] copy = new String[ROW][COLUMN]; 

    for (int counter1 = 0; counter1 < ROW; counter1++) {

        for (int counter2 = 0; counter2 < COLUMN; counter2++) {

            copy[counter1][counter2] = maze[counter1][counter2];
        }
    }
    return copy; 
    }

public void solveStack() throws IOException {

//save the maze
String[][] savedMaze = clone(); 

//declare the locations stack 
Stack<MazePosition> candidates = new Stack<MazePosition>(); 

//insert the start 
candidates.push(new MazePosition(userRow,userColumn)); 

MazePosition current, next;
while (!candidates.empty()) {

    //get current position
    current = candidates.pop();

    if (Done(current)) { 
        break;
    }

    //mark the current position 
    mark(current, "S");

    //put its neighbors in the queue
    next = current.north(); 
    if (inMaze(next) && Clear(next)) candidates.push(next);
    next = current.east(); 
    if (inMaze(next) && Clear(next)) candidates.push(next);
    next = current.west(); 
    if (inMaze(next) && Clear(next)) candidates.push(next);
    next = current.south(); 
    if (inMaze(next) && Clear(next)) candidates.push(next);
}

if (!candidates.empty()) 
    System.out.println("You got it!");
else System.out.println("You're stuck in the maze!");
print();

}

class MazePosition {
    public int row; 
    public int col;

    public MazePosition(int row, int col) {
        this.row = row; 
        this.col = col;
    }

    public int row() { return row; }

    public int col() { return col; }

    public void print() {
        System.out.println("(" + row + "," + col + ")");
    }

    //positions
    public MazePosition north() {
        return new MazePosition(row-1, col);
    }

    public MazePosition south() {
        return new MazePosition(row+1, col);
    }

    public MazePosition east() {
        return new MazePosition(row, col+1);
    }

    public MazePosition west() {
        return new MazePosition(row, col-1);
    }

}; 

}

错误如下:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at MazeGenerator.Clear(MazeGenerator.java:137)
at MazeGenerator.Clear(MazeGenerator.java:141)
at MazeGenerator.solveStack(MazeGenerator.java:217)
at MazeGenerator.main(MazeGenerator.java:40)

1 个答案:

答案 0 :(得分:0)

我认为你在inMaze(int row, int col)方法中犯了一个错误。我会尽力为你纠正

public boolean inMaze(int row, int col) {
    if (row >= 0 && col >= 0 && row < getWidth() && col < getHeight() ) {
        return true; 
    }
    return false;
}

因此,您必须将size()方法更改为getWidth()getHeight()

public int getWidth(){
    return maze[0].length;
}

public int getHeight(){
    return maze.length;
}