Ok, so I took some tips from another post I made and I have improved my code, but I still cannot swap my tiles. Also, it appears that all tile moves are legal and none of them print "illegal" even though some should. I do not know what to do. Thank you for everyone's help so far.
void init(void)
{
// TODO
int k = d*d-1;
for (int i = 0; i <= d - 1; i++)
{
for (int j = 0; j <= d - 1; j++)
{
board[i][j] = k;
k--;
}
}
if ((d*d -1)%2 != 0)
{
board[d-1][d-3] = 1;
board[d-1][d-2] = 2;
}
}
/**
* Prints the board in its current state.
*/
void draw(void)
{
// TODO
for (int k = 0 ; k <= d-1; k++)
{
for (int l = 0; l <= d-1; l++)
{
if (board[k][l] == 0)
{
printf(" _ ");
}
else
{
printf(" %2d", board[k][l]);
}
}
printf("\n");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
// TODO
if (tile < 1 || tile > (d*d-1))
{
return false;
}
for (int m = 0; m <= d-1; m++)
{
for (int n = 0; n <= d-1; n++)
{
if (board[m][n] == tile)
{
if (board[m -1][n] == 0)
{
int swap = tile;
tile = 0;
board[m-1][n] = swap;
return true;
}
if (board[m + 1][n] == 0)
{
int swap = tile;
tile = 0;
board[m+1][n] = swap;
return true;
}
if (board[m][n - 1] == 0)
{
int swap = tile;
tile = 0;
board[m][n-1] = swap;
return true;
}
if (board[m][n + 1] == 0)
{
int swap = tile;
tile = 0;
board[m][n+1] = swap;
return true;
}
}
}
}
return false;
}