0

I am working on a program that will solve find a path in a maze. The maze is represented by 0's, 1's and an E to represent the Exit. The maze is represented by a 20x30 (0's represent the path, 1's represent walls). I am using a stack to keep track of a previous usable location.

I think I have most of the code figured out, but whenever i try to run it, i get an ArrayIndexOutOfBoundsException. I think the main problem is that there are no defined walls around the border. Maybe surround the maze with a border of 1's?

My code is as follows:

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);
    }

}; 

}

The error is as follows:

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)
Jflee
  • 25
  • 6

1 Answers1

0

i think you're doing a mistake in inMaze(int row, int col) method. I'll try to correct it for you

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

therefore you have to change the size() method into getWidth() and getHeight()

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

public int getHeight(){
    return maze.length;
}
Martin Frank
  • 3,238
  • 1
  • 25
  • 41