Question
Write a Maze class with the following requirements: Takes any two-dimensional array that represents a maze: 0s (zeros) for the walls, and 1s for the
Write a Maze class with the following requirements:
Takes any two-dimensional array that represents a maze: 0s (zeros) for the walls, and 1s for the available paths. A zip file has been uploaded to the project folder for you to complete.
There will be only one constructor that takes a two-dimensional array
A method named displayMaze should print the maze (bird-view) that shows the walls and the available paths,
A method named displayPath should print the maze (bird-view) that shows the walls and the available paths,and the route for the solution until the current location (for example the location of the mouse and the path it took from start point to current location)
A method named takeStep that takes one step each time when it is called and displays the maze again
A method named findExit runs and finds the solution all the way to the exit and displays the maze showing thesuggested path
Assume that entry point of the maze is always the last row and the first column and the first move direction will be to the north. Therefore, with the constructor, set the location and direction properly. Ignore checking exit for the first 3 steps.
Test program is given for your testing
The program should work with any Maze including different size of maze.
Here is the code I have so far:
package Maze;
import java.util.Scanner;
public class Maze {
private char direction;
private int r; // x position of the mouse
private int c; //y position of the mouse
private int counter = 0;
private boolean exitFound = false;
private int[][] maze;
//takes passed 2d array, sets mouse's y coordinates based on the length of the array, sets the direction to north, and sets the passed array to a local array
public Maze(int[][] arrMaze) {
r = 0;
c = arrMaze.length-1;
direction = 'N';
maze = arrMaze;
}
//Prints out the maze without solution, editing array so that the mouse is represented
public void displayMaze(int[][] dispMaze) {
dispMaze[c][r] = 3;
for(int i = 0; i < dispMaze.length;i++) {
for(int j = 0; j < dispMaze[0].length;j++) {
System.out.print(dispMaze[i][j]);
}
System.out.println();
}
}
//displays the Maze with the path taken
public void displayPath() {
displayMaze(maze);
}
//takes user input and adjusts coordinates of mouse as directed after validating input
public boolean takeStep() {
//creates temporary values for x and y, along with value that determines wheither the loop repeats
int x,y;
boolean repeat;
Scanner scan = new Scanner(System.in);
//asks user for direction to be moved to
System.out.println("Please enter the direction you would like to take with the WASD keys. W=North\tA=West\tS=South\tD=East");
//loop which checks if user input is a valid movement and requests different value if movement invalid
do {
String dir = scan.nextLine().trim().toUpperCase();
repeat = false;
//sets temporary values based on user input, representing space to be moved into in maze
switch(dir.charAt(0)) {
case 'W':
x = r;
y = c-1;
break;
case 'A':
x = r-1;
y = c;
break;
case 'S':
x = r;
y = c+1;
break;
case 'D':
x = r+1;
y = c;
break;
default:
repeat = true;
x = r;
y = c;
}
//checks if user input is a valid path, calls appropriate movement method, otherwise repeats loop requesting a different direction
if(x >= 0 && y >= 0 && y <= maze.length-1 && x <= maze[0].length-1 && maze[y][x] == 1 && repeat != true) {
switch(dir.charAt(0)) {
case 'W':
moveNorth();
break;
case 'A':
moveWest();
break;
case 'S':
moveSouth();
break;
case 'D':
moveEast();
break;
}
repeat = false;
} else {
System.out.println("Your input is a non-valid path, please enter a valid direction of movement");
repeat = true;
}
} while(repeat);
//shows path taken so far by user
displayPath();
return isAnExit();
}
//all moveDirection() methods give user feedback on which direction they moved and adjust the x/y values accordingly
private void moveNorth() {
System.out.println("Moved North");
c = c-1;
}
private void moveSouth() {
System.out.println("Moved South");
c = c+1;
}
private void moveEast() {
System.out.println("Moved East");
r = r+1;
}
private void moveWest() {
System.out.println("Moved West");
r = r-1;
}
//method to find if current position of mouse is a valid exit
private boolean isAnExit() {
//counter variable so that method does not check for a exit until method is called three times
if(counter < 3) {
counter++;
} else {
//checks if current player position is on valid exit space, defined by being a space on the most distant x value of the array
if(r == maze[0].length-1) {
exitFound = true;
} else {
exitFound = false;
}
}
return exitFound;
}
//finds the path without stopping at every step
public void findExit(int[][] arrMaze) {
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started