Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package lab7; import java.util.Scanner; //Sammy Student, Programmer public class

ID: 3926386 • Letter: P

Question

package lab7;
import java.util.Scanner;
//Sammy Student, Programmer
public class Maze
{
static Scanner sc = new Scanner(System.in);
// maze movements
static char myMove = '';
// 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


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;
       }
      

       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


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

Modifythe above code like this:

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

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

            {

                  .     .     .

            }

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

            {

                  .     .     .

            }

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

            {

                  .     .     .

            }

2. utilize the score variable to assign a score to each move. Also for extra credit you can track the number of moves that the user makes in their escape of the maze.

Explanation / Answer

Please follow the code and comments for description :

CODE :

import java.util.Scanner;

//Sammy Student, Programmer

public class Maze { // class that runs the code

    static Scanner sc = new Scanner(System.in);
    // maze movements
    static char myMove = '';
    // 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("***************************");
        System.out.println("Total Moves Done : "+score);
    }// end main() method

    static void displayMenu() {
        System.out.println("");
        System.out.println("***************************");
        System.out.println("----The Maze Strategy---");
        System.out.println("");
    }// end method

    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);
        score++;
        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;
            }

            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;
            }
        }
      
        if (currentCell == 5 && 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') {
                currentCell = 6;
                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 == 6 && 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') {
                currentCell = 9;
                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 == 9 && 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') {
                currentCell = 9;
                advance = false;
                System.out.println("Escaped Through the Maze.!");
                return advance;
            }
            if (myMove == 'S') {
                advance = false;
                System.out.println("continue through the maze");
                return advance;
            }
        }      
        return advance;
        // end program menu
    }// end method

    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


OUTPUT :


***************************
----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)?
Y
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 6
move again(Y or N)?
Y
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 9
move again(Y or N)?
Y
make your move (W, A, S, D)
(W = up, A = left, S = down, D = right)
D
you moved to the right
Escaped Through the Maze.!
current position: cell 9
move again(Y or N)?
N
***************************
Total Moves Done : 5


Hope this is helpful.