I've wanted to make a text based C++ game for a while now, and i've slowly started planning the basics for it now.
One of the things i'd like is some kind of save/load function. So every once in a while, the program will ask the user if he/she wants to save the game, if yes, the program will output a code to a text file.
At the start of the game, the user will then have a choice of loading the last game, and the program will fetch the code and compare it to some hard coded codes that represent the players progress in the game, this is where i got stuck.
My plan was to make my own branch table and then use GoTo statements to jump to the saved location in the game. Something along the lines of this:
And so on...
My question is then: Would this be considered poor programming? I've heard alot about GOTO, and how bad it is. But in this case, it doesn't really produce spaghetti code, actually, i kind of like my save/load function implementation, and i have a hard time figuring out another way of doing it.
One way could be to base the entire game on a linked list, so the first lvl would be in the first node of the list, and so on. And then just traversing through the list until the codes in the save game files matched a certain variable in my list, but i don't like this solution as much as the other one.
What else could i do? And do you think this qualifies as a good place to use GOTO?
I'd use external files as well as a naming convention like level1.lvl, level2.lvl ... And then simply read that file for loading the level...
I'm not sure how you think goto will help you. i certainly can't see any reason to use it based on your description of the problem.
How about wrapping the code for each level inside its own function, then you could have a switch statement which looks like this
Personally I would make a generic load function, that accepts a level name or number as a parameter. Doing that you could avoid using goto's and probably cut out some repeating code.
Use function calls instead.
Originally Posted by Neo1
The best place to use a 'goto' is to the left of the cursor, when the backspace key is being held down.