1

I know this is a common topic, but none of the other posts could help me with my program.

In this Maze we have to read a file which in it's first line has the width and height of our array, in the second line has the coordinations of the starting point E and from the third line the MazeRunner array begins. I have to follow all the 0's until I find an exit.

Here is an example :

8 7
0 3
1 1 1 1 1 E 1
1 0 0 0 1 0 1
1 0 1 0 0 0 1
1 1 1 0 1 1 1
1 0 0 0 0 0 1
1 0 1 1 1 0 1
1 1 0 0 0 0 1
0 1 0 1 1 1 1

In the following code, the readFile method works fine. I have a problem figuring out how to take advantage of my stack (which is defined in another file).The solveMaze method starts just fine but when it comes to a point where there are 2 or more zeros near the point we currently are it just follows the first direction that it finds which leads to an infinite loop. Apparently, I can't find a way for backtracking..

The Coords class is also defined in another file and it just creates a point.

import java.io.*;

public class Maze{

private static boolean foundExit = false; /* Decides if the maze has a possible exit or not. */
private static int row, col;
private static Stack<Coords> stack = new Stack<Coords>();
private static char[][] MazeRunner;
private static int width, height;
private static int rowCoord,colCoord;

public static void readFile(){
    File file = null;
    BufferedReader reader = null;
    int counter=0;

    try {
        file = new File("C:/Users/user01/workspace/p1/src/thiseas.txt");
    } catch (NullPointerException e) {
        System.err.println("File not found.");
    }

    try {
        reader = new BufferedReader(new FileReader(file));
    } catch (FileNotFoundException e) {
        System.err.println("Error opening file!");
    }
    try{
        String line=reader.readLine();
        String[] split=line.split(" ");
        height = Integer.parseInt(split[0]);
        width = Integer.parseInt(split[1]);
        counter++;
        line=reader.readLine();
        split=line.split(" ");
        rowCoord=Integer.parseInt(split[0]);
        colCoord=Integer.parseInt(split[1]);
        counter++;
        MazeRunner=new char[height][width];
        char[] ch;
        while((line=reader.readLine())!=null){
            ch = line.toCharArray();
            for(int i = 0;i < 2*width-1;i+=2){
                MazeRunner[counter-2][i/2] = ch[i];
            }
            counter++;
        }

    }catch (IOException e) {
        System.out.println("Error reading line " + counter + ".");
    }

    solveMaze(rowCoord, colCoord);
}

private static void solveMaze(int a, int b) {
    row = a;
    col = b;
    Coords coor=new Coords(row,col); //coordinations
    stack.push(coor);

    int j=0; //counts how many '0' are near our pointer

    while (foundExit == false) {

        if (col > 0 && MazeRunner[row][col-1] == '0') { //goes left
            if(col-1 == 0) {
                foundExit = true;
            }else {
                coor.setCol(col-1);
                j++;
                stack.push(hold);
            }

        }
        if (col < width && MazeRunner[row][col+1] == '0') { //goes right
            if(col+1 == width) {
                foundExit = true;
            }else if(j==0){
                coor.setCol(col+1);
                j++;
                stack.push(hold);
            }
        }
        if (row > 0 && MazeRunner[row-1][col] == '0') { //goes up
            if(row-1 == 0) {
                foundExit = true;
            }else {
                coor.setRow(row-1);
                j++;
                stack.push(hold);
            }

        }
        if(row<height && MazeRunner[row+1][col] == '0'){ //goes down
            if(row+1 == height) {
                foundExit = true;
            }else{
                coor.setRow(row+1);
                j++;
                stack.push(hold);
            }

        }
        row = coor.getRow(); 
        col = coor.getCol();
        //row and col will get wrong values because hold changes
        coor= stack.peek();
    }
    System.out.println("The maze has been solved!. ");
    System.out.println("Exit coordinations: " + coor.toString());
}

public static void main(String[] args) {
    readFile(); 
}

}

Any help would be appreciated!

Darshan Patel
  • 2,839
  • 2
  • 25
  • 38
George.K
  • 13
  • 1
  • 8
  • Possible duplicate of [Using a stack to traverse and solve a maze - Java](http://stackoverflow.com/questions/9196514/using-a-stack-to-traverse-and-solve-a-maze-java) – antlersoft Nov 11 '16 at 23:49
  • If you want a super easy and naive approach, just always follow the left-hand wall from the starting point until you're on one of the borders (which means you found an exit). No stack/recursion necessary. It's not going to be efficient, but it's pretty easy to code up. Just a while loop and a couple if/else statements – Araymer Nov 12 '16 at 02:50
  • @Araymer 7 Well, I can't understand how would that work. This would only find a solution if all the '0' where left from the starting point, otherwise it would return false coordinations. – George.K Nov 12 '16 at 10:28
  • You just make sure there's always a wall on your left, and if a wall is in front of you, you turn left and try to go straight again. If there's a wall there, turn left and go straight again, etc.... You'll retrace some squares, but eventually you'll find a way out. Just have to keep track of the direction you're "facing" and a boolean condition on whether the wall on the left is there or not (always make left turns when they pop up) – Araymer Nov 14 '16 at 15:27

0 Answers0