ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm rather new to programming in C++ and programming in general, but I learn best by actually trying to write the code, and then ironing out bugs and bad practices later on.
For my first big C++ project I'm writing a little game based on Critter Crunch using ncurses.
I have basic functionality down, but I'm having a small problem:
When mCritter (%) eats sCritter (*), mCritter gets a full tummy and becomes mfCritter (%). Then if mfCritter eats another sCritter... POP! You get your points.
My issues lies with comparing to see if the critter eating sCritter is the m* or mf*(full) from the array. All C++ sees when comparing is the %, and not the variable it's self. I've read a lot about pointers, and figured that would solve my problems. I could point to the memory address rather than the variable, but I can't seem to figure out how to store pointers in arrays. I always get a bunch of char/char* conversion errors.
Here's a couple snippets of code as it is now (don't laugh too hard):
I took out the snippets of code that compliment my issues...
Again, try not to be harsh, I'm completely new, and this is my first real C++ project. I've watched a lot of tutorial videos, and I've read a couple C++ book (only one all the way through), and this is my first real project on my own.
Any help would be greatly appreciated, and perhaps some pointers on pointers? :P
I'm rather new to programming in C++ and programming in general, but I learn best by actually trying to write the code, and then ironing out bugs and bad practices later on.
For my first big C++ project I'm writing a little game based on Critter Crunch using ncurses.
I have basic functionality down, but I'm having a small problem:
When mCritter (%) eats sCritter (*), mCritter gets a full tummy and becomes mfCritter (%). Then if mfCritter eats another sCritter... POP! You get your points.
My issues lies with comparing to see if the critter eating sCritter is the m* or mf*(full) from the array. All C++ sees when comparing is the %, and not the variable it's self. I've read a lot about pointers, and figured that would solve my problems. I could point to the memory address rather than the variable, but I can't seem to figure out how to store pointers in arrays. I always get a bunch of char/char* conversion errors.
Here's a couple snippets of code as it is now (don't laugh too hard):
I took out the snippets of code that compliment my issues...
Again, try not to be harsh, I'm completely new, and this is my first real C++ project. I've watched a lot of tutorial videos, and I've read a couple C++ book (only one all the way through), and this is my first real project on my own.
Any help would be greatly appreciated, and perhaps some pointers on pointers? :P
I'm going to try not to be harsh... but you really should be shot for writing code like this. Code this incomprehensible in an abomination.
My 2 cents:
1. Don't use pointers... any of them. You have enough complexity issues here as it -- pointers rarely make life any simpler, especially if you don't know what you are doing.
2. Modularize, for heaven's sake! I think you need to really separate out your code into at least three distinct parts: one part which keeps track of the critters (where they are, what they are, et cetera), another part that does stuff to them (moves them, changes their world, etc.), and another part that interprets the critter data and prints it out onto the screen. I'll admit you're code is very hard to read, but it looks as though you have meshed all that functionality together in a very ungodly manner.
3. To do some Web research and learn what the K.I.S.S. principle is and how you can apply it to your programming. (Also functional programming principles are great too, but that would probably be dropping too much on you at once.)
There's more that could be said, I'm sure, but I would want to see all the code first. (Or maybe I don't...)
I'm going to try not to be harsh... but you really should be shot for writing code like this. Code this incomprehensible in an abomination.
My 2 cents:
1. Don't use pointers... any of them. You have enough complexity issues here as it -- pointers rarely make life any simpler, especially if you don't know what you are doing.
2. Modularize, for heaven's sake! I think you need to really separate out your code into at least three distinct parts: one part which keeps track of the critters (where they are, what they are, et cetera), another part that does stuff to them (moves them, changes their world, etc.), and another part that interprets the critter data and prints it out onto the screen. I'll admit you're code is very hard to read, but it looks as though you have meshed all that functionality together in a very ungodly manner.
3. To do some Web research and learn what the K.I.S.S. principle is and how you can apply it to your programming. (Also functional programming principles are great too, but that would probably be dropping too much on you at once.)
There's more that could be said, I'm sure, but I would want to see all the code first. (Or maybe I don't...)
Thanks.
A side-note: I have poor self-esteem, and usually take things the wrong way. I usually don't post on the boards because of that. I'll take your criticism constructively, because I KNOW I'm not a programmer, I'm completely new to this kind of stuff.
1.The only reason I asked about pointers and such, is because a lot of tutorials on writing games said that you needed to be good at it.
2.I'm working on trying to get down the object oriented style of programming. I start out doing okay, and then it eventually turns into one mass heap of code that only 'I' can read. It comes from poor programming practices taught to me using old gaming languages as a kid, and writing bash scripts.
3.I know K.I.S.S. from my Slackware buddies, but just like any other person that's new at something, I over complicate things. I'll probably get better the more I learn... Unfortunately a lot of these books teach you in small bits, and rarely show you any practical use of what they're teaching you. So I only get snippets of information at a time, and have no idea how a well written C++ program looks like from start-to-finish.
I'm pretty sure you don't want to see the rest of the code. In-fact, even before I read your comment, I started to move everything into separate files' While before this I only had two.
EDIT:
I'm not sure if you realized, but the ...'s are HUGE gaps of code. Although it's still a structured program, there's are three separate sections of my code that are properly labeled and commented on. I know it doesn't make it any better, but I just wanted to be clear that it isn't ALL crammed together like that. That top part is part of my initialization section which sets variables, arrays, etc. The second part is just preparing Level 1. I'm not very far into the coding and I didn't want to keep working on broken code if I couldn't get it to detect the differences between mCritter and mfCritter, so I went ahead and just placed a bunch of critters in the array to work with. The last part is a huge mess, but all of it's apart of my //Controls. It's a series of if/for statements that detect where/if a critter should be places, and how the critters should react to one another. That reactions part is what I'm having issues with...
That was meant to be more of a humorous response -- using stinging sarcasm and good-natured insult to make a point more forcefully. But sometimes I forget that forum posters can't see my expressions or hear the tone of my voice. So I hope you aren't discouraged from your work in programming... personally, I find that the learning experience itself can provide immense satisfaction.
Having said that, I'll stress again: it seems to me, from your original post, that you were trying to solve some of the complication issues in your code by making your code more complicated. If you are having difficulty analyzing and manipulating array data, then throwing low-level references into the mix is just going to make things worse. The ideal is to make the data in your program more meaningful, which is best done by abstracting out data into types (for example, structs and classes) that have meaningful structure. And also by separating out code components that are conceptually distinct. For example, there shouldn't be a single array that both represents character locations/descriptions and provides an exact representation of the screen -- because it is easier to code when you can adjust one without affecting the other.
The fact that there was a ton of really complicated iteration going on also turns me off -- that usually means you haven't separated out your code into simple and manageable functions.
Anyway... when you are done, be sure to post a link to the source code!
Thanks.
A side-note: I have poor self-esteem, and usually take things the wrong way. I usually don't post on the boards because of that. I'll take your criticism constructively, because I KNOW I'm not a programmer, I'm completely new to this kind of stuff.
Well done for taking it that way I would encourage you to persevere - I would be scared to look at the first programmes I wrote in C/C++ (only last October) and I am still learning...
On your actual code, I think you need to modularise it, as tinyTux suggested. You talked about using OOP, and I think this is a good idea; you also had lots of lower-level (eg. drawing) code mixed in with higher-level (eg. "is a critter eating another critter?") code. I would also get rid of the mCritter and mfCritter, replacing them with one "mCritter" class which has a variable that tells you if it's full or not
Code:
class mCritter {
private:
int x,y;
bool full;
public:
mCritter();
drawMe();
}
This is obviously only an example (and I've no idea if it's practical based on the rest of what you want to do), but just simple things like having the drawMe() function will help you loads - "critterList[i].drawMe()" is far, far, far more readable, both to you and anyone else, than a whole load of "addch()" functions mixed in with "if" statements
And if you would like me to have a look at the rest of your code / help you in any other way, I would be more than happy to do so.
EDIT: *cough* I, er, kinda forgot to answer your question. Essentially, you need to store more information than just the '%'. You could do this any number of ways - you could have another array of chars in parallel to your array of where the critters are, which stores whether or not the critters are full. However, a 'better' (read: easier to understand, maintain and develop, in my opinion) would be to use lists / arrays of classes to store information about all the critters, like I suggested above
tinyTux:
The thought crossed that you were poking a bit of fun, but with good intentions. As yous said though, sometimes it's hard to interpret text :P
I do get what you're saying though, and I'm going to take a couple steps back, study a bit more, then continue working on the code.
Snark1994:
That simple example just made my brain explode with ideas. This would definitely fit well in my code. I actually wrote a Snake game following a tutorial, and just started to work off that to create my next game. I took the things I didn't completely understand and removed them temporary, but forgot about them in the end.
As I thought, I was going about it the wrong way... Mostly because I don't have enough experience with things like objects, classes, or even functions. I was getting to the same destination, but take a much, MUCH, longer route to get there. I have to go to work now, and I work throughout the morning, so I hope I have some time this weekend to clean it all up, and start structuring it so everything is a bit more manageable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.