Vous êtes sur la page 1sur 7

/* GAME OF NUMBERS IMPLEMENTATION

FOR C++ 3.0.


/* Game Implementation */
/* GAME.C */
*/
# include <stdio.h>
# include <conio.h>
# include <dos.h>
# include <process.h>
# define UP_ARROW 72 /* Scan code of up arrow */
# define DOWN_ARROW 80 /* Scan code of down arrow */
# define LEFT_ARROW 75 /* Scan code of left arrow */
# define RIGHT_ARROW 77 /* Scan code of right arrow */
# define ESC 1 /* Scan code of escape key */
int a[4][4]={ /* Array to store the numbers */
{4,6,12,1},
{2,8,10,14},
{3,7,13,9},
{5,11,15,0}
};
int r=3; /* Row of the array */
int c=3; /* Coloumn of the array */
int moves=0; /* To count no. of moves */
/* Prototype Declaration */
void Display(void); /* Prototype of display function */
void Boxes(void); /* Prototype of boxes function */
void Game(void); /* Prototype of game function */
void Esc(void); /* Prototype of escape function */
int Getkey(void); /* Prototype of getkey function */
void Install(void); /* Prototype of install function */
void Rules(void); /* Prototype of rules function */
void Win(void); /* Prototype of win function */
/* Main Game Function */
void Game(void)
{
int ch;
int temp;
Boxes();
Display();
while(1)
{
ch=Getkey();
switch(ch)
{
case DOWN_ARROW :
if(r==0)
{
printf("a");
break;
}
temp=a[r][c];
a[r][c]=a[r-1][c];
a[r-1][c]=temp;
r--;
moves++;
Display();
break;
case UP_ARROW :
if(r==3)
{
printf("a");
break;
}
temp=a[r][c];
a[r][c]=a[r+1][c];
a[r+1][c]=temp;
r++;
moves++;
Display();
break;
case LEFT_ARROW :
if(c==3)
{
printf("a");
break;
}
temp=a[r][c];
a[r][c]=a[r][c+1];
a[r][c+1]=temp;
c++;
moves++;
Display();
break;
case RIGHT_ARROW :
if(c==0)
{
printf("a");
break;
}
temp=a[r][c];
a[r][c]=a[r][c-1];
a[r][c-1]=temp;
c--;
moves++;
Display();
break;
case ESC :
Esc();
break;
default :
break;
}
}
}
/* Escape Function */
void Esc(void)
{
Win();
gotoxy(27,22);
textcolor(LIGHTGREEN);
cprintf("No. of moves = %d",moves);
getch();
exit(0);
}
/* Display Function */
void Display(void)
{
int r1=9,c1=30;
int i,j;
textcolor(MAGENTA);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]==0)
{
gotoxy(c1,r1);
cprintf(" ");
}
else
{
gotoxy(c1,r1);
cprintf("%d",a[i][j]);
}
c1=c1+3;
}
c1=30;
r1=r1+2;
}
}
/* Function To Draw Boxes */
void Boxes(void)
{
int i,j;
gotoxy(26,3);
textcolor(MAGENTA);
cprintf("GAME OF THE CENTURY");
textcolor(YELLOW);
for(i=8;i<=16;i=i+2)
{
for(j=29;j<=41;j++)
{
gotoxy(j,i);
cprintf("%c",196);
}
}
for(i=29;i<=42;i=i+3)
{
for(j=8;j<=16;j++)
{
gotoxy(i,j);
cprintf("%c",179);
}
}
gotoxy(29,8);
cprintf("%c",218);
gotoxy(29,16);
cprintf("%c",192);
gotoxy(41,8);
cprintf("%c",191);
gotoxy(41,16);
cprintf("%c",217);
for(i=32;i<=38;i=i+3)
{
gotoxy(i,8);
cprintf("%c",194);
}
for(i=32;i<=38;i=i+3)
{
gotoxy(i,16);
cprintf("%c",193);
}
for(i=10;i<=14;i=i+2)
{
for(j=32;j<=38;j=j+3)
{
gotoxy(j,i);
cprintf("%c",197);
}
}
for(i=10;i<=14;i=i+2)
{
gotoxy(29,i);
cprintf("%c",195);
}
for(i=10;i<=14;i=i+2)
{
gotoxy(41,i);
cprintf("%c",180);
}
}
/* Function To Get The Scan Code of Key */
int Getkey(void)
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
/* Install Menu Function */
void Install(void)
{
int i,j;
int c=11,r=19;
int time=0;
gotoxy(32,13);
textcolor(BROWN);
cprintf("INSTALLING MENU");
textcolor(YELLOW);
gotoxy(1,19);
cprintf("Installing");
for(i=20;i<=22;i=i+2)
{
for(j=1;j<=80;j++)
{
gotoxy(j,i);
cprintf("%c",196);
}
}
for(i=20;i<=22;i++)
{
gotoxy(1,i);
cprintf("%c",179);
}
for(i=20;i<=22;i++)
{
gotoxy(80,i);
cprintf("%c",179);
}
gotoxy(1,20);
cprintf("%c",218);
gotoxy(1,22);
cprintf("%c",192);
gotoxy(80,20);
cprintf("%c",191);
gotoxy(80,22);
cprintf("%c",217);
for(i=2;i<=79;i++)
{
gotoxy(i,21);
printf("%c",176);
}
for(i=2;i<=79;i++)
{
gotoxy(i,21);
delay(500);
textcolor(BLUE);
cprintf("%c",219);
gotoxy(67,19);
printf(" ");
if(c==15)
{
for(j=11;j<=15;j++)
{
gotoxy(j,19);
printf(" ");
}
c=11;
}
gotoxy(c,r);
textcolor(YELLOW);
cprintf(".");
c++;
gotoxy(67,19);
textcolor(YELLOW);
cprintf("%d% Completed",time);
if(time<=60)
{
time++;
}
else if(time<=71)
time=time+2;
else
time=time+3;
if(i==78)
time=time-3;
}
}
/* Rules Menu Function */
void Rules(void)
{
int i;
textcolor(LIGHTCYAN);
gotoxy(39,5);
cprintf("RULES");
textcolor(GREEN);
gotoxy(18,7);
cprintf("1. Your aim is to arrange numbers in ascending order");
gotoxy(18,9);
cprintf("2. The player with minimum no. of moves wins the game");
textcolor(LIGHTGRAY);
for(i=17;i<=71;i++)
{
gotoxy(i,6);
cprintf("%c",196);
}
for(i=17;i<=71;i++)
{
gotoxy(i,10);
cprintf("%c",196);
}
for(i=6;i<=10;i++)
{
gotoxy(17,i);
cprintf("%c",179);
}
for(i=6;i<=10;i++)
{
gotoxy(71,i);
cprintf("%c",179);
}
gotoxy(17,6);
cprintf("%c",218);
gotoxy(17,10);
cprintf("%c",192);
gotoxy(71,6);
cprintf("%c",191);
gotoxy(71,10);
cprintf("%c",217);
}
/* Function To Check For Win */
void Win(void)
{
int i,j,pos;
int flag=1;
int arr[20];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
pos=i*4+j;
arr[pos]=a[i][j];
}
}
for(i=0;i<pos;i++)
{
if(arr[i]>arr[i+1])
{
flag=0;
break;
}
}
if(flag==1)
{
gotoxy(30,21);
printf("YOU WIN");
}
}
/* The Main Function */
void main(void)
{
clrscr();
Install();
sleep(1);
clrscr();
Rules();
getch();
clrscr();
Game();
getch();
}

Vous aimerez peut-être aussi