Question

In: Computer Science

This maze project assumes that a cell is rectangular, and that there is an entrance into...

This maze project assumes that a cell is rectangular, and that there is an entrance into and an exit from the cell.  What if the entrance and the exit were closed doors and the user was to choose which door leads to the next cell?  What would be some pseudocode for this scenario?

Notice that " switch case " statements are used for this project along with " if " statements.  Can they be interchanged and still produce the same results [ i.e., make the switch case statements into if statements, and the if statements into switch case statements ] ? Support your answer!

Reference source code for more information:

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

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;

                        }

            }

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

Solutions

Expert Solution

Ans:- Yes, the switch and if statement can be interchanged and produced the same output. Both, if and switch are the types of branching statement. Switch statements are basically used to evaluate condition based on the value of single variable or object and if statements are used to run the block if a particular condition is met. If a code test again a single condition, you can choose between any method (if and switch) without any hesitation.  SInce,in the above code, if and switch are both used to test again single value of variable, they can be used interchangeably. For more clearance, i am attaching the ouput of both the code before and after update.

Updated Code:-

import java.util.*;
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)?");

      if(sc.hasNext())
      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() 
{
            if(sc.hasNext())
            myMove = sc.next().charAt(0);

            score++;
  //Change the switch into if statement

              if(myMove == 'W') // if user entered 'W' MoveUp() will executed
                 MoveUp(); 

              if(myMove == 'A') // if user entered 'A' MoveLeft() will executed
                MoveLeft();

              if(myMove == 'S') // if user entered 'S' MoveDown() will executed
                MoveDown();
              
              if(myMove == 'A') // if user entered 'A' MoveRight() will executed
                MoveRight();
        
  
            // end program menu

}// end method

static boolean checkYourMove()

{

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

            {  //here we change the if statement into switch statement
                switch(myMove)
                {
                    // if user entered 'W' case 'W' will executed
                    case 'W': { 
                                advance = false;
                                System.out.println("try again"); 
                                break;
                              }
                    // if user entered 'A' case 'A' will executed
                    case 'A': { 
                                advance = false;
                                System.out.println("SORRY, there is no return");
                                break;
                              }
                    // if user entered 'S' case 'S' will executed    
                    case 'S': { 
                                    advance = false;
                                    System.out.println("continue through the maze");
                                    break;
                              }
                    // if user entered 'D' case 'D' will executed 
                    case 'D': { 
                                    currentCell = 2;
                                    advance = true;
                                    System.out.println("continue through the maze"); 
                                    break; 
                              }
             }
            }

             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 (before updation):-

Output (Updated Code):-

Here, you can see that both the code( before updation and after updation) produced the same output. Hence, both if and switch statements can be used interchangeably in this code.


Related Solutions

c++ maze game Project Scope – Details of the Project: Sample Maze Layout: User Interface: Data...
c++ maze game Project Scope – Details of the Project: Sample Maze Layout: User Interface: Data Structures: Data Classes: Project Details: Algorithm (flowchart and/or pseudo-code):
In a maze running study, a rat is run in a T maze and the result...
In a maze running study, a rat is run in a T maze and the result of each run recorded. A reward in the form of food is always placed at the right exit. If learning is taking place, the rat will choose the right exit more often than the left. If no learning is taking place, the rat should randomly choose either exit. Suppose that the rat is given n = 100 runs in the maze and that he...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same rate equal to the IRR. However, in reality the reinvested cash flows may not necessarily generate a return equal to the IRR. Thus, the modified IRR approach makes a more reasonable assumption other than the project’s IRR. Consider the following situation: Green Caterpillar Garden Supplies Inc. is analyzing a project that requires an initial investment of $2,500,000. The project’s expected cash flows are: Year...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same rate equal to the IRR. However, in reality the reinvested cash flows may not necessarily generate a return equal to the IRR. Thus, the modified IRR approach makes a more reasonable assumption other than the project’s IRR. Consider the following situation: Celestial Crane Cosmetics is analyzing a project that requires an initial investment of $450,000. The project’s expected cash flows are: Year Cash Flow...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same rate equal to the IRR. However, in reality the reinvested cash flows may not necessarily generate a return equal to the IRR. Thus, the modified IRR approach makes a more reasonable assumption other than the project’s IRR. Consider the following situation: Cute Camel Woodcraft Company is analyzing a project that requires an initial investment of $450,000. The project’s expected cash flow are: Year Cash...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same rate equal to the IRR. However, in reality the reinvested cash flows may not necessarily generate a return equal to the IRR. Thus, the modified IRR approach makes a more reasonable assumption other than the project’s IRR. Consider the following situation: Fuzzy Button Clothing Company is analyzing a project that requires an initial investment of $2,750,000. The project’s expected cash flows are: Year Cash...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same...
The IRR evaluation method assumes that cash flows from the project are reinvested at the same rate equal to the IRR. However, in reality the reinvested cash flows may not necessarily generate a return equal to the IRR. Thus, the modified IRR approach makes a more reasonable assumption other than the project’s IRR. Consider the following situation: Blue Llama Mining Company is analyzing a project that requires an initial investment of $550,000. The project’s expected cash flows are: Year Cash...
nursing entrance essay
nursing entrance essay
A two-dimensional rectangular crystal has a unit cell with sides a1 = 0.468 nm and a2...
A two-dimensional rectangular crystal has a unit cell with sides a1 = 0.468 nm and a2 = 0.342 nm. (a) Draw to scale a diagram of the reciprocal lattice. Label the reciprocal lattice points for indices in the range 0<h<3 and 0<k<3. (b) Draw the first and second Brillouin zones using the Wigner-Seitz construction.
Huang Industries is considering a proposed project whose estimated NPV is $12 million. This estimate assumes...
Huang Industries is considering a proposed project whose estimated NPV is $12 million. This estimate assumes that economic conditions will be "average." However, the CFO realizes that conditions could be better or worse, so she performed a scenario analysis and obtained these results: Economic Scenario Probability of Outcome NPV Recession 0.05 ($44 million) Below average 0.20 (12 million) Average 0.50 12 million Above average 0.20 20 million Boom 0.05 36 million Calculate the project's expected NPV, standard deviation, and coefficient...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT