Sign in to follow this  

Text RPG code problems

This topic is 2397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently coding a very simple text RPG and so far everything is working well, but the code is starting to get confusing because of so many strings being used. How could I make the code work simpler and not require so many braces ("{" "}")

[code]
//Very simple text RPG
#include <iostream>

using namespace std;

int main()
{
cout << "Hello and welcome to my game!" << endl; //greet the user
cout << "What is your name?" << endl; //ask for name
string name; //define name
cin >> name; //read into name


//write the greeting
cout << "Hello , " << name << "!" << endl;
cout << "You are about to embark on an epic journey" << endl;
cout << "Good luck, " << name << endl;
cout << "You are faced with a path, it forks off in two ways" << endl;
cout << "Do you wish to go on the left path or right?" << endl; //first path choice
cout << "Left (a)" << endl;
cout << "Right (b)"<<endl;
string path1;
cin >> path1;
if (path1 == "a")
{
cout << "You are faced with a very large ogre" << endl; //choice if you choose a
cout << "Punch it (a)" << endl;
cout << "Flee (b)" << endl;
string path2;
cin >> path2;
if (path2 == "a")
{
cout << "Your strike deflects back off it's hard forehead" << endl;
cout << "The ogre picks up a large boulder and smashes you with it" << endl;
cout << "GAME OVER, " << name << "!" << endl;

}
if (path2 == "b")
{
cout << "Your desicion was wise. The ogre is slow!" << endl;
cout << "You arrive at a strange swamp, a strange man is standing next to it" << endl;
cout << "Attack him (a)" << endl;
cout << "Attempt to speak to him (b)" << endl;
string path21;
cin >> path21;
}
}
if (path1 == "b")
{
cout << "You arrive at a dead end, in front of you is a chest" << endl; //choice if you chose b
cout << "Attempt to open it (a)" << endl;
cout << "Run (b)" << endl;
string path3;
cin >> path3;
}
return 0;
}
[/code]


Thank you.

Share this post


Link to post
Share on other sites
You don't have all that many braces. But the code will be more readable and easier to follow if you move the text into functions (or data structures called by functions), which you call where needed to display a block of text.

Pseudocode:

[code]
// Function definition

public void Greeting(string name)
{

cout << "Hello, " << name << "!" << endl;
....
}

int main()
{

string name;
cin >> name;

// Display the greeting
Greeting(name);
}
[/code]

And so on. Sorry if the code I wrote sucks, it's been a while since I did anything in C++. But hopefully it's enough to give you the idea.

Share this post


Link to post
Share on other sites
[quote name='tasticmaster' timestamp='1306363253' post='4815804']
I am currently coding a very simple text RPG and so far everything is working well, but the code is starting to get confusing because of so many strings being used. How could I make the code work simpler and not require so many braces ("{" "}")

[code]
//Very simple text RPG
#include <iostream>

using namespace std;

int main()
{
cout << "Hello and welcome to my game!" << endl; //greet the user
cout << "What is your name?" << endl; //ask for name
string name; //define name
cin >> name; //read into name


//write the greeting
cout << "Hello , " << name << "!" << endl;
cout << "You are about to embark on an epic journey" << endl;
cout << "Good luck, " << name << endl;
cout << "You are faced with a path, it forks off in two ways" << endl;
cout << "Do you wish to go on the left path or right?" << endl; //first path choice
cout << "Left (a)" << endl;
cout << "Right (b)"<<endl;
string path1;
cin >> path1;
if (path1 == "a")
{
cout << "You are faced with a very large ogre" << endl; //choice if you choose a
cout << "Punch it (a)" << endl;
cout << "Flee (b)" << endl;
string path2;
cin >> path2;
if (path2 == "a")
{
cout << "Your strike deflects back off it's hard forehead" << endl;
cout << "The ogre picks up a large boulder and smashes you with it" << endl;
cout << "GAME OVER, " << name << "!" << endl;

}
if (path2 == "b")
{
cout << "Your desicion was wise. The ogre is slow!" << endl;
cout << "You arrive at a strange swamp, a strange man is standing next to it" << endl;
cout << "Attack him (a)" << endl;
cout << "Attempt to speak to him (b)" << endl;
string path21;
cin >> path21;
}
}
if (path1 == "b")
{
cout << "You arrive at a dead end, in front of you is a chest" << endl; //choice if you chose b
cout << "Attempt to open it (a)" << endl;
cout << "Run (b)" << endl;
string path3;
cin >> path3;
}
return 0;
}
[/code]


Thank you.
[/quote]

In addition to what Khaiy said I would recommend separating your game logic from your game data. You can define all of the rooms, items, and monsters in separate text/xml files. If you do this correctly then you should be able to add more to your game without expanding the code at the same rate.

