Wednesday 13 November 2013

Puzzle game in C language


Here is a puzzle game in C Language.This is an interesting Puzzle Game which keeps you busy for hours.When we all get tired and need some break we all love to play this type of games at that time.In this game you have to arrange all the numbers in increasing order.You have to use only arrow keys to move the numbers.




 SOURCE CODE OUTPUT EXPLANATION DOWNLOAD


 #define show printf("\n\n\t\t\t     PUZZLE GAME\n\nArrange all the numbers in increasing order in minimum no. of moves,use arrow   keys to move the numbers\nPress Esc to exit");
void print(int cha[][4])
{
  int i,j,b=6,a;
  for(i=0;i<4;i++)
  {
    b+=2;a=25;
    for(j=0;j<4;j++)
    {
       a+=4;
       gotoxy(a,b);
       if(cha[i][j]==80)
       printf("");
       else
       printf("%d",cha[i][j]);
    }
    printf("\n");
  }
}
int getkey()
{
  int ch;
  ch=getch();
  if(ch==0)
  {
    ch=getch();
    return ch;
  }
  return ch;
}
int main()
{
  int mo=0,z=0,ret,i,j,b,x=3,y=3;
  int ch[4][4];
  clrscr();
  printf("\n\n\n\n\n\t\t\t     MOHAMMED UJJAIN WALA\n\n\t\t\t\t  PRESENTS");
  printf("\n\n\n\t\t\t\tPUZZLE GAME");
  printf("\n\n\n\npress any key to continue");
  getch();
  phir:
  ch[0][0]=1,ch[0][1]=4,ch[0][2]=15,ch[0][3]=7,ch[1][0]=8,ch[1][1]=10,ch[1][2]=2,ch[1][3]=11,ch[2][0]=14 ;
  ch[2][1]=3,ch[2][2]=6,ch[2][3]=13,ch[3][0]=12,ch[3][1]=9,ch[3][2]=5,ch[3][3]= 80 ;
  mo=0,z=0,x=3,y=3;
  clrscr();
  printf("\n\n\t\t\tPUZZLE GAME");
  printf("\n\n\n\t1.) START\t\t\tpress 1\n\n\t2.)INSTRUCTIONS\t\t\tpress 2\n\n\t3.) EXIT\t\t\tpress 3 ");
  scanf("%d",&ret);
  switch(ret)
  {
    case 1:
    clrscr();
    show;
    print(ch);
    printf("\nNumber of moves  %d",mo);
    while(1)
    {
      b=getkey();
      if(b==27)
      goto phir;
      z=0,mo++;
      switch(b)
      {
case 72:
clrscr();
x=x+1 ;
if(x<4)
{
 ch[x-1][y]=ch[x][y];
 ch[x][y]=80;
}
else
{
 x=x-1;
 printf("\a");
}
show;
print(ch);
break;
case 80:
clrscr();
x=x-1 ;
if(x>-1)
{
 ch[x+1][y]=ch[x][y];
 ch[x][y]=80;
}
else
{
 x=x+1;
 printf("\a");
}
show;
print(ch);
break;
case 75:
clrscr();
y=y+1;
if(y<4)
{
 ch[x][y-1]=ch[x][y];
 ch[x][y]=80;
}
else
{
 y=y-1;
 printf("\a");
}
show;
print(ch);
break;
case 77:
clrscr();
y=y-1;
if(y>-1)
{
  ch[x][y+1]=ch[x][y];
  ch[x][y]=80;
}
else
{
 printf("\a");
 y=y+1;
}
show;
print(ch);
break;
default:
clrscr();
show;
print(ch);
printf("\nPress only Arrow keys");
      }
      printf("\nNumber of moves  %d",mo);
      for(i=0;i<4;i++)
      {
for(j=0;j<3;j++)
{
 if(ch[i][j]<ch[i][j+1])
 z++;
}
      }
      for(i=0;i<3;i++)
      if(ch[i][3]<ch[i+1][0])
      z++;
      if(z==15)
      break;
    }
    clrscr();
    show;
    print(ch);
    printf("\n\n\t\t\t*******YOU WON THE GAME*******  \n\n Total number of moves %d\n Thank you for playing the game \n\n\npress any key to continue",mo);
    getch();
    goto phir;
    case 2:
    clrscr();
    printf("\t\t\t\tHOW TO PLAY\nArrange all the numbers in increasing order (in following manner) in minimum no.of moves.You can move only one number at a time ,use arrow keys to move the     numbers.");
    printf("\n\n\t\t\t\t1   2   3   4\n\n\t\t\t\t5   6   7   8\n\n\t\t\t\t9   10  11  12\n\n\t\t\t\t13  14  15    ");
    printf("\n\n\n\t\t\t  ****ENJOY THE GAME****\n\n\n press any key to continue..............");
    getch();
    goto phir;
    case 3:
    exit(0);
    default:
    printf("\nSorry Wrong Choice");
    getch();
    goto phir;
  }
}

