home *** CD-ROM | disk | FTP | other *** search
- How to Write an Adventure part 4
-
-
- The Command Parser
-
- Once you have displayed the status screen, you then must display a message
- asking for the adventurer's input, and fetch his response. A simple input
- statement works everytime:
-
- 1200 INPUT"WHAT WOULD YOU LIKE TO DO";ANSWER$
- 1210 IF ANSWER$="" THEN 1200
-
- The above routine gets input and immediately checks to see if anything was
- entered. If not, it goes back and tries again. DON'T OMIT THIS TRAP. The code
- will spend lots of time doing nothing if you do.
- Once the input is fetched, it must be evaluated. The first test is to
- determine if a one or two word command was entered. This is done by checking
- for the presence of a space in the input string. The MID$ function allows us to
- look at each character in a string one by one. If a space is found, then there
- obviously must be two words in the input string, and we can break it into two
- words... VERB$ and NOUN$. (Don't forget to clear the variables before you start
- doing you check!!!!!):
-
- 1220 VERB$="":NOUN$=""
- 1230 FOR X=1 TO LEN(ANSWER$):IF MID$(ANSWER$,X,1)<>" " THEN 1250
- 1240 VERB$=LEFT$(ANSWER$,X-1):NOUN$=RIGHT$(ANSWER$,LEN(ANSWER$)-X):
- X=LEN(ANSWER$)
- 1250 NEXT X:IF VERB$="" THEN 2000
-
- At line 2000 is the single command list, with which we will deal in a
- moment.
- Let us assume that two words have been found. We now must find out what
- those words are, and determine if the program knows them. This is accomplished
- as follows:
-
- 1260 V=0:N=0:FOR X=1 TO VERBS:IF VERB$(X)=VERB$ THEN V=X:
- X=VERBS
- 1270 NEXT:IF V=0 THEN PRINT"I DON'T KNOW HOW TO ";VERB$;" SOMETHING":
- GOTO 1200
- 1280 FOR X=1 TO NOUNS:IF NOUN$(X)=NOUN$ THEN N=X:X=NOUNS
- 1290 NEXT:IF N=0 THEN PRINT"I DON'T KNOW WHAT A ";NOUN$;" IS":
- GOTO 1200
-
- And there we are. We first pass through the VERB$ list to see if the verb
- we have entered is in the list. If it is not (V doesn't get assigned a value),
- then we tell the adventurer that the verb is not understood. If the verb is
- good, then we check the noun in the same way. If both pass the west then we go
- the the VERB executive. This is simply an ON V GOTO statement that directs
- program flow to the general area of the program that handles the verb
- possibilities. It might look like this:
-
- 1300 ON V GOTO 3000,3100,3200,3300,3400,3500,3600,3700,3800
-
- If you need more ON V GOTO's than the line length allows, add more this
- way:
- 1310 ON V-9 GOTO 3900,4000,4100,4200,4300,4400
-
- We stated earlier that if the entered string was one word, we dealt with it
- in a single word lookup list. It might look like this:
-
- 2000 IF ANSWER$="LOAD" THEN 10000
- 2010 IF ANSWER$="SAVE" THEN 11000
- 2020 IF ANSWER$="HELP" THEN 12000
- 2030 IF ANSWER$="INVENTORY" THEN 13000
-
- and so forth. In each case, the word triggers a branch to the routine to
- give the desired result. We'll talk more about the routines to DO things in the
- next chapter.
-
-
-
-
-
-
-
-