0

...I really don't get this one. I don't. First, the sandbox.

https://sandbox.cs50.net/checks/7928ff4d4d08499ab0e97441cb6dd2d5

Now the code:

void init(void)
{
 //partial credit to code goes to LYXPUDOX
 int slots = d * d - 1;
 for (int i = 0; i < d; i++)
 {
    for (int j = 0; j < d; j++)
    {
        board[i][j] = slots;
        slots--;
    }
 }

 //Switch the tiles if the size of the dimensions are even
 if (d % 2 == 0)
 {
    board[d-1][d-2] = 2;
    board[d-1][d-3] = 1;
 }
}

/**
* Prints the board in its current state.
*/
void draw(void)
{
 //draw the board
 for (int i = 0; i < d; i++)
 {
    for (int j = 0; j < d; j++)
    {
        if (board[i][j] != 0)
        {
            printf("%3d", board[i][j]);
        }
        else
        {
            printf("  _");
        }
    }
  printf("\n");    
 }


}

/**
* If tile borders empty space, moves tile and returns true, else
* returns false. 
*/
bool move(int tile)
{
 //check to see if a move is legal, if it is not, deny it (partial credit to LYXPUDOX)
 for (int i = 0; i < d; i++)
 {
    for (int j = 0; j < d; j++)
    {
        if (board[i][j + 1] == 0 && j + 1 < d - 1)
        {
            int replacement = board[i][j];
            board[i][j] = board[i][j + 1];
            board[i][j + 1] = replacement;
            return true;
        }
        if (board[i][j - 1] == 0 && j - 1 <= d - 1)
        {
            int replacement = board[i][j];
            board[i][j] = board[i][j - 1];
            board[i][j - 1] = replacement;
            return true;
        }
        if (board[i - 1][j] == 0 && i - 1 <= d - 1)
        {
            int replacement = board[i][j];
            board[i][j] = board[i - 1][j];
            board[i - 1][j] = replacement;
            return true;
        }
        if (board[i + 1][j] == 0 && i + 1 < d - 1)
        {
            int replacement = board[i][j];
            board[i][j] = board[i + 1][j];
            board[i + 1][j] = replacement;
            return true;
        }
    }
 }
 return false;

}

/**
* Returns true if game is won (i.e., board is in winning configuration), 
* else false.
*/
bool won(void)
{
 int winner = 1;
 for (int i = 0; i < d; i++)
 {
    for (int j = 0; j < d; j++)
    {
        if(board[i][j] == winner)
        winner++;
    }
 }
 if (winner == d * d && board[d - 1][d - 1] == 0)
 {
    return true;
 }

 return false;
}

Any help would be appreciated on this, as I think the code is correct (it complies), so I'm not sure where this error comes from.

  • Does your program compile locally? The sandbox output shows that it didn't compile. – Cliff B Aug 12 '16 at 0:28
  • It does compile locally. – jayfeather31 Aug 12 '16 at 2:28
2

Assuming it does compile, the move function has a couple of serious flaws. First, given the math used, the tests should be comparing to d, not d-1. This error is preventing anything on the right side and bottom from being moved. Next, two of the moves allow swapping off the edge of the board. Instead of comparing to d-1, they should be comparing to 0.

Next, the move function does a nice job of checking for the 0 tile, but it isn't doing anything with the target tile value. This is allowing invalid moves while not searching to make sure that the target tile and the 0 tile are adjacent.

As for the compile error, I am suspicious that something was changed elsewhere in the file, either accidentally or deliberately, that is preventing your code from compiling.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • I didn't change anything, not deliberately at least! Thanks for the help anyhow. – jayfeather31 Aug 12 '16 at 2:28
  • I'm inclined to believe you, since the code you posted above compiled when plugged into my version. The only other scenario I can think of is that you've been editing one version and submitted a different, earlier version of your source code that doesn't compile to check50. – Cliff B Aug 12 '16 at 2:30
  • That's certainly a possibility. I'll look over the code again, see if I messed up somewhere, and use your suggestions. UPDATE: It runs now. Seems to be what you said. – jayfeather31 Aug 12 '16 at 2:37

You must log in to answer this question.

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