Hey need some help with a java project! Comments would be a big help! This assig
ID: 3595623 • Letter: H
Question
Hey need some help with a java project! Comments would be a big help!
This assignment involves writing a program using recursion to find a path through a maze. The “maze” is represented as a 15 x 15 element integer array. Each element in the array will hold either a 1 or a 0. A “1” indicates an empty pathway in the maze; a “0” indicates a wall. The entry into the maze is through element [0,0] and the exit is through element[14,14]
You will write a recursive method that receives two integer values, a “row/column” pair that represents your current location in the maze. When the method is called for the first time it will receive the pair [0,0]. The traversal is successful when the end of the array is reached, which is when the method receives the pair [14,14]. When the end of the maze is reached, your method should return TRUE. It should return FALSE, if a move runs into a wall, a square that has already been visited, or outside the bounds of the array. AS we traverse the maze, we mark each cell visited, by using a 3, finally, the correct path through your maze, should be marked by storing a 7 in the array elements.Once in the maze, you can only move up, down, left or right, as long as you do not run into a “wall” or outside the boundaries of our array. So if we are at position (0,0), we can only try element (1,0) and element (0,1). We may NOT go outside of the bounds of our array, and we can not move diagonally. Each attempted move is a call to your function.
AS we visit locations, we need to mark them as “visited”, by placing a “3” into them, sometimes paths overlap, and there may be more than one possible path through the maze, marking a cell as visited, can help us from revisiting a path that has already been proven to be unsuccessful, thus avoiding infinite recursion.
For this solution you MUST create the following:
In a package named project1
A class containing a main method that is responsible for instantiating the array representing the matrix, prompting the user for a file name containing the maze contents and opening a Scanner attached to the file, and calling all of the other methods. THIS METHOD MUST ALSO USE TRY/CATCH BLOCKS TO INSURE THAT THE FILES OPENS CORRECTLY.
A separate MazeOperations class which contains 3 methods to manipulate a maze:
public static void fillMaze(int[][] theMaze, Scanner inputFile) : This method will fill the maze from a data file containing integers. You may assume that the data file will contain enough numbers to completely fill the maze with correctly read. EACH integer in the file is separated by a space, so that you can use nextInt() to read the integers.
public static void printMaze(int[][] theMaze) : This method is called before and after the traversal to print out the contents of the maze. Each element in the maze should be printed in a row dominate fashion, one row per line. Each value in the row should be separated by a single space so that the maze contents will line up in rows and columns. IE: Sample maze is smaller to save space.
1 1 0 1 0
1 0 0 0 0
1 1 1 0 0
0 0 1 0 0
1 1 1 1 1
public static boolean traverseMaze( int[][] theMaze, int row, int col) : this is the recursive traversal algorithm. When called from main for the first activation, it will be invoked with col == 0 and row == 0
You should use the printMaze function to validate that your traversal algorithm correctly finds a path through the maze, and marks each cell that is part of the path with a 7.
Here is the Testdata
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 1 1 1 0 0 0 0 0
0 0 1 0 0 0 1 1 0 1 0 0 0 0 0
0 0 1 0 0 0 1 0 0 1 0 0 0 0 0
0 0 1 1 1 1 1 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
//Defines class MazeNumber
public class MazeNumber
{
//Set the size of the matrix
static int N;
//Declares maze question 2d matrix
static int question[][];
//Method to print solution matrix solution[N][N]
void printMaze(int solution[][])
{
//Loops till N number of rows
for (int r = 0; r < N; r++)
{
//Loops till N number of columns
for (int c = 0; c < N; c++)
System.out.print(" " + solution[r][c] + " ");
System.out.println();
}//End of inner for loop
}//End of method
// Method to check if r,c is valid index for N * N maze
boolean isSafe(int maze[][], int r, int c)
{
// Checks if (r, c outside maze) return false
return (r >= 0 && r < N && c >= 0 && c < N && maze[r][c] == 1);
}//End of method
// Method returns false if no path is possible.
// Otherwise return true
// Prints the solution path in the form of 1 dots.
boolean traverseMaze(int maze[][], int row, int column)
{
//Creates a 2D matrix for solution and initializes it to 0
int solution[][] = new int [row][column];
for(int r = 0; r < row; r++)
{
for(int c = 0; c < column; c++)
{
solution[r][c] = 0;
}//End of inner for loop
}//End of outer for loop
//Checks whether solution available or not
if (solveMazeUtil(maze, 0, 0, solution) == false)
{
System.out.print("Solution doesn't exist");
return false;
}//End of if
// Displays the solution
printMaze(solution);
//Returns true for success
return true;
}//End of method
// Recursive method to give solution to maze
boolean solveMazeUtil(int question[][], int r, int c, int solution[][])
{
// Checks if (r, c is goal) return true
if (r == N - 1 && c == N - 1)
{
// Assigns "." dot to the r and c index position of the solution matrix
solution[r][c] = 1;
//Returns true
return true;
}//End of if
// Check if maze[r][c] is valid
if (isSafe(question, r, c) == true)
{
// Assigns "." dot to the row and column index position of the solution matrix
solution[r][c] = 1;
// Move forward in row direction
if (solveMazeUtil(question, r + 1, c, solution))
return true;
// Checks if moving in r i.e., row direction doesn't give solution then move down in c i.e., column direction
if (solveMazeUtil(question, r, c + 1, solution))
return true;
// Checks if none of the above movements work then use back tracking
// Mark row and column as part of solution path to 0
solution[r][c] = 0;
//Return false for not success
return false;
}//End of if
return false;
}//End of method
//main method definition
public static void main(String ss[])
{
//Creates an object of the class Maze
MazeNumber rat = new MazeNumber();
Scanner input = new Scanner(System.in);
String fileName = "";
//Try block
try
{
System.out.println("Enter the file name: ");
fileName = input.next();
//File Reader object created to read data from Popularity.txt file
File reader = new File(fileName);
//Scanner class object created
Scanner sc = new Scanner(reader);
//Reads number or row and column for 2D matrix from file
N = sc.nextInt();
//Allocate memory for 2D matrix
question = new int[N][N];
//Loops till data available
while(sc.hasNextInt())
{
//Creates a 2D matrix question for maze and initializes the problem
//Loops till number of rows
for(int x = 0; x < N; x++)
{
//Loops till number of columns
for(int y = 0; y < N; y++)
{
//Reads data and stores in matrix
question[x][y] = sc.nextInt();
//Displays the number
}//End of inner for loop
}//End of outer for loop
}//End of while
sc.close();
}//End of try block
//Catch block to handle file not found exception
catch(FileNotFoundException e)
{
System.out.println("File Not found");
e.printStackTrace();
}//End of catch
//Calls the method solveMaze() to give solution
rat.traverseMaze(question, N, N);
input.close();
}//End of main method
}//End of class
File Name: MazeData.txt
15
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 1 1 1 0 0 0 0 0
0 0 1 0 0 0 1 1 0 1 0 0 0 0 0
0 0 1 0 0 0 1 0 0 1 0 0 0 0 0
0 0 1 1 1 1 1 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Sample Run1:
Enter the file name: MazeData.txt
Solution doesn't exist
File Name: MazeData1.txt
5
1 1 0 1 0
1 0 0 0 0
1 1 1 0 0
0 0 1 0 0
1 1 1 1 1
Sample Run2:
Enter the file name: MazeData1.txt
1 0 0 0 0
1 0 0 0 0
1 1 1 0 0
0 0 1 0 0
0 0 1 1 1