Hello everyone,

I'm working on a school work to create a simple text-based dungeon game. In this game, a player goes from one room to another room. Each room may have an item to be taken. Commands used are like: go south, go north, take sword, drop sword, look (to get the description of current room), etc.

To implement this in C++, I've come across some ideas and therefore I've created some classes. They are:

  • Item class - item has a name, weight, and value.
  • Room class - a room has a name, description, and treasure (Item-data type)
  • Action class - action class captures the command typed by the user. This will store each word typed (max. 5 words as command). Usually only the first 2 words will be taken as command (e.g. go west, take box)
  • Player class - this class will organize the flow of the game. It has a function play() that will be called from main function to start the game. A player contains a currentRoom and actions (Action-data type)

In the main function, I create the objects needed for the game, such as the items and the rooms by hardcoding the name and description for each room and item. Each item will be put into a Room. So I will have an array of rooms. I will pass this array of rooms into the play() function.

Well, now I'm kinda confused about the idea. First, what would an Action class exactly do? If it captures the command typed by user, then how am I going to connect the command to particular action? Does it have to be hardcoded in someway, such as: if command == "go" then do this, etc ?

Secondly, I don't have any idea on how I am going to interconnect all the rooms into a map. The player should go from the starting point. Let's say, it goes north. Then when player goes south, he will come back to previous room. Each room should be connected to other rooms and there may be some direction that cannot be taken, e.g. from room A you can only go east, going west is not allowed.

Can I have some solutions or suggestions to my problems? Thanks in advance.

Say you have Rooms map[10]; // there are 10 rooms in the map Each room has an array of neighbours

enum directions { NORTH, EAST, SOUTH, WEST, MAXDIRS };
int neighbours[MAXDIRS];

Having initialised all your data, then moving north say would be simply currentRoom = map[currentRoom].neighbours[NORTH]; Valid moves would be a value of 0 to 9.
Then you can have special values to indicate such things as "Wall" or "This way is magically blocked".

I started this a while ago and I have a skeleton of a system on my site with the source and the executable since this was discussed earlier on the forums (search for Several C++ design questions)

The game I have written is pretty much identical to what you are saying accept that currently it supports mob(enemy) scripting, game saves and has a custom equipment system. It's on the devblog section of my site LazyCode.info/DevBlog
Note: It is GPLd as all programs on my site as per the agreement in my Programs page.

This article has been dead for over six months. Start a new discussion instead.