Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Parsing user commands in a text adventure is a spectrum from Adventure's simple "go north" to some mind-bogglingly clever ones in hhgttg.

I seem to remember reading nice how-tos in computer magazines back in the 80s, but now I find almost nothing on the 'net except a brief Wikipedia ref.

How would you do it?


Update: I went with the simplest approach possible in my Ludum Dare entry.

share|improve this question
3  
Is there a particular problem you're trying to solve? –  Trevor Powell Apr 8 '12 at 12:17
    
@TrevorPowell considering embarking on making a little text adventure for fun, and just want to acquaint myself of the 'state of the art' rather than just diving in and solving it my way is all –  Will Apr 8 '12 at 21:04
1  
Use Inform; that's the best strategy you could ever use. There is virtually no reason to hand-code a text adventure these days. –  Nicol Bolas Apr 9 '12 at 18:29
    
@NicolBolas unless Ludum Dare is approaching? ;) –  Will Apr 10 '12 at 5:44
1  
It's not defeatist as much as being pragmatic. Going into all the techniques advanced text parsing (beyond the obvious stuff anybody can come up with) is probably outside the scope of a single answer here. –  Tetrad Apr 12 '12 at 15:48
show 2 more comments

6 Answers

up vote 7 down vote accepted

Did you search in the interactive fiction community? They still write parsers and some try to push the envelope by implementing new techniques such as natural language processing.

See for example this link for articles describing approaches used:

http://ifwiki.org/index.php/Past_raif_topics:_Development:_part_2#Parsing

share|improve this answer
    
Aha, "text adventure" becomes "interactive fiction" and suddenly its much more googlable! Who'd have thought it'd even change name since I played it? :) Still, looking at those leads, and actually not much gets explained sadly –  Will Apr 8 '12 at 21:09
add comment

In my first-year at university we made an adventure game in Prolog, and for the user input we had to use definite clause grammar or DCG. See http://www.amzi.com/manuals/amzi/pro/ref_dcg.htm#DCGCommandLanguage for an example of using it as a command language. It seemed like a principled (it was uni after all) and flexible approach at the time.

share|improve this answer
add comment

The state of the art for making text adventures today is using Inform 7. Inform 7 source reads "like English," in the same way that Inform-based games let you "write English." For example, from Emily Short's Bronze:

A thing has some text called scent. The scent of a thing is usually "nothing".
The block smelling rule is not listed in any rulebook.
Carry out smelling something:
    say "From [the noun] you smell [scent of the noun]."
Instead of smelling a room:
    if a scented thing can be touched by the player, say "You smell [the list of scented things which can be touched by the player].";
    otherwise say "The place is blissfully odorless."

The Inform 7 parser is closely integrated with the Inform 7 IDE, and the entire source code is not available for study yet:

share|improve this answer
add comment

The term you want is 'natural language processing', or NLP. However, bear in mind that formal methods are designed to try and understand real world texts, whereas you only usually need something that works for a limited subset of your natural language.

Typically you can start out with a simple grammar and vocabulary, then write a parser for it. A grammar might be something simple like this:

sentence = verb [preposition] object
verb = "get" | "go" | "look" | "examine"
preposition = "above" | "below"
object = ["the"] [adjective] noun
adjective = "big" | "green"
noun = "north" | "south" | "east" | "west" | "house" | "dog"

The above is a variant on Backus-Naur form, the standard way of representing grammars. Anyway, you can use a parser generator to generate code to parse this grammar, or write your own fairly easily if your language has decent string handling. (Look up 'recursive descent parsers', which use one function for each line of the grammar.)

Once parsed, you can work out if the sentence makes sense - "go north" may make sense, but "get the green north" does not. You can solve this in 2 ways; make the grammar more formal (eg. have different types of verbs only valid with certain types of noun) or check the nouns against the verb afterwards. The first way can help you to give out better error messages to the player, but you always need to do the second to some degree anyway, as you always need to check context - eg. "take the green key" is grammatically correct and syntactically correct, but you still need to check that the green key is present.

Eventually your program ends up with a validated command with all the various parts checked; then it's just a case of calling the right function with the arguments to perform the action.

share|improve this answer
add comment

You need to define a domain specific language that is all the sentences which are correct in your game. To this end you have to define a grammar for your language (vocabulary and syntax). The type of grammar you need is a Context Free Grammar and there are tools which automatically generate a parser starting from a synthetic description of the grammar such as ANTLR (www.antlr.org). The parser only checks whether a sentence is correct or not and produce an Abstract Syntax Tree (AST) of the sentence which is a navigable representation of the sentence where each word has the role you specified in the grammar. By navigating the AST you have to add the code that assess what is the semantics each word takes when playing that role with respect to the other words in the sentence and verify whether the semantics is correct.

For instance the sentence 'The stone eats the man' is syntactically correct but not necessarily semantically correct (unless in your world stones, maybe magic stones, can eat men).

If also the semantics is correct then you can, for instance, change the world according to it. This could change the context and thus the same sentence could no longer be semantically correct (for instance there could be no man to eat)

share|improve this answer
add comment

The two best current sources for learning to create a text adventure parser are (as was mentioned) the IF community and the mud community. If you search the major forums for those (Intfiction.org/forum, the newsgroup rec.arts.int-fiction, Mud Connector, Mudbytes, Mudlab, Top Mud Sites) you'll find some answers, but if you're just looking for articles I would recommend Richard Bartle's explanation of the parser in MUD II:

http://www.mud.co.uk/richard/commpars.htm

And this explanation on rec.arts.int-fiction:

http://groups.google.com/group/rec.arts.int-fiction/msg/f545963efb72ec7b?dmode=source

No disrespect meant to the other answers, but creating a CF grammar or using BNF is not the solution for this problem. That's not to say it couldn't be a solution for a different problem, that is, creating a more advanced natural language parser, but that's the subject of considerable research and not IMO in the scope of a text adventure.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.