Maze Solver T > ☐ ☑ va src\MazeTest.java × TextFileWriter.java 11 at: (9, 8) ll at: (7, 8) ll at: (8, 9) ll at: (8, 7) Visiting: (8, 6) Visiting: (9, 6) Out of bounds or already visited or wall at: (9, 6) Visiting: (7, 6) Out of bounds or already visited or wall at: (7, 6) Visiting: (8, 7) Out of bounds or already visited or wall at: (8, 7) Visiting: (8, 5) Out of bounds or already visited or wall at: (8, 5) No path found. □ MazeSolver.java > src > MazeTest 46°F Rain showers O Search Current File v Єmaze2.txt ☐ Ő oct A1 8:1 (543 chars, 18 line breaks) IJ ... @ Х CRLF UTF-8 4 spaces 12:29 PM 11/4/2024
Maze Solver T > ☐ ☑ va src\MazeTest.java × TextFileWriter.java 11 at: (9, 8) ll at: (7, 8) ll at: (8, 9) ll at: (8, 7) Visiting: (8, 6) Visiting: (9, 6) Out of bounds or already visited or wall at: (9, 6) Visiting: (7, 6) Out of bounds or already visited or wall at: (7, 6) Visiting: (8, 7) Out of bounds or already visited or wall at: (8, 7) Visiting: (8, 5) Out of bounds or already visited or wall at: (8, 5) No path found. □ MazeSolver.java > src > MazeTest 46°F Rain showers O Search Current File v Єmaze2.txt ☐ Ő oct A1 8:1 (543 chars, 18 line breaks) IJ ... @ Х CRLF UTF-8 4 spaces 12:29 PM 11/4/2024
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 14RQ
Related questions
Question
Hi, I havelogic issue in my code that need your assistance on it. The maze is working well, but it can find a path, i assumed its a logical issues in the code. I have attached the output.
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);
}
}
}
}
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
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Recommended textbooks for you
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT