Skip to main content
GameDev.net gamedev.net
🔒 Locked

making a text based rpg. need help.

Started by jakpandora Jul 27, 2004 at 2:22 PM 10 replies 2k views
Original Post
jakpandora
jakpandora
ok. I am have been learning c++ for a while. I think I now know enough to make what I have wanted to make for a while; a small text based rpg. could someone just write me a simple one, just to get me started? I would like it to have four rooms that you can move through. like say you type"north" then you will move to the next room north. when you enter the room. it will give you details about. also, in one of the rooms it may say there is a book in it so you type"book" and you read the book. could someone also implement a very simple battle system. I know this shouldnt be too hard. if I use any of your code i promise I will give you credit. someone help!
______________________________My website: Quest Networks
Agony
Agony
I highly doubt you're going to get any response to your request, in the form that you desire. We all have things to do, none of us want to do your work for you.

That said, what many of us are willing to do is to help you do your work. You're going to have to write it yourself, but if during the design or the implementation you run into a snag, we're here to help.

To start off with, though, try to come up with a simple design. A few functions that will handle various things, such as parsing input, executing a command, displaying information on a room, etc. And design how you will store information regarding rooms, items, and how they relate to each other. From there, consider how you might put it into code.

If you still want source code, though, you might be able to find some text-based RPG engines that are already made. But I doubt anyone is so bored that they're going to work on one from scratch. Much of the work of making games is figuring out how to make the general structure of a game. If you can't do that, then you're going to have problems with plenty of other things as well.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
CodeTitan
CodeTitan
If you're interested in making a text-based RPG, then you should check out ARENA: Evolution. It was written in C++, and seems to have everything that you want. Study the code. It's somewhere on that website...[smile]
jakpandora
jakpandora
ok, then heres my first question. how would you make it so you travel through rooms? would I use if statments? as in if i tyoe north, an if statemnt will execute, if it is possible to go north, it will execute a function for that room. and if not, say that it is invalid. i think thats right. but how would I create the room, would I store it in a variable?

*edit* I hope this is clear enough. now that I look back at it I barely understand it :)

[Edited by - jakpandora on July 27, 2004 3:45:24 PM]
______________________________My website: Quest Networks
OffDaHizzleN
OffDaHizzleN
I am currently in the process of making a text-based RPG, using the commands n,w,s,e,ne,se,sw,nw (North, South, Northeast, etc, etc.) You wouldn't use variables, atleast I am not. You would want to use a map array, how you would do that, I have no idea, because my friend coded it for me. xD lol. Well Just look up some tutorials on Arrays, shouldn't be too hard. And believe me you should do EVERYTHING yourself or else you will never learn. I regret having my friend do the array for me, because I dont understand it still now.
http://www.technation.000k.net/ = Under Construction
Palidine
Palidine
well, here's a basic architecture to get you going. i'm assuming (perhaps incorrectly) that you don't yet understand pointers and how to use them, so i'm keeping it really simple and, arguably, inefficient.

represent your world with a 2D array of Room structs/classes. the Room struct will contain all the data for a give room (like the existence of a book, etc). it will also have some function like isRoom() which will return whether or not this spot in your map is actually a room (it could also be a wall or some other thing that you can't 'enter'). parts of your map that are rooms will return true, parts that are walls will return false.

when you are in any given room, typing any of the cardinal directions will attempt to move you in that direction on the map array. if the direction indicated returns false from isRoom or falls outside the bounds of the 2D array, then you cannot move in that direction, else you move the player to that room.

some things you will need to make this work:
1) the ability to read typed commands. i'd start by making this a console app and just using cin to get the user's commands. start by only supporting the directional commands before you worry about things like books, items, monsters, etc.
2) an understanding of 2D arrays and structs/classes. if you are unclear on these things, look up a C/C++ tutorial on the web and learn about them; better yet, find a book and start learnin.

for starters, just hardcode your room array. later on you'll want some way of reading in a map from a text file or something similar, but don't worry about that for now as it's easy to put in later and will only distract you for now.

-me
DigitalDelusion
DigitalDelusion
Ok, here's everything but the battle system coded this in around 30minutes it's playable but quite limited (for example it only allows one item per room) and really this is not how theese boards usually work.

Feel free to toy around with it.

But hope it helps, it's a really epic cave adventure using 4 rooms and I substitued the book for a button.

Controls should be simple enough, to use an item simply type the item name, to go in a specified direction type that direction in lowercase if you fudgeup it should tell you about the availible commands.

Enjoy!

