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
  • help - no arguments
  • orientation - 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

      @Deprecated public InputParser(String input)
      Deprecated.
      Takes the raw line the user typed and immediately validates it

      This variant is not favoured as it does not explicitly throw errors

    • InputParser

      public InputParser(String input, boolean strictErrors) throws UnsupportedRobotWorldCommand
      Stricter Command Input Parsers that enforces validation
      Parameters:
      input - the raw string input given by the client
      strictErrors - if errors are strictly enforced or not
      Throws:
      UnsupportedRobotWorldCommand - thrown if an unsupported robot world command is given
  • 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
    • getRobotName

      public Optional<String> getRobotName()
      Returns:
      a potentially null robot name
    • getCommand

      public ImplementedClientCommands 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
    • getPart

      public String getPart(int index)
    • getArg

      public String getArg(int index)
    • getAllArgs

      public String[] getAllArgs()