...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.