LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2011, 11:19 PM   #1
Jinx-Wolf
LQ Newbie
 
Registered: Feb 2011
Distribution: Slackware
Posts: 26

Rep: Reputation: 1
Problem comparing variables


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):
Code:
...
char critterMap[7][6];
...
	//Vines
	for(int y=0;y<7;y++)
	{
		for(int x=0;x<13;x+=2)
		{
			move(y+1,x+3);
			init_pair(1, COLOR_GREEN, COLOR_BLACK);
			attron(COLOR_PAIR(1));
			addch(vinechar);
			attroff(COLOR_PAIR(1));
		}
	}
	//Level 1
	critterMap[0][0]=lCritter;
	critterMap[0][1]=mCritter;
	critterMap[0][2]=lCritter;
	critterMap[0][3]=mCritter;
	critterMap[0][4]=mCritter;
	critterMap[0][5]=sCritter;
	critterMap[1][0]=lCritter;
	critterMap[1][1]=sCritter;
	critterMap[1][2]=mCritter;
	critterMap[1][3]=mCritter;
	critterMap[1][4]=lCritter;
	critterMap[1][5]=sCritter;
	critterMap[2][0]=mCritter;
	critterMap[2][1]=lCritter;
	critterMap[2][2]=sCritter;
	critterMap[2][3]=mCritter;
	critterMap[2][4]=sCritter;
	critterMap[2][5]=lCritter;
	for(int y=3;y<7;y++)
	{
		for(int x=0;x<6;x++)
			critterMap[y][x]=(' ');
	}
	for(int y=0;y<7;y++)
	{
		for(int x=0,c=0;x<12,c<6;x+=2,c++)
		{
			move(y+1,x+4);
			addch(critterMap[y][c]);
		}
	}
...

			for(int i=6,r=7;i>0,r>0;i--,r--)
			{
				spitCritter=critterMap[i][arrayPosition];
					if(spitCritter!=' ')
					{
						if(spitCritter==mCritter && playerchar[1]=='*')
						{
							critterMap[i][arrayPosition]=mfCritter;
							move(r,position+1);
							attron(A_BOLD);
							addch(critterMap[i][arrayPosition]);
							attroff(A_BOLD);
							playerchar[1]=' ';
							refresh();
							break;
						}
						else if(spitCritter==mfCritter && playerchar[1]=='*')
						{	
							critterMap[i][arrayPosition]=' ';
							move(r,position+1);
							addch(critterMap[i][arrayPosition]);
							playerchar[1]=' ';
							score+=50;
							move(2,19);
							printw("%d",score);
							refresh();
							break;
						}
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
 
Old 07-26-2011, 12:23 PM   #2
tinyTux
Member
 
Registered: Mar 2011
Location: Extended Memory
Distribution: Gentoo
Posts: 64

Rep: Reputation: 9
Quote:
Originally Posted by Jinx-Wolf View Post
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):
Code:
...
char critterMap[7][6];
...
	//Vines
	for(int y=0;y<7;y++)
	{
		for(int x=0;x<13;x+=2)
		{
			move(y+1,x+3);
			init_pair(1, COLOR_GREEN, COLOR_BLACK);
			attron(COLOR_PAIR(1));
			addch(vinechar);
			attroff(COLOR_PAIR(1));
		}
	}
	//Level 1
	critterMap[0][0]=lCritter;
	critterMap[0][1]=mCritter;
	critterMap[0][2]=lCritter;
	critterMap[0][3]=mCritter;
	critterMap[0][4]=mCritter;
	critterMap[0][5]=sCritter;
	critterMap[1][0]=lCritter;
	critterMap[1][1]=sCritter;
	critterMap[1][2]=mCritter;
	critterMap[1][3]=mCritter;
	critterMap[1][4]=lCritter;
	critterMap[1][5]=sCritter;
	critterMap[2][0]=mCritter;
	critterMap[2][1]=lCritter;
	critterMap[2][2]=sCritter;
	critterMap[2][3]=mCritter;
	critterMap[2][4]=sCritter;
	critterMap[2][5]=lCritter;
	for(int y=3;y<7;y++)
	{
		for(int x=0;x<6;x++)
			critterMap[y][x]=(' ');
	}
	for(int y=0;y<7;y++)
	{
		for(int x=0,c=0;x<12,c<6;x+=2,c++)
		{
			move(y+1,x+4);
			addch(critterMap[y][c]);
		}
	}
...

			for(int i=6,r=7;i>0,r>0;i--,r--)
			{
				spitCritter=critterMap[i][arrayPosition];
					if(spitCritter!=' ')
					{
						if(spitCritter==mCritter && playerchar[1]=='*')
						{
							critterMap[i][arrayPosition]=mfCritter;
							move(r,position+1);
							attron(A_BOLD);
							addch(critterMap[i][arrayPosition]);
							attroff(A_BOLD);
							playerchar[1]=' ';
							refresh();
							break;
						}
						else if(spitCritter==mfCritter && playerchar[1]=='*')
						{	
							critterMap[i][arrayPosition]=' ';
							move(r,position+1);
							addch(critterMap[i][arrayPosition]);
							playerchar[1]=' ';
							score+=50;
							move(2,19);
							printw("%d",score);
							refresh();
							break;
						}
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...)
 
Old 07-26-2011, 09:15 PM   #3
Jinx-Wolf
LQ Newbie
 
Registered: Feb 2011
Distribution: Slackware
Posts: 26

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by tinyTux View Post
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...

Last edited by Jinx-Wolf; 07-26-2011 at 09:37 PM.
 
Old 07-27-2011, 12:05 PM   #4
tinyTux
Member
 
Registered: Mar 2011
Location: Extended Memory
Distribution: Gentoo
Posts: 64

Rep: Reputation: 9
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!
 
Old 07-27-2011, 12:28 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by Jinx-Wolf View Post
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

Hope this helps,

Last edited by Snark1994; 07-27-2011 at 12:32 PM.
 
Old 07-27-2011, 03:27 PM   #6
Jinx-Wolf
LQ Newbie
 
Registered: Feb 2011
Distribution: Slackware
Posts: 26

Original Poster
Rep: Reputation: 1
Thank you both so much!

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.

I appreciate the help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two files problem caponewgp Linux - Newbie 5 09-17-2009 02:20 PM
problem with comparing strings in php Yoyo302 Programming 5 06-30-2009 04:16 AM
awk comparing a column value with a stored variables value bugg_deccan Programming 4 12-05-2008 07:08 AM
c++ - if/else problem comparing strings babag Programming 14 05-19-2008 11:32 PM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration