I'm a beginner so please bear with me. For the game of fifteen I have successfully implemented the Draw function but I'm not having the same luck with Move. It is very erratic, sometimes the tiles move, other times they're just replaced with the blank tile. I end up with a lot of blank tiles on my board. Also, moves that are out of bounds aren't always counted as illegal, but in one of my previous tries I found that as long as the tile was in the same row or column, not necessarily adjacent to the blank, the move would be permitted. Why is this? I'm obviously missing something very important but after hours of going over this and trying to change it, I can't figure it out on my own. Time has come to ask for help. If anyone can hint at what I'm missing or what's wrong I will greatly appreciate it. Let me know if you need more information or code.
bool move(int tile)
{
int blank = board[d][d];
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
if(board[(i-1)][j] == blank) //up
{
board[(i-1)][j] = tile;
board[i][j] = blank;
return true;
}
if(board[(i+1)][j] == blank) //down
{
board[(i+1)][j] = tile;
board[i][j] = blank;
return true;
}
if(board[i][(j-1)] == blank) //left
{
board[i][(j-1)] = tile;
board[i][j] = blank;
return true;
}
if(board[i][(j+1)] == blank) //right
{
board[i][(j+1)] = tile;
board[i][j] = blank;
return true;
}
}
}
}
return false;
}