Help need with adventure text game.

1
1.Hello please I need a help with the following in adventure.cpp game.Please advise.Thanks
Create constructors and destructors for each class you make from a converted “struct”

Create overloaded constructor functions
Create overloaded constructor functions for the one or more classes you converted from “structs”

Class public/private members
Make sure you have public/private members of your classes (data members or member functions)

Use “setters/getters” for private data members
Use functions to get/set the values of your private data members in your classes (“setters/getters”)

Use a static data member to limit number of objects
Consider using a static data member that limits the maximum number of objects that may be created for your converted class (word, room, noun)

Use constructors to create the objects
Use a constructor to initialize your data, instead of a function that initializes the data in the structs (Note - you may still call a function that in turn will call your constructors)
50
Change rooms array into vectors
Change the rooms[ROOMS] array into a vector, use the STL (sort, shuffle, etc.) where appropriate

Change directions array into vectors
Change the directions[DIRS] array into a vector, use the STL (sort, shuffle, etc.) where appropriate

Change verbs array into vectors
Change the verbs(VERBS] array into a vector, use the STL (sort, shuffle, etc.) where appropriate

Change nouns array into vectors
Change the nouns[NOUNS] array into a vector, use the STL (sort, shuffle, etc.) where appropriate
25
Re-theme the game
Make it your own. Instead of rooms at a casino, make it a “dungeon”, or a “cave”, or a “forest”. This means the nouns and verbs should also follow the theme.

I have cut the code to make it fit.So I will be posting the source code in 3 steps.
Thanks.

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
  Put the code you need help with here.


   #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 :
	       string word;
		   int code ;
		   Words (); // This a constructor declaration
		   ~Words (); // This is a destructor declaration
	  
	  };
	  Words::Words():
	  {
	  
	  }
	  
	  
	  class Room
	  {
	  public:
	  string description;
	  int exits_to_Room[DIRS];
	  Room ();// This a constructor declaration
	  ~Room ();// This is a destructor declaration
	  
	  };
	  Room::Room(int exiits_to_Room);
	  
	  class Noun
	  {
	  public:
	     string word ;
		 string description;
		 int code ;
		 int location;
		 bool can_carry ;
		 Noun (); // This a constructor declaration
		 ~Noun (); // This is a destructor declaration
	  
	  };
	  
	  Noun::Noun()
	  {
	  
	  }
	  
	  //------------------------------------------------------------------------------------------------------------------------------------------------
	  void set_rooms(vector<Room> &rms)
	  {
	     rms.push_back(Room(SPORTSHOP,"sports shop",NONE,NONE,CARPARK,NONE));
	     
		 rms[CASINO].description.assign("bustling casino");
		 rms[CASINO].exits_to_Room[NORTH] = NONE ;
		 rms[CASINO].exits_to_Room[EAST] = NONE ;
		 rms[CASINO].exits_to_Room[SOUTH] = LOBBY ;
		 rms[CASINO].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[NORTH] =  CARPARK;
		 
		 rms[RESTAURANT].description.assign(" restaurant");
		 rms[RESTAURANT].exits_to_Room[NORTH] =  NONE;
		 rms[RESTAURANT].exits_to_Room[EAST] =  NONE;
		 rms[RESTAURANT].exits_to_Room[SOUTH] =  NONE;
		 rms[RESTAURANT].exits_to_Room[NORTH] =  LOBBY;
		 
		 rms[CORRIDOR].description.assign(" corridor");
		 rms[CORRIDOR].exits_to_Room[NORTH] =  LOBBY;
		 rms[CORRIDOR].exits_to_Room[EAST] =  NONE;
		 rms[CORRIDOR].exits_to_Room[SOUTH] =  GARDEN;
		 rms[CORRIDOR].exits_to_Room[NORTH] =  NONE;
		 
		 rms[STOREROOM].description.assign("store room");
		 rms[STOREROOM].exits_to_Room[NORTH] =  NONE;
		 rms[STOREROOM].exits_to_Room[EAST] =  NONE;
		 rms[STOREROOM].exits_to_Room[SOUTH] =  NONE;
		 rms[STOREROOM].exits_to_Room[NORTH] =  NONE;
		 
		 rms[POOL].description.assign(" swimming pool area");
		 rms[POOL].exits_to_Room[NORTH] =  NONE;
		 rms[POOL].exits_to_Room[EAST] =  GARDEN;
		 rms[POOL].exits_to_Room[SOUTH] =  PUMPROOM;
		 rms[POOL].exits_to_Room[NORTH] =  NONE;
		 
		 rms[GARDEN].description.assign(" tranquil garden");
		 rms[GARDEN].exits_to_Room[NORTH] =  CORRIDOR;
		 rms[GARDEN].exits_to_Room[EAST] =   POND;
		 rms[GARDEN].exits_to_Room[SOUTH] =  NONE;
		 rms[GARDEN].exits_to_Room[NORTH] =  POOL;
		 
		 rms[PUMPROOM].description.assign(" damp pump room");
		 rms[PUMPROOM].exits_to_Room[NORTH] =  POOL;
		 rms[PUMPROOM].exits_to_Room[EAST] =  NONE;
		 rms[PUMPROOM].exits_to_Room[SOUTH] =  NONE;
		 rms[PUMPROOM].exits_to_Room[NORTH] =  NONE;
	  }
	 
	 
	 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	 

	
Last edited on
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

	 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	 
	 void 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 set_verbs(Words *vbs)
	 {
	 
	     // enum en_VERBS { GET, DROP , 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 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 close 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 ;
		 nns [METER].word = "METER";
		 nns [METER].code = METER;
		 nns [METER].description = " a  parking meter";
		 nns [METER].can_carry = false;
		 nns [METER].location = CARPARK ;
		 nns [ROULETTE].word = "ROULETTE";
		 nns [ROULETTE].code = ROULETTE;
		 nns [ROULETTE].description = " a roulette wheeel";
		 nns [ROULETTE].can_carry = false;
		 nns [ROULETTE].location = CASINO ;
		 nns [MONEY].word = "MONEY";
		 nns [MONEY].code = MONEY;
		 nns [MONEY].description = " some money";
		 nns [MONEY].can_carry = true;
		 nns [MONEY].location = NONE ;
		 nns [FISHROD].word = "ROD";
		 nns [FISHROD].code = FISHROD;
		 nns [FISHROD].description = " a fishng rod";
		 nns [FISHROD].can_carry = false;
		 nns [FISHROD].location = SPORTSHOP ;
	 
	 }
	 //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	 
	 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;
		 }
		 
	 }

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
// the 3rd and last part of the entire source code.
	 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	 void look_around(int loc, vector<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, vector<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;
	 }
	 
	 // Action 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 open the door." << endl;
					 return true;
				 }
				 else if(door_state == true)
				 {
				     cout << "The door is already open." << endl;
					 return true;
				 }
			 }
			 else
			 {
			     cout << "The is no door to open here." << endl;
				 return true;
			 }
		 }
		 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];
		set_rooms: vector(rooms);
		Words directions[DIRS];
		set_directions(directions);
		
		Words verbs[VERBS];
		set_verbs(verbs);
		
		Noun nouns[NOUNS];
		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, vector<Rooms>, nouns);
		   }
		}
		return 0 ;
		
	}
	 
Topic archived. No new replies allowed.