0
bool move(int tile)
{   
   int xaxis = d - 1;
   int yaxis = d - 1;
   int tile_i;
   int tile_j;

   search_the_board(tile);

   if (tile_i == xaxis + 1 || tile_i == xaxis - 1)
     if (tile_j == yaxis + 1 || tile_j == yaxis - 1)
        {
            swap(&xaxis, &tile_i);
            swap(&yaxis, &tile_j);
        }
    return false;
}

int search_the_board(int tile)
{

     for (int i = 0; i < d-1; i++)                 
   {                                     
       for (int j = 0; j < d-1; j++)   
        {
            if (board[i][j] == tile) 
            {
                tile_i = i;
                tile_j = j;
            }

        }
   }

};

Error message 1: Initialize the variable "tile_i" to silence this warning

I thought that since I'm calling the function search_the_board in the move function, the values are returned? Please help!

2 Answers 2

0

It would REALLY be of great help to yourself to give the loop variables names that are easier to infer what they do (i.e. row and col). Inside those two fors you should call swap but not without checking if the move is legal first. Easier, faster, simpler.

0

The compiler is unhappy because you are checking the value of a variable which has not yet been initialized. To silence the warning, initialize the variables when you declare them:

int tile_i = 0;
int tile_j = 0;

Now that I've answered the actual question you asked, I may as well add that Matthew Knowler's advice is also good. In general, you'll find it easier to read your own code if you give your variables more descriptive names like tile_row and tile_col.

Finally, you don't need to mess with pointers to swap those two tiles. You can find the empty tile (which should have a value of 0) and assign the value of the adjacent tile to it, taking care not to swap a tile which is not adjacent to the blank tile either vertically or horizontally. Once you have done that, simply assign a 0 to the tile you moved, making it the new blank. No actual swapping is necessary.

You must log in to answer this question.

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