0

I have an issue with my code in Game of Fifteen. I get "illegal move" when I try to swap the tiles, even if it's a "legal" tile. I tried everything but I really don't understand what's wrong. Does anyone have a hint? Thank you!! Here is my code for the move fonction:

bool move(int tile)
{
int blank; 
int tile_move;
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if (board[i][j] == 0)
        {
            blank = board[i][j];
        }

       else if (board[i][j] == tile)
        {
            tile_move = board[i][j];

            if (board[i-1][j] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;
            }

            else if (board[i+1][j] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;
            }

            else if (board[i][j-1] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;
            }

            else if (board[i][j+1] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;

            }
        }
    }
}
return false;
}

And here is my code for the other functions, in case the problem is there:

void init(void)
{
// Compute highest number of the array 
int total = d*d;

// iterate over the grid
// for each row
for (int i = 1; i <= d; i++)
{
    // for each column
    for (int j = 1; j <= d; j++)
    {

        // Compute the value of that case
        board [i][j] = ((total) - j);
    }
    // compute the number starting the next row
    total = (d*d) - (i*d);
}
// if even d, swap 1 and 2
if ((d*d)%2 == 0)
{    
    board[d][d-2] = 1;
    board[d][d-1] = 2;
}
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{   
// iterate over grid
// for each row
 for (int i = 1; i <= d; i++)
{
    // for each column
    for (int j = 1; j <= d; j++)
    {
        // print an undescore for empty space
        if (board[i][j] == 0)
        {
            printf("  _");
        }
        // print the board in its current state
        else
        {
             printf ("%3i", board [i][j]);
        }
    }
    // print a new line
    printf ("\n");
}
}


/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
int blank; 
int tile_move;
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if (board[i][j] == 0)
        {
            blank = board[i][j];
        }

       else if (board[i][j] == tile)
        {
            tile_move = board[i][j];

            if (board[i-1][j] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;
            }

            else if (board[i+1][j] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;
            }

            else if (board[i][j-1] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;
            }

            else if (board[i][j+1] == blank)
            {
                blank = tile_move;
                tile_move = 0;
                return true;

            }
        }
    }
}
return false;
}

/**
 * Returns true if game is won (i.e., board is in winning 
configuration), 
 * else false.
 */
bool won(void)
{
int n = 1;  

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {              
        if (board[i][j] == 0)
            n = 0;

        if (board[i][j] != n)              
            return false; 

    n++;  
    }

}

return true;
 }

1 Answer 1

1

You use 0-based indexing (the C standard) in some places (move, won), but 1-based indexing in others (init, draw).

You use a strange variable blank. This variable is initially of undefined value (could even be read before receiving a defined value), and the only value that could ever be assigned to it is 0.

You have an equally strange variable tile_move. Again, there's only one value that can be assigned to it, which is blank.

Then, you swap the values of those variables. You should swap the content of two board cells instead.

There are two common approaches:

  1. Find values of i and j for both the tile and the empty tile, then after the loop, if those are neighbouring tiles, swap and return true
  2. Find the cell with the tile. Now check the neighbouring tiles, respecting the board boundaries (a cell at (0, d-1) would have only 2 neighbours), and if one is the empty tile, swap the two, return true

Some other solutions keep track of the positions of individual cells in another array.

1
  • Thank youuu!! It took me some time but I finally got it!
    – Manon H
    Commented May 26, 2017 at 10:27

You must log in to answer this question.

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