0

I made the init function in a different tab so I can check it, and it won't switch 1 and 2. My code is /** * fifteen.c * * Implements Game of Fifteen (generalized to d x d). * * Usage: fifteen d * * whereby the board's dimensions are to be d x d, * where d must be in [DIM_MIN,DIM_MAX] * * Note that usleep is obsolete, but it offers more granularity than * sleep and is simpler to use than nanosleep;man usleep` for more. */

define _XOPEN_SOURCE 500

include

include

include

include

int main(int argc, string argv[]) { if (argc == 2) {

int d = atoi(argv[1]);
    //printf("%i\n", d);
    int stnumber = d * d - 1;
    //printf("%i\n", stnumber);

    while (stnumber >= 0) {
        printf(" ");
        if (stnumber % d == 0) {
            if (stnumber > 9) {
                printf("%i ", stnumber);
                printf("\n\n");
            }
            else {
                printf("%i  ", stnumber);
                printf("\n\n");
            }
        }

        else {
            if (stnumber > 9) {

                printf("%i ", stnumber);
            }
            else {
                if (d % 2 == 0) {
                    if (stnumber == 2) {
                        stnumber--;
                    }
                    else if (stnumber == 1) {
                        stnumber++;
                    }
                }
                printf("%i  ", stnumber);
            }
        }
        stnumber--;
    }
    printf("\n");
}

} `

0

Your init function should not be printing anything. It should be filling the board array with the numbers, such that, in a 3x3 game, the top left of the board board[0][0] is set to 8 and it counts down from there. You will probably find a loop helpful.

The draw function will then print the array to the screen.

4
  • So how do I fill the array without printing? – Joel Banks Jul 22 '17 at 19:28
  • edited my answer – curiouskiwi Jul 23 '17 at 0:09
  • Does this look about right ` void init(void) { int num = d * d - 1; // TODO for (i = 0; i < d; i++) { for (j - 0; j < d; j++) { board[i][j] = int num; num--; } } }` @curiouskiwi (I had trouble formatting this) – Joel Banks Jul 23 '17 at 1:18
  • board[i][j] = num; (not int num). – curiouskiwi Jul 24 '17 at 9:23

You must log in to answer this question.

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