In my class we have started programming a Text Based Adventure and the instructor has not yet taught functions and i am impatient, I need to know how to jump around in my code so that i would be able to go from room A to room B then have the choice of going to room C, D, or back to A without have a ridiculous amount of code. From what i have been told in order to do this i would have to use functions, what would be the easiest way of going about doing this?
Without knowing what you have been taught and the parameters for the adventure game, it is hard to recommend a strategy to you. But you are much safer asking these sorts of questions in the beginner forum, where the members are more sensitive to these sorts of things. The answers in the general forum tend to assume a more broad-based knowledge of the language.
Well, you need to sit down and design how you want things to be in your text based adventure.
The simplest implementation I would think would have to use classes to describe the room, with each room having up to four exits (north, south, east, west) which are pointers to other rooms. The current room is held as a reference by the main loop, which will get the list of possible exits (possibly stored as a vector), display the options to the user and then take appropriate action (change the "current" object).
Text based adventure games are harder to design well than you may think. Start with an easier game like a simple vertical shooter or something. And don't use the console.
Disch is correct of course. Text based adventures aren't really as simple as some would expect. I'm assuming you're meant to make something more MUD (sans mobs) than nethack?
Was it good though? Creating something to allow you to move through a series of "rooms" with simple commands is indeed fairly simple, creating an "adventure" is another matter entirely.
More on point: yes, functions are pretty much the basic ingredient of anything C++. The rand, srand, while, if you mention are all functions built by other people.
http://www.cplusplus.com/doc/tutorial/functions/ will give you the basics of the syntax, it isn't all that different to the function you probably used in Python, apart from having the C++ type identifiers for the input/output.
def rooma();
}
if (choice == left)
then roomb();
}
def roomb();
}
if (choice == right)
then rooma();
if (choice == left)
then roomc();
}
and so on, and that allowed us to jump around the code quite a bit, im not sure if i was told the wrong thing to use to allow me to do this in C++ but from what ive been told you would use functions to hop around your code like that.
Hmm, yes. You've confused your syntax quite a bit there. Python tends to avoid semicolons with something approaching pathological fear.
That's a very inefficient way to do what you're wanting, as I'm assuming you're taking user input in each room? It's almost a recursive approach to text-based game navigation. :) Your call stack just keeps on growing and growing until the program ends, which is generally considered a bad thing if there's not some definite end in sight.
As I said before, seperating the rooms and the user navigation function would make for a much cleaner design.
I apologize if this is a stupid question but i havent done much coding in 2 years but would i use those in int main(int argc, char *argv[]) or would those be completely off on their own?