#include <iostream>#include <string>#include <map>#include <algorithm>#include <memory>#include <cstdlib>//need a command interfaceclass iCommand{public:	virtual ~iCommand(void){}	virtual std::string description(void) const { return "unknown usage\n";}	virtual void execute(void) = 0;};//this is a roomclass cRoom{public:	enum eDir {North, West, South, East, DirectionCount};	cRoom(const std::string& desc): m_strDesc( desc), m_pCommand( 0)	{		for(int i = 0; i != DirectionCount; ++i)			m_pRooms = 0;	}	~cRoom(void)	{		delete m_pCommand;	}	void enter(void) const	{		std::cout << m_strDesc << std::endl;		if( m_pCommand)			std::cout << "\nthere's a " << m_pCommand->description() << " here.\n" << std::endl;	}	void setRoom(eDir dir, cRoom *room)	{		m_pRooms[dir] = room;	}	void setDescription(const std::string &newDesc)	{		m_strDesc = newDesc;	}	cRoom* getRoom(eDir dir) const 	{		return m_pRooms[dir];	}	void setCommand(iCommand *pCommand)	{		delete m_pCommand;		m_pCommand = pCommand;	}	iCommand* getCommand(void) { return m_pCommand;}private:	std::string m_strDesc;	typedef std::map<std::string, std::string> objects_t;	objects_t m_Objects;	cRoom *m_pRooms[DirectionCount];	iCommand *m_pCommand;};//it's always nice to be able to quit.class cQuitCommand : public iCommand{	bool &m_rbQuit;public:	cQuitCommand(bool &rbQuit) : m_rbQuit( rbQuit){}	std::string description(void) const { return "quit this awsome game";}	void execute(void){ m_rbQuit = true;}};//handle movementclass cMoveCommand : public iCommand{	cRoom *&m_prRoom;	cRoom::eDir m_dir;	std::string direction(void) const	{		switch( m_dir)		{			case cRoom::North : return "North";			case cRoom::South : return "South";			case cRoom::East : return "East";			case cRoom::West : return "West";		}		return "crazy!";	}public:	cMoveCommand(cRoom *&prRoom, cRoom::eDir dir): m_prRoom( prRoom), m_dir( dir){}	std::string description(void) const { return "go " + direction();}	void execute(void)	{		if(cRoom *pNext = m_prRoom->getRoom( m_dir))		{			m_prRoom = pNext;		}		else			std::cout << "can't go that way" << std::endl;	}};//the command to win the game!class cFinalExit : public iCommand{	bool &m_rbWin;public:	cFinalExit(bool &rbWin): m_rbWin( rbWin){}	std::string description(void) const { return "exit";}	void execute(void)	{		m_rbWin = true;	}};//this is the object for the button in the control roomclass cMagicButton : public iCommand{	cRoom *m_pChange;	bool &m_rbWin;public:	cMagicButton(cRoom *pChangee, bool &rbWin): m_pChange( pChangee), m_rbWin( rbWin){}	std::string description(void) const { return "button";}	void execute(void)	{		std::cout << "as you press the button you hear a distant rumbling and clinging of metal." << std::endl;		m_pChange->setDescription(			"You're stainding a small oval chamber you can feel a light draft comming\n"			"through a open gate you can smell the fresh air of freedom all that remains\n"			"is to run for the exit."		);		m_pChange->setCommand( new cFinalExit(m_rbWin));	}};typedef std::map<std::string, iCommand*> commands_t;//how to output a commandstd::ostream& operator<<(std::ostream &os, const commands_t::value_type cmd){	return os << cmd.first << " -" << cmd.second->description();}int main(void){	//define our epic saga	cRoom StartRoom(		"You're standing in front of a big evil looking cavern, your adventure\n"		"lies before you. You begin to have second thoughts about this endevour\n"		"but the murky cave beckons you to step forward and embrace your destiny."		);	cRoom Entrance(		"Looking back the warm sunlight outside seems like a distant dream.\n"		"The damp murky interior of the cave seems hostile but your sense of\n"		"adventure tells you to continue, the goal of your voyage must be\n"		"hidden deeper into the cave. Suddenly a cave in closes your only path out."		);	cRoom Exit(		"You're stainding a small oval chamber you can feel a light draft comming\n"		"through a locked gate, smell the fresh air of freedom if you only knew how\n"		"to open it."		);	cRoom ControlRoom(		"You hear the faint sound of dripping water, fungi covers the walls in this\n"		"small chamber. A strange device sits at the middle of the room eminating a\n"		"eriee glow."		);	StartRoom.setRoom( cRoom::North, &Entrance);	Entrance.setRoom( cRoom::West, &ControlRoom);	Entrance.setRoom( cRoom::East, &Exit);	Exit.setRoom( cRoom::West, &Entrance);	ControlRoom.setRoom( cRoom::East, &Entrance);	//some control vars	cRoom *pCurrentRoom = &StartRoom	bool bQuit = false, bWon = false;	std::string strCommand;	ControlRoom.setCommand(new cMagicButton( &Exit, bWon));	//somebody set us up a command list	commands_t commands;	commands.insert( std::make_pair( "quit", new cQuitCommand( bQuit)));	commands.insert( std::make_pair( "north", new cMoveCommand( pCurrentRoom, cRoom::North)));	commands.insert( std::make_pair( "south", new cMoveCommand( pCurrentRoom, cRoom::South)));	commands.insert( std::make_pair( "east", new cMoveCommand( pCurrentRoom, cRoom::East)));	commands.insert( std::make_pair( "west", new cMoveCommand( pCurrentRoom, cRoom::West)));	while( !bQuit && !bWon)	{		pCurrentRoom->enter();		std::cout << "\nexits:";		if( pCurrentRoom->getRoom( cRoom::North)) std::cout << "North ";		if( pCurrentRoom->getRoom( cRoom::South)) std::cout << "South ";		if( pCurrentRoom->getRoom( cRoom::East)) std::cout << "East ";		if( pCurrentRoom->getRoom( cRoom::West)) std::cout << "West ";		std::cout << std::endl << "$";		std::cin >> strCommand;		//try to parse given command		iCommand* pThing = pCurrentRoom->getCommand();		if(pThing && pThing->description() == strCommand)			pThing->execute();		else		{			commands_t::iterator cmd = commands.find( strCommand);			if( cmd == commands.end())			{				std::cout << "command:" << strCommand << " unknown, availible commands are." << std::endl;				std::copy( commands.begin(), commands.end(), std::ostream_iterator<commands_t::value_type>(std::cout, "\n"));				endl( std::cout);			}			else				cmd->second->execute();		}	}	if( bWon)	{		std::cout << 			"As you lounge forward into the freedom outside you take a deep breath\n"			"the sweet summer night smells refreshing, the chirping crickets welcome you.\n"			"Looking up at the stars and full moon you realize that your adventure is over\n"			"somehow you can't help but wonder what use it all was...\n\n\n" << std::endl;		system( "pause");	}	//cleanup	for(commands_t::iterator itr = commands.begin(); itr != commands.end(); ++itr)		delete itr->second;}
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
jakpandora
jakpandora
thanks for everyones help. could someone explain a how I would make rooms out of an array? my book doesnt explain arrays clearly at all. oh and I know what pointeres are and how to use them

