简体   繁体   中英

How can I alter my code to allow my retrace command to work more than once?

Before I get into my issue here is a quick little summary of my program in relation to my issue so you understand. My program relates to a robot, below represents some commands that my program executes to the robots. The commands inputted by the user are stored in the commandList.size array.

  • L command means the robot turns left.
  • F command means the robot moves forwards.
  • T command means the robot retraces the commands backwards from which the user inputted into the program. For example, if the user already entered the commands "L COMMAND, R COMMAND" then the user enters "T 2" this will need to make the robot turn right then turn left so the array of commands is being read backwards. "T 2" means retrace the previous last two commands.

ISSUE

If the user makes my robot turn left then the user enters “ T 1” to retrace the previous inputted commands, then after the user makes the robot go forward then types in “ T 2” to retrace the previous two commands the program stops working. The program should make the robot go forward then turn left as they were two last commands entered. The “T 1” command needs to be ignored as that's not counted. How can I get my program to successfully retrace the commands more than once?

Another example

L COMMAND (Turn left)
"T 1" (Retrace the left command - this work)
B COMMAND (Move backwards)
"T 2" (THIS IS WHERE THE PROGRAM STOPS WORKING - the program needs to skip "T 1" and retrace the F command / B command - Here is the issue) you understand? 

This issue above is to do with the T command I'm sure within my program.

CONSOLE EXAMPLE TO HIGHLIGHT THE ISSUE - This doesn't work. The robot should go backwards and move to the right. The T 1 command should be ignored.

>Move robot right 
>T 1 to retrace last step which works
>Backwards command
>T 2 retrace last two commands. 

https://codepen.io/code

I think I understand what the issue is. Your commandList collection should always hold every command supplied by User unless the supplied command is invalid. This way you can always retrace the actions already done. It's just a matter of reading through that list in reverse order and ignoring the Retrace (or back-step) commands. See if the following code helps you out a little. It is well commented:

Scanner input = new Scanner(System.in);
List<String> commandList = new ArrayList<>();
String command = "";

while (!command.equals("Q")) {
    System.out.println("Enter Robot Command: ");
    command = input.nextLine().trim().toUpperCase();

    // Is 'Q'uit desired?
    if (command.equals("Q")) {
        break; // Break out of WHILE loop
    }
    // Validate Command Input!
    if (!command.matches("(?i)[LRBF]{1}\\s+\\d{1}\\s+\\d+|(?i)[T]\\s{0,}\\d+")) {
        System.out.println("INVALID INPUT!. Try Again...");
        continue; // Prompt again...
    }

    // Add command to list
    commandList.add(command);

    // Is a Back-Step command issued?
    if (command.startsWith("T")) {
        // Yes...get the step number from the command
        int step = Integer.parseInt(command.replaceAll("\\s+", "").split("")[1]);
        // Is the Back-Step Valid?
        if (step > (commandList.size() - 1)) {
            // NO it's not
            System.out.println("INVALID BACKSTEP INPUT!. Robot hasn't moved "
                             + "that many steps! Try Again...");
            // Remove failed Back-Step command from list.
            commandList.remove(commandList.size() - 1);
            continue;  // Prompt again...
        }
        List<String> backStep = new ArrayList<>(); // Used to hold all back-steps.

        // Last index value of commandList (NOT includinmg the Back-step command).
        int cmdLastIndex = commandList.size() - 2; 

        // Acquire the Back-Steps from the commandList in 
        // reverse order from end to start.
        int stepCount = 0; // Keep track of steps gong back
        for (int i = cmdLastIndex; i >= 0; i--) {
            // If you DON'T want to ignore 'T' commands in 
            // back-step then comment the following IF block:
            if (commandList.get(i).startsWith("T")) {
                continue;
            }
            backStep.add(commandList.get(i)); // Add to backStep List
            moveBot(commandList.get(i));      // Move the Bot this particular step location.
            stepCount++;                      // Back-Step oount increment (by 1).
            if (stepCount == step) { break; } // If step count = step, exit FOR loop.
        }

        // For console display purpose...
        System.out.println(backStep);  // Display to console. 
        // Do whatever you want with the backStep List.

        continue;   // Prompt again for Bot movement input...
    }

    // Not a Back-Step so....
    moveBot(command);   // Move the Bot this particular step location.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM