Question: Hi , run my code and see where logic error is and update it . It work well, however, its not finding appropriate path. import
Hi run my code and see where logic error is and update it It work well, however, its not finding appropriate path.
import javax.swing.;
import java.awt.;
import java.ioBufferedReader;
import java.ioFileReader;
import java.ioIOException;
class MazeSolver extends JFrame
private static int n;
private static char maze;
private boolean visited;
private int startX, startY, goalX, goalY;
private MazePanel mazePanel;
public MazeSolverString filename throws IOException
readMazefilename;
visited new booleannn;
setTitleMaze Solver";
setSizen n ;
setDefaultCloseOperationJFrameEXITONCLOSE;
mazePanel new MazePanelmaze n;
addmazePanel;
setVisibletrue;
public void readMazeString filename throws IOException
BufferedReader br new BufferedReadernew FileReaderfilename;
n Integer.parseIntbrreadLine;
maze new charnn;
for int i ; i n; i
String line brreadLine;
for int j ; j n; j
mazeij line.charAtj;
if mazeijS
startX i;
startY j;
else if mazeijG
goalX i;
goalY j;
brclose;
private boolean solveMazeint x int y
Debug information
System.out.printlnVisiting: x y ;
if x x n y y n mazexy# visitedxy
System.out.printlnOut of bounds or already visited or wall at: x y ;
return false;
if x goalX && y goalY
mazexyG;
mazePanel.repaint;
return true;
visitedxy true;
mazexy;
mazePanel.repaint;
try
Thread.sleep;
catch InterruptedException e
eprintStackTrace;
Try to move in all directions
if solveMazex y solveMazex y solveMazex y solveMazex y
return true;
Backtrack
mazexy;
mazePanel.repaint;
visitedxy false;
return false;
public void solveMaze
if solveMazestartX startY
System.out.printlnPath found!";
else
System.out.printlnNo path found.";
public static void mainString args throws IOException
MazeSolver solver new MazeSolvermazetxt;
solver.solveMaze;
for int i ; i n; i
for int j ; j n; j
System.out.printmazeij;
System.out.println;
public void displayMaze
class MazePanel extends JPanel
private final char maze;
private final int n;
public MazePanelchar maze, int n
this.maze maze;
this.n n;
@Override
protected void paintComponentGraphics g
super.paintComponentg;
int cellSize ;
for int i ; i n; i
for int j ; j n; j
switch mazeij
case # gsetColorColorBLACK;
case gsetColorColorWHITE;
case S gsetColorColorBLUE;
case G gsetColorColorRED;
case gsetColorColorGREEN;
default gsetColorColorWHITE;
gfillRectj cellSize, i cellSize, cellSize, cellSize;
gsetColorColorGRAY;
gdrawRectj cellSize, i cellSize, cellSize, cellSize;
Test Class
import java.ioIOException;
public class MazeTest
public static void mainString args throws IOException
MazeSolver solver new MazeSolvermazetxt;
solver.displayMaze; Display initial state of the maze
solver.solveMaze; Solve the maze using DFS
Maze file
G
#########
#########
######
########
#######
#########
#########
S#######
#########
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
