Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
insertwackynamehere

C++ Text RPG question

Recommended Posts

How can I go about having a general code that controls which way the player goes (ie N, S, E, W, I, O, U, D), in a console based text RPG in C++? I want it to control every room, but I want the room to be to be able to set limits (because not every room is going to allow every option). Thanks!

Share this post


Link to post

Create a Room class

Create a Room object for each room in the RPG

Have each Room object contain pointers to other Room objects for each direction you can travel from that room, or NULL if you cant go in that direction.

eg.

typedef enum {
    DIRECTION_UP,
    DIRECTION_DOWN,
    DIRECTION_NORTH,
    DIRECTION_SOUTH,
    DIRECTION_EAST,
    DIRECTION_WEST,
    NUM_DIRECTIONS
} direction;

class Room {
    Room *exits[NUM_DIRECTIONS];

    ... rest of class

}

Share this post


Link to post
insertwackynamehere said:

Do not use gotos if you can avoid it.

C++ is an Object-Oriented language. This means that your program consists of a bunch of "objects" which represent pieces of data or concepts within the program. For example, you might have objects representing rooms in the game, and objects representing the items in the game (player avatars, monsters, things you can pick up etc), and if it was multiplayer maybe objects representing each player in the game.

Certain objects "do" the same thing: for example, you might have two objects representing two rooms in your game. These represent different rooms but they are obviously similar; each represents the concept of a room. This is called the "class" of an object. The class of an object defines what it can do. Things you can do to an object of one class you can probably not do to an object of another class; for example, you might be able to make a player move north but it doesnt really make sense to tell a room to move north.

You create a class like this:

class Room {
        Room *exits[NUM_EXITS];
public:
        void add_exit(direction exit, Room *room) {
                exits[exit] = room;
        }
};
Here I have defined a Room class. It has an exits member variable: this means that each object which is of the Room class will store a piece of data containing exits to other rooms.

I also defined a method called "add_exit". A method is a piece of code that does something to an object. You essentially send the object a "message" by invoking the method, telling the object to do something. Here I have added a method that adds an exit to the room.

You can now test this out by creating some rooms and making links between them:
        Room *room1 = new Room();
        Room *room2 = new Room();

        room1->add_exit(DIRECTION_NORTH, room2);
        room2->add_exit(DIRECTION_SOUTH, room1);
Here I've created two rooms (named "room1" and "room2"). This is done with the "new" operator which creates new objects. I then send the room1 object a message: telling it to invoke the add_exit method to add an exit to the object. I tell it that there is an exit north, leading to room2. For consistency I add an exit back to room1 from room2.

I hope this helps a bit. Object Oriented Programming can be difficult to get your head round at first.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×