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.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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 MazeSolver(String filename) throws IOException {
readMaze(filename);
visited = new boolean[n][n];
setTitle("Maze Solver");
setSize(n *40, n *40);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mazePanel = new MazePanel(maze, n);
add(mazePanel);
setVisible(true);
}
public void readMaze(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
n = Integer.parseInt(br.readLine());
maze = new char[n][n];
for (int i =0; i < n; i++){
String line = br.readLine();
for (int j =0; j < n; j++){
maze[i][j]= line.charAt(j);
if (maze[i][j]=='S'){
startX = i;
startY = j;
} else if (maze[i][j]=='G'){
goalX = i;
goalY = j;
}
}
}
br.close();
}
private boolean solveMaze(int x, int y){
// Debug information
System.out.println("Visiting: ("+ x +","+ y +")");
if (x <0|| x >= n || y <0|| y >= n || maze[x][y]=='#'|| visited[x][y]){
System.out.println("Out of bounds or already visited or wall at: ("+ x +","+ y +")");
return false;
}
if (x == goalX && y == goalY){
maze[x][y]='G';
mazePanel.repaint();
return true;
}
visited[x][y]= true;
maze[x][y]='*';
mazePanel.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e){
e.printStackTrace();
}
// Try to move in all directions
if (solveMaze(x +1, y)|| solveMaze(x -1, y)|| solveMaze(x, y +1)|| solveMaze(x, y -1)){
return true;
}
// Backtrack
maze[x][y]='.';
mazePanel.repaint();
visited[x][y]= false;
return false;
}
public void solveMaze(){
if (solveMaze(startX, startY)){
System.out.println("Path found!");
} else {
System.out.println("No path found.");
}
}
public static void main(String[] args) throws IOException {
MazeSolver solver = new MazeSolver("maze.txt");
solver.solveMaze();
{
for (int i =0; i < n; i++){
for (int j =0; j < n; j++){
System.out.print(maze[i][j]+"");
}
System.out.println();
}
}
}
public void displayMaze(){
}
}
class MazePanel extends JPanel {
private final char[][] maze;
private final int n;
public MazePanel(char[][] maze, int n){
this.maze = maze;
this.n = n;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
int cellSize =40;
for (int i =0; i < n; i++){
for (int j =0; j < n; j++){
switch (maze[i][j]){
case '#'-> g.setColor(Color.BLACK);
case '.'-> g.setColor(Color.WHITE);
case 'S'-> g.setColor(Color.BLUE);
case 'G'-> g.setColor(Color.RED);
case '*'-> g.setColor(Color.GREEN);
default -> g.setColor(Color.WHITE);
}
g.fillRect(j * cellSize, i * cellSize, cellSize, cellSize);
g.setColor(Color.GRAY);
g.drawRect(j * cellSize, i * cellSize, cellSize, cellSize);
}
}
}
}
//Test Class
import java.io.IOException;
public class MazeTest {
public static void main(String[] args) throws IOException {
MazeSolver solver = new MazeSolver("maze2.txt");
solver.displayMaze(); // Display initial state of the maze
solver.solveMaze(); // Solve the maze using DFS
}
}
//Maze file
10
.........G
.#########
.#########
.#...#####
.#.#######
...#######
.#########
.#########
..S#######
.#########

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!