0

I have been working on this forever and cannot find the problem. My move function works ALMOST always. I have no idea why, but sometimes it calls legal moves illegal. Thanks in advance! (my brain is melting)

bool move(int tile) { int right; int left; int up; int down;

for (int i = 0; i<d; i++)
{
    for (int j = 0; j<d; j++)
    {
        if(board[i][j] == tile)
        {
            if ((j+1<d) && (board[i][j+1] == 0))
            {
                right = board[i][j];
                board [i][j] = board [i][j+1];
                board [i][j+1] = right;
                return true;
            }
            if (j-1>0 && board[i][j-1] == 0)
            {
                left = board[i][j];
                board [i][j] = board [i][j-1];
                board [i][j-1] = left;
                return true;
            }
            if ((i+1<d) && (board[i+1][j] == 0))
            {
                down = board[i][j];
                board[i][j] = board[i+1][j];
                board [i+1][j] = down;
                return true;
            }
            if (i-1>0 && board[i-1][j] == 0)
            {
                up = board[i][j];
                board [i][j] = board [i-1][j];
                board [i-1][j] = up;
                return true;
            }

        }


    }
}
return false;

}

Thanks again!

0

Let me guess... If the blank or 0 tile is in the top row or left column, the moves are illegal, right?

if (j-1>0

Think about it.

You must log in to answer this question.

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