Any C++ Programmers out there?

warning: Creating default object from empty value in /home/buckman/public_html/neo/modules/advanced_forum/advanced_forum.module on line 492.
23 replies [Last post]
Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006

Greetings! I was wondering if any of you are proficient in C++ programming. I'm trying to learn the language and am encountering lots of curious spots (and have some general questions) that you could probably answer if you know the language. I'm just starting out, but maybe others could also benefit from the discussion.

Right now, I'm trying to get a better handle on input for an adventure game. I have a shell setup now that will let players explore a series of connected rooms by typing n for north, e for east, and so on. However, this rudimentary approach has plenty of problems. Typing "WEST," for example, causes the player to move west, then east, then get two errors about "You can't go in that direction." What's happening is that the whole stream is W-E-S-T is entered as separate commands...Not ideal behavior by any means.

Ultimately, I need to be able to parse an entire string like "GO NORTH" or "UNLOCK DOOR", but I'm not sure how to go about it without a really massive set of IF statements.

Anyway, I'll go into more detail if anyone will rise to the occasion here and shed some insight on these issues. If you know anything about parsing strings of input, please share your knowledge!

n/a
Maarten
Offline
Joined: 08/31/2006
thanks, but..

I didn't know Microsoft had an Express edition which was free! thanks for the suggestion. I knew of Visual Studios and Borland, which both cost some amount of money. (I'm not aware of exact prices but I'm presuming it's too expensive for someone who's still living off of his parents financially.) I'll be sure to give that Express edition a whirl once I get to it. (I'm not in a hurry, since DevC++ fulfills my immediate needs)

The thing I hate about libraries, is that it's so hard to figure out what functions do. The standard library encoded into my compiler has only the headers accessible, or in the least I haven't been able to find the source code of the prototype functions defined in headers. Initially, after checking iostream.h, I figured the read() function of an istream object would fulfill my needs as it(, I figured on a basis of the data provided by function parameters,) appeared to allow me to read the last n items of the streambuffer, so by putting n to 1 I hoped I could get the last character inputted. This didn't work though, as streambuffer only became accessible after an enter was pressed, and then threw me the contents of the streambuffer in chuncks of n characters, and at the next function call it would give me the next n characters upto the point that streambuffer was empty. this, and the fact that streambuffer also had the final '\n' in it really messed up my program.

so if I want to use libraries, I would want to use a couple for which I can easily access the exact content of whatever it is the library does. this sadly appears to just be very hard to do with the standard library. I've never tried 3rd party libraries yet, so it may indeed yield results, although preliminary searches for DirectX and OpenGL seem to imply these libraries are geared more towards output than input. (but I did find the contents of a DirectX library pretty quick, and it did seem complete.) Output's also interesting, but something I want to save for messing around with at a later time when I start attempting genuinely complex stuff. I'll try looking into other lib's more detailedly later. I must say that despite it probably being hard, I think I'm still more interested in learning and understanding the low level computations that accompany input, if only because it's fun to play around with.

Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
modules

From what it sounds like, you might need a third-party library to handle some of this stuff. I'm not sure what you mean about using blood-dev for "economic reasons" (Microsoft Visual C++ Express is free as in zero-cost). Honestly, I find the Microsoft IDE and compiler much more user-friendly.

For input and the like, the advice I've heard is that you will probably want to check out either SDL or managed Direct X. Both of these are supposedly simple to use. There are countless other libraries that you could look into as well. OpenGL and DirectX seem to be the most professional but also the most difficult for hobbyists.

At any rate, trying to "hard code" something as "close to the machine" as input can be a real bear.

n/a
Maarten
Offline
Joined: 08/31/2006
I also have I/O trouble with my program...

I've recently restarted attempting to also learn C++. My first pretty tame project, beside some minor playing around with general sytax is tetris, which more or less took me two days to program. It runs pretty much perfectly, as far as I can tell anyway, but there are two major problems bugging me right now which prevent it from genuinely running as tetris:

1) for some reason, I can't get the clrscr() function from the conio header to function properly. I'm using bloodshed dev C++ for mostly economic reasons, and there's something in the helpfile about it not working because it's a borland specific library file or something, but I need something to clean the screen after a change has taken place and before the the current status is printed to the screen again. some searching has already shown that this can most likely be solved by using system("CLS") from stdlib.h, or using fflush(stdout) from someplace else. I haven't tried these solutions yet (currently not at my home PC) but in case these don't work I wanted to pose this question here just in case anyone is somewhat aware of how else possibly to do this.

2) now for the real problem that's bugging me: how do I implement real-time input? I basicly want my input function to just check the status of any input periphal and return some value which can be used to decipher which buttons and stuff are currently pressed to which the program can respond. The iostream header appears ill-suited for this cause. I presume there might be some adress to which input devices are linked from which I can retrieve this data or something, but this is just a guess on my part, I don't know much on the internals of PC's and the like. (don't let that stop you from going in depth on how the information transfer between input devices and the system take place if you know that though.)

If anyone could help me with this problem I'd be much obliged.

Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
Just an update--I've finally

Just an update--I've finally got most of the kinks worked out of my engine and have been working up the content for the game. Hopefully, I'll have something to show for all this effort soon. I'll probably release the game first and then try to massage the code into something readable afterwards.

I'll say one thing--I have nothing but respect for Infocom. I never really thought about how complicated the code for IF could be until I actually started designing my own engine. And my game will not even approach the complexity of the typical infocom title.

Still, hopefully, you guys will actually enjoy the game! It's going to be a science fiction tale called "Initial State." I'm counting on my writing skills to make up for my lack of coding wizardry. :-)

Matt

n/a
Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
What does "tokenizing" mean?

