0

Ok, so I took some tips from another post I made and I have improved my code, but I still cannot swap my tiles. Also, it appears that all tile moves are legal and none of them print "illegal" even though some should. I do not know what to do. Thank you for everyone's help so far.

void init(void)
{
    // TODO

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




    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)
            {

                if (board[m -1][n] == 0)
                {
                    int swap = tile;
                    tile = 0;
                    board[m-1][n] = swap;
                    return true;
                }

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

                    int swap = tile;
                    tile = 0;
                    board[m+1][n] = swap;
                    return true;
                }

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

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

                    int swap = tile;
                    tile = 0;
                    board[m][n+1] = swap;
                    return true;
                }


            }

        }

    }
    return false;


}

1 Answer 1

1

You'd have to protect your code against non-existent coordinates. So instead of

if (board[m -1][n] == 0)

do

if (m > 0 && board[m -1][n] == 0)

because if m>0, then m-1 is still in range. If left side of && operator is false, it won't even evaluate the right side. Use <d-1 to check when doing +1.

Also, tile = 0; is pretty useless, since tile is not at all related to board[m][n] and changes won't propagate. Assign to board[m][n] instead. So the code might look like

if (m > 0 && board[m -1][n] == 0)
{
    board[m-1][n] = tile;
    board[m][n] = 0;
    return true;
}
1
  • Thank you! got it!
    – R_C
    Commented Oct 5, 2017 at 1:33

You must log in to answer this question.

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