what's wrong with this?

Recommended Posts

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[i][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?

Share on other sites
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.

Share on other sites
Quote:
 Original post by passwordI'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;		}	}}

Share on other sites
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.

Share on other sites
Quote:
Original post by templewulf
Quote:
 Original post by passwordI'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.

Share on other sites
if (board[w][h]>0)

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

Share on other sites
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[i][h] = 0;	}	out:}

One of the few cases were goto makes sense :)

Share on other sites
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.

Share on other sites
Quote:
 Original post by EasilyConfusedI'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!

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627644
• Total Posts
2978367

• 10
• 12
• 22
• 13
• 33