Skip to content

Instantly share code, notes, and snippets.

/fifteen.c Secret

Created December 9, 2016 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a6cbbc29a9f839d2ebc6865e2cd0c65c to your computer and use it in GitHub Desktop.
Save anonymous/a6cbbc29a9f839d2ebc6865e2cd0c65c to your computer and use it in GitHub Desktop.
fifteen.c - shared from CS50 IDE
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(board[3][1]==2 && board[3][2]==1)
me_swap(&board[3][1], &board[3][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)
{
board[i][j]='_';
printf("%c \t",board[i][j]);
}
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
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if(tile==board[i][j])
{
int blank_space='_';
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment