what's wrong with this?

Started by
7 comments, last by password 17 years, 5 months ago
I'm trying to check if a line is filled with a value greater than 0. If some of the lines in the array is filled with those values, it's going to reset them. Check the following code:

        bool foundline=true;
        
        for (int h=0;h<BOARDHEIGHT;h++) {        
            for (int w=0;w<BOARDWIDTH;w++) {        
                if (board[w][h]>0)
                    foundline=false;
            }
                 
            if (foundline==true) {                           
                // delete lines
                for (int i=0;i<BOARDWIDTH;i++) {        
                    board[h]=0;
                }
            } else
                foundline=true;
        }


I can't find anything wrong in this code, if I remove the if statement it works, but deletes everything. See anything wrong with it?
Advertisement
You're setting foundline to false whenever you detect a value that is greater than zero in a given line, thereby preventing the next loop from clearing the line.
Quote:Original post by password
I'm trying to check if a line is filled with a value greater than 0. If some of the lines in the array is filled with those values, it's going to reset them. Check the following code:
When you say "it's going to reset them", do you mean just the values greater than 0 or all of them?

Quote:
*** Source Snippet Removed ***

I can't find anything wrong in this code, if I remove the if statement it works, but deletes everything. See anything wrong with it?
My best guess would be that you meant the first foundline=false; line to read foundline=true.

Also, if you want to reset values greater than 0, couldn't you do that in the first loop? As in:

for(int h=0; h < BOARDHEIGHT; ++h){	for(int w=0; w < BOARDWIDTH; ++w)	{		if(board[w][h] > 0)		{			board[w][h] = 0;		}	}}


XBox 360 gamertag: templewulf feel free to add me!
That's the meaning of it, because I want it to be set to false if it finds even one value which is not 0 thereby preventing it to clear a line that isn't full.
Quote:Original post by templewulf
Quote:Original post by password
I'm trying to check if a line is filled with a value greater than 0. If some of the lines in the array is filled with those values, it's going to reset them. Check the following code:
When you say "it's going to reset them", do you mean just the values greater than 0 or all of them?

Quote:
*** Source Snippet Removed ***

I can't find anything wrong in this code, if I remove the if statement it works, but deletes everything. See anything wrong with it?
My best guess would be that you meant the first foundline=false; line to read foundline=true.

Also, if you want to reset values greater than 0, couldn't you do that in the first loop? As in:

*** Source Snippet Removed ***


No, it's only supposed to delete a line if the whole line is full and I mean the values greater than 0.
if (board[w][h]>0)

I think this should be
if (board[w][h] == 0)

You do not even need the foundline boolean:

for (int h = 0; h < BOARDHEIGHT; h++){	for (int w = 0; w < BOARDWIDTH; w++)	{		if (board[w][h] == 0)		goto out;	}	for (int i = 0; i < BOARDWIDTH; i++)	{		board[h] = 0;	}	out:}


One of the few cases were goto makes sense :)
I'd split it into several small functions for readability. I've read the above and I think you mean if, and only if, every element in a line is >0, set the whole line to zero. Yes? (Tetris?)

int BW,BH; // boardwidth, boardheightbool LineFull(int Y){    for(int X=0;X<BW;++X) if(Board[X][Y]<=0) return false;    return true;}void ClearLine(int Y){    for(int X=0;X<BW;++X) Board[X][Y]=0;}void CheckBoard(){    for(int Y=0;Y<BH;++Y)        {        if(LineFull(Y)) ClearLine(Y);        }}


Might seem like overkill but if it gets complicated by a future development of some kind, it'll be easier to update.
Quote:Original post by EasilyConfused
I'd split it into several small functions for readability. I've read the above and I think you mean if, and only if, every element in a line is >0, set the whole line to zero. Yes? (Tetris?)

*** Source Snippet Removed ***


Exactly..
The code works like I want to aswell, thanks!

This topic is closed to new replies.

Advertisement