0

My "move" function lets me make one move. After that, anything I do is illegal.

This is my code:

bool move(int tile)
{
int a = d - 1;
int b = d - 1;

for (int i = 0; i < d; i++) 
{
    for (int j = 0; j < d; j++) 
    { 
        if (tile == board[i][j])
        {
            if ((a == i - 1 && b == j) || (a == i + 1 && b == j) || (a == i && b == j - 1) || (a == i && b == j + 1))
            {
                board[a][b] = tile;
                board[i][j] = 0;
                return true;
            }   
        }
    }
}

return false;
}

Can anyone tell me what I'm doing wrong? Is there a way to fix this code other than completely rewriting it?

0

This code will always set a and b equal to d-1, the lower right corner of the board because a and b are essentially hardcoded in the first two lines of the function. The a:b coordinates look like they are supposed to track the blank tile, but they don't, so it only works when the blank is in that initial position.

You need to alter the code to either find the 0 tile each time it's called, or keep track of the 0 tile from one call to the next.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .