1

X signifies the starting point and E is the Exit. The problem is I cannot make my X move. Can someone help me with this. We are only allowed to used the main class and main method, no any other than that. Every time the user input, the maze map will display showing the current position of the player. When the user choose to face left or right, the current position should not change.

import java.util.Scanner;

public class G7 {
    public static void main(String[] args) {
        String[][] M = {{"0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"X", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", " ", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " ", "0", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "0", " "}
                , {"0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "0", " ", "E", " ", "0", " "}};
        
        do {
            System.out.println("-------------------------MAZE------------------------------");
            System.out.println();
            for (String[] a : M) {
                for (String b : a) {
                    System.out.print(b);
                }
                System.out.println();
            }
            System.out.println("-----------------------------------------------------------");
            System.out.print("F - move Forward\tB - move Backward\t\nR - face Right\t\tL - face Left\n");
            System.out.print("Enter your move: ");
            String c = new Scanner(System.in).next();
            if (c.equals("F")) {
                M[1][2] = M[1][0];
                M[1][0] = " ";
            }

        } while (true);

    }
    
}
aboger
  • 2,214
  • 6
  • 33
  • 47
  • 2
    You can read the command [Getting Keyboard Input](https://stackoverflow.com/questions/17538182/getting-keyboard-input) and then you can re-write the maze [Java: Updating text in the command-line without a new line](https://stackoverflow.com/questions/4573123/java-updating-text-in-the-command-line-without-a-new-line) on screen – Butiri Dan Nov 22 '19 at 06:55

1 Answers1

1

one cheap solution would be that you print the maze with the updated position after every keyboard input

Tasks:

  • move M from the main method into a constant - static String[][] M = ...;
  • setup loop that draws after keyboard input - static boolean isDone
  • create a method for drawing the maze static void printMaze()
  • create a method for handling input static void readInput()
  • handle the player position with variables - static int xPlayer & static int yPlayer
static String[][] M = ...
static boolean isDone;
static int xPlayer;
static int yPlayer;

public static void main(String []args){
    //repeated drawing of the screen after input
    while(!isDone){
        printMaze();
        readInput();    
    }
}

static void printMaze(){
    int height = M.length;
    for(int y = 0; y < height; y++){
        int width = M[0].length;
        for(int x = 0; x < width; x++){
            System.out.print(M[x][y]);
            if(x == xPlayer && y == yPlayer){
                System.out.print("X");
            }
        }
        System.out.println();
    }
    System.out.println();
}

static void readInput(){
    System.out.print("F - move Forward\tB - move Backward\t\nR - face Right\t\tL - face Left\n");
    System.out.print("Enter your move: ");
    String c = new Scanner(System.in).next();

    //handle input
    if (c.equalsIgnore("f")){
        yPlayer = yPlayer - 1;
    }
    //TODO: handle the other 3 directions the same way

    //BONUS: quit the loop 
    if (c.equalsIgnore("quit)){
        isDone = true;
    }
}
Martin Frank
  • 3,445
  • 1
  • 27
  • 47