I've been reading a book on C++ and I decided it'd be good practice to code something from scratch instead of outside of the book. I decided it wouldn't be too hard to code a knock off of ZORK...I was wrong

It's not that I find it HARD to code a knock off of ZORK but one thing after another keeps adding up and my main.cpp is getting so huge im started to get lost! ...I know, i know, its not THAT big, but for someone like me who is used to coding little things like calculators and other basic things, its pretty long ;p

I know I've probably coded this in a stupid way so If anyone has some suggestions as to how I can split it into other files or reuse parts of code so I don't have to manually re-write things, etc. Maybe some new methods I could check into because idk if this is normal but I feel like I used if, if else, and else statements for practically everything! xD

I've been there before! I think I might even still have my first text adventure game somewhere. Mine was worse.

Two things that are bloating your project:1) Use a std::vector of separate commands, not a variable for each command. I posted a few other ideas in this thread not that long ago.As much as possible, make your code do the work of connecting things (commands, rooms, items, etc...) together for you.

2) Your rooms need to be loaded from text files, not hard-coded into the game, otherwise you're in for a world of pain. If this is your first attempt, that's fine that you're hard coding it, but for the sake of your own sanity, give yourself a hard-limit (say: 10 rooms), then scrap the project and learn how to load the rooms and connect the rooms together using external files.

I'm currently working on my first game - a clone of pong written in Java. After coding for several hours I had "hit the wall" so to speak, so I went and found a "Write pong in Java" tutorial online. I looked through it, to see how they had tackled the problems I was having trouble with. After understanding their code I went back and continued working on my code with the knowledge I had gained. This way I'm not just doing a copy / paste. I can call the code my own because I have written every line of it myself. All I "took" from the tutorial was the basic concept of how to attack the problem. You might try finding a similar Zork tutorial and scan through it for optimizations that will make your code easier to handel. Happy coding!

Early on in game development I got heavily into tool development, and believe me it saves so much time and hassle from hard coding. I still remember my first text game in C++, I had a ton of classes: Weapon, Enemy, Player, Tool, Battle System, ect...

One suggestion you should look into is creating a class to handle each "area" (typically called rooms in Text Adventure games). Something like this:

// In TRoom.h, you could declare the class like this
class TRoom
{
public:
TRoom(std::string description);
~TRoom();
// AddItem puts an item in the room to be displayed when looking, and get be picked up
// an "Item" (which can be carried in your inventory, used, and picked up) should
// probably be it's own class, and you would pass it to Add Item. it would have a name, and a description at least.
void AddItem(std::string itemName, std::string itemDescription);
// SetExit set a room to move to when going that direction
void SetExit(std::string exitDirection, TRoom& roomConnection);
// Update handles the command given to it, like "look" will print the Description
// it returns the new room to move to
TRoom& Update(std::string command);
private:
std::string Description;
// You may want to use a std::map here, but I'll use two vectors to store the directions
// and the rooms to move
std::vector<std::string> Directions;
std::vector<TRoom&> RoomsToMove;
};

That's only the header file. You should probably look at some tutorials about classes if you don't understand this, but moving to that kind of coding will help you a lot. Then, when writing the actual code, your main loop would be this:

Wow thanks guys. Some really great responses. Looks like I've got lots of new things to look into I don't know anything about classes atm or loading rooms via text files, etc.

Thanks for all the great responses!

EDIT: Today i scraped the old one and did it again from scratch (changed the dialogue alittle ;p) but it still seems as I am hard-coding it, which is what i was trying to avoid doing again xD I haven't yet looked into classes/vecters/arrays/etc, so I will take a look at those tonight and hopefully that will make my games less "hard-coded" ;p

At least, you are doing something.
Making a data driver text-adventure game engine at this point of skill would be really amazing. Just keep it up, you'll figure it out someday. no rush.

PS: If ever you want to try linux some day, first thing to install : valgrind. This tool is the savior for every C and C++ developer, particularly beginners. Because it tells where are the runtime mistakes, when they happen, and not 3 days later.
PS2: also you could have a lot of fun with ncurses to extend your game.

yes yes i'm quite familiar with Linux actually ;) was even reading a book on linux shell scripting and crap so i got pretty familiar with the basics of how things work in linux and knowing my way around the terminal ;)

I've been reading a book on C++ and I decided it'd be good practice to code something from scratch instead of outside of the book. I decided it wouldn't be too hard to code a knock off of ZORK...I was wrong

It's not that I find it HARD to code a knock off of ZORK but one thing after another keeps adding up and my main.cpp is getting so huge im started to get lost! ...I know, i know, its not THAT big, but for someone like me who is used to coding little things like calculators and other basic things, its pretty long ;p

I know I've probably coded this in a stupid way so If anyone has some suggestions as to how I can split it into other files or reuse parts of code so I don't have to manually re-write things, etc. Maybe some new methods I could check into because idk if this is normal but I feel like I used if, if else, and else statements for practically everything! xD

any advice / critic is appreciated. Thanks!

(lots of code on single line)... if (confused = true) ...(lots of code on single line)

I posted this in another thread, but I thought it'd be helpful here too.

This might be a bit more complex than what you are looking for, but here's a simple example I put together for the heck of it. There are 5 source files for this, and I'll list them here, but it's generic enough, it should handle a good, fun game. The only thing I would modify is better handling of SpecializedCheck() function. I think this should be integrated into the rooms as well.

Oops, there's a little bug in my code. I was assigning a separate TRoom CurrentRoom from the Rooms vector, but that wasn't holding the changes to the actual rooms themselves. Here's the updated main() function:

wow thanks for the release in the source, once I find the time I will definitely setup a project and paste all your code it, study it, modify it to learn new things, etc!
Love looking at other people's source code, I find it a good way to study it, unless you have no idea what any of it is then its just like trying to read Chinese ;p