Incorporating classes
Using a class
for both locations and objects will make things easier to
modify going forward. You don’t need to add any new commands, locations, or
objects, but your program should contain two classes:
class object {
...
};
class location {
...
};
location
should contain all the details about a location, including the
text printed when you LOOK
, all the directions you can GO
from that
location and where they lead to, and all object
s which are at that location.
object
s, similarly, should contain the text printed when you LOOK
at the
object, the name of the object (used with GET
, DROP
, and printed when you
take INVENTORY
), and any other relevant information (e.g., whether a lamp
is on or off).
The player’s inventory and the contents of each location should now be represented
as vector<object>
s. You should represent all the location in the game as a
vector<room>
. Then, using an int current_room
variable to keep track of
where the player is; the value of this variable is an index into the vector.
Functions for commands that apply to the current location (LOOK
without a
noun, GO
) should be turned into function members on the location
class.
Commands for verbs which require a noun can stay as functions, since they
often need to know about both object
s and location
s.
By now, there are several parts of your program which could throw
exceptions (.at()
on vectors and strings, .substr()
, etc.). You should wrap
these in try...catch
and catch the relevant exceptions (usually just
out_of_range
). You can “handle” the exception by just printing an error
message and ending the program.
Submitting
Save your source code file(s) into ~/projects/stage3/
.