0

my code is notifying me with "Illegal Move" every time I test it. I was wondering if someone can help figure out what is wrong with my code?

bool move(int tile)
{
// TODO
int i, j;
int swap;
//rows
for (i = 0; i < d; i++)
{
    //columns
    for (j = 0; j < d; j++) {

        if (tile == board[i][j]) {
            //to the right of the blank
            if (tile == board[i][j + 1]) {
                //swap
                swap = board[i][j];
                board[i][j] = board[i][j + 1];
                board[i][j + 1] = swap;
                return true;
            }
            //to the left of the blank
            if (tile == board[i][j - 1 ]) {
                //swap
                swap = board[i][j];
                board[i][j] = board[i][j - 1];
                board[i][j - 1] = swap;
                return true;
            }
            //above the blank
            if (tile == board[i + 1][j]) {
                //swap
                swap = board[i][j];
                board[i][j] = board[i + 1][j];
                board[i + 1][j] = swap;
                return true;
            }
            //below the blank
            if (tile == board[i - 1][j]) {
                //swap
                swap = board[i][j];
                board[i][j] = board[i - 1][j];
                board[i - 1][j] = swap;
                return true;
            }
            else {

                return false;
            }
        }
    }
}
return false;
}
0

Ask yourself: if this if (tile == board[i][j]) is true, can this if (tile == board[i][j + 1]) ever be true? (Assuming a valid board with unique tiles, of course). The function never actually looks for the blank tile!

You must log in to answer this question.

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