Question: PROJECT Traversing a Maze - Using Methods in Java Objective To type a simple Java program, execute ( run ) the program for some particular

PROJECT Traversing a Maze - Using Methods in Java

Objective To type a simple Java program, execute ( run ) the program for some particular values, observe the output and then modify the program.

PROJECT DESCRIPTION

Type, compile and run the basic Java program that is shown in Figure 3 , which follows.

Then compile and run your program, observe the output then modify the program.

Information About This Project

For this project, we will create an application that traverses a rectangular grid - like maze, as shown in Figure 1 below. The maze has a start entrance position and an exit or finishing position.

Figure 1 ( Sample Maze Format )

start

finish

One way to program a maze structure is to envision the maze as comprised of cells or zones. This maze has none cells or zones.

Figure 2 ( Another Sample Maze Format )

start

zone 1

zone 2

zone 3

zone 4

zone 5

zone 6

zone 7

zone 8

zone 9

finish

The optimal solution ( minimal movements ) to the above maze ( Figure 2 ) would be:

cell start position

movement

action

cell end position

start ( outside maze )

move 1

move right

zone 1

zone 1

move 2

move right

zone 2

zone 2

move 3

move down

zone 5

zone 5

move 4

move right

zone 6

zone 6

move 5

move down

zone 9

zone 9

move 6

move down

finish

Steps to Complete This Project

STEP 1 Open Eclipse or NetBeans

Open Eclipse or NetBeans and create a Java project with the following details.

For Project Name include: Maze

For the Main Class include: Maze

In your Code window, shown below, copy in the program code shown in Figure 3 below, in the appropriate places, except substitute your own name in place of Sammy Student.

PROJECT Traversing a Maze - Using Methods in Java

Figure 3 Source Code for the Traversing a Maze Program

import java.util.Scanner;

//Sammy Student, Programmer

public class Maze

{

static Scanner sc = new Scanner(System.in);

// maze movements

static char myMove = '\0';

// cell position

static int currentCell = 0;

static int score = 0;

static boolean advance = true;

static boolean checkThis = false;

public static void main(String args[])

{

// the local variables declared and initialized

char answer = 'Y';

displayMenu();

while(answer == 'Y' || answer == 'y')

{

displayMovement();

makeYourMove();

checkThis = checkYourMove();

mazeStatus();

System.out.println("move again(Y or N)?");

answer = sc.next().charAt(0);

}

System.out.println("***************************");

}// end main() method

static void displayMenu()

{

System.out.println("");

System.out.println("***************************");

System.out.println("----The Maze Strategy---");

System.out.println("");

}// end method

PROJECT Traversing a Maze - Using Methods in Java

Figure 3 Source Code for the Traversing a Maze Program ( continued )

static void displayMovement()

{

if(currentCell == 0)

{

System.out.println("You have entered the maze!!");

System.out.println("There is no turning back!!");

currentCell = 1;

mazeStatus();

advance = true;

}

System.out.println("make your move (W, A, S, D)");

System.out.println("W = up, A = left, S = down, D = right)");

}// end method

static void makeYourMove()

{

myMove = sc.next().charAt(0);

switch(myMove)

{

case 'W': { MoveUp(); break; }

case 'A': { MoveLeft(); break; }

case 'S': { MoveDown(); break; }

case 'D': { MoveRight(); break; }

}

// end program menu

}// end method

static boolean checkYourMove()

{

if(currentCell == 1 && advance == true)

{

if (myMove == 'W')

{

advance = false;

System.out.println("try again");

return advance;

}

if (myMove == 'A')

{

advance = false;

System.out.println("SORRY, there is no return");

return advance;

}

PROJECT Traversing a Maze - Using Methods in Java

Figure 3 Source Code for the Traversing a Maze Program ( continued )

if (myMove == 'D')

{

currentCell = 2;

advance = true;

System.out.println("continue through the maze");

return advance;

}

if (myMove == 'S')

{

advance = false;

System.out.println("continue through the maze");

return advance;

}

}

if(currentCell == 2 && advance == true)

{

if (myMove == 'W')

{

advance = false;

System.out.println("try again");

return advance;

}

if (myMove == 'A')

{

advance = false;

System.out.println("SORRY, there is no return");

return advance;

}

if (myMove == 'D')

{

advance = false;

System.out.println("continue through the maze");

return advance;

}

if (myMove == 'S')

{

currentCell = 5;

advance = true;

System.out.println("continue through the maze");

return advance;

}

}

return advance;

// end program menu

}// end method

PROJECT Traversing a Maze - Using Methods in Java

Figure 3 Source Code for the Traversing a Maze Program ( continued )

static void MoveLeft()

{

System.out.println("you moved to the left");

}//end method

static void MoveRight()

{

System.out.println("you moved to the right");

}//end method

static void MoveUp()

{

System.out.println("you moved up (forward)");

}//end method

static void MoveDown()

{

System.out.println("you moved down (downward)");

}//end method

static void mazeStatus()

{

System.out.println("current position: cell " + currentCell);

}//end method

}// end class

STEP 2 Build, Compile and Run the Program

From the menu select [ Run ] and click [ Run Project ] to run your app.

STEP 3 Test the Program

Once you have successfully compiled your program, review the output Console window of your Java editor.

Enter the sample information shown in Figure 4 that follows. Observe the output of the starter code, after you enter the Figure 4 data.

Reviewing the starter code we see that the program has us traverse the maze format given in Figure 2 . Basically, the starter code is comprised of a group of methods that have us enter the maze and then move from cell to cell until we reach the exit point of the maze. When examining the checkYourMove() method, we notice that only movement through the cells 1 and 2 , from Figure 2 , have been programmed. The rest of the cell movements, for cells 3 through 9 , must be also be programmed, as instructed in the next step of this project, even if any of cells 3 through 9 are not in the solution path to successfully exit the maze.

Movement through the cells is performed by entering W for upward movement, A to go left to a cell, D to move to the right and S to move downward.

PROJECT Traversing a Maze - Using Methods in Java

Figure 4 Initial Test Run

***************************

----The Maze Strategy---

You have entered the maze!!

There is no turning back!!

current position: cell 1

make your move (W, A, S, D)

W = up, A = left, S = down, D = right)

D

you moved to the right

continue through the maze

current position: cell 2

move again(Y or N)?

Y

make your move (W, A, S, D)

W = up, A = left, S = down, D = right)

S

you moved down (downward)

continue through the maze

current position: cell 5

move again(Y or N)?

N

***************************

STEP 5 Modify the Program

Supplement the checkYourMove() method by including the remaining if statements that allow the maze participant to escape the maze.

if(currentCell == 5 && advance == true)

{

. . .

}

if(currentCell == 6 && advance == true)

{

. . .

}

if(currentCell == 9 && advance == true)

{

. . .

}

Also, include similar if() statements for the remaining cells, even if the cell is not in the escape cell sequence for the maze, i.e. include the ability to move to any cell within the maze even if that cell is not in the maze minimal success traversal pattern.

PROJECT Traversing a Maze - Using Methods in Java

Now, utilize the score variable to assign a score to each move. Also track the number of moves that the user makes in their escape of the maze.

STEP 6 Submit Your Project

Once you have determined that your modified program is correctly displaying the maze path solution, complete the submission process as follows:

Open MS Word and type a heading for a new document that includes your full name, course number, lab number and date.

Within the document paste in a snapshot of your modified code. Label your snapshot of your modified run with a reasonable description.

Include other snapshots to show the operation of your program as it is used by the program user.

After your snapshot, paste in your finished source code as well copied in from your Java editor.

Submit your MS Word document, when complete.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!