SCREENSHOTS

Previous                                                                 Next
In my program I have use getkey()  function to take input from the user .In this game we cannot use scanf(), instead of it we will use getkey() function. Now what special this function does, actually this function returns scan codes of arrow keys.
Now what are scan code and how getkey() function works?
When we press any key from keyboard two codes are generated which are stored in keyboard buffer (memory) i.e. ASCII code and scan code. Now ASCII code of some special keys such as arrow keys  are 0 therefore we will use their scan code .Scan code of up arrow key is 72,down arrow key is 80,left arrow key is 75,right arrow key is 77.Run the following program and press arrow keys you will see ASCII code and scan code of arrow keys.
void main()
{
int  i,scan;
for(i=0;i<=10;i++)
{
scan=getch();
printf("%d",scan);
}
}

OUTPUT


From the output you can see that first 0 is printed which is ASCII code of up arrow key and than 72 is printed which is scan code up arrow key .It means that when we press up arrow key at first zero is stored in scan variable and just after that 72 is stored in scan. Therefore I have written following code in getkey() function.
ch=getch();
  if(ch==0)
  {
    ch=getch();
    return ch;
  }
I hope you have understood the concept of scan code.
Now what is gotoxy() function ?
Actually this function is use to place the cursor at any position on the screen ,two parameters are passed to the functions which act as coordinates of that point where we have to place our cursor, following program illustrate this.
void main()
{
int i,j,a=10,b=0;
for(i=0;i<10;i++)
{
b++,a=10;
for(j=0;j<i;j++)
{
gotoxy(a,b);
printf("*");
a--;
}
}
getch();
}

OUTPUT


Now we will understand program from starting
At first we will store the numbers randomly in 2d array and x and y are initialized with 3 i.e. blank space will be at last position .Now suppose game starts and player presses Down arrow key getkey() function will return scan code of Down arrow key i.e.80 Now case 80 will execute  x=3-1=2 ,ch[3][3]=ch[2][3] and c[2][3]=80.In this way we will interchange the numbers .Show is a macro It will simply print the following statements. After that print() function is called


Print()
 In print() function base address of 2d array is passed as a parameter .This function simply prints the following output .It should be noted that when cha[i][j]=80 than a blank space is printed. Now

 


we have to give such condition so that computer can find out whether all the numbers are arranged in increasing order or not.
for(i=0;i<4;i++)
      {
                for(j=0;j<3;j++)
                {
                  if(ch[i][j]<ch[i][j+1])
                  z++;
                }
      }
      for(i=0;i<3;i++)
      if(ch[i][3]<ch[i+1][0])
      z++;
      if(z==15)
      break;
In above fragment of code computer simply checks whether a number is greater than its previous number or not .If yes than value of z is incremented by 1. when z value become 15 this shows that all the numbers are arranged in increasing order and loop breaks .In our case loop will not break .In this way you can see other cases also.  
Any questions regarding to program please write in comments.


RELATED POST:-

No comments:

Post a Comment