Qbasicnews.com

Full Version: Text Adventure Lib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm making a Text Adventure library, so people can make their own Text Adventures with QB.
I need code to determine whether a word is a verb, noun, or adjective. Should I use Select Case, or is there a faster way?
P.S. I don't know ASM or FB, this is pure QBasic.

Anonymous

You need some kind of lookup table. However, that will be slow in qb i bet, it would be better to hardcode special keywords, I think.
Just a thought! If the only thing you need is to identify a given word that is input, and you know, beforehand, all the possible words that can be used, perhaps you can first have the words classified in one of 26 SUBs, according to their first letter. Then, you would have sub-SUBs, say some 10 per group maximum, each with up to 10 words, in a SELECT CASE group. With this strategy, you could have 26*10 *10= 2600 words, which is probably too many, and could access any word fairly easy.

To identify a word as a noun, adjective or verb, you could have each word in the final SELECT CASE be grouped as one of those three parts of speech, so that the word would identify its own part of speech type by being found in the particular group.

Would this work for you?
I'd suggest making 3 text files (1 for verb, noun, adjective) and loading them at the start of the program. Load them into a custom data type like so:

Code:
TYPE TWORD

    Word AS STRING
    Type AS STRING

END TYPE

DIM Words(500) AS TWORD
500 is the maximum number of words. If you want more just make it higher.

All words will go into this array and so will their types.

Example:

Code:
Words(123).Word = "blue"
Words(123).Type = "ADJ"  '- (or "VERB" for a verb, "NOUN" for a noun)

Then when you want to compare it with a string the user entered, perform an iterative search on the array. When you get a match you will also get what type of word it is.

Hope this helps.
Here's an example to show how it'd all word together:

Code:
OPTION EXPLICIT

TYPE TWORD
    
    Word AS STRING
    Typ AS STRING

END TYPE

DIM Words(500) AS TWORD
DIM Key AS STRING
DIM I AS INTEGER

Words(1).Word = "Joe":      Words(1).Typ = "noun"
Words(2).Word = "Rachel":   Words(2).Typ = "noun"
Words(3).Word = "Raplh":    Words(3).Typ = "noun"
Words(4).Word = "bite":     Words(4).Typ = "verb"
Words(5).Word = "kick":     Words(5).Typ = "verb"
Words(6).Word = "punch":    Words(6).Typ = "verb"
Words(7).Word = "kill":     Words(7).Typ = "verb"
Words(8).Word = "blue":     Words(8).Typ = "adj"
Words(9).Word = "hairy":    Words(9).Typ = "adj"
Words(10).Word = "soft":    Words(10).Typ = "adj"

Key = "blue" '- or any word you want

'- search for word
FOR I = 1 TO 10
    
    IF UCASE$(Words(I).Word) = UCASE$(Key) THEN
        
        PRINT Words(I).Word + " is a " + Words(I).Typ
        EXIT FOR
        
    END IF
    
NEXT I

SLEEP

END
I coded this in FB and it works. Since it's simple it will probably work in QB too. The speed will depend on how many words you have and how efficient your search algorithm is. This is the most basic search algorithm. An example of a faster one would be binary search, but that may prove difficult since you are dealing with words insead of numbers.
Hi na_th_an, I wasn't ignoring your tuts, I just forgot about them. :oops: I remembered the previous thread on this subject after I posted this one.

EDIT: In chapter 5, you suggest setting puton and wear as synonyms. What happens when you try to put something on a table? :???:
Quick question:
Would you folks prefer a library, so you can make your own games by calling the functions when you need them, or would you rather have an engine and editor suite where the editor provides an easy-to-use interface to make your own games- laying out the rooms, objects, etc.?

P.S. Like my new avatar?
Quote:I wrote a series of articles at QBE about this topic that everyone seems to be ignoring :lol:

What you ask for:

http://petesqbsite.com/sections/express/issue6/#ifgames
http://petesqbsite.com/sections/express/issue7/#ifgames
http://petesqbsite.com/sections/express/issue8/#ifgames
http://petesqbsite.com/sections/express/issue9/#ifgames
http://petesqbsite.com/sections/express/...0/#ifgames
http://petesqbsite.com/sections/express/...2/#ifgames
http://petesqbsite.com/sections/express/...3/#ifgames

Check the chapter in issue 10, it discusses about vocabulary management and parsing techniques.
lol I never read the first sentence until now. You probably should have just spelled it out entirely instead of using the acronym.

Skyler:

I'd prefer no library to use because the fun part I like about coding text games is the coding.

Your avatar is ok.

Did you find any of my help useful?
What I mean is, would you prefer a stack of functions/subs that you can use in your own program, or an automated GUI-like interface so you don't interact directly with the source code?

P.S. No, RR, I didn't use your code. I might in something else(if I can fins\d something to use it in...)