1

I have two issues with my code. First, while the code correctly moves the underscore to the new space, it does not move the tile to the space where the underscore once was. So I end up with a bunch of underscores each time I select a new tile. Secondly, my code seems to be allowing me to move tiles even when there is no open space. Here is what I have so far:

void init(void)
{
    int tile = (d*d) - 1;
    //Check if d is odd
    if (d%2) 
    {
        for (int i = 0; i<d; i++)
        {
            for (int j = 0; j<d; j++)
            {
                if (tile > 0)
                {
                    board[i][j] = tile;
                    tile-=1;
                }
            }
        }
    }
    else
    {
        for (int i = 0; i<d; i++)
        {
            for (int j = 0; j<d; j++)
            {
                if (tile>2 || tile==0)
                {
                    board[i][j] = tile;
                    tile-=1;
                }
                else if (tile==2)
                {
                    board[i][j] = 1;
                    tile-=1;
                }
                else if (tile==1)
                {
                    board[i][j] = 2;
                    tile-=1;
                }
            }
        }
    }
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    for (int i = 0; i<d; i++)
        {
            for (int j = 0; j<d; j++)
            {
                //if (d==i+1 && d==j+1)
                if (board[i][j]==0)
                {
                    printf(" _\n");
                }
                else if (d==j+1)
                {
                    printf("%2i\n", board[i][j]);
                }
                else
                {
                    printf("%2i ", board[i][j]); 
                }
            }
        } 
}

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

    for (int i = 0; i<d; i++)
        {
            for (int j = 0; j<d; j++)
            {
                if (board[i][j]==tile)
                {
                    if (board[i][j+1]==blank_tile)
                    {
                        board[i][j] = blank_tile;
                        board[i][j+1] = tile;
                        return true;
                    }
                    else if (board[i][j-1]==blank_tile) 
                    {
                        board[i][j] = blank_tile;
                        board[i][j-1] = tile;
                        return true;
                    }
                    else if (board[i+1][j]==blank_tile)
                    {
                        board[i][j] = blank_tile;
                        board[i+1][j] = tile;
                        return true;
                    }
                    else if (board[i-1][j]==blank_tile)
                    {
                        board[i][j] = blank_tile;
                        board[i-1][j] = tile;
                        return true;
                    }
                    return false;
                }
            }
        }
        return false;
}

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
    int tile = 1;
    for (int i = 0; i<d-1; i++)
        {
            for (int j = 0; j<d-1; j++)
                {
                    if (board[i][j]==tile)
                    {
                        tile+=1;
                        if (tile==(d*d)-1)
                        {
                            return true;
                        }
                    }
                    return false;
                }
        }
        return false;
}
1

In move () you are reading out of bounds. For example On a 3x3 board if j is 0 you will read j-1. This goes for the whole perimeter of the board. You need to account for these bounds.

You also need to have a closer look at draw() when you move left and right. Think carefully about when you need to add a newline.

| improve this answer | |
  • That makes sense to me. Thanks for the help! – Alex Durante May 25 '17 at 17:48

You must log in to answer this question.

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