Class InputParser

java.lang.Object
za.co.wethinkcode.robots.client.InputParser

public class InputParser extends Object
Reads a line of user input and checks whether it is a valid, well-formed command.

This class only checks SYNTAX — it does NOT talk to the server or check whether a move is legal in the world. That is the server's job.

Valid commands and what they need:

  • launch - two words after "launch"
  • forward - one positive number after "forward"
  • back - one positive number after "back"
  • turn invalid input: '<'left|right> - exactly "left" or "right" after "turn"
  • look - no arguments
  • fire - no arguments
  • repair - no arguments
  • reload - no arguments
  • state - no arguments
  • world - no arguments


If the input is valid, getError() returns null and getParts() returns the words. If the input is invalid, getError() returns a message explaining what is wrong.
  • Constructor Details

    • InputParser

      public InputParser(String input)
      Takes the raw line the user typed and immediately validates it
  • Method Details

    • isValid

      public boolean isValid()
      Returns true if the command is valid and ready to send
    • getError

      public String getError()
      Returns the error message if the command was invalid, otherwise null
    • getCommand

      public String getCommand()
      Returns the command name e.g. "forward" // Only call this if isValid() is true
    • getParts

      public String[] getParts()
      Returns all the words in the input as an array e.g. ["forward", "5"] // Only call this if isValid() is true
    • getArg

      public String getArg(int index)