Question: Please correct the code and make sure it does the following: Write a MazeSolver class in Java. This program needs to prompt the user for

Please correct the code and make sure it does the following:

Write a MazeSolver class in Java. This program needs to prompt the user for a maze filename and then explore the maze. Display how many exits were found and the positions (not indices) of the valid exits. Your program can display the valid exits found in any order. Run the code in your program to verify that it runs.

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays;

public class MazeSolver { int numRows; int numCols; int startRow; int startCol; int rows; int col; public static void main (String[] args){ MazeSolver addmaze = new MazeSolver(); addmaze.loadMaze(); } public void loadMaze(){ //int numRows; //int numCols; int startRow; int startCol; int rows; int col; try{ Scanner input = new Scanner(System.in); System.out.println("Please enter file name:"); String filename = input.nextLine(); File file = new File(filename); Scanner fileInput = new Scanner(file); numRows = fileInput.nextInt(); numCols = fileInput.nextInt(); ArrayList wallCols= new ArrayList(); ArrayList wallRows = new ArrayList(); String[][] maze= new String [numRows][numCols]; //Reads in maze data for (int i = 0; i exitRows = new ArrayList(); ArrayList exitCols = new ArrayList(); //int numRows; boolean[][] visited = new boolean[numRows][numCols]; // Call recursive method to explore the maze exploreMaze(startRow, startCol, visited, maze, exitRows, exitCols); // Print number of exits found System.out.println("Number of exits found: " + exitRows.size()); // Print positions of exits for (int i = 0; i exitRows, ArrayList exitCols) { // Recursive method to explore the maze if (row = numRows || col >= numCols || visited[row][col]) { // Base case: if current position is out of bounds or already visited return; } // Base case: if current position is an exit if (maze[row][col] == ' '){ exitRows.add(row); exitCols.add(col); return; }

// Mark current position as visited visited[row][col] = true; exploreMaze(row-1, col, visited, maze,exitRows, exitCols); // up exploreMaze(row, col+1, visited, maze,exitRows, exitCols); // right exploreMaze(row+1, col, visited, maze,exitRows, exitCols); // down exploreMaze(row, col-1, visited, maze,exitRows, exitCols); // left

// Recursively explore neighboring positions }

}

Here are the maze files that I will be inserting to test it.

Please correct the code and make sure it does the following: Write

mazec 35 \# \# \# \#\# \#\#

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 Databases Questions!