-
-
Save anonymous/a6cbbc29a9f839d2ebc6865e2cd0c65c to your computer and use it in GitHub Desktop.
fifteen.c - shared from CS50 IDE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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