HAND CRICKET


game cover design



 Hand Cricket! I bet if you're a student like me in India you'd definitely know that it's most enjoyable hand game one can play at anytime and anywhere. If you know the rules of cricket then it's pretty simple to understand this game. Game of cricket consists of 3 parts:

  1. Toss: Here you have to select heads or tails... if the luck is on your side and the tossed coin flips to the side you selected then you'll have the advantage of selecting whether to bat first or bowl first... on the other hand if you loose; the advantage is taken by the opponent
  2. 1st innings: What you do in the first inning is decided by the Toss.
  3. 2nd Innings: Here's the interesting and final part of the game... Rise to defend if you have batted first and score at will and win if you'd chosen to bowl first
Batting and Bowling in Hand cricket:
  •  In the game you have fixed amount of wickets ( lives ), you have to score before you loose all your wickets
  • At each turn, you can choose any number from 1 to 6
  • if you're batting and you select the same number (between 1-6) as the opponent chooses at the same time then, you lose your wicket. Similarly, if you're bowling, then your opponent would lose a wicket
  • if you're batting and both your selections mismatch, then your selection is added to your score which is initially 0... and similarly, if opponent is batting and your selections mismatch then his selection will be added to his score



This is  another text based game that I've created using C language... I provided link below to download exe file but, it might not run on all systems so I've provided my code below the download section. Just copy paste in your C compiler and execute it!



