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!