need help with data members/functions

Still a beginner at C++.
On line 251,
1
2
loc = rms[loc].exits_to_room[dir[i].code];
                cout << "I am now in a " << rms[loc]. description << "." << endl;

I have the incorrect path to find out what room I am in. Any help on how to find this out?


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
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

using namespace std;

enum en_DIRS {NORTH, EAST, SOUTH, WEST};
enum en_ROOMS {EASTSTREET, WESTSTREET, FRONTYARD_1, FRONTYARD_2, FRONTYARD_3, FRONTYARD_4,
 LIVINGROOM_1,LIVINGROOM_2,LIVINGROOM_3,LIVINGROOM_4, KITCHEN_1, KITCHEN_2, KITCHEN_3, 
 KITCHEN_4, HALLWAY_1, HALLWAY_2, HALLWAY_3, HALLWAY_4, RESTROOM_1, RESTROOM_2, 
 RESTROOM_3, RESTROOM_4, BEDROOM_1, BEDROOM_2, BEDROOM_3, BEDROOM_4, BEDROOM_5, 
 BEDROOM_6, BEDROOM_7, BEDROOM_8, HOUSE_1, HOUSE_2, HOUSE_3, HOUSE_4};
enum en_VERBS {GET, DROP, USE, OPEN, CLOSE, EXAMINE, INVENTORY, LOOK, YELL, SIT}; //2 more verbs

enum en_NOUNS {FRIDGE, CHAIR, TABLE, DOOR, MAILBOX, PIZZA, GARBAGECAN, WOMAN, MAN, KID, BIKE, CAR,BAT, NEWSPAPER, LAWNMOWER}; //make 15+ nouns
// New nouns = FRIDGE, CHAIR, TABLE, DOOR, MAILBOX, PIZZA, GARBAGECAN, WOMAN, MAN, KID, BIKE, CAR, 
// BAT, NEWSPAPER, LAWNMOWER
const int NONE= -1;
const int DIRS = 4;
const int ROOMS = 34;		// make 30 rooms
const int VERBS = 10;		// make 2 more verbs with appropriate actions
const int NOUNS = 15;

class Words
{
    
public:
    
    Words();
    
    string word;
	int code;
};

Words::Words()
{
}


class Room
{
public:
    Room();
    Room(string desc, int exitNORTH, int exitEAST, int exitSOUTH, int exitWEST);
    
	string description;
	int exits_to_room [DIRS];
};

Room::Room()
{
}

Room::Room(string desc, int exitNORTH, int exitEAST, int exitSOUTH, int exitWEST)
{
    description = desc;
    exits_to_room[NORTH] = exitNORTH;
    exits_to_room[EAST] = exitEAST;
    exits_to_room[SOUTH] = exitSOUTH;
    exits_to_room[WEST] = exitWEST;
}

class Noun
{
public:
    Noun();
	
    string word;
	string description;
	int code;
	int location;
	bool can_carry;
};
Noun::Noun()
{
}

//-------------------------------------------------------------------

void set_rooms (vector<Room> &rms)
{

    rms.push_back(Room("We're in the street", FRONTYARD_1, EASTSTREET, FRONTYARD_2, NONE));
    
    rms.push_back(Room("We're in the street", FRONTYARD_3, NONE, FRONTYARD_4, WESTSTREET));
    
    rms.push_back(Room("The front yard", HOUSE_1, FRONTYARD_3, WESTSTREET, NONE));
    
    rms.push_back(Room("The Front yard", WESTSTREET, FRONTYARD_4, HOUSE_3, NONE));
    
    rms.push_back(Room("The Front yard", HOUSE_3, NONE, EASTSTREET, FRONTYARD_1));
    
    rms.push_back(Room("The Front yard", EASTSTREET, NONE, HOUSE_4, FRONTYARD_2));
    
    rms.push_back(Room("The living room", KITCHEN_1, NONE, HOUSE_1, BEDROOM_2));
	
    rms.push_back(Room("The living room", HOUSE_2, NONE, KITCHEN_2, BEDROOM_3));
    
    rms.push_back(Room("The living room", KITCHEN_3, NONE, HOUSE_3, BEDROOM_6));
    
    rms.push_back(Room("The living room", HOUSE_4, NONE, KITCHEN_4, BEDROOM_7));
	
    rms.push_back(Room("The kitchen", NONE, NONE, LIVINGROOM_1, BEDROOM_1));
    
    rms.push_back(Room("The kitchen", LIVINGROOM_2, NONE, NONE, BEDROOM_4));
    
    rms.push_back(Room("The kitchen", NONE, NONE, LIVINGROOM_3, BEDROOM_5));
    
    rms.push_back(Room("The kitchen", LIVINGROOM_4, NONE, NONE, BEDROOM_8));
    
    rms.push_back(Room("The hallway", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The hallway", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The hallway", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The hallway", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The restroom", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The restroom", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The restroom", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The restroom", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The restroom", NONE, NONE, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, KITCHEN_1, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, LIVINGROOM_1, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, LIVINGROOM_2, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, KITCHEN_2, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, KITCHEN_3, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, LIVINGROOM_3, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, LIVINGROOM_4, NONE, NONE));
    
    rms.push_back(Room("The bedroom", NONE, KITCHEN_4, NONE, NONE));
    
    rms.push_back(Room("Welcome to our house", LIVINGROOM_1, NONE, FRONTYARD_1, NONE));
    
    rms.push_back(Room("Welcome to our house", FRONTYARD_2, NONE, LIVINGROOM_2, NONE));
    
    rms.push_back(Room("Welcome to our house", LIVINGROOM_3, NONE, FRONTYARD_3, NONE));
    
    rms.push_back(Room("Welcome to our house", FRONTYARD_4, NONE, LIVINGROOM_4, NONE));
    
    
}

//--------------------------------------------------------------------------------

void set_directions(vector<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 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;
    }
}

//-----------------------------------------------------------------------------

bool parser(int &loc, string wd1, string wd2, vector<Words> &dir, vector<Words> &vbs,
            vector<Room> &rms, vector<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;
              
                if(loc == FRONTYARD_1 || loc == HOUSE_1)
                {
                    nns [DOOR].location = loc;
                }
                //....
                return true;
            }
            else
            {
                cout << "No exit that way." <<endl;
                return true;
            }
        }
    }
    

Last edited on
Topic archived. No new replies allowed.