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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
enum en_DIRS {NORTH, EAST, SOUTH, WEST};
enum en_ROOMS {SPORTSHOP, CASINO, CARPARK, LOBBY, RESTAURANT, CORRIDOR,
STOREROOM, POOL, GARDEN, POND, PUMPROOM};
enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK};
enum en_NOUNS {STORE_DOOR, MAGNET, METER, ROULETTE, MONEY, FISHROD};
const int NONE = -1;
const int DIRS = 4;
const int ROOMS = 11;
const int VERBS = 8;
const int NOUNS = 6;
class words
{
public:
void set_directions(words *dir);
void set_verbs(words *vbs);
string word;
int code;
};
class room
{
public:
void set_rooms(room *rms);
string description;
int exits_to_room[DIRS];
};
class noun
{
public:
void set_nouns(noun *nns);
string word;
string description;
int code;
int location;
bool can_carry;
};
void room::set_rooms(room *rms)
{
rms[SPORTSHOP].description.assign("sports shop");
rms[SPORTSHOP].exits_to_room[NORTH] = NONE;
rms[SPORTSHOP].exits_to_room[EAST] = NONE;
rms[SPORTSHOP].exits_to_room[SOUTH] = CARPARK;
rms[SPORTSHOP].exits_to_room[WEST] = NONE;
rms[CARPARK].description.assign("car park");
rms[CARPARK].exits_to_room[NORTH] = SPORTSHOP;
rms[CARPARK].exits_to_room[EAST] = LOBBY;
rms[CARPARK].exits_to_room[SOUTH] = NONE;
rms[CARPARK].exits_to_room[WEST] = NONE;
rms[LOBBY].description.assign("hotel lobby");
rms[LOBBY].exits_to_room[NORTH] = CASINO;
rms[LOBBY].exits_to_room[EAST] = RESTAURANT;
rms[LOBBY].exits_to_room[SOUTH] = CORRIDOR;
rms[LOBBY].exits_to_room[WEST] = CARPARK;
}
void words::set_directions(words *dir)
{
dir[NORTH].code = NORTH;
dir[NORTH].word = "NORTH";
dir[EAST].code = EAST;
dir[EAST].word = "EAST";
dir[SOUTH].code = SOUTH;
dir[SOUTH].word = "SOUTH";
dir[WEST].code = WEST;
dir[WEST].word = "WEST";
}
//-----
void words::set_verbs(words *vbs)
{
// enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK}
vbs[GET].code = GET;
vbs[GET].word = "GET";
vbs[DROP].code = DROP;
vbs[DROP].word = "DROP";
vbs[USE].code = USE;
vbs[USE].word = "USE";
vbs[OPEN].code = OPEN;
vbs[OPEN].word = "OPEN";
vbs[CLOSE].code = CLOSE;
vbs[CLOSE].word = "CLOSE";
vbs[EXAMINE].code = EXAMINE;
vbs[EXAMINE].word = "EXAMINE";
vbs[INVENTORY].code = INVENTORY;
vbs[INVENTORY].word = "INVENTORY";
vbs[LOOK].code = LOOK;
vbs[LOOK].word = "LOOK";
}
//--
void noun::set_nouns(noun *nns)
{
// enum en_NOUNS {STORE_DOOR, MAGNET, METER, ROULETTE, MONEY, FISHROD}
nns[STORE_DOOR].word = "DOOR";
nns[STORE_DOOR].code = STORE_DOOR;
nns[STORE_DOOR].description = "a closed store room door";
nns[STORE_DOOR].can_carry = false;
nns[STORE_DOOR].location = CORRIDOR;
nns[MAGNET].word = "MAGNET";
nns[MAGNET].code = MAGNET;
nns[MAGNET].description = "a magnet";
nns[MAGNET].can_carry = true;
nns[MAGNET].location = NONE;
}
//
void section_command(string Cmd, string &wd1, string &wd2)
{
string sub_str;
vector<string> words;
char search = ' ';
size_t i, j;
for (i = 0; i < Cmd.size(); i++)
{
if (Cmd.at(i) != search)
{
sub_str.insert(sub_str.end(), Cmd.at(i));
}
if (i == Cmd.size() - 1)
{
words.push_back(sub_str);
sub_str.clear();
}
if(Cmd.at(i) == search)
{
words.push_back(sub_str);
sub_str.clear();
}
}
for (i = words.size() - 1; i > 0; i--)
{
if (words.at(i) == "")
{
words.erase(words.begin() + i);
}
}
for (i = 0; i < words.size(); i++)
{
for (j = 0; j < words.at(i).size(); j++)
{
if (islower(words.at(i).at(j)))
{
words.at(i).at(j) = toupper(words.at(i).at(j));
}
}
}
if (words.size() == 0)
{
cout << "No command given" << endl;
}
if (words.size() == 1)
{
wd1 = words.at(0);
}
if (words.size() == 2)
{
wd1 = words.at(0);
wd2 = words.at(1);
}
if (words.size() > 2)
{
cout << "Command too long. Only type one or two words (direction or verb and noun)" << endl;
}
}
void look_around(int loc, room *rms, words *dir, noun *nns)
{
int i;
cout << "I am in a " << rms[loc].description << "." << endl;
for (i = 0; i < DIRS; i++)
{
if (rms[loc].exits_to_room[i] != NONE)
{
cout << "There is an exit " << dir[i].word << " to a "
<< rms[rms[loc].exits_to_room[i]].description << "." << endl;
}
}
// The look command should check which objects (nouns) are in the
// current room and report them to the player.
for (i = 0; i < NOUNS; i++)
{
if (nns[i].location == loc)
{
cout << "I see " << nns[i].description << "." << endl;
}
}
}
bool parser(int &loc, string wd1, string wd2, words *dir, words *vbs,
room *rms, noun *nns)
{
static bool door_state = false; //false is a closed door.
int i;
for (i = 0; i < DIRS; i++)
{
if (wd1 == dir[i].word)
{
if (rms[loc].exits_to_room[dir[i].code] != NONE)
{
loc = rms[loc].exits_to_room[dir[i].code];
cout << "I am now in a " << rms[loc].description << "." << endl;
// A special case for the corridor storeroom door:
if (loc == STOREROOM || loc == CORRIDOR)
{
nns[STORE_DOOR].location = loc;
}
// ....
return true;
}
else
{
cout << "No exit that way." << endl;
return true;
}
}
}
int NOUN_MATCH = NONE;
int VERB_ACTION = NONE;
for (i = 0; i < VERBS; i++)
{
if (wd1 == vbs[i].word)
{
VERB_ACTION = vbs[i].code;
break;
}
}
if (wd2 != "")
{
for (i = 0; i < NOUNS; i++)
{
if (wd2 == nns[i].word)
{
NOUN_MATCH = nns[i].code;
break;
}
}
}
if (VERB_ACTION == NONE)
{
cout << "No valid command entered." << endl;
return true;
}
if (VERB_ACTION == LOOK)
{
look_around(loc, rms, dir, nns);
return true;
}
// Actions for usage of VERB OPEN:
if (VERB_ACTION == OPEN)
{
if (NOUN_MATCH == STORE_DOOR)
{
if (loc == CORRIDOR || loc == STOREROOM)
{
if (door_state == false)
{
door_state = true;
rms[CORRIDOR].exits_to_room[EAST] = STOREROOM;
rms[STOREROOM].exits_to_room[WEST] = CORRIDOR;
nns[STORE_DOOR].description.clear();
nns[STORE_DOOR].description.assign("an open store room door");
cout << "I have opend the door." << endl;
return true;
}
else if (door_state == true)
{
cout << "The door is already open." << endl;
return true;
}
}
else
{
cout << "There is no door to open here." << endl;
}
}
else
{
cout << "Opening that is not possible." << endl;
return true;
}
}
// ....
return false;
}
//------
int main()
{
string command;
string word_1;
string word_2;
room rooms[ROOMS];
room*roomset_rooms(rooms);
words directions[DIRS];
words*set_directions(directions);
words verbs[VERBS];
words*set_verbs(verbs);
noun nouns[NOUNS];
noun*set_nouns(nouns);
int location = CARPARK;
while (word_1 != "QUIT")
{
command.clear();
cout << "What shall I do? ";
getline(cin, command);
word_1.clear();
word_2.clear();
section_command(command, word_1, word_2);
if (word_1 != "QUIT")
{
parser(location, word_1, word_2, directions, verbs, rooms, nouns);
}
}
return 0;
}
|