Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
To understand the code's goal, please go to:

<a href="http://cdn.cs50.net/2016/x/psets/3/pset3/pset3.html#the_game_begins">Problem Set 3: Game of Fifteen</a> 

My code doesn't show any error. My output, however, is wrong in the last part ( switching the 1 and 2 if the d dimension is even). The 1 and 2 won't switch.

<pre>Some example outputs:

<pre>~/workspace/pset3/fifteen/ $ ./fifteen 4
WELCOME TO GAME OF FIFTEEN
  15  14  13  12
  11  10   9   8
   7   6   5   4
   3   2   1   _
Tile to move: ^C

~/workspace/pset3/fifteen/ $ ./fifteen 3
WELCOME TO GAME OF FIFTEEN
   8   7   6
   5   4   3
   2   1   _
Tile to move: 


Detailed Expl.:

If d - dimension is odd, then the order of the last 2 numbers should be: 1 2.
If d is even, the order of the last 2 numbers should be: 2 1.

The mistake in my output is when those 2 numbers get printed in the wrong order with d as even. In the above example, my output for d as 4 gives the last 2 numbers as 2 1 instead of 1 2.


PS: I tried gdb. I couldn't conclude any clear error yet (by breakpoints and printing values along).

Thanks a lot,

What I have tried:

void init(void)
{
    int c = 1;
    for (int a = 0; a < d; a++)
    {
        for (int b = 0; b < d; b++)
        {
            
            board[a][b] = d*d - c;
            c++;
            if (c == d*d -2)
            {
                if (d % 2 == 0)
                {
                    board[d-1][d-3] = 1;
                    board[d-1][d-2] = 2;
                    board[d-1][d-1] = 0;
                }
                
                else if (d % 2 != 0)
                {
                    board[d-1][d-3] = 2;
                    board[d-1][d-2] = 1;
                    board[d-1][d-1] = 0;
                }
                
             }
            
            
        }
    }
    
     
     
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
   for (int e = 0; e < d; e++)
   {
       for (int f = 0; f < d; f++)
       {
           if (!( ( e == d-1) && ( f == d-1) ))
             {
                printf("%c %2d",' ', board[e][f]);
              }

            else
               {
                   printf("%2s","   _");
               }
            
       }
       printf("\n");
   }
    
}
Posted
Updated 29-Dec-16 19:21pm
v2
Comments
Patrice T 29-Dec-16 18:13pm    
"My output, however, is wrong", wrong is not informative, define what is wrong.
Show actual output and desired output.
Member 12919791 29-Dec-16 18:20pm    
If d - dimension is odd, then the order of the last 2 numbers should be: 1 2.
If d is even, the order of the last 2 numbers should be: 2 1.

The mistake in my output is when those 2 numbers get printed in the wrong order with d as even. In the above example, my output for d as 4 gives the last 2 numbers as 2 1 instead of 1 2.

Thanks,
Patrice T 29-Dec-16 18:28pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 12919791 29-Dec-16 18:30pm    
Ok, thank you.
Richard MacCutchan 30-Dec-16 4:05am    
You should start by using meaningful names for your variables. Using a,b,c etc tells us nothing. You also need to explain what the code is supposed to be doing; as it stands it is impossible to guess.

1 solution

Quote:
My code doesn't show any error. My output, however, is wrong in the last part

You really need to pay attention and understand a few things.
- When the compiler find errors, it just means that your program do not conform to the language syntax and have some "spelling" errors.
- No compiler error, does not imply that the program is doing what it is supposed to do. "The cat is flying" is correct English, but also nonsense.

Quote:
I tried gdb. I couldn't conclude any clear error yet

The debugger don't find errors in your program, it just show you what the program is doing, it is your task to compare with what is should do and spot mismatches.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900