Share this post


Link to post
Share on other sites
[quote name='tasticmaster' timestamp='1306363253' post='4815804']
I am currently coding a very simple text RPG and so far everything is working well, but the code is starting to get confusing because of so many strings being used. How could I make the code work simpler and not require so many braces ("{" "}")

[code]
//Very simple text RPG
#include <iostream>

using namespace std;

int main()
{
cout << "Hello and welcome to my game!" << endl; //greet the user
cout << "What is your name?" << endl; //ask for name
string name; //define name
cin >> name; //read into name


//write the greeting
cout << "Hello , " << name << "!" << endl;
cout << "You are about to embark on an epic journey" << endl;
cout << "Good luck, " << name << endl;
cout << "You are faced with a path, it forks off in two ways" << endl;
cout << "Do you wish to go on the left path or right?" << endl; //first path choice
cout << "Left (a)" << endl;
cout << "Right (b)"<<endl;
string path1;
cin >> path1;
if (path1 == "a")
{
cout << "You are faced with a very large ogre" << endl; //choice if you choose a
cout << "Punch it (a)" << endl;
cout << "Flee (b)" << endl;
string path2;
cin >> path2;
if (path2 == "a")
{
cout << "Your strike deflects back off it's hard forehead" << endl;
cout << "The ogre picks up a large boulder and smashes you with it" << endl;
cout << "GAME OVER, " << name << "!" << endl;

}
if (path2 == "b")
{
cout << "Your desicion was wise. The ogre is slow!" << endl;
cout << "You arrive at a strange swamp, a strange man is standing next to it" << endl;
cout << "Attack him (a)" << endl;
cout << "Attempt to speak to him (b)" << endl;
string path21;
cin >> path21;
}
}
if (path1 == "b")
{
cout << "You arrive at a dead end, in front of you is a chest" << endl; //choice if you chose b
cout << "Attempt to open it (a)" << endl;
cout << "Run (b)" << endl;
string path3;
cin >> path3;
}
return 0;
}
[/code]


Thank you.
[/quote]




You have achieved level 2 in programming lol.

That's what i heard , first we start with lots of if blocks, then move onto object orientated, ie classes and that, then polymorphism,

It's really satisfying making your own classes and methods :)

Share this post


Link to post
Share on other sites
Wait, wait. Putting your rooms and data into external files will be wise in the long run, but I would focus on putting your code into objects 1st. Don't worry about the external files (especially trying to load with TinyXML, which may require above beginner experience)yet.

You do need to make your rooms into objects, and connect the room. I would suggest doing something like this (this is pseudo code. it won't build as is, it's just to give you na idea of what you should be looking for):

in file Room.h
[code]
class Room
{
public:
// sets up the room with a generic description
Room (string RoomDesc, int RoomId);
~Room();

void AddExit(string ExitCommand, int RoomToExit);
void AddItem(Item ItemInRoom);
void Display();
// Return the new room if we've moved
int Act(string RoomInput);
int GetId();

private:
int RoomId;
string RoomDesc;
int RoomToExit[4];
string RoomToExitCommand[4];
};
[/code]

in file Items.h
[code]

class Item
{
public:
Item (string ItemDescription, int ItemId);
~Item();

void Display();
int GetId();
}
[/code]

