Parsing Input

We are making a text adventure game, albeit a small one. We realized today, that we have an absurd amount of code to examine one thing. For example, we need to use a set of stairs to go down, and up and we have about 12 different sets of "go down stairs" and "go up stairs".

So, our question to you is how can we make our program read the input of the player and analyze the words. If the phrase has a set of words in it, than it will display a certain text.

For example, if a player enters "Walk up the stairs", the program will read "Up", and "Stairs" and display the appropriate text or do the associated action.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 if (strcmp(stairs1, "walk stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "walk the stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "walk down stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "walk down the stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "walk the down stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "go stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "go down stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "go down") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "go down the stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "climb down stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "climb down the stairs") == 0){
                       ClearScreen();
                       stairs2();
                       }
     if (strcmp(stairs1, "go up") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "climb stairs") == 0){
                       monkey();                  
                       }
     if (strcmp(stairs1, "climb up stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "climb up the stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "walk the up stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "walk up stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "walk up the stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "go up stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "go up the stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "move up") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "move up the stairs") == 0){
                       monkey();
                       }
     if (strcmp(stairs1, "move up stairs") == 0){
                       monkey();
                       }


~Purple Dizzy.
Regular expressions is probably what you're looking for. Using it can be daunting at first, but it doesn't take long to pick up.

I don't have any resources to link you on hand but google is your friend, and I'm sure other members can help.
regular expressions don't really make sense when you're looking for keywords.
I'm sure this is only one of many places in your code where you have this problem, so you might want to look at a more generalized solution.

In your example, the words fall into four categories:
- A verb
- A direction
- Noise words (the)
- An object (stairs)

There is a powerful programming technique call Finite State Machines (FSM) that might be suitable.
http://en.wikipedia.org/wiki/Finite-state_machine

You could create a state table consisting of the current state (location), the possible event (verb), the action to take and the next state (location). Then it becomes a simple matter of a table lookup using the player's current location and the verb (event). This determines the action to take and the next state (location).
Topic archived. No new replies allowed.