0

Here's my move function. The problem here is i know im not following the instructions. Because i don't understand why do i need to track and how to track the blank tile. My function works as expected. But to learn more, i want to know the official way of doing it...

bool move(int tile) {

for(int i = 0; i < d; i++)
{
  for(int j = 0; j < d; j++)
  {
    if(board[i][j] == tile)
    {
      if((j - 1 > 0) && (board[i][j - 1] == 0))
      {
        swap(&board[i][j], &board[i][j - 1]);

        return true;
      }

      if((j + 1 < d) && (board[i][j + 1] == 0))
      {
        swap(&board[i][j], &board[i][j + 1]);

        return true;
      }

      if((i - 1 > 0) && (board[i - 1][j] == 0))
      {
        swap(&board[i][j], &board[i - 1][j]);

        return true;
      }

      if((i + 1 < d) && (board[i + 1][j] == 0))
      {
        swap(&board[i][j], &board[i + 1][j]);

        return true;
      }


    }


  }
}

return false; }

0

The other approach would be to store the row and the column of the blank tile in global variables and, this way, you won't need to search for the tile to be moved first. All you'll need to do is to check the adjacent tiles to the blank tile. If one of them is the tile to be moved, you swap them and return true; Otherwise, you return false.

| improve this answer | |
0

In order to work is not mandatory to keep track of the blank tile, but if you do the program can run faster or be more efficient.

In the way you wrote this funtion, for every time you enter a tile to move, the function move() check every tile until it finds the one you want to move, and then checks if the blank tile is in one side of this one, so imagin the worst case in the 9x9 board your function is going to check about 80 times to find the tile you want to move and then four more to look for the blank tile, and finally you swap the tiles.

If you keep track of the blank tile, when you enter a tile to move what you can do is check only the four sides of the blank tile to see if one of them is the tile you want to move, then you swap the tiles and update the position of the blank tile.

I think the easiest way to do this is as follows:

  • declare global variables to store the indexes of the blank tile

  • initialize the variables inside the init() function dependig on the dimensions of the board

  • update this variables in the move() function or in the swap() function if you want in your case.

| improve this answer | |
  • Thanks for your clear explanation wallek876! When I initialize the variables inside the init() function though, I get an error while compiling: "unused variable" and the program won't run. Do you know how to solve this? – Maple Mar 25 '17 at 8:55
  • @Maple its almost impossible to answer without seen the code, probably you should open a new question and explain there your problem – wallek876 Mar 25 '17 at 10:34

You must log in to answer this question.

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