Code:
#include<stdio.h>#include<conio.h>
#include<stdlib.h>
int a[][4] = {15,9,10,25,6,2,4,7,32,19,42,8,21,17,18,0};
void boxes();
void display();
void main()
{
int ch,r=3,c=3,t;
clrscr();
gotoxy(20,7); printf("USE ARROW KEYS TO PLAY THIS GAME");
gotoxy(20,8); printf("--------------------------------");
boxes(),
display();
while(1)
{
ch=getch();
if(ch==0 || ch==224)
{
switch(getch())
{
case 72:
if(r!=0)
{
t=a[r][c];
a[r][c]=a[r-1][c];
a[r-1][c]=t;
display(); r--;
}
else
printf("\a");
break;
case 80:
if(r!=3)
{
t=a[r][c];
a[r][c]=a[r+1][c];
a[r+1][c]=t;
display(); r++;
}
else
printf("\a");
break;
case 75:
if(c!=0)
{
t=a[r][c];
a[r][c]=a[r][c-1];
a[r][c-1]=t;
display(); c--;
}
else
printf("\a");
break;
case 77:
if(c!=3)
{
t=a[r][c];
a[r][c]=a[r][c+1];
a[r][c+1]=t;
display(); c++;
}
else
printf("\a");
break;
case 1:
exit(1);
default:
printf("\a");
}
}
}
getch();
}
void boxes()
{
int c,r;
gotoxy(20,10); printf("%c",218);
gotoxy(32,10); printf("%c",191);
gotoxy(20,18); printf("%c",192);
gotoxy(32,18); printf("%c",217);
for(c=21;c<32;c++)
{
gotoxy(c,10); printf("%c",196);
gotoxy(c,12); printf("%c",196);
gotoxy(c,14); printf("%c",196);
gotoxy(c,16); printf("%c",196);
gotoxy(c,18); printf("%c",196);
}
for(r=11;r<18;r+=2)
{
gotoxy(20,r); printf("%c",179);
gotoxy(23,r); printf("%c",179);
gotoxy(26,r); printf("%c",179);
gotoxy(29,r); printf("%c",179);
gotoxy(32,r); printf("%c",179);
}
for(c=23;c<30;c+=3)
{
gotoxy(c,10); printf("%c",194);
gotoxy(c,18); printf("%c",193);
}
for(r=12;r<17;r+=2)
{
gotoxy(20,r); printf("%c",195);
gotoxy(32,r); printf("%c",180);
}
for(c=23;c<30;c+=3)
{
gotoxy(c,12); printf("%c",197);
gotoxy(c,14); printf("%c",197);
gotoxy(c,16); printf("%c",197);
}
}
void display()
{
int i,j,r=11,c=21;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
gotoxy(c,r);
if(a[i][j]!=0)
printf("%d",a[i][j]);
else
printf(" ");
c+=3;
}
r+=2; c=21;
}
}