Call me obtuse or whatever other name you prefer. My university didn't ever properly teach me OOP, and because of that I much prefer to code in C. HOWEVER, in the interest of branching out and teaching myself some new skills, I've decided to try and teach myself some more object oriented skills as well as engage one of my other passions/hobbies, text adventures. I will be attempting this in C++, since that's the language I used for most of my undergrad so I know it the best. What I'm getting hung up on is I have written a generic .h file for the base attributes and methods a "location" should have. That's all well and good if all I want to do is create a bunch of rooms and have the ability to go N/S/E/W to another generic room and print a description of the room. What I don't know how to do is make a more specific implementation of this header file. I fear I haven't explained my problem very well, so let me illustrate with an example:
I have a room, lets call it "Living Room". To it's north is "Kitchen". To it's south is "Den". There is a door between "Living Room" and "Den". The user can say "N" and goNorth() will get called and all will be peachy. However, if the user says "S", I want it to give them some sort of a "you can't go that way" until they open the door. This would require me to either 1) completely abandon my OOP idea and just write each room as it's own set of functions and variables (how I would do it in the past) or 2) make a specific implementation of the header file I have containing all these functions and variables for each room (how I'd theoretically like to do it) however I just don't know if that's technically possible and if it is how to do that. If someone could point me in the right direction, keeping the name calling to a minimum I would ever so much appreciate it.
And just so you know, I'm well aware there are entire languages and engines dedicated to this process, I'm not trying to make a grand full scale game right now, all I want to do is sharpen my programming skills, that's why I'm taking this route.
This will likely be closed as too broad, as there are many ways to solve it. Here's approximately what I've done in a similar situation:
Room
has a method GetConnection(direction)
and returns a pointer to a Connection
object (or a null pointer if there's no way to go that way).
The Connection
object has a virtual Traverse()
method. The basic Connection
has a member that points to the destination, and the Traverse()
method just updates the player's location to the destination.
For a Connection
with additional restrictions, a derived class, say ConnectionThroughDoor
has an overridden Traverse()
method that checks that the door is open (or whatever the restrictions are) before calling the base class Traverse()
to actually move the player.
To generalize, think in terms of the programming interfaces and consider whether you need additional classes to represent other concepts (like a Connection
between Rooms
).