Archived

This topic is now archived and is closed to further replies.

TearsKnight

Efficient?

Recommended Posts

In a few previous posts, I mentioned I was making a text-based adventure game. Well, the thing is, I want to make it somewhat large. So in order to do this, it seems like I have to make tons and tons of statements; You know, four for each area (North, South, East, West). Is it possible to make this more efficient? Oh, and I''m not that advanced at programming (meaning I don''t know that much about objects, or classes, etc.)... So a simpler explanation would be appreciated. Or just tell me that I need to study more if that is the case....

Share this post


Link to post
Share on other sites
There is a difference between optimizing and good code design. You definitely don''t want to use tons of if() statements to do what you''re doing. I would advise creating a few structs or classes to represent rooms, characters, items, etc. Do something like this:

/////////
enum Directions { NORTH, SOUTH, EAST, WEST };

struct Room
{
int exit[4];
char name[64];
char description[512];
};
/////////

You would then create an array of all of the rooms in your game:

/////////
Room rooms[64]; //create an array of 64 rooms
int iCurrentRoom = 0; //set the current room that the player is located in
/////////

When you actually create the rooms, you would set the name, description, and exit[] fields for each individual room. The exit[] array would contain the ID #''s of the room''s adjacent to the specified room.

/////////
rooms[0].exit[NORTH] = 1; //exiting room 0 to the north will bring you to room 1
rooms[0].exit[EAST] = 2; //exiting room 1 to the east will bring you to room 2

rooms[1].exit[SOUTH] = 0; //exiting room 1 to the north will bring you to room 0
/////////

By using a system like this, you can basically have a single game loop that processes the data of the current room that the player is in. Whenever the player types "north", for instance, the game would look at the entry for rooms[iCurrentRoom].exit[NORTH], and would find the ID # of the room to which the player will now move.

This is just one way to do it, and their may be better ways out there. I hope that helped a little (and I hope it wasn''t too confusing). Good luck!

-Mike

Share this post


Link to post
Share on other sites
you could make it data driven. Store all the statements in a text file and load it up during the game when you need it. I dont know if that solves your problem though.
i really need more information to help you much. If you are doing something like
if(xloc==1&&yloc==1)
{}
elseif(xloc==1;yloc==2)
etc.
then yes, there is an easier way. Use functions. For example.

load each areas info from the textfile into an array and info about which directions you can move into an array. then a function like this will display the info. you can go from here this is just a simple example.

char* info[xsize][ysize];
int directions[xsize][ysize];
Update(xloc,yloc)
{
cout< if(directions==0)
cout<<"you can only move north";
if(directions==1)
cout<<"you can only move west";
etc.
}

Share this post


Link to post
Share on other sites