0

I've been stuck on this problem for a really long time. I think I'm almost done with the move function, but I need someone to explain what I need to do. Please help me. My code is:

bool move(int tile)
{
int blank_col = d-1;
int blank_row = d-1;
/* locate the tile, save its coordinates */
for (int i = 0; i < d; i++) {
    for (int j = 0; j < d; j++) {
        if (board[i][j] == tile) {
            tile_col = i;
            tile_row = j;
        }
    }
}
/* if the value of tile was found, we know it's coordinates, otherwise 
coordinates remain set to -1 */
//if (tile_col != -1 && tile_row != -1) {
    /* The logic, nothing fancy, but it basically checks if tile borders 
with blank tile*/



    // TO DO: now it can check wrong tiles, e.g.: if tile_row = 5 and d = 5, 
then tile_row + 1 does not exist and will cause error
    /*if (board[tile_col + 1][tile_row] == board[blank_col][blank_row] || 
board[tile_col - 1][tile_row] == board[blank_col][blank_row] || 
board[tile_col][tile_row + 1] == board[blank_col][blank_row] || 
board[tile_col][tile_row - 1] == board[blank_col][blank_row]) {

         The swap 
        int temp = board[tile_col][tile_row];
 board[tile_col][tile_row] = board[blank_col][blank_row];
 board[blank_col][blank_row] = temp;*/

    if (tile == board[blank_col+1][blank_row]) {
        int temp = board[blank_col][blank_row];
        board[blank_col][blank_row] = tile;
        tile = temp;
        blank_col++;
        return true;
    }
    else if (tile == board[blank_col-1][blank_row]) {
        int temp = board[blank_col][blank_row];
        board[blank_col][blank_row] = tile;
        tile = temp;
        blank_col--;
        return true;
    }
    else if (tile == board[blank_col][blank_row+1]) {
        int temp = board[blank_col][blank_row];
        board[blank_col][blank_row] = tile;
        tile = temp;
        blank_row++;
        return true;
    }
    else if (tile == board[blank_col][blank_row-1]) {
        int temp = board[blank_col][blank_row];
        board[blank_col][blank_row] = tile;
        tile = temp;
        blank_row--;
        return true;
    }
    else {
        return false;
    }
//}
return false;

}

0

You could check surrounding tiles, like

if (tile_row > 0 && board[tile_row - 1][tile_col] == 0) {
    // empty tile above
} else if ...

The tile_row > 0 && would prevent the second part from being executed if tile_row - 1 was out of range. I originally did it that way.

You also could have blank_col and blank_row global variables, set to d-1 in init. Then, you can swap like

if (abs(blank_col - tile_col) + abs(blank_row - tile_row) == 1) {
    board[blank_row][blank_col] = tile; // swap made easy,
    board[tile_row][tile_col] = 0;      // as we know the values
    blank_row = tile_row;
    blank_col = tile_col;
    return true;
} else {
    return false;
}

I used abs function from math.h, but you don't have to, the condition just has to test that exactly one coordinate differs, and differs by a value of exactly one.

| improve this answer | |

You must log in to answer this question.

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