Text Based Rooms

Started by
7 comments, last by Sphet 18 years, 9 months ago
In my text based game, there are obviously rooms. Now these rooms would be done in either a struct or a class. So if I made my class/struct struct room { int x, y, z; string roomTitle, roomDesc; } Now the room is shown onscreen cout << roomTitle; cout << roomDesc; (Very basic, just for example) Now if I wanted the character to move around (By typing north east etc) I'm guessing the easiest way to do this would be to use a Pointer. What would be the best way to go about this? The C++ tutorial I use doesn't seem to have much about this type of thing.
Advertisement
Quote:Original post by Woodlander
In my text based game, there are obviously rooms. Now these rooms would be done in either a struct or a class. So if I made my class/struct

struct room {
int x, y, z;
string roomTitle, roomDesc;
}

Now the room is shown onscreen

cout << roomTitle;
cout << roomDesc;

(Very basic, just for example)
Now if I wanted the character to move around (By typing north east etc) I'm guessing the easiest way to do this would be to use a Pointer. What would be the best way to go about this? The C++ tutorial I use doesn't seem to have much about this type of thing.


This doesn't make much sense to me if your going to have multiple rooms you would need more then just a struct like an array or the rooms x,y outside of its self.

Also by doing:cout << roomTitle;cout << roomDesc;Your not putting in something like:cout << MyObject.roomTitle; cout << MyObject.RoomDesc;

Which tends to imply that you have no struct object created.

Im not quite sure what you want could you explain more.
If you are wanting to allow the character to move around the room, I would say that you only really should need to create a character structure and have position variables inside the structure. This way if you want to have NPCs in the room with the character you can just use the same structure.

Maybe a little more information on what you want to accomplish might help.

--Ter'Lenth
--Ter'Lenth
Feel free to look at some code I rustled up for someone a while back:- here

It's very basic, but shows you how I linked up the rooms.
I don't think there is a real point in having a polar coordinate system for a text-based game, you would probably be better off just linking the rooms via exits (as most text-based game engines do). Because there really is no depth in such a game, it would be a lot harder to actually create an area, unless of course you're going to make your own level editor. That's just my thoughts, because you could simply convert existing area editor's to your format and go from there. It would be easier.

The way most MUD (text-based game) engine's work, each room has a unique number (sometimes called VNUM, RID, RNUM) and then a linked list (or array) of exits to other rooms. Exits would have flags (hidden, lockable, etc), directions (north, south, northwest, etc) and then the ID that the exit goes to. So all you have to do is go through the link list (or array) and display the exit information.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
If you're making a text-adventure game, which it sounds like you are, you may want to look up an already-existing game engine such as Inform or TADS. Even if you don't end up using them, they may give you ideas of how to make your own system. The learning curve for Inform at least may be a bit steep, but it definately works.
http://www.inform-fiction.org/
http://www.tads.org/

Of course, if you're not making a text-adventure, this is almost completely useless for you. :-)
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192
I'm not sure, but it seems to me that this person isn't talking about movement within the room, but moving to an adjacent one.

[A ROOM]
This room has stuff, but not really since in most MUDs you won’t be able to interact with most of the things mentioned in this room description. So expect a lot of, "(I can't find what you're looking for)s."
Exits: NORTH, SOUTH, and SOUTHWEST.

North
You exit the room going North.
------------------------------------------------
If this is what was meant, then I would suggest loading your base directions into a dictionary. For instance my plan is to do this, but also add in the ever common LOOK and QUIT commands and a few others. Then if the player enters a single word then I don’t have to pass the command though my fancy parser. I can also create aliases for the commands. N and North, Quit and QQ, NW and Northwest. There’s more, but that’s my basic approach.

As to the exits themselves. If you are asking if the exits are found by room pointers, as far as I know that would be the fastest and best practice.
You wouldn't need pointers to the other rooms, but instead, I would just have a list of exits that you could lookup the room ID via a hash table and return the pointer for the room. Or you could simply just base your movement system on these numbers, and require a movement type to be passed along with it.

Basically:
player->move( this->vnum, 1001, MOVEMENT_WALK );

The move command would basically deal with "can he move here, can he not" related stuffs. From there, you would simply check if this is an exit to that, if that exit is open, closed, locked, whatever.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
I love those old text based games! Here's a quick type up of how I might do it:

// textadv.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <string>#include <ostream>enum eExit{	EXIT_NORTH = 0,	EXIT_SOUTH,	EXIT_EAST,	EXIT_WEST,		EXIT_COUNT};std::string exitString[] = { "North", "South", "East", "West" };class Room{public:	Room( const std::string& _title, const std::string& _description);	std::string	title;	std::string	description;	Room*		pExits[EXIT_COUNT];	void		AddExit( eExit exit, Room* room );	void		Display();	Room*		TakeExit( eExit exit );};Room::Room( const std::string& _title, const std::string& _description){	title = _title;	description = _description;	// Set all the exits to 0, they don't exist yet	for ( int i = 0 ; i < EXIT_COUNT; i++ )	{		pExits = 0;	}}void Room::AddExit( eExit exit, Room* pRoom ){	pExits[ exit ] = pRoom;}void Room::Display( ){	std::cout << title << '\n';	std::cout << description << '\n';	std::cout << "\n";	std::cout << "Exits: ";	for ( int i = 0; i < EXIT_COUNT; i++ )	{		if ( pExits != 0 )		{			std::cout << exitString << " ";		}	}	std::cout << "\n";}Room* Room::TakeExit( eExit exit ){	if ( pExits[exit] != 0 )	{		std::cout << "You take the " << exitString[exit] << " exit\n" ;		return pExits[exit];	}	std::cout << "There is no " << exitString[exit] << " exit!\n" ;	return this;}int main(int argc, char* argv[]){	Room* pCurrentRoom = 0;	Room frontDoor( "Front Door", "It was a dark and stormy night" );	Room hallWay( "Hallway", "It is much colder inside then outside" );	frontDoor.AddExit( EXIT_NORTH, &hallWay );	hallWay.AddExit( EXIT_SOUTH, &frontDoor );	// Start at the front door	pCurrentRoom = &frontDoor	pCurrentRoom->Display();		pCurrentRoom = pCurrentRoom->TakeExit( EXIT_NORTH );	pCurrentRoom->Display();	pCurrentRoom = pCurrentRoom->TakeExit( EXIT_SOUTH );	pCurrentRoom->Display();	return 0;}


The rooms would be added to a list, not declared inside main() of course, and would most likely come from a text file. Regardless, see how each room is attached to another by pointers? This would work pretty well. Of course, you might want to have the exits not be hard coded but come from a list - you could add an exit like Room.AddExit( "Tunnel", &theTunnel ), and then you wouldn't be stuck with just North, South, East or West.

Anyways, I think the pointer scheme works pretty well - there's only one pointer to hold onto and you just use it to figure out how to walk through the game area.

Hope that makes some sense.

This topic is closed to new replies.

Advertisement