0

My program works perfectly. Though, my Check50 says that my program only compiles and runs, nothing else. I think this has to do with my draw function, but I can't spot the mistake Can anyone help me with this?

From,

Samuel

void init(void)
{

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {

        board [i][j] = (d * d) - ((d*i) + j + 1);
    }
}

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

/**
* Prints the board in its current state.
*/
void draw(void)
{

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if (board [i][j] == 0)
        {
            printf(" _");
        }
        else
        {
            int counter = 0;
            while (counter != board [i][j])
            {
                counter++;
            }
            if (counter >= 10)
            {
               printf("%i", board [i][j]);
            }
            else
            {
            printf(" %2i", board [i][j]);
            printf(" ");
            }
        }
    }
printf("\n"); 
printf("\n"); 
}
0

Judging from the code only, haven't run anything.

Your output has odd spacing, the lines do not seem separated, the numbers >=10 are not separated.

Odd code

            int counter = 0;
            while (counter != board [i][j])
            {
                counter++;
            }

is effectively same as

            int counter = board [i][j];

I would replace your if (board [i][j] == 0) { ... } else { ... } with

        if (board[i][j] == 0)
        {
            printf(" _ "); // or "__ "?
        }
        else
        {
            printf("%2i ", board[i][j]);
        }

and put one of the printf("\n");s inside the outer loop, so one } up.

0

Since the first check50 test after the compile is "init initializes 3x3 board correctly", are you sure your init logic is "perfect"? When you run ./fifteen 3 is your board numbered exactly like the example in the spec?

$ ./fifteen 3 WELCOME TO GAME OF FIFTEEN

8 7 6

5 4 3

2 1 _

Tile to move:

Also from the spec:

If, however, and only if the board contains an odd number of tiles (i.e., the height and width of the board are even), the positions of tiles numbered 1 and 2 must be swapped, as in the below.

Does this if (d % 2 != 0) do the swap according to the spec?

0

Change your draw function to

 void draw(void)
 {
  for(int i=0;i<d;i++)
   {
     for(int j=0;j<d;j++)
     {
         printf("%4i  ",board[i][j]);
     }
     printf("\n");
   }
 }

and you may use 0 instead of _

You must log in to answer this question.

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