Text RPG code problems

Started by
7 comments, last by BeerNutts 12 years, 11 months ago
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 ("{" "}")


//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;
}



Thank you.
Advertisement
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:


// Function definition

public void Greeting(string name)
{

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

int main()
{

string name;
cin >> name;

// Display the greeting
Greeting(name);
}


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.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~


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 ("{" "}")


//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;
}



Thank you.


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.

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 ("{" "}")


//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;
}



Thank you.





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 :)
Thank you for the help everyone :) I will have a go at using some text/xml data, and use some functions.
TinyXML worked well for my project so maybe it'll work well for you too. (There are other small and/or fast XML parsers out there so you won't be starved for choices.)
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

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];
};


in file Items.h


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

void Display();
int GetId();
}


and in your main file (Game.cpp, let's say)

#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->GetId() == RoomId)
{
return Rooms;
}
}
}

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


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

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];
};


in file Items.h


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

void Display();
int GetId();
}


and in your main file (Game.cpp, let's say)

#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->GetId() == RoomId)
{
return Rooms;
}
}
}



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.


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.



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 :)

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement