1
\$\begingroup\$

I managed to move it up, left and right but when i try to move it down it goes to the last Y position in the array instead of the Y position below it.

the code i wrote to move it up, left and to the right works!

the name of the array is "mapa[h][b]", the "help" variable has the same value as the values i want to move but as an int instead of a char, the variable "i" is for the Y position and the variable "j" is for the X position

//movement upwards
         for (i=0;i<h;i++){
            for (j=0;j<b;j++){
                if(mapa[i][j]==help){
                    aux=mapa[i-1][j];
                    mapa[i-1][j]=mapa[i][j];
                    mapa[i][j]=aux;
                    break;
                    break;
                }
        }
        }   
//movement to the left
        for (i=0;i<h;i++){
            for (j=0;j<b;j++){
                if(mapa[i][j]==help){
                    aux=mapa[i][j-1];
                    mapa[i][j-1]=mapa[i][j];
                    mapa[i][j]=aux;
                    break;
                    break;
                }
        }
        }
//movement to the right
        for (i=0;i<h;i++){
            for (j=0;j<b;j++){
                if(mapa[i][j]==help){
                    aux=mapa[i][j+1];
                    mapa[i][j+1]=mapa[i][j];
                    mapa[i][j]=aux;
                    break;
                    break;
                }
        }
        }

And as I said before, this part works, what I did its just to exchange the value I want to move with the one on the position I want to move it to, to locate the value I want to move I use the for() cycles to "scan" the array and the if() is meant to find it, when the if() finds the wanted value I add one to the Y or X position, but when i try to move it down using the same logic

for (i=0;i<h;i++){
        for (j=0;j<b;j++){
            if(mapa[i][j]==help){
                aux=mapa[i+1][j];
                mapa[i+1][j]=mapa[i][j];
                mapa[i][j]=aux;
                break;
                break;
            }
    }
    }

it just moves to the bottom of the array regardles of its current location on the Y position

\$\endgroup\$
1
\$\begingroup\$

Putting two break statements in the inner for-loop doesn't break from the outer loop.

The 2nd break is ignored so the object gets moved all the way down as the outer loop keeps going.

You need to either use a goto or force the loop to exit by forcing the iteration variable to the end condition:

for (i=0;i<h-1;i++){
    for (j=0;j<b;j++){
            if(mapa[i][j]==help){
                aux=mapa[i+1][j];
                mapa[i+1][j]=mapa[i][j];
                mapa[i][j]=aux;
                goto exit_loops;
            }
    }
}
exit_loops:

Or:

for (i=0;i<h-1;i++){
    for (j=0;j<b;j++){
            if(mapa[i][j]==help){
                aux=mapa[i+1][j];
                mapa[i+1][j]=mapa[i][j];
                mapa[i][j]=aux;
                i=h; // force i to end the loop
                break;
            }
    }
}

Alternatively you can change your loop to be a single loop by treating the 2D array as a linear array:

for (i=0;i<((h-1)*b);i++){
    if(mapa[0][i]==help){
        aux=mapa[0][i+b];
        mapa[0][i+b]=mapa[0][i];
        mapa[0][i]=aux;
        break;
    }
}

Also don't forget to reduce your vertical loop by 1 (not the whole height) as you are swapping with one line further down: use (h-1) otherwise doing i+1 will overrun your array and trash memory.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.