and in your main file (Game.cpp, let's say)
[code]
#include "Item.h"
#include "Room.h"

#define DEAD_ROOM 0

Room *Rooms[MAX_ROOMS];
int NumRooms = 0;

int main()
{
Room *pRoom;
bool bRunning = true;
int CurrentRoomId = 1;

// add rooms
AddRooms();

while(bRunning)
{
string Input;
pRoom = GetRoom(CurrentRoomId);

pRoom->Display();
cin >> Input;

CurrentRoomId = pRoom->Act(Input);

if (CurrentRoomId == DEAD_ROOM)
{
bRunning = false;
}
}

}

void AddRooms(void)
{
pRoom = new Room("You are faced with a path, it forks two ways\nDo you wish to go left or right\n", 1);
pRoom->AddExit("n", 2);
pRoom->AddExit("s", 3);


Rooms[NumRooms ++] = pRoom;


pRoom = new Room("You Arrive at a DeadEnd\nThe only exit is south\n", 2);
pRoom->AddExit("s", 1);

Item ChestItem("A Large Chest", 100);

pRoom->AddItem(ChestItem);

Rooms[NumRooms++] = pRoom;

}

Room *GetRoom(int RoomId)
{
for (int i = 0; i < NumRooms; i++)
{
if (Rooms[i]->GetId() == RoomId)
{
return Rooms[i];
}
}
}
[/code]

Share this post


Link to post
Share on other sites
[quote name='BeerNutts' timestamp='1306437621' post='4816140']
Wait, wait. Putting your rooms and data into external files will be wise in the long run, but I would focus on putting your code into objects 1st. Don't worry about the external files (especially trying to load with TinyXML, which may require above beginner experience)yet.

You do need to make your rooms into objects, and connect the room. I would suggest doing something like this (this is pseudo code. it won't build as is, it's just to give you na idea of what you should be looking for):

in file Room.h
[code]
class Room
{
public:
// sets up the room with a generic description
Room (string RoomDesc, int RoomId);
~Room();

void AddExit(string ExitCommand, int RoomToExit);
void AddItem(Item ItemInRoom);
void Display();
// Return the new room if we've moved
int Act(string RoomInput);
int GetId();

private:
int RoomId;
string RoomDesc;
int RoomToExit[4];
string RoomToExitCommand[4];
};
[/code]

in file Items.h
[code]

class Item
{
public:
Item (string ItemDescription, int ItemId);
~Item();

void Display();
int GetId();
}
[/code]

and in your main file (Game.cpp, let's say)
[code]
#include "Item.h"
#include "Room.h"

#define DEAD_ROOM 0

Room *Rooms[MAX_ROOMS];
int NumRooms = 0;

int main()
{
Room *pRoom;
bool bRunning = true;
int CurrentRoomId = 1;

// add rooms
AddRooms();

while(bRunning)
{
string Input;
pRoom = GetRoom(CurrentRoomId);

pRoom->Display();
cin >> Input;

CurrentRoomId = pRoom->Act(Input);

if (CurrentRoomId == DEAD_ROOM)
{
bRunning = false;
}
}

}

void AddRooms(void)
{
pRoom = new Room("You are faced with a path, it forks two ways\nDo you wish to go left or right\n", 1);
pRoom->AddExit("n", 2);
pRoom->AddExit("s", 3);


Rooms[NumRooms ++] = pRoom;


pRoom = new Room("You Arrive at a DeadEnd\nThe only exit is south\n", 2);
pRoom->AddExit("s", 1);

Item ChestItem("A Large Chest", 100);

pRoom->AddItem(ChestItem);

Rooms[NumRooms++] = pRoom;

}

Room *GetRoom(int RoomId)
{
for (int i = 0; i < NumRooms; i++)
{
if (Rooms[i]->GetId() == RoomId)
{
return Rooms[i];
}
}
}
[/code]
[/quote]

As this seems a bit directed at my advice I must respectfully disagree with your statement that he should not focus on xml/files at this point. I would counter with Object Oriented Programming is not a "beginner" technique and working with external files is valuable knowledge towards a wide range of programming applications. I could also point out that an object oriented design might be overkill at this point and using functions could be fine for a simple game at this stage. OOP implies re-usability, it also requires a knowledge of what to encapsulate and represent as an object... which requires experience...

Using files will also remove a lot of the text and lines of code, allowing you to refactor and ultimately arrive at an object oriented design anyway... Focusing on OOP first will force the data to be drilled into the classes and that will lead to terrible design. Focusing on loading files would also help direct an object oriented design and provide incite into WHAT to model. Blindly starting an object oriented design does not seem to make a large amount of sense.

So once again, I respectfully disagree.

Share this post


Link to post
Share on other sites
[quote name='shadowisadog' timestamp='1306468273' post='4816286']

As this seems a bit directed at my advice I must respectfully disagree with your statement that he should not focus on xml/files at this point. I would counter with Object Oriented Programming is not a "beginner" technique and working with external files is valuable knowledge towards a wide range of programming applications. I could also point out that an object oriented design might be overkill at this point and using functions could be fine for a simple game at this stage. OOP implies re-usability, it also requires a knowledge of what to encapsulate and represent as an object... which requires experience...

Using files will also remove a lot of the text and lines of code, allowing you to refactor and ultimately arrive at an object oriented design anyway... Focusing on OOP first will force the data to be drilled into the classes and that will lead to terrible design. Focusing on loading files would also help direct an object oriented design and provide incite into WHAT to model. Blindly starting an object oriented design does not seem to make a large amount of sense.

So once again, I respectfully disagree.
[/quote]


Well, you missed my point. His code doesn't have one function in it. He needs to work on some basic programming before getting into reading data from external files. He doesn't have to use objects, it can be all procedural, but considering he's writing in C++, it'd be one of the 1st things he learns. And, this type of text RPG fits well into objects.

Once he feels good about writing modular code, he SHOULD look into extracting his data from outside his program in external files.

So, I'll disagree with YOU...respectfully of course :)

Share this post


Link to post
Share on other sites

This topic is 2397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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