Search code examples
javarecursionmaze

Java Recursive Maze


I have this assignment in which I'm supposed to create a maze solver in Java. The algorithm I decided to apply works the following way: it's a recursive method to move that calls itself again each time it finds a path. If it runs into a dead end, it calls the second recursive method "goBack", which keeps going back until it founds a new path. Walls are 0s, paths are 1s, walked paths are 2s and paths that were walked on twice are 3s. The idea is pretty simple, but I just can't get it to work. ArrayOutOfBounds exception keeps showing up all the time. Does anybody have any ideas on this?

public class Project5v2 {

static String mazecsv = "/Users/amorimph/Documents/COMP 182/Project 5/mazeinput.csv";
static File solvedMaze = new File("/Users/amorimph/Documents/COMP 182/Project 5/solvedMaze.txt");
static int[][] maze = new int[50][50];
static int trigger = 0;
static int mazeWidth;
static int mazeHeight;

public static void main(String[] args) {

    readCSV(mazecsv);
    start(maze);
    mazeToString(maze);

}

public static void readCSV(String csvfile) {

    BufferedReader br = null;
    String line = "";
    String csvSplitBy = ",";
    int x = 1;
    int y = 0;


    try {

        br = new BufferedReader(new FileReader(csvfile));
        br.readLine();

           while ((line = br.readLine()) != null) {

               String[] info = line.split(csvSplitBy);

               for (x = 1; x < info.length; x++) {                       
                      maze[y][x] = Integer.parseInt(info[x]);
                }
                mazeWidth = info.length;
               y++;
               mazeHeight = y;

             }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 }

public static void start(int[][] maze) {

int i = 0;
while(maze[0][i] != 1) {
    i++;
}
System.out.println(i);
move(maze,i,1);

}

public static void move(int[][] maze, int x, int y) {


for (int i = 0; i < 4; i++) {
    switch(i) {

    case 0: if(maze[x][y-1] == 1) {
        maze[x][y] = 2;
        maze[x][y-1] = 2;
        move(maze, x, y-1);
        break;
    }

    case 1: if(maze[x-1][y] == 1) {
        maze[x][y] = 2;
        maze[x-1][y] = 2;
        move(maze, x-1, y);
        break;
    }

    case 2: if(maze[x+1][y] == 1) {
        maze[x][y] = 2;
        maze[x+1][y] = 2;
        move(maze, x+1, y);
        break;
    }

    case 3: if(maze[x][y+1] == 1) {
        maze[x][y] = 2;
        maze[x][y+1] = 2;
        move(maze, x, y+1);
        break;
    }

    //case 4:
    //  maze[x][y] = 2;
    //  goBack(maze, y, x);
    //  break;

    }
}
}

public static void goBack(int[][] maze, int x, int y) {

for (int i = 0; i < 7; i++) {
    switch(i) {

    case 0: if(maze[x][y-1] == 1) {
        maze[x][y] = 2;
        maze[x][y-1] = 2;
        move(maze, x, y-1);
        break;
    }

    case 1: if(maze[x-1][y] == 1) {
        maze[x][y] = 2;
        maze[x-1][y] = 2;
        move(maze, x-1, y);
        break;
    }

    case 2: if(maze[x+1][y] == 1) {
        maze[x][y] = 2;
        maze[x+1][y] = 2;
        move(maze, x+1, y);
        break;
    }

    case 3: if(maze[x][y+1] == 1) {
        maze[x][y] = 2;
        maze[x][y+1] = 2;
        move(maze, x, y+1);
        break;
    }

    case 4: if(maze[x][y+1] == 2) {
        maze[x][y] = 3;
        goBack(maze, x, y+1);
        break;
    }

    case 5: if(maze[x+1][y] == 2) {
        maze[x][y] = 3;
        goBack(maze, x+1, y);
        break;
    }

    case 6: if(maze[x-1][y] == 2) {
        maze[x][y] = 3;
        goBack(maze, x-1, y);
        break;
    }

    case 7: if(maze[x][y-1] == 2) {
        maze[x][y] = 3;
        goBack(maze, x, y-1);
        break;
    }
    }
}
}

public static void FWriter(String content, File file) {

try {         
       if (!file.exists()) {
        file.createNewFile();
       }     
       FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
       BufferedWriter bw = new BufferedWriter(fw);
       bw.write(content);
       bw.close();           
}         
catch (IOException e) {
        e.printStackTrace();
}
}

public static void mazeToString(int[][] maze) {

    for(int i = 0; i < mazeHeight; i++) {
        for(int j = 0; j < mazeWidth; j++) {
            FWriter(Integer.toString(maze[i][j]), solvedMaze);
        }
        FWriter("\n", solvedMaze);
    }
}

}

Solution

  • Your maze is 50*50.

    Looking into your move method, I can't see anything that stops you from falling out of the maze (i.e. moving to an index larger than 49 or lower than 0).

    More generally, If you want to use recursion, and you don't want to end up in trouble, you must check for when you're done. Your move method will never stop calling itself, so think about at what point the move method should terminate.