On Parsing

Hey all. My name's Ben, and I'm a programmer. I wrote my own JS text-adventure engine, and I thought it would be cool to share how some of it works.

The parser takes input from a plain old regular HTML input box, but the page is styled so it looks like an old school command line. When the player presses enter, the text in the text box is sent to a parser for processing. There are actually like 8 different separate parsers controlled by game statuses. The game processes input differently depending what status it's on, so if status==8 for example, the game knows it's in combat mode and will send received inputs to parseCombat instead of parseInput.

The important parser is parseInput, which handles primary dungeon navigation (ie, 'look around', 'take object', 'talk to orc', etc). It works like this:

  1. Check for specific inputs like XYZZY to do things like teleports.
  2. If none are found, check if the input is a 'look around' query, and if it is, return a formatted list of objects in the room.
  3. If it's not, separate the input by spaces and start processing it word by word.
  4. If one of the words is found to match the name of an object in the room, set the subject to the object
  5. If one of the words is found to match a verb that the parser knows, set the action to the verb.
  6. If both a verb and an action are found, read off the corresponding text (or execute a corresponding function) from the targeted object.
  7. If a verb was found but an object wasn't, ask the player what they want to perform that verb on (ie, if the player entered 'take' the game will ask, "what do you want to take?"), and then if the next user input is just an object, perform the action on that object.
  8. If none of the above conditions are met, return a message that the command wasn't understood.

That's the long and short of primary dungeon navigation. Not actually that complicated when you get down to it, but certainly took a while to write. The whole game takes up about 8000 lines of code, give or take. Always makes me chuckle to think that my engine (written between 2015 and now) is only just getting close to the power of Zork's (which came out in 1977 :p ).

Hope that was somewhat interesting. You can see the engine in action in my game Underground Dungeon. I'll be posting some massive bug fixes and engine improvements in the next couple days.


I've given your game a try and the parser works fine.
If it doesn't get the commands entered it makes clear why it doesn't. Great job!
One thing though, it doesn't understand 'restart'?


Game is here: https://textadventures.co.uk/games/view/gfeokxwhhkacym2c6njupq/underground-dungeon

My only feedback is that your choices for verbs differ from the conventions used in other text adventure games:

The standard shortcut abbreviation for "look" in TA games (the general "see what's in the room") is "l". And the shortcut for "examine" (or "look at") is "x". You seem to have those reversed. You have "x" on its own mapping to "look", whereas "l" is mapped to "examine" (or "look at"). While it's up to you how you map your commands, I'd recommend sticking with what people know from decades of game playing. :)

(A minor note about the javascript: check out when to use "===" vs "==".)

Nice work, in general!


@Jay Nabonne - Thank you for the pointers! I don't know why, but at the time I was under the impression "===" was for comparing disparate data types (which makes no sense), so that's something I need to correct. The parsers I'm used to are very oldschool and haven't had 'x' 'i' etc. I'll fix that when I get a chance.

@Ewald - I'll add a restart command, thanks for the recommendation!


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums