#include<stdio.h>
#include<dos.h>
int t[4][4]={1,14,6,8,3,10,15,9,13,2,11,5,12,7,4,' '};
main()
{
FILE *f,*f2;
int a,b,c,i,j,ch,p,moves=0,counter,entry;
char name[50];
textcolor(1);
clrscr();
f=fopen("scores.dat","a");
display(t);
while(1)
{
ch=getkey();
entry=0;
if(ch==1)
break;
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
if(t[a][b]==' ')
{
p=999;
break;
}
if(p==999)
break;
}
p=0;
if(ch==72)
{
if(a!=3)
{
entry=1;
t[a][b]=t[a+1][b];
t[a+1][b]=' ';
display();
}
}
if(ch==80)
{
if(a!=0)
{
entry=1;
t[a][b]=t[a-1][b];
t[a-1][b]=' ';
display();
}
}
if(ch==77)
{
if(b!=0)
{
entry=1;
t[a][b]=t[a][b-1];
t[a][b-1]=' ';
display();
}
}
if(ch==75)
{
if(b!=3)
{
entry=1;
t[a][b]=t[a][b+1];
t[a][b+1]=' ';
display();
}
}
gotoxy(1,2);
if(entry==1)
moves++;
printf("no of moves:%d",moves);
if(moves>0)
{
for(a=0,c=1,counter=0;a<4;a++)
for(b=0;b<4;b++,c++)
if(t[a][b]==c)
counter++;
if(counter==15)
{
gotoxy(25,20);
printf("you have finished the game");
gotoxy(25,21);
printf("enter your name:");
scanf("%s",name);
fprintf(f,"%s %d",name,moves);
textcolor(4);
clrscr();
f2=fopen("scores.dat","r");
printf("\t\t\tTHE RECENT SCORES\n");
while(fscanf(f2,"%s %d",name,&moves)!=EOF)
printf("%s\t%d\n",name,moves);
getch();
credit();
exit();
}
}
}
fclose(f);
fclose(f2);
}
display()
{
int a,b,i,j;
clrscr();
look();
gotoxy(2,1);
printf("press esc to quit");
for(a=0,j=10;a<4;a++,j=j+2)
for(b=0,i=35;b<4;b++,i=i+5)
{
gotoxy(i,j);
if(t[a][b]==' ')
printf("%c",t[a][b]);
else
printf("%d",t[a][b]);
}
}
getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
look()
{
int i,j;
for(i=34;i<52;i++)
{
gotoxy(i,9);
printf("�");
gotoxy(i,17);
printf("�");
}
for(j=10;j<17;j++)
{
gotoxy(52,j);
printf("�");
gotoxy(33,j);
printf("�");
}
gotoxy(52,9);
printf("�");
gotoxy(33,17);
printf("�");
gotoxy(52,17);
printf("�");
gotoxy(33,9);
printf("�");
}
credit()
{
textcolor(10);
clrscr();
gotoxy(35,10);
printf("WRITTEN BY");
gotoxy(37,12);
printf("MAVERICK");
gotoxy(34,14);
printf("KREC SURATHKAL");
getch();
}