Search code examples
javaexceptionmaze

How do I change my code so it stops catching valid inputs?


I am a java beginner that has to complete a school assignment. It tells me to create a maze that allow the user to move through the maze until he/she reaches the end. The movement keys are w, a, s, d. I've made the maze and basically created the program, however my try and catch block is forcing an error even if the input is valid.

This is my maze, the player is represented by an asterisk and E means the end. An X represents a wall and any space is a path.

 X  X  X  X  X  X  X  X  X  X  X 
 X     X  X        X  X  X     X 
 X     X  X     X  X  X  X     X 
 X              X  X  X  X     X 
 X  X     X  X  E        X     X 
 X  X     X  X  X  X     X     X 
 X  X     X  X  X  X     X       
 X  X                          X 
 X  X  X     X     X  X  X     X 
 X  X  X  X  X  X  X  X  X  *  X 

This is my code, I apologize if it is too long.

    Scanner myInput = new Scanner(System.in);
    String mazeOutput = "", tempInput;
    char playerMove;
    int playerRowPosition, playerColPosition;

OuterWhile:

        while (!mapOfMaze[4][5].equals(" * "))
        {
          
          for(int row = 0; row < mapOfMaze.length; row++)
          {
            for(int col = 0; col < mapOfMaze[row].length; col++)
            {
              System.out.print(mapOfMaze[row][col]);
            }
            
            System.out.println("");
          }
InnerWhile:
        while (true)
        {
          System.out.println("\nWhat direction would you like to move (Controls: w - a - s - d): ");
          tempInput = myInput.nextLine();
          
          try
          {
             
            for (int i = 0; i < tempInput.length(); i++)
            {       
              if (Character.isDigit(tempInput.charAt(i)))
              {
                Integer.parseInt("a");
              }  
            }                
            
            if (tempInput.length() != 1)
            {
              Integer.parseInt("a");
            }
            
            playerMove = tempInput.toUpperCase().charAt(0);
            
            if ((int)playerMove == 87 || (int)playerMove == 65 || (int)playerMove == 83 || (int)playerMove == 68)
            {
              playerMove = playerMove;
            }
            
            else
            {
              Integer.parseInt("a");
            }
            break;
          }
          
          catch (NumberFormatException error)
          {
            System.out.println("\nPlease provide a move, the only valid moves are w, a, s or d.");      
          }
        }

continue OuterWhile

          try
          {
            playerRowPosition = 9;
            playerColPosition = 8;
            
            if ((int)playerMove == 87 && mapOfMaze[playerRowPosition - 1][playerColPosition].equals("   "))
            {
              playerRowPosition = playerRowPosition - 1;
              mapOfMaze[playerRowPosition][playerColPosition] = " * ";
            }
            
            else if ((int)playerMove == 65 && mapOfMaze[playerRowPosition][playerColPosition - 1].equals("   "))
            {
              playerColPosition = playerColPosition - 1;
              mapOfMaze[playerRowPosition][playerColPosition] = " * ";
            }
            
            else if ((int)playerMove == 83 && mapOfMaze[playerRowPosition + 1][playerColPosition].equals("   "))
            {
              playerRowPosition = playerRowPosition + 1;
              mapOfMaze[playerRowPosition][playerColPosition] = " * ";
            }
            
            else if ((int)playerMove == 68 && mapOfMaze[playerRowPosition][playerColPosition + 1].equals("   "))
            {
              playerColPosition = playerColPosition + 1;
              mapOfMaze[playerRowPosition][playerColPosition] = " * ";
            }
            
            else
            {
              throw new NumberFormatException ();
            }
          }
          
          catch (NumberFormatException error)
          {
            System.out.println("\nThat move is not possible, the player is not able to move on walls or out of the maze.");
          }
        }

I tried to change the position of the player based on the input but also catch any exceptions, but it seems like the code is catching valid inputs as well. For example, if I input w, the output will be

What direction would you like to move (Controls: w - a - s - d): 
 [DrJava Input Box]

That move is not possible, the player is not able to move on walls or out of the maze.

I appreciate any help.


Solution

  • First, move the line to extract the next character that was input to just below the for loop statement:

    while (true) {
          System.out.println("\nWhat direction would you like to move?"
             + "(Controls: w - a - s - d): ");
          tempInput = myInput.nextLine();          
          try {             
            for (int i = 0; i < tempInput.length(); i++) {       
              playerMove = tempInput.toUpperCase().charAt(i);
    ... 
    

    Note this uses the index controlled by the for loop, rather than always using 0.

    Next, examine the playerMove to put it into one of the following categories:

    • Whitespace: continue; the for loop.
    • Valid and possible: process the move.
    • Valid and impossible:
      • Bump into wall: Display error message
      • Out of bounds: Display error message

    By "valid", I mean one of w, a, s, d, as specified in the question. By "valid and impossible", I mean the input criteria were met, but the move would go out of bounds or into a wall.

    Don't try to use exceptions to categorize the move. Use break to exit a loop and continue to jump to the next iteration.

    Example:

      for (int i = 0; i < tempInput.length(); i++) {       
          playerMove = tempInput.toUpperCase().charAt(i);
      if (Character.isWhitespace (playerMove) { 
          continue;
      }
      if (    playerMove != 'W' && playerMove != 'D' 
           && playerMove != 'A' && playerMove != 'S') {
        System.out.println ("\nPlease provide a move, the only valid moves"
              + " are w, a, s or d.");
        continue;
      }
    

    That would be followed by code to check if the player remains in bounds and avoids a wall. After that would be the code to make valid move.