Whenever I run Check50 my program passes all the tests except the last two, the terminal spits out
:( 3x3 board: make sure game is winnable timed out while waiting for program to exit
:( 4x4 board: make sure game is winnable timed out while waiting for program to exit
I believe that the problem might be in my won function but here's the entirety of my program, can someone please point out what's wrong with my code?
void init(void)
{
int value = (d * d) - 1;
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
board[i][j] = value;
value--;
}
}
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
{
printf("%2i ", board[i][j]);
}
}
printf("\n");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] == tile)
{
if(j > 0)
{
if(board[i][j - 1] == 0)
{
int temp = tile;
board[i][j] = board[i][j - 1];
board[i][j - 1] = temp;
return true;
draw();
}
}
if(j < d - 1)
{
if(board[i][j + 1] == 0)
{
int temp = tile;
board[i][j] = board[i][j + 1];
board[i][j + 1] = temp;
return true;
draw();
}
}
if(i > 0)
{
if(board[i - 1][j] == 0)
{
int temp = tile;
board[i][j] = board[i - 1][j];
board[i - 1][j] = temp;
return true;
draw();
}
}
if(i < d - 1)
{
if(board[i + 1][j] == 0)
{
int temp = tile;
board[i][j] = board[i + 1][j];
board[i + 1][j] = temp;
return true;
draw();
}
}
}
}
}
return false;
}
/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(void)
{
int x = 0;
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] < board[i][j + 1])
{
x++;
if(board[i][j] == d * d)
{
return true;
}
}
}
}
return false;
}