Every tile I try to move, it return "Illegal move", I was quite sure the code was right, but I can't find where is the mistake. :( Any tip you can give me is appreciated!
bool move(int tile)
{
int blank_x, blank_y;
int pos_x, pos_y, swap;
if (tile > dmax + 1)
{
return false;
}
for (int i = 0, j; i < d; i++)
{
for (j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
pos_x = i;
pos_y = j;
}
if (board[i][j] == 0)
{
blank_x = i;
blank_y = j;
}
}
}
if (pos_x == (blank_x - 1) || pos_x == (blank_x + 1) || pos_y == (blank_y - 1) || pos_y == (blank_y + 1))
{
swap = board[pos_x][pos_y];
board[blank_x][blank_y] = swap;
board[pos_x][pos_y] = 0;
return true;
}
return false;
}
btw, dmax = d * d - 1
.