*edit* I recently looked through digital delusions code and barely understood it at all! however, I just startd learning cleses, so I should be ok
______________________________My website: Quest Networks
DigitalDelusion
DigitalDelusion
Quote:
Original post by jakpandora
thanks for everyones help. could someone explain a how I would make rooms out of an array? my book doesnt explain arrays clearly at all. oh and I know what pointeres are and how to use them


What part about arrays seems strange to you? if you look at the code I posted you can see it's really quite easy to make something small without getting to messed up with arrays and stuff like that :)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
jakpandora
jakpandora
your code is fine. thanks for makin for me. I just dont understande arrays in general too well becuase my book just says"arrays are really useful, youll use them all the time" but it barely explains them, and the two examples it uses just seem to substitute variables for arrays. maybe I should get a better book.
______________________________My website: Quest Networks
JNewt
JNewt
*WARNING: NON-TECHNICAL EXPLANATION IMMINENT!*

You can think of an array as a bunch of variables that you can call by the same name.

Say you had an array called 'Names':
Names[0]="Jeff";
Names[1]="Justin";
Names[2]="John";

...etc..

You could reference these names by doing this:

myName=Names[1]

This is alot easier than declaring lots of different vars (Name1, Name2, Name3...) and can be vital when using loops.

Assuming that you had filled the Names array up with various names, you could print them out like this:
for (j=0, j==50, j++) {
write Names[j]
}

It gets better! You can two (or more!)-dimensional arrays. Think of a grid like the game Battleship; at the coordinates B7 there might be a ship or a piece of data.

Rooms[0,0]="The Kitchen";
Rooms[0,1]="The Den";
Rooms[1,1]="The Library";

You can create a Room class and fill each 'cell' in the array with an instance of the Room (loaded with data that you can read from a file, of course).

Then you simply need to track the player's current X, Y coordinates and display information about the Room at that location.
7|-|3 p057 @b0v3 i5 c3/^7i|=i3|) 1337!100|< |=0/^ j||3|/|7

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.