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
:: 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.
Get Answers For Free
Most questions answered within 1 hours.