0

I have completed functions init; draw; and move. My gameboard draws, but does not draw the move. My logic in move works (I hope) since it returns correct feedback (ie "illegal move" when it is supposed to be), but it will not draw the move.

Thank you in advanced.

void init(void)
{
    // TODO

    int k = 1;
    for (int i = 0; i <= d - 1; i++)
    {
        for (int j = 0; j <= d - 1; j++)
        {
            board[i][j] = d * d - k;
            k++;
        }
    }
    board[d-1][d-1] = 0;



    if ((d*d -1)%2 != 0)
    {
        board[d-1][d-3] = 1;
        board[d-1][d-2] = 2;

    }
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    // TODO
    for (int k = 0 ; k <= d-1; k++)
    {
        for (int l = 0; l <= d-1; l++)
        {

            if (board[k][l] == 0)
            {
                printf("  _ ");
            }
            else
            {
                printf(" %2d", board[k][l]);
            }
        }
        printf("\n");
    }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false.
 */
bool move(int tile)
{
    // TODO


    if (tile < 1 || tile > (d*d-1))
    {
        return false;
    }

    for (int m = 0; m <= d-1; m++)
    {
        for (int n = 0; n <= d-1; n++)
        {
            if (board[m][n] == tile)
            {
                int blankspace = 0;
                if (board[m -1][n] == blankspace)
                {

                    tile = board[m-1][n];
                    board[m-1][n] = blankspace;
                    return true;
                }

                if (board[m + 1][n] == blankspace)
                {

                    tile = board[m + 1][n];
                    board[m+1][n] = blankspace;
                    return true;
                }

                if (board[m][n - 1] == blankspace)
                {

                    tile = board[m][n-1];
                    board[m][n-1] = blankspace;
                    return true;
                }
                if (board[m][n + 1] == blankspace)
                {

                    tile = board[m][n + 1];
                    board[m][n + 1] = blankspace;
                    return true;
                }
            }
        }
    }


    return false;
}
1

Why are you initializing blankspace in draw? Because of this, the lower right position will always be drawn as a blank.

Also, in init, why is the lower right initialized to 9999. I believe the instructions say to use 0. (At least in prior versions, probably still true.)

Finally, a tip. While the calculation of the value of each tile looks correct (I'm assuming it is because you said it appears to be working), wouldn't it have been easier to set k to d*d-1 and decrement by 1 each time?

There may be more issues, but this should get you going. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Ok I changed initializing blankspace in draw and the value to 0, but now all moves are "legal" and it still will not draw? – R_C Oct 3 '17 at 3:39

You must log in to answer this question.

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