1
I’m trying to implement the Depth first algorithm to solve a maze, like the one below, where the starting position is S and the output is E
_SW______\n
_WWW_W_WW\n
_____W__E\n
In the Mazereader class I read the file . txt for the Maze array which should then have its contents imported into the Depth first class
public class MazeReader extends JFrame {
private static char[][] Maze;
private static ArrayList<String> mazeBuffer;
private static int startrow,startcol,finishcol,finishrow;
private final List<Integer> path = new ArrayList<Integer>();
private int pathIndex;
public MazeReader() {
setTitle("Maze");
setSize(640,480);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DepthFirst.searchPath(Maze, 0, 1, path);
pathIndex = path.size() - 2;
}
public static void readTextFile(String filename) throws MazeFileWrongChar, MazeFileNumCols {
startrow=startcol=finishcol=finishrow=-1;
mazeBuffer=new ArrayList<String>();
int numCols=0;
try {
Scanner file=new Scanner(new File(filename));
while(file.hasNext()) {
String nextLine=file.nextLine().replace("\\n", "");
mazeBuffer.add(nextLine);
if(nextLine.length()>numCols) {
numCols=nextLine.length();
}
}
}catch(Exception e) {
System.out.println(filename + "there is a problem");
}
int numrows=mazeBuffer.size();
System.out.println("number of rows: "+ numrows + "\nnumber of columns: " + numCols);
Maze=new char[numrows][numCols];
for(int i=0;i<numrows;i++) {
String row = mazeBuffer.get(i);
for (int j = 0; j < numCols; j++) {
try {
Maze[i][j] = row.charAt(j);
} catch (Exception e) {
throw new MazeFileNumCols(j);
}
if(Maze[i][j]!=('S') && Maze[i][j]!=('W') && Maze[i][j]!=('_') && Maze[i][j]!=('E') && Maze[i][j]!=(' ') ) {
throw new MazeFileWrongChar(i,j);
}
}
}
}
Below the code of the class Depth first
public class DepthFirst {
public static boolean searchPath(char[][] maze,int x,int y, List<Integer> path) {
if(maze[y][x]=='E') {
path.add(x);
path.add(y);
return true;
}
if(maze[y][x]=='_') {
int dx=-1;
int dy=0;
if(searchPath(maze,x+dx,y+dy,path)) {
path.add(x);
path.add(y);
return true;
}
dx = 1;
dy = 0;
if (searchPath(maze, x + dx, y + dy, path)) {
path.add(x);
path.add(y);
return true;
}
dx = 0;
dy = -1;
if (searchPath(maze, x + dx, y + dy, path)) {
path.add(x);
path.add(y);
return true;
}
dx = 0;
dy = 1;
if (searchPath(maze, x + dx, y + dy, path)) {
path.add(x);
path.add(y);
return true;
}
}
return false;
}
}
Here is the main class
public class Main {
static MazeReader reader = new MazeReader();
public static void main(String[] args) throws MazeFileWrongChar,MazeFileNumCols {
try {
MazeReader.readTextFile("maze.txt");
reader.printMaze();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MazeReader view = new MazeReader();
view.setVisible(true);
}
});
} catch (MazeFileWrongChar e ) {
System.out.println(e.getMessage());
} catch(MazeFileNumCols e) {
System.out.println(e.getMessage());
}
}
}
I’m getting the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.NullPointerException
at DepthFirst.searchPath(DepthFirst.java:10)
at MazeReader.<init>(MazeReader.java:23)
But I am getting the Nullpointerexception error because supposedly the Maze array is not being initialized correctly, some way to get it done?
From what I understand you manipulate the same class in 2 different threads. Have you tried to do everything within the run()?
– user28595
yes handle different threads because it is a requirement, and hence the problems
– MDordio
Do you know that the instance of
MazeReader
outside the EDT is not the same used inside this ne thread? And you do not need to instantiate an object to manipulate statics.– user28595
And that’s what’s causing the problem?
– MDordio
Maybe. Try removing the instance in main.
– user28595
if I do, I get the error: Exception in thread "AWT-Eventqueue-0" java.lang.Arrayindexoutofboundsexception: -1 At Depthfirst.searchPath(Depthfirst.java:10)
– MDordio