Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions