My move function generally works properly, but every so often it adds an extra 0 in to the table, so that there are two. Both can then be moved around. I can't figure out what corner case I haven't accounted for that is causing this bug.
bool move(int tile)
{
// TODO
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
if (board[i + 1][j] == 0 && i + 1 < d)
{
board[i][j] = 0;
board[i + 1][j] = tile;
return true;
}
else if (board[i - 1][j] == 0 && i - 1 < d)
{
board[i][j] = 0;
board[i - 1][j] = tile;
return true;
}
else if (board[i][j + 1] == 0 && j + 1 < d)
{
board[i][j] = 0;
board[i][j + 1] = tile;
return true;
}
else if (board[i][j - 1] == 0 && j - 1 < d)
{
board[i][j] = 0;
board[i][j - 1] = tile;
return true;
}
}
}
}
return false;
}
Where is the flaw in my logic?