Created
May 31, 2017 10:12
-
-
Save shivamg7/11b4305449e09c9548aef8a8de9a519b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* fifteen.c | |
* | |
* | |
* | |
* Implements Game of Fifteen (generalized to d x d). | |
* | |
* Usage: fifteen d | |
* | |
* whereby the board's dimensions are to be d x d, | |
* where d must be in [DIM_MIN,DIM_MAX] | |
* | |
* | |
*/ | |
#define _XOPEN_SOURCE 500 | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <stdlib.h> | |
#include <unistd.h>//contains usleep() | |
// constants | |
#define DIM_MIN 3 | |
#define DIM_MAX 9 | |
// board | |
int board[DIM_MAX][DIM_MAX]; | |
// dimensions | |
int d; | |
int i,j,locr,locc,tiler,tilec,temp; | |
// prototypes | |
void clear(void); | |
void greet(void); | |
void init(void); | |
void draw(void); | |
bool move(int tile); | |
bool won(void); | |
int main(int argc, char* argv[]) | |
{ | |
// ensure proper usage | |
if (argc != 2) | |
{ | |
printf("Usage: fifteen d\n"); | |
return 1; | |
} | |
// ensure valid dimensions | |
d = atoi(argv[1]); | |
if (d < DIM_MIN || d > DIM_MAX) | |
{ | |
printf("Board must be between %i x %i and %i x %i, inclusive.\n", | |
DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX); | |
return 2; | |
} | |
// open log | |
FILE* file = fopen("log.txt", "w"); | |
if (file == NULL) | |
{ | |
return 3; | |
} | |
// greet user with instructions | |
greet(); | |
// initialize the board | |
init(); | |
// accept moves until game is won | |
while (true) | |
{ | |
// clear the screen | |
clear(); | |
// draw the current state of the board | |
draw(); | |
// log the current state of the board (for testing) | |
for (int i = 0; i < d; i++) | |
{ | |
for (int j = 0; j < d; j++) | |
{ | |
fprintf(file, "%i", board[i][j]); | |
if (j < d - 1) | |
{ | |
fprintf(file, "|"); | |
} | |
} | |
fprintf(file, "\n"); | |
} | |
fflush(file); | |
// check for win | |
if (won()) | |
{ | |
printf("ftw!\n"); | |
break; | |
} | |
// prompt for move | |
printf("Tile to move: "); | |
int tile; | |
scanf("%d",&tile); | |
// quit if user inputs 0 (for testing) | |
if (tile == 0) | |
{ | |
break; | |
} | |
// log move (for testing) | |
fprintf(file, "%i\n", tile); | |
fflush(file); | |
// move if possible, else report illegality | |
if (!move(tile)) | |
{ | |
printf("\nIllegal move.\n"); | |
usleep(50000); | |
} | |
// sleep thread for animation's sake | |
usleep(50000); | |
} | |
// close log | |
fclose(file); | |
// success | |
return 0; | |
} | |
/** | |
* Clears screen using ANSI escape sequences. | |
*/ | |
void clear(void) | |
{ | |
// printf("\033[2J"); | |
// printf("\033[%d;%dH", 0, 0); | |
// clrscr(); | |
} | |
/** | |
* Greets player. | |
*/ | |
void greet(void) | |
{ | |
clear(); | |
printf("WELCOME TO GAME OF FIFTEEN\n"); | |
usleep(2000); | |
} | |
/** | |
* Initializes the game's board with tiles numbered 1 through d*d - 1 | |
* (i.e., fills 2D array with values but does not actually print them). | |
*/ | |
void init(void) | |
{ | |
int temp; | |
int count =d*d-1; | |
for(i=0;i<d;i++) | |
{ | |
for(j=0;j<d;j++) | |
{ | |
if(count==0) | |
board[i][j]=0;//changes to 0 | |
else | |
board[i][j]=count--; | |
} | |
} | |
if(d%2==0) | |
{ | |
temp=board[d-1][d-2]; | |
board[d-1][d-2]=board[d-1][d-3]; | |
board[d-1][d-3]=temp; | |
} | |
} | |
/** | |
* Prints the board in its current state. | |
*/ | |
void draw(void) | |
{ | |
printf("\n"); | |
for(i=0;i<d;i++) | |
{ | |
for(j=0;j<d;j++) | |
{ if(board[i][j]==(0))//changes to 0 | |
{ printf(" "); locr=i;locc=j;} | |
else | |
printf("%2d ",board[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
/** | |
* If tile borders empty space, moves tile and returns true, else | |
* returns false. | |
*/ | |
bool move(int tile) | |
{ | |
bool pass=false; | |
printf("%d",tile); | |
for(i=0;i<d;i++) | |
{ | |
for(j=0;j<d;j++) | |
{ | |
if(board[i][j]==tile) | |
{tiler=i;tilec=j;pass=true;break;} | |
} | |
} | |
if(pass) | |
{ | |
if((tiler==locr&&(tilec==(locc-1)||tilec==(locc+1)))||(tilec==locc&&(tiler==(locr+1)||tiler==(locr-1)))) | |
{ | |
temp=board[tiler][tilec]; | |
board[tiler][tilec]=board[locr][locc]; | |
board[locr][locc]=temp; | |
return true; | |
} | |
else | |
{return false;} | |
} | |
else | |
{ return false;} | |
} | |
/** | |
* Returns true if game is won (i.e., board is in winning configuration), | |
* else false. | |
*/ | |
bool won(void) | |
{ | |
for(i=0;i<d;i++) | |
{ | |
for(j=0;j<d;j++) | |
{ | |
if(!(board[i][j]==(i*d+j+1)||board[i][j]==0)) | |
{return false;} | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment