0

so I've been stuck on this darn 'move' function for about a week now, and at this point I'm not sure how to move forward with things. At this point, whenever I make a move, I get "Illegal Move".

bool move(int tile)
{   

int y = d-1;
int z = d-1;
int empty = board[d-1][d-1]; //location of the '_' tile in the array at the beginning of the game
int tileloc; //location of the inputed tile


 for (int i = 0; i < d-1; i++)         //Step 1: Find Tile location (if on board) and save it    
   {                                   //Finds the tiles location on the board; 
       for (int j = 0; j < d-1; j++)   //Returns its location through 'tileloc'
        {
            if (board[i][j] == tile) 
            {
                tileloc = board[i][j];
            }

        }
   }


 if (board[y - 1][z] == tileloc)
 {
     swap(&empty, &tileloc);
     return true;
 }
 else if (board[y + 1][z] == tileloc)
 {
     swap(&empty, &tileloc);
     return true;
 }
 else if (board[y][z - 1] == tileloc)
 {
     swap(&empty, &tileloc);
     return true;
 }
 else if (board[y][z + 1] == tileloc)
 {
     swap(&empty, &tileloc);
     return true;
 }
 else 
 {
     return false;
 }

}

The swap function I have is:

void swap(int* ab, int* bc)
{
     int temporary = *ab;
     *ab = *bc;
     *bc = temporary;

};

Thank you for your help! :)

1

I believe that your problem lies in how you are assigning tileloc. When you say tileloc = board[i][j], what you are really doing is taking whatever value is in that index of the array and assigning that to tileloc; that is to say, you are making tileloc equal to the value being searched for.

I recommend creating two variables that represent the row and column values of the array and if board[i][j] = tile, then make one of those variables equal to the row and the other to the column.

2
  • Additionally, you are setting the blank tile to a fixed position, the position at the start of the game. The blank tile moves around and has to be identified with each move. In short, your move intends to swap the given tile with the lower right corner tile every time. This is only guaranteed to be valid on the first move.
    – Cliff B
    Jul 8 '16 at 17:26
  • Got it! Thanks y'all :) Jul 9 '16 at 16:23

You must log in to answer this question.

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