0

Alright, so I've had a go at this for a very long time, and haven't been making much progress. My problems is with this move function. It doesn't accept some illegal moves, however, some illegal moves will be accepted. What is happening and how can i fix it? Thanks.

bool move(int tile)
{
// TODO


int placeholder;
int tileX;
int tileY;

if(tile > d*d -1 || tile < 1){
    return false;
}

for(int i = 0; i < d; i++){
    for(int j = 0; j < d; j++){
        if(board[i][j] == tile){
            tileX = i;
            tileY = j;
            break;
        }
    }
}
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]){
    placeholder = board[tileX][tileY];
    board[tileX][tileY] = board[blankX][blankY];
    board[blankX][blankY] = placeholder;
    blankX = tileX;
    blankY = tileY;
    return true;
}

else{ return false; } }

1 Answer 1

1

What you are describing is "unpredictable results". That happens often when you access "unallocated" memory. Each test in the big if runs the risk of accessing "unallocated" memory. Remember the board edges. row and column can never be < 0, so tileX - 1 and tileY - 1 can go off the edge. row and column can never be > d - 1; tileX + 1 and tileY + 1 can go off the edge.

You must log in to answer this question.

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