20100118

Game Text...

                This post has to do with a Human Text reaction system.  It was a common tool used in many telnet games, as well as similar principals are pulled in from Intellisense, and even web searches in some places.  It’s the ability for the application to determine ideas of what you meant from less letters than even a complete word.  A simple idea is a text based game that you have to constantly move East, West, North and South.  It would be a pain for the players to have to type West 20 times in order to go from one city to the next, so the words can be “shortcutted” (interesting word).  Instead of typing in “west” you could just type “w” and it would determine that the most logical word is West, and treat it as if you had typed the whole word.

                I happened to build one out of boredom the other day, so I figured I would share that.  The code is fairly well documented, so this is only going to cover the principals, and basic use of it.  First off, it should be noted that this needs more than a light understanding of C#.  It’s using Events and Delegation, which is roughly the same as turning methods into variables so they can be set, replaced and passed around. 

                Essentially, the first word in a line is a command.  In my case I chose the word “Verb” to name the class for it.  Essentially a Verb is given a word that it relates to, and then it is tied to a method, so if it ever gets triggered, it will call that method.  That part only requires a simple understanding of delegates.  The next part of the complications was in the “Manager” class. 

                The Manager can hold multiple verbs.  When a new verb is passed into it, it looks inside its list of verbs and places it in Alphabetic order.  So if you added West, then North, then South, it would be sorted as North, South, West.  A command called Execute in the manager separates out the first word from the text passed into it, then compares it against each word in its list to see if it matches up.  So, when I type in “s” for south, it goes to the first word, and compares the first letter, “s” does not equal “n” (the first verb being north), so it moves on the next word (south) and sees that the given letter “s” matches.  Once found, if executes that verb, passing in any additional text that was with it.

                When the manager is created, it needs a verb to execute incase it doesn’t find any matching verbs.  So if I typed “X”, which has no verbs, the manager would trigger the “unknown” verb. 

                There is one step farther.  Some words are used far more commonly, like North, South, East and West for various games.  (The words would be different for different systems)  Given this, I wanted to give priority to certain words over others.  Let us say that my game had a command called “Next”, which automatically reads the latest message or something similar.  My players now have to type in “no” at minimum for the system to know they meant North.  Or worse yet, perhaps there is a verb called “Norse” if my game has certain mythological tendencies of some sort, now my players have to type in “nort” before it recognizes “North” as the verb. 

                The priority system is simple.  If you want 2 levels of priority, which should probably be the most you ever use, then you create a “PriorityManager”, with 2 priority levels.  This class would then create 2 managers, and assign each one a priority.  This system treats the lowest priority as the lowest number.  It also uses 1-Based indexes, instead of 0-Based indexes.  It creates its own unknown verb for the managers, and when one manager doesn’t know the word, it passes it all on to the next.  If it gets through all the managers unsuccessful, the priority manager calls its own unknown, to let the system know that the command is unknown.

                The attached project was written in VS 2010 Beta 2, as many of my personal practices and projects are now written in that.  I believe it is free for everyone to download from Microsoft, but the CS files are easy enough to import into C# Express 2008 or 2005.

Download the Project Files Here

No comments: