0

For fifteen, I tried to make init in another tab so I could check to see if I have it right. Also, I'm not sure if I swap 1 and 2 in the init function or in the move function. My code so far 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 <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

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 {
                    printf("%i  ", stnumber);
                }
            }
            stnumber--;
        }
        printf("\n");
    }
}
2
  • Code seems to be correct. And yes, you have to swap 1 and 2 in init itself, because that is required at the start of the game according to the board size. You can add a condition for that. Jul 21 '17 at 17:45
  • How would I go about swapping those two?
    – Joel Banks
    Jul 21 '17 at 18:06
0

Code seems to be correct. And yes, you have to swap 1 and 2 in init itself, because that is required at the start of the game according to the board size. You can add a condition for that.

You have to swap 1 and 2 if the dimension of the board is either odd or even, I can't remember, you'll have to check in the specifications. So when stnumber becomes 2 and the dimension is odd or even(depending upon the specifications) , print 1 and then 2 and break out of the loop. Otherwise, 1 will be printed after 2 as is already the case in your code. Basically, add a else if condition within the else part as 2 won't be divisible by the dimension. Wrote an answer as the comment was getting a bit long. If this answered your question, click the green check mark.

You must log in to answer this question.

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