What does "tokenizing" mean? I'm afraid I'm not that far into my programming. I agree that the console window is a pain, but I don't know anything about GUI interfaces at the moment and don't want to get sidetracked again if I can help it. I was hoping to finish this project before moving onto learning more about GUIs (either something like managed directx or perhaps SDL again).

I've found vectors to be useful in setting up the rooms and objects for the adventure game. The problem I was having was that I didn't know how many array members I would need until after I knew the total number of rooms and objects, but to get those, I had to scan the textfile and count the number of items. I ended up having to do multiple scans of the files--once to get the #, then again to populate the arrays, and always having to allocate them with new. It was a pain, and if anything was off, the program would crash and it would take me forever to find the glitch.

With the vector setup, though, all I have to do is use push_back to keep expanding the array. Then I can use the vector.size() to figure out the total objects I have. Plus, I've heard that using vector's at() is safer than the array [] if you overshoot them (though neither compiler error is particularly informative). What I've found is that the string and vector stuff gives me lots of extremely useful tools. String's find, compare, and substr have proven useful as well.

I'm sure that any professional who dared to look at my code would probably either have a conniption fit or a heart attack. I'm definitely just hacking things together at the moment and trying to get things to work. It's amazing how complicated things can get with this. I almost drove myself batty trying to resolve the problem of objects with similar names, like "bronze key" and "brass key." I had to implement a routine that would scan through the object names and flag ambiguities (like "get key"), and force the user to type the full name. I also hate to force the player to do so when the context is clear (i.e., there's only one key there). Stuff like this would be easy if I wanted to hard-code them in, but I'm still hoping that all of this can be programmed in just by entering simple codes (hopefully readable ones) into the text files.

I'm really struggling with objects with multiple states (open, closed, locked, unlocked) and how to relate all this to other objects. But I won't give up until I'm really stumped.

n/a
number6
number6's picture
Offline
Joined: 05/20/2006
Why learn arrays when there are vectors?

I guess the point is you need to understand how arrays work so you can work with Vectors in the first place. Also, there are times when a Vector is possibly overkill when you know you will never have more than N items to work with. Also, Vectors are less efficient than static length arrays. A vector has to keep allocating space on the heap as it grows and this can be inefficient. Of course from your perspective this is probably not all that important. When you have to deal with huge volumes of data or if timing is critical it might concern you. As to your original request about parsing strings there is a function called strtok that tokenizes strings which you may find useful. If you have a code snippet that you think needs refinement you could always post it for review. It's kind of hard to give advice not knowing exactly what you are doing. I personally don't likw writing code that runs on the command line. I prefer GUI interfaces that can restrict what the user can/cannot do thus simplifying error handling which is what you are talking about.

Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
Geez...

Wow. I just learned today about vectors and how to use them. D'oh!! If only I'd known about them a week or so ago, I could've saved myself an immense headache with arrays. I've been having to warp my head around some pretty convoluted code to get those dynamic arrays setup right! :-)

It seems like books have a habit of saving the best for last. I mean, why sweat through all that business with character arrays when you have strings? Why sweat through arrays when you have vectors? I bet there are probably plenty of other things I'm coding by hand that I'd be much better off taking from the STL!

At any rate, I have been making steady progress on the engine. I've got doors working now. :-)

n/a
Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
You know, sometimes I think

You know, sometimes I think that programming is almost as much fun and addicting as videogames! I've been continuing to develop my adventure game engine, and even small victories are enough to get the endorphins kicking--though I've been flummoxed by so many small bugs it's insane.

One of the biggest things that keeps tripping me up is array values. C++ gets very upset if you try to stick something in or access a non-existent array value. Bugs like this can be very hard to see, particularly if you're using lots of references and pointers. I think I finally got the last bug found and eliminated.

Right now I'm in the process of setting up the objects the player can interact with in the game. I already have the rooms set up, and the player can move about them by typing cardinal directions. For now, I'll be happy if I can fixed objects showing up in the right locations, and perhaps some simple actions like "examine" working.

I've been thinking that about how to set up the verbs. Perhaps I might just define a few dozen verbs and then include a text file that can map them on to lists of synonyms or close approximations. So, verbs like "get," "pick up," "acquire," "grab," and so on might all resolve to "take" internally, thus simplifying the code. The *last* thing I want to create is a stupid guess-the-right-phrase interface "puzzle." On the other hand, I'm not trying to recreate natural speech! I figure most adventure gamers would know about the common verbs and not try anything too bizarre.

I was toying with the idea of being really simplistic and just using a set of standard verbs like most GAGs, such as look at, take, open, close, move (push/pull), talk to, and use (and use on). Really, you can do a lot with just these few commands. I really never saw the point of trying to get too sophisticated with the inputs anyway. What do you think? Does it really matter if you have to type "USE KEY ON LOCK" rather than "UNLOCK WITH KEY?"

At this stage of the game, I'm not even going to attempt to offer realistic bots to chat with or the like!

n/a
Matt Barton
Matt Barton's picture
Offline
Joined: 01/16/2006
Very true, Mr. Custard.

Very true, Mr. Custard. Unfortunately, I don't know hardly anything about reusing other code. The stuff I've been reading is good about telling you how to do things yourself, but since they want to keep things generic and non-commercial, I'm left kinda clueless when it comes to using stuff from other folks (besides the standard libraries, of course).

I figure I'll start to take the next big step after this, when I start working more with graphics. Obviously, I'll have to use either OpenGL or DirectX, so I'll definitely enter the world of reuse then!

n/a
mrCustard (not verified)
Woops.

Woops.

Thanks for fixing that.

Gamertag: Custardo

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.