0
void init(void)
{
    // TODO
    int fill = ((d * d) - 1);
    int brow = d;

    //fill entire grid except the bottom
    for(int i = 1; i <= d; i ++)
    {
        for(int j = 1; j <= d; j ++)
        {
            board [i] [j] = fill;
            fill --;
        }
    }
    //now fill the bottom row
    //these are all the numbers until the swap
    int bfill = d - 1;
    for(int k = 1; k < (d - 2); k ++)
    {
        board [brow] [k] = bfill;
        bfill --;
    }
    //if grid will have odd amount of tiles, then swap 1 and 2
    if((d / 2) * 2 == d)
    {
        //swap 1 and 2
        board [brow] [(d - 2)] = 1;
        board [brow] [(d - 1)] = 2;
        board [brow] [d] = 999;
    }
    //if grid will have odd amount of tiles, then don't swap 1 and 2
    if((d / 2) * 2 != d)
    {
        board [brow] [(d - 2)] = 2;
        board [brow] [(d - 1)] = 1;
        board [brow] [d] = 999;
    }
}

My program works perfectly fine when manually playing the game, and when inputting the 3x3.txt and 4x4.txt files, but when I try to check it with 'check50', it does not work. The reason is that when initializing the board, I indexed the visible board by 1. (that is, the value of board[1][1] is 8 in the beginning of a 3x3 game) the reason I did that was because I found that was the only way that I could check to see if moves were legal or not. When I tried indexing the board at 0, I received errors when checking for legal moves. How could I index the board at 0 without getting these errors?

1 Answer 1

0

check50 will assume it is indexed from 0.

You will need to change your move function to handle this. Perhaps you need a second if condition to prevent you from checking outside the bounds.

You must log in to answer this question.

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