Skip to main content
2 of 2
Updated some errors after input from Blauelf (thanks mate!)
Matt Friend
  • 121
  • 1
  • 2
  • 11

Pset3 - Game of Fifteen - init function

I am trying to complete the first step of game of fifteen - writing the INIT function. I've partially written a function that I think should work. The thing that is bothering me is this: I want to run the function and then print out the contents of the array, and then break the programme (otherwise it will clear the screen and move on to ask for a tile to move). But the programme doesn't break! It doesn't appear to print, either. I have no idea why not. Your help appreciated. I've now updated my code for INIT (i) after spotting some errors and especially after help from Blauelf, and (ii) I've now included a function that changes the penultimate two positions in the array, if the array size is even. The updated code is below:

void init(void)
{
    // MF Note - fill the whole board with ints from (d-1) to 0.
    int ctr = 1, board[d][d];
    for(int row = 0; row < d; row++)
    {
        for(int col = 0; col < d; col++)
        {
            board [row][col] = ((d*d)-ctr);
            ctr++;
        }
    }

    // MF Note - now check whether the array is of even or odd size; and if even, swap array positions (d-2) and (d-3) - i.e. the 1 and 2
    if(d % 2 == 0)
    {
        int temp = board[d-1][d-1];
        board[d-1][d-1] = board[d-1][d-2];
        board[d-1][d-2] = temp;
    }
    // MF Note - now declare position [d-1][d-1] (the bottom right position) is 100 (will have been filled with 0 by the nested for-loops, above)
    board [d-1][d-1] = 100;

    // MF Note - now print out the contents of the array, to check whether it works, then break.
    for(int i = 0; i < d; i++)
    {
        for(int j = 0; j < d; j++)
        {
            printf("%d\n", board[i][j]);
        }
    }
    break;
}
    

The output I get when I run the programme (e.g. with "./fifteen 3") is: WELCOME TO GAME OF FIFTEEN (wait for 2 seconds; then screen clears and prints:) Tile to move:

Please help - why doesn't the break command get invoked (let alone the printf command)!

Matt Friend
  • 121
  • 1
  • 2
  • 11