Question
For parsing player commands, I've most often used the split method to split a string by delimiters and then to then just figure out the rest by a series of ifs or switches. What are some different ways of parsing strings in Java?
Answer
I assume you're trying to make the command interface as forgiving as possible. If this is the case, I suggest you use an algorithm similar to this:
- Read in the string
- Split the string into tokens
- Use a dictionary to convert synonyms to a common form
- For example, convert "hit", "punch", "strike", and "kick" all to "hit"
- Perform actions on an unordered, inclusive base
- Unordered - "punch the monkey in the face" is the same thing as "the face in the monkey punch"
- Inclusive - If the command is supposed to be "punch the monkey in the face" and they supply "punch monkey", you should check how many commands this matches. If only one command, do this action. It might even be a good idea to have command priorities, and even if there were even matches, it would perform the top action.
0 comments:
Post a Comment