Sign in to follow this  

what's wrong with this?

This topic is 4077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
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;
}
}
}



Share this post


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

Share this post


Link to post
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 this post


Link to post
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, boardheight

bool 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 this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

This topic is 4077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this