
In my spare time for the past few days, I've been writing a text adventure game program. Actually, I started it last year sometime, put it away for a long time, and picked it up again this weekend. I've run into a particularly odd spot of trouble, which I am now going to talk about at length in hopes of realizing my mistake.
UPDATE: Um, never mind. I figured it out. Details inside if you are interested.
The program parses typed commands and searches for verbs from a list of things it knows how to do. If it finds one, it looks around the current location for objects that might match the ones mentioned in the command. The command structure is along these lines:
Verb article direct object preposition article indirect object.
Put the cat in the box. Take the golden statue off of the stone pedestal. That sort of thing. Depending on what the verb is, any or all of the other words may be optional. And it works. It works great. It's case-sensitive, and checks various flags on the objects in question to determine if the action succeeds.
But I couldn't move. I hadn't implemented doors yet. This is what I started working on yesterday. It should be a relatively simple process. Each door object has a listed destination and a list of words which will cause movement through it. For instance, a door along the north wall of room 5 might have as its destination Room 15, and the player could go through it by typing any of the words "north, n, out, o." Check the typed input against the list and move the player if there is a match.
The reason I do it this way is so that I'm not restrained to a maximum of ten doors per room: north, south, east, west, northeast, northwest, southeast, southwest, up down. If I want a hundred doors leading out of one room, and can come up with unique names for all of them, I can do it.
And that works too. I check for doors before dividing up the input into individual words, then proceed into the verb-hunting section of the code if it doesn't find one.
Here's the problem. As long as all I am doing is entering door commands, it works fine. But if I ever input a verb command, then follow it with a door command, the program crashes. I have a function that performs the door-identifying and returns a pointer to the proper door object. That object can then have tests performed on it to see if the action will succeed. For that to work the program needs to remember which door it's supposed to be looking at. I have pinpointed the moment of failure to be when the value returned by the function gets assigned to a variable.
Obviously, something is happening during the verb command processing that fouls up what I'm trying to do. But I can't conceive of what that might be, or how it would affect a variable that gets created just before it is used and ceases to exist at the end of the function in which it resides.
All I can think of to do is start disabling parts of the verb command processor code until I find the bit that makes it blow up when it works but not when it doesn't. Hopefully, there is such a thing.
EXTENDED UPDATE: It's never what you think it is. I went home during lunch to bash my head against it a few times. Turns out the problem had to do with the way I allocate and free memory when I slice a command into its component words. It was trying to delete dynamic memory when there wasn't anything to delete.
In my defense, I wrote that part of the program much lower on the learning curve. There's bound to be some blunders in there.
Now I just need to make sure doors open and close from both directions at the same time.