Question

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?

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

Homework Answers

Answer #1

:: Solution ::

Ans:- Yes, the switch and if statement can be interchanged and produced the same output. Both, if and switch are the types ofbranching statement. Switch statements are basically used to evaluate condition based on the value of single variable or object andif 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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
How can I alter this Java method so that I convert and then return every sentence...
How can I alter this Java method so that I convert and then return every sentence in the array of strings instead of just the first sentence? public static char[] charConvert ( String [] sentences ) { int totLength = sentences[0].length() + sentences[1].length() + sentences[2].length() + sentences[3].length() + sentences[4].length(); char [] letter = new char[totLength]; int x = 0; int y = 0;    while ( y < sentences[x].length() ) { switch ( sentences[x].charAt(y) ) { case 'a': case 'A':...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Download the attached .java file. Run it, become familiar with its processes. Your task is to...
Download the attached .java file. Run it, become familiar with its processes. Your task is to turn TemperatureConversion into GUI based program. it should, at the least, perform similar functions as their text output versions. The key factor to remember is that the workings should remain the same (some tweaks may be necessary) between text and GUI programs, while the means pf visual presentation and user interaction changes. You must properly document, comment, indent, space, and structure both programs. import...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT