Here are my maze class and my test class. \"The test class is already done and c
ID: 3624208 • Letter: H
Question
Here are my maze class and my test class. "The test class is already done and correct im just putting it in here for reference." I have posted the method specs below that for the methods in the maze class. The higlighted ones are the ones giving me trouble,for fillMaze() im guessing its just something dumb that I'm not doing right. The one that I'm have the most trouble with is the one that actually traverses the maze. If you could also look and check the rest to make sure that it looks right as the method specs describe that would be great.I know there is a ton of writing at the bottom, especially about the traverseMaze() method, but I thought the more info there was about it the easier it would be to help.
import java.util.*;
import java.io.*;
public class maze
{
private char mazeArray[][];
private int rows;
private int cols;
private char wall ;
private char path;
public maze(int rows, int cols, char wall, char path)
{
this.rows = rows;
this.cols = cols;
this.wall = wall;
this.path = path;
char[][] mazeArray = new char[rows][cols];
}
public void fillMaze()
{
System.out.println("Enter the name of the file");
Scanner input = new Scanner(new FileReader(input.next()));
The two test mazes look as follows:
V V N N N
N V V N N
N N V V V
N N V N V
1 0 0 0 0 1 1 1 1
1 1 1 1 1 1 0 0 0
0 0 0 1 0 0 1 1 1
0 1 1 1 1 0 1 0 1
1 1 0 0 1 1 1 0 1
1 0 1 1 1 0 0 0 1
1 0 0 0 0 0 1 1 1
1 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1
// the two test mazes are tittled test1.txt and test2.txt
}
public void printMaze()
{
System.out.println(mazeArray[rows][cols]);
}
public Boolean traverseMaze(int row,int column)
{
// 1
if
(mazeArray[rows][cols] !='3' || mazeArray[rows][cols] != 7 || mazeArray[rows][cols] != wall
&& mazeArray[rows][cols] < rows && mazeArray[rows][cols] < cols)
mazeArray[rows][cols] = 3;
//2
if(mazeArray[rows][cols] =rows && mazeArray[rows][cols] = cols)
mazeArray[rows][cols] = 7;
return true;
//3 a
Else
{
mazeArray[rows][cols] = mazeArray[rows+1][cols];
//b
if mazeArray
}
}
}
__________________________________________________________________________
Test Class
public class TestMaze {
public static void main(String[] args) throws Exception{
Maze m1 = new Maze(4,5,'N','V');
// {
m1.fillMaze();
System.out.println("Maze");
m1.printMaze();
m1.traverseMaze(0,0);
System.out.println("");
System.out.println("Traversed");
m1.printMaze();
// }// catch(Exception e) {
//System.out.println("Exception Thrown: " + e.getMessage());
//}
Maze m2 = new Maze(9,9,'0','1');
//try {
m2.fillMaze();
System.out.println("Maze");
m2.printMaze();
m2.traverseMaze(0,0);
System.out.println("");
System.out.println("Traversed");
m2.printMaze();
//} catch(Exception e) {
//System.out.println("Exception Thrown: " + e.getMessage());
//}
}
}
_______________________________________________________________________________________
Public Operations:
public Maze(int rows,int cols, char wall, char path)
Maze constructor: This constructor that recieves 4 parameters, the number of rows, and number of columns, wall character, and space charac ter. THis constructor stores the parameters into the private data memebers and allocates a character array with the number of rows and columns indicated. The wall and path characters can NOT be a '3' or '7'.
public void fillMaze()
fillMaze: This method prompts the user for the name of an input data file, and reads the contents of the file into the maze array. The contents of the file will be a series of characters, separated by spaces.
public void printMaze()
printMaze: Prints the contents of the maze array, one row per line.
public Boolean traverseMaze(int row,int cols)
1. If the indicated location has not been visited before(doesn't contain 3),is not outside the bounds of the array, or does not contain a wall, we mark the current element as visited.
2. If we are the in the LAST element in the array(row,column),then we are done,and we mark the cell as being part of the solution by storing '7',and finally the method should exit returning true.
3. Else
a. We move down one element
b. If the previous call was not "done" , try to move one element to the left
c. If the previous call was not done, try to move one element up.
d. if not done, try moving one element to the right.
Here's some extra information more detailed about how traverseMaze() works:
Suppose the "maze" is represented as a 15 x 15 element character array.(Can be of any size). 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.(Also must work for letters). The entry into the maze is through element[0,0], and the exit is through element[14,14] in this example.
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 the row/column pair 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 the maze, should be marked by storing a '7' in the array elements.
Once in the maze, we mark the cell as visited, storing a '3'. Next you can only move up,down,left or right, as long as you don't run into a "wall" or outside the boundaries of the array.Once the end of the maze is reached, we want to set every cell in the correct path to 7, to indicate that it is part of the solution. So once the end is reached, as the last step in the recursive function, if it is "done" store 7 in the current element and exit returning true. Each of the active recursive calls will in turn place a 7 in each of the elements pointed to by the tow/column pair they received.
Explanation / Answer
please rate - thanks
hope this is good or at least helpful
import java.util.*;
import java.io.*;
public class Maze
{
private char mazeArray[][];
private int rows;
private int cols;
private char wall ;
private char path;
public Maze(int rows, int cols, char wall, char path)
{
this.rows = rows;
this.cols = cols;
this.wall = wall;
this.path = path;
mazeArray = new char[rows][cols];
}
public void fillMaze()throws FileNotFoundException
{Scanner in=new Scanner(System.in);
System.out.println("Enter the name of the file");
Scanner input = new Scanner(new FileReader(in.next()));
int i;
int j;
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
mazeArray[i][j]=input.next().charAt(0);
}
public void printMaze()
{int i;
int j;
for(i=0;i<rows;i++)
{for(j=0;j<cols;j++)
System.out.print(mazeArray[i][j]+" ");
System.out.println();
}
}
public Boolean traverseMaze(int row,int column)
{if( row>=rows||column>=cols||row<0||column<0||
mazeArray[row][column] =='3'|| mazeArray[row][column]== wall )
return false;
mazeArray[row][column]='3';
if(row==rows-1&&column==cols-1)
{mazeArray[row][column]='7';
return true;
}
else
{
traverseMaze(row+1,column);
traverseMaze(row,column-1);
traverseMaze(row-1,column);
traverseMaze(row,column+1);
}
mazeArray[row][column]='7';
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.