https://gist.github.com/fb943980ec3ee81f98d9ff6ff1b4cfef
Sorry for the readers. I tried formatting the code and text but was unable.
Any help will be appreciated too.
:) init initializes 3x3 board correctly
:( init initializes 4x4 board correctly
\ expected "1" character 3 of line 4, not "2":( 3x3 board, move blank up twice
\ expected prompt for input, not exit code of 0
:( 3x3 board, move blank left twice
\ expected prompt for input, not exit code of 0
:( 3x3 board, move blank left then right
\ expected prompt for input, not exit code of 0
:( 3x3 board, move blank up then down
\ expected prompt for input, not exit code of 0
:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
\ expected prompt for input, not exit code of 0
:( 3x3 board, from start try to move tile 2, 4, 5, 6, 7, 8
\ expected prompt for input, not exit code of 0
:( 3x3 board, move blank left (tile 1) then up (tile 4), then try to move tiles 1, 2, 6, 8
\ expected prompt for input, not exit code of 0
:( 3x3 board, move blank up (tile 3), then try tile 2
\ expected prompt for input, not exit code of 0
:| 3x3.txt wins the 3x3 board
\ can't check until a frown turns upside down
:| 4x4.txt wins the 4x4 board
\ can't check until a frown turns upside down
void init(void)
{
// TODO
int k=(d*d)-1;
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
board[i][j]=k--;
}
}
if(k%2==0)
{
if(board[d-1][d-3]==2 && board[d-1][d-2]==1)
me_swap(&board[d-1][d-3], &board[d-1][d-2]);
}
}
/**
* Prints the board in its current state.
*/
void draw(void)
{
// TODO
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(board[i][j]==0)
{
printf("_ \t");
}
else
{
printf("%2d \t ",board[i][j]);
}
}
printf("\n");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
// TODO
int blank_space=0;
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(tile==board[i][j] && tile<(d*d-1) && tile>0)
{
if(board[i-1][j]==blank_space)
{
me_swap(&board[i][j],&board[i-1][j]);
return true;
}
if( board[i][j-1]==blank_space)
{
me_swap(&board[i][j-1],&board[i][j]);
return true;
}
if(board[i+1][j]==blank_space )
{
me_swap(&board[i][j],&board[i+1][j]);
return true;
}
if(board[i][j+1]==blank_space)
{
me_swap(&board[i][j],&board[i][j+1]);
return true;
}
}
}
}
return false;
}
/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(void)
{
// TODO
int a=0;
int won_board[d][d];
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
won_board[i][j]= a++;
}
}
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(board[i][j]==won_board[i][j])
{
return true;
}
}
}
return false;
}
//call swap when in need to
void me_swap (int *a, int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}