0

I am working on game of fifteen as my title states. Can someone help explain why my swapping of "2" and "1" is not printing.

draw() is below : 

 if(d % 2 == 0)
{
    int temp;
    temp = board[d][d - 1];
    board[d][d - 1] = board[d][d - 2];
    board[d][d - 2] = temp; 
}

 for(int i = 0; i < d; i++)
{
    printf("\n");
    for(int j = 0; j < d; j++)
    {
        if(board[i][j] == 0)
        {
            printf("_");
        }
        else
        {
            printf("%.2d\t", board[i][j]);
        }
    }
}
0

Well, the big problem with the code posted is that the maximum value for either dimension of the array is d-1. Your code is trying to access an index of d.

But there's a bigger problem. Why are you trying to do that swap in draw()? The only thing that draw() should do is literally draw the board in it's current state. That swap is part of the initialization process. If you do it here, it's going to happen with every move.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thank you for your help! I initially had the swap in init() but read some bad comments on other posts with a similar problem. Thanks again! – Ryan A. James Jul 21 '16 at 4:03

You must log in to answer this question.

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