Repository navigation
Expand file tree
/
Copy pathAdvParser.cpp
More file actions
255 lines (222 loc) · 7.62 KB
/
Copy pathAdvParser.cpp
File metadata and controls
255 lines (222 loc) · 7.62 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <string>
#include <map>
#include "User.h"
#include "InteractObject.h"
#include "Door.h"
#include "Key.h"
#include "Painting.h"
#include "Mirror.h"
#include "Sword.h"
#include "AdvParser.h"
using namespace std;
//the class is static since there will only be one version needed.
typedef void (User::*userFunc)(string s);//tells the computer that pointer userFunc is a pointer to a void method
typedef InteractObject *objectPointer;
map<string, userFunc> playerVerbSynonym;//map between strings and a userFunc pointer This one checks to see if the verb is one specific to the user only
map<string, string> verbSynonym;//map between strings and a condensed string
map<string, objectPointer> dObjectSynonym;
map<string, objectPointer> iObjectSynonym;
void AdvParser::initMap()
{
playerVerbSynonym.insert(make_pair("move", &User::move));
playerVerbSynonym.insert(make_pair("go", &User::move));
playerVerbSynonym.insert(make_pair("inv", &User::printInventory));
playerVerbSynonym.insert(make_pair("look", &User::printRDescription));
verbSynonym.insert(make_pair("use", "use"));//used to condense and make the pairs
verbSynonym.insert(make_pair("push", "use"));
verbSynonym.insert(make_pair("pull", "use"));
verbSynonym.insert(make_pair("move", "use"));
verbSynonym.insert(make_pair("open", "open"));
verbSynonym.insert(make_pair("pull", "open"));
verbSynonym.insert(make_pair("unlock", "unlock"));
verbSynonym.insert(make_pair("get", "get"));
verbSynonym.insert(make_pair("pick", "get"));
verbSynonym.insert(make_pair("pickup", "get"));
verbSynonym.insert(make_pair("grab", "get"));
verbSynonym.insert(make_pair("take", "get"));
verbSynonym.insert(make_pair("drop", "drop"));
verbSynonym.insert(make_pair("remove", "drop"));
verbSynonym.insert(make_pair("look", "look"));
verbSynonym.insert(make_pair("examine", "look"));
verbSynonym.insert(make_pair("check", "look"));
verbSynonym.insert(make_pair("break", "break"));
verbSynonym.insert(make_pair("smash", "break"));
verbSynonym.insert(make_pair("hit", "break"));
verbSynonym.insert(make_pair("stab", "break"));
verbSynonym.insert(make_pair("burn", "burn"));
dObjectSynonym.insert(make_pair("door", new Door("A simple, wooden door.", "Mirror Room")));
iObjectSynonym.insert(make_pair("door", dObjectSynonym["door"]));
dObjectSynonym.insert(make_pair("mirror", new Mirror("A large floor to ceilling mirror spanning the entire wall.", "Mirror Room")));
iObjectSynonym.insert(make_pair("mirror", dObjectSynonym["mirror"]));
dObjectSynonym.insert(make_pair("sword", new Sword("A strangely ornate, if a bit rusty, sword.", "Tiny Room")));
iObjectSynonym.insert(make_pair("sword", dObjectSynonym["sword"]));
dObjectSynonym.insert(make_pair("key", new Key("A shiny key.", "Mirror Room")));
iObjectSynonym.insert(make_pair("key", dObjectSynonym["key"]));
dObjectSynonym.insert(make_pair("painting", new Painting("An unnerving portrait whose eyes seem to follow you around the room.", "Mirror Room", dObjectSynonym["key"])));
dObjectSynonym.insert(make_pair("portrait", dObjectSynonym["painting"]));
iObjectSynonym.insert(make_pair("painting", dObjectSynonym["painting"]));
}
void AdvParser::parseInput(string userInput, User& B)
{
//reset the fields
verb = "";
directObject = "";
indirectObject = "";
string input = userInput;
//Variables
int counter = 0;//used for tokenizing
string cursor = "";
//Make sure input has something in it.
if (input.length() < 1)
{
cout<<"What did you say?"<<endl;
return;
}
//Step 0: all to lower
for(int i = 0; i < input.length(); i++)
{
input[i] = tolower(input[i]);
}
//Use loop to process all input
if(input.find(' ') == -1)//only one word
{
//look for move
if (input == "north" || input == "n")
{
verb = "move";
directObject = "n";
}
else if (input == "south" || input == "s")
{
verb = "move";
directObject= "s";
}
else if (input == "west" || input == "w")
{
verb = "move";
directObject = "w";
}
else if (input == "east" || input == "e")
{
verb = "move";
directObject = "e";
}
else if(input == "i" || input == "inv" || input == "inventory")
{
verb = "inv";
directObject = "inv";
}
else if(input == "look")
{
verb = "look";
directObject ="look";
}
}
else
{
while(input.find(' ') != -1)
{
if (counter > 9)//too long
{
cout<<"Speak more consise please"<<endl;
return;
}
cursor = input.substr(0,input.find(' '));
if(counter == 0)//first word
{
verb = cursor;
userInput = input.substr(input.find(' ')+1);
}
else if(directObject == "")//if the directObject is not yet found
{
if(dObjectSynonym.find(cursor) != dObjectSynonym.end())
{
directObject = cursor;
}
}
else
{
if(iObjectSynonym.find(cursor) != iObjectSynonym.end())
{
indirectObject = cursor;
}
}
input = input.substr(input.find(' ')+1);//gets rid of word from input
counter++;
}
cursor = input.substr(0,input.find(' '));
if(counter == 0)//first word
{
verb = cursor;
}
else if(directObject == "")//if the directObject is not yet found
{
//cout<<input<<" "<<cursor<<endl; //use to error check
if(cursor == "north")//CONDENSE THIS AT SOME POINT
directObject = "n";
else if(cursor == "south")
directObject = "s";
else if(cursor == "east")
directObject = "e";
else if(cursor == "west")
directObject = "w";
else if(dObjectSynonym.find(cursor) != dObjectSynonym.end())
{
directObject = cursor;
}
}
else
{
if(iObjectSynonym.find(cursor) != iObjectSynonym.end())
{
indirectObject = cursor;
}
}
input = input.substr(input.find(' ')+1);//gets rid of word from input
}
if(directObject == "")
{
cout<<"Sorry, I don't understand \"" + userInput +"\"."<<endl;\
return;
}
//cout<<endl<<verbSynonym[verb]<<" "<<directObject<<" "<<indirectObject<<endl<<endl; //use to error check for verbs
interpretInput(B);
}
void AdvParser::interpretInput(User& B)//calls the methods using the dictionary
{
userFunc a;
string b;
objectPointer objPoint, iobjPoint;
if((playerVerbSynonym.find(verb) != playerVerbSynonym.end()) && (directObject == "n" || directObject == "s" || directObject == "w"|| directObject == "e"|| directObject == "inv" || directObject == "look"))//currently only checks if direct object is a direction, eventually could change into a dictionary of items.
{
a = playerVerbSynonym[verb];//creates a to store location
(B.*a)(directObject);//use pointer USES ACTUAL DIRECT OBJECT, OK FOR MOVE
}
else if((verbSynonym.find(verb) != verbSynonym.end() && dObjectSynonym.find(directObject) != dObjectSynonym.end() )&&indirectObject == "")
{
b = verbSynonym[verb];//creates a to store location
objPoint = dObjectSynonym[directObject];
objPoint->beCalled(b, B);
}
else if((verbSynonym.find(verb) != verbSynonym.end() && dObjectSynonym.find(directObject) != dObjectSynonym.end() )&&(iObjectSynonym.find(indirectObject) != iObjectSynonym.end() ))
{
b = verbSynonym[verb];//creates a to store location
objPoint = dObjectSynonym[directObject];
iobjPoint = iObjectSynonym[indirectObject];
objPoint->beCalled2(b, B,iobjPoint);
}
else
{
if(verbSynonym.find(verb) == verbSynonym.end())
cout<<"I don't understand " <<verb<<"."<<endl;
else
cout<<"I don't understand " <<directObject<<"."<<endl;
}
}
AdvParser::AdvParser()
{
verb = "";
directObject = "";
indirectObject = "";
};