Package za.co.wethinkcode.robots.client
Class InputParser
java.lang.Object
za.co.wethinkcode.robots.client.InputParser
Reads a line of user input and checks whether it is a valid, well-formed command.
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.
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 Summary
ConstructorsConstructorDescriptionInputParser(String input) Takes the raw line the user typed and immediately validates it -
Method Summary
Modifier and TypeMethodDescriptiongetArg(int index) Returns the command name e.g.getError()Returns the error message if the command was invalid, otherwise nullString[]getParts()Returns all the words in the input as an array e.g.booleanisValid()Returns true if the command is valid and ready to send
-
Constructor Details
-
InputParser
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
Returns the error message if the command was invalid, otherwise null -
getCommand
Returns the command name e.g. "forward" // Only call this if isValid() is true -
getParts
Returns all the words in the input as an array e.g. ["forward", "5"] // Only call this if isValid() is true -
getArg
-