Classes/Structs

Started by
2 comments, last by Zahlman 18 years, 9 months ago
I know (sortof) how to create classes and structs etc, but what I'm confused about is how to use them properly. (The code is just theoretical, not how my game actually looks/will look) Lets say that in my game I have a struct room string pInput struct room { int x, y, z string roomTitle, roomDesc bool n, e, s, w } Now I have the display at the beginning of the game which prints out the player's view cout << roomTitle << endl; cout << roomDesc << endl; cout << "Enter Command: "; cin >> pInput; Now lets say they went north, using the room struct, how would I change the rooms etc? Would I instantiate every room in the game first? Or would I instatiate a room when they entered, and then destroy them when they leave. If thats the case how could I make items 'persistent' so if they drop an item in a room, it's still there when they come back, or when they save and exit and load. How would I go about doing this? Thanks in advance =) I use C++
Advertisement
I would use a vector to store all the rooms... and that creation / termination part, thats what class constructors / deconstructors are for.

Read more about classes, it should jump straight at ya! :P Good luck.
Depending on the size of the rooms and the style of game you might want to instantiate them all first.

For example, you might want creatures wandering around in the rooms in which case you at least need some information available so that you can keep track of where things are.

Also, rather than bool's - you could have pointers to other room in the n, s, e, w members. If you then had a pointer to the current room and the user wants to go north - check if n is null, if it isn't move your current room pointer.

"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Indeed. Having the bools doesn't make particularly much sense. A room is *connected* to other rooms; the set of room objects logically is a graph. You accomplish this in code by pointers: each room has room* pointers, that point at the neighbouring rooms. Then when you need to "go to" another room, you can check the appropriate pointer of the current room, and change the "current room" pointer appropriately. So the game loop looks like (for example - very rough sketch!)

int main() {  // Create all the rooms here and link them up  Room* location = &firstRoom  while(!gameOver()) {    location->display();    // In askUserForDirection(), we iteratively prompt for input and then check    // what we're given; if it's valid, we return something that indicates    // which way the user wants to move - I'd recommend using an enum.    location = location->move(askUserForDirection());  }}// The Room class then has code like:void Room::display() {  // output the room title and description}Room* Room::move(Direction d) {  // Since the Room holds pointers to adjacent rooms, we just have to retrieve  // the pointer value that's appropriate for the requested direction.  switch(d) {    case NORTH: return n;    case EAST: return e;    case WEST: return w;    case SOUTH: return s;  }}


The hard part is creating all the rooms in a way that doesn't make you want to stab yourself because of the tedium of linking everything together etc. in code :) What you want to do is create some kind of file format to describe a room's contents, and read in the data and create Room objects from it at the program start. This can be rather involved; the best reference I can offer you is the project of this type I did myself as a sample ;) Rob, have you still got that CYOA code around?

This topic is closed to new replies.

Advertisement