Below this section is my code. To understand it, you must have basic ideas of concepts of
  • Arrays
  • Structures
  • Functions
  • Recursions
  • rand() function
  • Creativity :)




 So here's the code:

 #include <stdio.h>  
 #include <stdlib.h>  
 char out[2][7]={{'H','e','a','d','s','\0'},{'T','a','i','l','s','\0'}};  
 char sel[2][7]={{'B','a','t','\0'},{'B','o','w','l','\0'}};  
 int wik;  
 struct player  
      {  
           char name[20];  
           int score,wickets;  
      }u={"user",0,10},c={"cpu",0,10};  
 void clear()  
 {  
      system("cls");  
 }  
 void toss();  
 void game(int w);  
 void about();  
 int main()  
 {  
      int opt;  
      clear();  
      printf("\n\n\t\t\t HAND CRICKET VER 1.0");  
      printf("\n\n\n===============================================================================\n");  
      printf("\n\n\n\n\n\t\t\t1.Play\n\n\t\t\t2.About\n\n\t\t\t3.Exit");  
      printf("\n\n\n\t\tenter your selection : ");  
      scanf("%d",&opt);  
      switch(opt)  
      {  
      case 1:  
           clear();  
           toss();  
           break;  
      case 2:  
           clear();  
           about();  
           break;  
      case 3:  
           exit(0);  
      default :  
           clear();  
           printf("Invalid Entry");  
           main();  
      }  
      return 0;  
 }  
 void toss()  
 {  
      int i=0,j,w,l,agn; 



      do

      { 

           c.score=u.score=0;

           clear();
           printf("\n\n enter your team name : ");  
           scanf("%s",u.name);  
           printf("\n\n enter your opponent team name : ");  
           scanf("%s",c.name);  
           printf("\n\n enter number of wicket(s) for each team : ");  
           scanf("%d",&wik);  
           c.wickets=u.wickets=wik;  
           clear();  
           printf("press 0 to select %s\n   1 to select %s\n\n",out[0],out[1]);  
           printf("Your selection : ");  
           scanf("%d",&i);  
           if(i!=0&&i!=1)  
           {  
                clear();  
                printf("Invalid Entry\n\n");  
                toss();  
           }  
           j=rand()%2;  
           if(i==j)  
           {  
                printf("\n\n %s it is!! %s have won the toss! \n\n",out[i],u.name);  
                printf("press 0 to bat first\n   1 to bowl first\n\n");  
                scanf("%d",&w);  
                if(w!=0&&w!=1)  
                {  
                     clear();  
                     printf("Invalid Entry! Choose again\n\n");  
                     toss();  
                }  
                printf("%s chose to %s first\n\n",u.name,sel[w]);  
           }  
           else  
           {  
                printf("It's %s... %s lost the toss n\n\n",out[j],u.name);  
                l=rand()%2;  
                if(l==0)  
                     w=1;  
                else  
                     w=0;  
                printf("%s chose to %s first\n\n",c.name,sel[l]);  
           }  
           game(w);  
           printf("Want to play again? Enter 1 if yes else enter any other number to exit : ");  
           scanf("%d",&agn);  
      }while(agn==1);  
      main();  
 }  
 void game(int w)  
 {  
      int yc,oc;  
      printf("\n\n\n\n\n");  
      if(w==0)  
      {  
           while(u.wickets>0)  
           {  
                printf("%s batting\n\n",u.name);  
                //your choice  
                printf("You choose (only between 1 to 6) : ");  
                scanf("%d",&yc);  
                if(yc<1||yc>6)  
                {  
                          printf("invalid entry. choose only between 1 to 6 !\n\n");  
                          continue;  
                }  
                //opponent's choice using rand()  
                oc=(rand()%6)+1;  
                printf("\n\n%s chooses          : %d\n\n",c.name,oc);  
                if(yc==oc)  
                {  
                     u.wickets--;  
                     printf("%s lost a wicket.... %s has scored %d for loss of %d wicket(s)\n\n\n",u.name,u.name,u.score,wik-u.wickets);  
                }  
                else  
                {  
                     u.score+=yc;  
                     printf("it's a %d! ",yc);  
                     printf("%s has scored %d for loss of %d\n\n\n",u.name,u.score,wik-u.wickets);  
                }  
           }  
           printf("\n\n-------------------------------------------------\n\n");  
           printf("FINAL SCORE : %s has scored %d",u.name,u.score);  
           printf("\n\n-------------------------------------------------\n\n");  
           printf("%s batting\n\n",c.name);  
           while( c.wickets>0 && c.score<=u.score )  
           {  
                //your choice  
                printf("You choose (only between 1 to 6) : ");  
                scanf("%d",&yc);  
                if(yc<1||yc>6)  
                {  
                     printf("invalid entry. choose only between 1 to 6 !\n\n");  
                     continue;  
                }  
                //opponent's choice using rand()  
                oc=(rand()%6)+1;  
                printf("\n\n%s chooses          : %d\n\n",c.name,oc);  
                if(yc==oc)  
                {  
                     c.wickets--;  
                     printf("%s lost a wicket.... %s has scored %d for loss of %d wicket(s)\n\n\n",c.name,c.name,c.score,wik-c.wickets);  
                }  
                else  
                {  
                     c.score+=oc;  
                     printf("it's a %d! ",oc);  
                     printf("%s has scored %d for loss of %d\n\n\n",c.name,c.score,wik-c.wickets);  
                }  
           }  
           //RESULT  
           if( c.wickets==0 && ((u.score-c.score)>0) )  
                printf("\n\n\nCLEAN BOWLED!!! YOU DEFEATED %s by %d runs! YOU ARE VICTORIOUS\n\n",c.name,u.score-c.score);  
           else if(u.score==c.score)  
                printf("\n\n\nITS A DRAW! SUCH AN EPIC THRILLER... BETTER LUCK NEXT TIME \n\n\n");  
           else  
                printf("\n\n\n%s WON THE GAME BY %d wickets.... BETTER LUCK NEXT TIME!\n\n\n",c.name,c.wickets);  
      }  
      else  
      {  
           while(c.wickets>0)  
           {  
                printf("%s batting\n\n",c.name);  
                //your choice  
                printf("You choose (only between 1 to 6) : ");  
                scanf("%d",&yc);  
                if(yc<1||yc>6)  
                {  
                          printf("invalid entry. choose only between 1 to 6 !\n\n");  
                          continue;  
                }  
                //opponent's choice using rand()  
                oc=(rand()%6)+1;  
                printf("\n\n%s chooses          : %d\n\n",c.name,oc);  
                if(yc==oc)  
                {  
                     c.wickets--;  
                     printf("%s lost a wicket.... %s has scored %d for loss of %d wicket(s)\n\n\n",c.name,c.name,c.score,wik-c.wickets);  
                }  
                else  
                {  
                     c.score+=oc;  
                     printf("it's a %d! ",oc);  
                     printf("%s has scored %d for loss of %d\n\n\n",c.name,c.score,wik-c.wickets);  
                }  
           }  
           printf("\n\n-------------------------------------------------\n\n");  
           printf("FINAL SCORE : %s has scored %d",c.name,c.score);  
           printf("\n\n-------------------------------------------------\n\n");  
           printf("%s batting\n\n",u.name);  
           while( u.wickets>0 && u.score<=c.score )  
           {  
                //your choice  
                printf("You choose (only between 1 to 6) : ");  
                scanf("%d",&yc);  
                if(yc<1||yc>6)  
                {  
                     printf("invalid entry. choose only between 1 to 6 !\n\n");  
                     continue;  
                }  
                //opponent's choice using rand()  
                oc=(rand()%6)+1;  
                printf("\n\n%s chooses          : %d\n\n",c.name,oc);  
                if(yc==oc)  
                {  
                     u.wickets--;  
                     printf("%s lost a wicket.... %s has scored %d for loss of %d wicket(s)\n\n\n",u.name,u.name,u.score,wik-u.wickets);  
                }  
                else  
                {  
                     u.score+=yc;  
                     printf("it's a %d! ",yc);  
                     printf("%s has scored %d for loss of %d\n\n\n",u.name,u.score,wik-u.wickets);  
                }  
           }  
           //RESULT  
           if( u.wickets==0 && ((c.score-u.score)>0) )  
                printf("\n\n\n%s WON THE GAME BY %d runs.... BETTER LUCK NEXT TIME!\n\n\n",c.name,c.score-u.score);  
           else if(u.score==c.score)  
                printf("\n\n\nITS A DRAW! SUCH AN EPIC THRILLER... BETTER LUCK NEXT TIME \n\n\n");  
           else  
                printf("\n\n\nAND THAT'S THE WINNING RUN! YOU WON OVER %s by %d wickets \n\n\n",c.name,u.wickets);  
      }  
 }  
 void about()  
 {  
      int ex;  
      printf("\n\n\n\t\t\tABOUT\n\n\n\n\n\n");  
      printf(" \t\tDEVOLOPED BY *******Cherub7*******\n\n\n");  
      printf(" \t\tPOWERED  BY    MARS V ");  
      printf("\n\n\n\npress any key and hit enter to exit : ");  
      scanf("%d",&ex);  
      clear();  
      main();  
 }  

 So this is my code... Any doubts? I'd be happy to clarify via comments :)

Comments

Post a Comment

Popular posts from this blog

Beginner's guide to Solving the N-Queens problem using backtracking method

PvP Chain reaction game using Python

Guide to Solving Maximal Subarray Problem using Kadane's Algorithm