Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this java maze project as I'm not sure how to complete it or if I'm doing it right. Maze.java public class

I need help with this java maze project as I'm not sure how to complete it or if I'm doing it right.

Maze.java

public class Maze

{

private char direction;

private int r = 16; // x position of the mouse - starting row position

private int c = 1; //y position of the mouse - starting column position

private boolean exitFound = false;

public Maze(int[][] arrMaze) {

}

//Prints out the maze without solution

public void displayMaze(int[][] arrMaze)

{

//display the maze putting blank spaces where there are 1's in the array and putting

//display "#" where the zeroes are

for(int i = 0; i < arrMaze.length; i++){

System.out.println(" ");

for(int j = 0; j < arrMaze[i].length; j++){

if(arrMaze[i][j] == 1){

System.out.print(" ");

} if(arrMaze[i][j] == 0) {

System.out.print("#");

} if(arrMaze[i][j] == 2) {

System.out.println("@");

} if(arrMaze[i][j] == 3) {

System.out.println("~");

}

}

}

}

//displays the Maze with the path taken

public void displayPath(int[][] arrMaze)

{

//show the user how far the mouse has gone since the start.

//The path the mouse has gone will be filled in but the path ahead will not.

for (int i = 0; i < arrMaze.length; i++) {

System.out.println(" ");

for (int j = 0; j < arrMaze[i].length; j++) {

if (arrMaze[r][c] == 3) {

System.out.print("@");

} else if (arrMaze[r][c] == 0) {

System.out.print("#");

} else if (arrMaze[r][c] == 1) {

System.out.print(" ");

} else if (arrMaze[r][c] == 2) {

System.out.print("~");

} else {

}

}

}

}

//checks to see if a step can be taken in the designated direction

//will add a ~ to the space that the @ moved from

public boolean takeStep(int[][] newMaze) {

for (int i = 0; i < newMaze.length; i++) {

for (int j = 0; j < newMaze[i].length; j++) {

if (newMaze[r][c] == 3) {

moveNorth(newMaze);

System.out.print("~");

} else if (newMaze[r][c] == 2) {

System.out.print("@");

} else {

}

}

}

return isAnExit(newMaze);

}

public void moveNorth(int[][] arrMaze)

{

//complete the code here

//method will check for a 0 or a 1 in the position above the current position

//and then if not a 0 will move up to the row above it, but in the same column.

if (arrMaze[r - 1][c] != 0){

arrMaze[r - 1][c] = 3;

arrMaze[r + 1][c] = 2;

}else {

moveSouth(arrMaze);

}

displayPath(arrMaze);

}

public void moveSouth(int[][] arrMaze)

{

//complete the code here

//method will check for a 0 or a 1 in the position below the current position and then if not a 0

//it will move down to the row below it, but in the same column.

if (arrMaze[r + 1][c] != 0){

arrMaze[r + 1][c] = 3;

arrMaze[r + 1][c] = 2;

} else {

moveNorth(arrMaze);

}

displayPath(arrMaze);

}

public void moveEast(int[][] arrMaze)

{

//complete the code here

//method will check for a 0 or a 1 in the position to the right of the current position and then if

//not a 0 will change the current position to the column to the east (right) but the same row.

if (arrMaze[r][c + 1] != 0) {

arrMaze[r][c + 1] = 3;

arrMaze[r][c - 1] = 2;

} else {

moveWest(arrMaze);

}

displayPath(arrMaze);

}

public void moveWest(int[][] arrMaze)

{

//complete the code here

//method will check for a 0 or a 1 in the position to the left of the current position and then if

//not a 0 will change the current position to the column to the west (left) but the same row.

}

private boolean isAnExit(int[][] arrMaze) {

//method will return true if the user arrives into the last column of the array because there is only one

//location in the last column that is a 1,

//so if the user reaches the array[i].length then that means that it found an exit.

if (arrMaze[r][c] > arrMaze.length) {

exitFound = true;

} else {

exitFound = false;

}

return exitFound;

}

//finds the path without stopping at every step

//method will show the complete path from start to finish of the maze and the suggested route to the end.

public void findExit(int[][] arrMaze) {

if (arrMaze[r][c] > arrMaze.length) {

for (int i = 0; i < arrMaze.length; i++) {

takeStep(arrMaze);

}

}

}

}

TestMaze.java

import java.util.Scanner;

public class TestMaze { public static void main(String[] args){ int[][] mazeArray = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0}, {0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0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boolean keepAsking = true; Scanner scan = new Scanner(System.in); String input = ""; myMaze.displayPath(mazeArray); System.out.println(" Maze Project"); do { System.out.println(" T = Take a step | S = Show path | Q = Quit"); System.out.print("Enter command: "); input = scan.nextLine(); input.trim(); input.toLowerCase(); if(input.equals("t")) { } else if(input.equals("s")) { myMaze.findExit(mazeArray); keepAsking = false; } else if(input.equals("q")) { keepAsking = false; } else { System.out.println("ERR: Invalid input"); } } while(keepAsking); System.out.println("Quitting program..."); scan.close(); } }

You can change them, however you need to keep the same maze, constructor, and methods. The maze should also be able to work if the maze were to change.

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_2

Step: 3

blur-text-image_3

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

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

Students also viewed these Databases questions

Question

U11 Informing Industry: Publicizing Contract Actions 317

Answered: 1 week ago

Question

=+How will this affect the recruiting process?

Answered: 1 week ago