So, I can locate the blank tile and the called number to swap, swap them (although through a number of steps), and then print the new board. The problem arises when I move the largest number. The program can move the number once, but after that, it still thinks the largest number stayed in place. So, a number can skip over the largest number, resulting into this:
..my code for move is
bool move(int tile){
int tempX = 0;
int tempY = 0;
int tileX = 0;
int tileY = 0;
if (tile < ((d*d)-1) && tile > 0){
for(int i = 0;i < d;i++){
for (int j = 0;j < d;j++){
if(tile == board[i][j]){
//locate the tile
tileX = i;
tileY = j;
}
}
}
}
//checks if the number called is next to the blank
if(board[blankX][blankY] == board[tileX+1][tileY] || board[blankX][blankY] == board[tileX-1][tileY] || board[blankX][blankY] == board[tileX][tileY-1]|| board[blankX][blankY] == board[tileX][tileY+1]){
//swaps the number called with blank
tempX = blankX;
tempY = blankY;
board[blankX][blankY] = board[tileX][tileY];
blankX = tileX;
blankY = tileY;
board[tileX][tileY] = 0;
tileY = tempY;
tileX = tempX;
return true;
}
else{
return false;
}
}
Can the error be in the 0? Should I rewrite the whole swap function? It's the only one that worked so far.