0

I have been able to successfully draw the board with the correct configuration, but when I try to implement move(), I cannot get the values of tile to swap with the blank space. As it currently stands, the program recognizes a legal vs illegal move. Help would be much appreciated!

Relevant code from move() is below.

bool move(int tile)
{
 blankX = d - 1;
 blankY = d - 1;
 if (board[blankX][blankY - 1] == tile && j >= 0)//check tile to left 
of blank and not out of bounds  
    {
      //swap tiles
      blankY = blankY + 1;
      int temp = blankY;
      blankY = tile;
      tile = temp;
      return true;
    }
    else if (board[blankX][blankY + 1] == tile && j < d) //check if tile is to the right of blank and not out of bounds
    {
      //swap tiles
      blankY = blankY + 1;
      int temp = blankY;
      blankY = tile;
      tile = temp;
      return true;
    }
    else if (board[blankX + 1][blankY] == tile && i < d) //check if tile is below blank and not out of bounds
    {
        //swap tiles
        blankX = blankX + 1;
        int temp = blankX;
        blankX = tile;
        tile = temp;
        return true;
    }
    else if (board[blankX - 1][blankY] == tile && i >= 0) //check if tile is above blank and not out of bounds
    {
        //swap tiles
        blankX = blankX - 1;
        int temp = blankX;
        blankX = tile;
        tile = temp;
        return true;
    }
    else
    {
        return false;
    }
return false;}                                                                 
0

There are several problems here. First, look at the following:

bool move(int tile)
{
 blankX = d - 1;
 blankY = d - 1;

This means that the blank tile will ALWAYS be treated as being at the bottom right corner. How would move ever work once the blank tile is moved? Also, after the first successful tile move, I don't think anything is going to be recognized as a successful move.

Next, the values of blankX, blankY, temp and tile are being manipulated and swapped, but no values in board[][] are ever changed, so nothing is ever done, no tiles are moved at all. Maybe this is a case of getting so focused on how to move, that the idea of what to move was overlooked? It happens. ;-)

This should get you going again.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

| improve this answer | |
  • Thank you for the help. I figured out where I was going wrong (I was definitely overthinking the issue). Thanks again! – gnmoody Jun 21 '17 at 0:20

You must log in to answer this question.

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