SOURCE CODE
#include<conio.h>
#include<time.h>
#include<stdio.h>
int score()
{
int run;
run=rand()%8;
if(run<4)
printf("\n\nYou Score %d runs ",run);
else
switch(run)
{
case 4:
printf("\n\nGood Shot! You hit a boundanry ");
break;
case 5:
run=0 ;
printf("\n\nYou Score %d runs ",run);
break;
case 6 :
printf("\n\nGreat Shot !! That's a huge hit ");
break;
case 7:
printf("\n\nOOPs !! You are out");
break;
}
return run;
}
int bat1(int e,char *c,int check)
{
int i,orig,totalruns=0,we;
clrscr();
printf("%s Get ready to Bat \nPress any key to hit the Ball",c);
e=e*6;
for(i=1;i<=e;i++)
{
getch();
if(check<totalruns)
return totalruns;
orig=score();
if(orig==7)
break;
else
{
totalruns=totalruns+orig;
printf(" Your total score is %d",totalruns);
}
}
getch();
return totalruns;
}
void con(int we,int ew,char *c,char *d)
{
if(we<ew)
printf("\n\n\t\t\t*******Congratulation*******\n %s has won the game \nScore of %s is %d\nScore of %s is %d",d,c,we,d,ew);
else if(we>ew)
{
printf("\n\nTotal score of Player %s is %d",d,ew);
printf("\n\n\t\t\t\t*******Congratulation******* \n%s has won the game\nScore of %s is %d\nScore of %s is %d",c,c,we,d,ew);
}
else
{
printf("\n\nTotal score of Player %s is %d",d,ew);
printf("\n\n\t\t\t\t*******GAME HAS DRWRAN********\nScore of %s is %d\nScore of %s is %d",c,we,d,ew);
}
}
void select()
{
int yu,e,i,toss,y,aso,we=1000,ew=1000;
char player1[50],player2[50];
time_t po;
(unsigned) time(&po);
srand(po);
toss=rand()%2;
clrscr();
printf("\nEnter First Players Name ");
scanf("%s",player1);
printf("\n\nEnter Second Players Name ");
scanf("%s",player2);
printf("\nHow many overs to play\n");
scanf("%d",&e);
for(yu=0;yu>=80;yu++)
printf("=");
printf("\n\nNow its time for toss\n\nPlayer %s Please Choose heads or Tails (Press 1 for heads and 2 for tails)",player1);
scanf("%d",&aso);
if(toss==0)
{
printf("%s Has Won The Toss",player1);
printf(" Select \n1.)Batting \t\t\t\tpress 1 \n2.)Bowling\t\t\t\tpress 2");
scanf("%d",&y);
switch(y)
{
case 1:
we=bat1(e,player1,ew);
ew=bat1(e,player2,we);
con(we,ew,player1,player2);
break;
case 2:
we=bat1(e,player2,ew);
ew=bat1(e,player1,we);
con(we,ew,player2,player1);
break;
default :
printf("\n\nSorry wrong choice");
break;
}
}
else
{
printf("%s Has Won The Toss",player2);
printf(" Select \n1.)Batting \t\t\t\tpress 1 \n2.)Bowling\t\t\t\tpress 2");
scanf("%d",&y);
switch(y)
{
case 1:
we=bat1(e,player2,ew);
ew=bat1(e,player1,we);
con(we,ew,player2,player1);
break;
case 2:
we=bat1(e,player1,ew);
ew=bat1(e,player2,we);
con(we,ew,player1,player2);
default :
printf("\n\nSorry wrong choice");
break;
}
}
}
void main()
{
int ret;
char ch;
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 CRICKET WORLD CUP 2016");
printf("\n\n\n\npress any key to continue");
getch();
while(1)
{
clrscr();
printf("\n\n\t\t\tCRICKET WORLD CUP 2016");
printf("\n\n\n\t1.) START\t\t\tpress 1\n\n\t2.) INSTRUCTIONS\t\tpress 2\n\n\t3.) EXIT\t\t\tpress 3");
scanf("%d",&ret);
switch(ret)
{
case 1:
clrscr();
select();
break;
case 2:
clrscr();
printf("\t\t\t\tHOW TO PLAY\nWhen game starts you have to press any key ,when you press any key computer ");
printf("will generate random number and that random number will be your runs. When 7 number is generated than you ");
printf("get out so the game totaly depends on your luck. \n\n \n\t\t\t\tENJOY THE GAME\n press any key to continue..............");
getch();
break;
case 3:
exit(0);
default:
printf("\nSorry Wrong Choice");
break;
}
}
}
#include<time.h>
#include<stdio.h>
int score()
{
int run;
run=rand()%8;
if(run<4)
printf("\n\nYou Score %d runs ",run);
else
switch(run)
{
case 4:
printf("\n\nGood Shot! You hit a boundanry ");
break;
case 5:
run=0 ;
printf("\n\nYou Score %d runs ",run);
break;
case 6 :
printf("\n\nGreat Shot !! That's a huge hit ");
break;
case 7:
printf("\n\nOOPs !! You are out");
break;
}
return run;
}
int bat1(int e,char *c,int check)
{
int i,orig,totalruns=0,we;
clrscr();
printf("%s Get ready to Bat \nPress any key to hit the Ball",c);
e=e*6;
for(i=1;i<=e;i++)
{
getch();
if(check<totalruns)
return totalruns;
orig=score();
if(orig==7)
break;
else
{
totalruns=totalruns+orig;
printf(" Your total score is %d",totalruns);
}
}
getch();
return totalruns;
}
void con(int we,int ew,char *c,char *d)
{
if(we<ew)
printf("\n\n\t\t\t*******Congratulation*******\n %s has won the game \nScore of %s is %d\nScore of %s is %d",d,c,we,d,ew);
else if(we>ew)
{
printf("\n\nTotal score of Player %s is %d",d,ew);
printf("\n\n\t\t\t\t*******Congratulation******* \n%s has won the game\nScore of %s is %d\nScore of %s is %d",c,c,we,d,ew);
}
else
{
printf("\n\nTotal score of Player %s is %d",d,ew);
printf("\n\n\t\t\t\t*******GAME HAS DRWRAN********\nScore of %s is %d\nScore of %s is %d",c,we,d,ew);
}
}
void select()
{
int yu,e,i,toss,y,aso,we=1000,ew=1000;
char player1[50],player2[50];
time_t po;
(unsigned) time(&po);
srand(po);
toss=rand()%2;
clrscr();
printf("\nEnter First Players Name ");
scanf("%s",player1);
printf("\n\nEnter Second Players Name ");
scanf("%s",player2);
printf("\nHow many overs to play\n");
scanf("%d",&e);
for(yu=0;yu>=80;yu++)
printf("=");
printf("\n\nNow its time for toss\n\nPlayer %s Please Choose heads or Tails (Press 1 for heads and 2 for tails)",player1);
scanf("%d",&aso);
if(toss==0)
{
printf("%s Has Won The Toss",player1);
printf(" Select \n1.)Batting \t\t\t\tpress 1 \n2.)Bowling\t\t\t\tpress 2");
scanf("%d",&y);
switch(y)
{
case 1:
we=bat1(e,player1,ew);
ew=bat1(e,player2,we);
con(we,ew,player1,player2);
break;
case 2:
we=bat1(e,player2,ew);
ew=bat1(e,player1,we);
con(we,ew,player2,player1);
break;
default :
printf("\n\nSorry wrong choice");
break;
}
}
else
{
printf("%s Has Won The Toss",player2);
printf(" Select \n1.)Batting \t\t\t\tpress 1 \n2.)Bowling\t\t\t\tpress 2");
scanf("%d",&y);
switch(y)
{
case 1:
we=bat1(e,player2,ew);
ew=bat1(e,player1,we);
con(we,ew,player2,player1);
break;
case 2:
we=bat1(e,player1,ew);
ew=bat1(e,player2,we);
con(we,ew,player1,player2);
default :
printf("\n\nSorry wrong choice");
break;
}
}
}
void main()
{
int ret;
char ch;
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 CRICKET WORLD CUP 2016");
printf("\n\n\n\npress any key to continue");
getch();
while(1)
{
clrscr();
printf("\n\n\t\t\tCRICKET WORLD CUP 2016");
printf("\n\n\n\t1.) START\t\t\tpress 1\n\n\t2.) INSTRUCTIONS\t\tpress 2\n\n\t3.) EXIT\t\t\tpress 3");
scanf("%d",&ret);
switch(ret)
{
case 1:
clrscr();
select();
break;
case 2:
clrscr();
printf("\t\t\t\tHOW TO PLAY\nWhen game starts you have to press any key ,when you press any key computer ");
printf("will generate random number and that random number will be your runs. When 7 number is generated than you ");
printf("get out so the game totaly depends on your luck. \n\n \n\t\t\t\tENJOY THE GAME\n press any key to continue..............");
getch();
break;
case 3:
exit(0);
default:
printf("\nSorry Wrong Choice");
break;
}
}
}
EXPLANATION
Let start with select() function
Whenever game starts first select() function
is called. At first toss randomly
gets value either 0 or 1, now we enter our names which are stored in variable
player1 and player2 and then enter number of over’s which is stored in e. Now
when you press 1 or 2 for toss than condition is checked (if(toss==0)) ,
nothing is related to head or tails computer randomly choose who wins the toss
.Now suppose player 1 wins the toss and let he/she select batting than case 1
will execute and bat1() function is called
bat1()
Three parameters are passed to the
function i.e. number of over’s, name of player 1 and ew(player 2 score).There
is no need of ew in first inning but as we are using bat1() function for
batting of both the players and not making any separate function for batting of
second player therefore we have passed ew with initial value 1000 so that
condition if(check<totalrun) always become false in first innings .
In this function number of over’s
are multiplied by six, for loop will execute for that much times. Now getch()
function is called it simply holds the screen until you press any key . When
you press any key than score() function
is called.
score()
This is the main part of our
program . Now random numbers are generated between 0-7 using rand() function
which are stored in run .We will discuss later how random numbers are generated
.If you will see the program than you find that I have not taken case 1,case
2,etc instead I have started from case 4 ,this is because when value of run
become 0,1,2,3 than same line get printed so it is only to make the program
more compact.
bat1() function returns total runs
which is stored in we and batting of second player starts .In batting of player
2, loop will break in three condition i.e. either over’s are finished or it
gets out or it score more runs than player 1 .now when player 2’s batting is
over than con() function is called .
con()
Four parameters are passed to the
function i.e. player 1’s name and score and player 2’s name and score.
By comparing the scores it decides
who won the game and print it on the screen.
In this way you can see other
cases also.
Now let us understand how random
numbers are generated in C language?
To generate random numbers in C
you can use rand() function that produces a random number in the rang 0 to
RAND-MAX .The RAND-MAX is defined in stdlib.h which gives you the maximum value
returned by rand(). Consider the following code that generates five random
numbers:
#include<stdio.h>
void main()
{
int a,i;
for(i=0;i<5;i++)
{
a=rand();
printf("%d\n",a);
}
getch();
}
Re-Execute the above code you will
get the same numbers ,that means random numbers are being repeated .If you want
to generate different random numbers every time , use srand() function .The
srand() function starts or seeds the random number generator from a point that
depends on the value we pass to srand () as argument. Therefore to generate
different random numbers every time, you should seed the generator with truly
variable value such as system time.
Now if you change the above code
as given below, you will get different random numbers every time you execute
the code.
#include<stdio.h>
#include<time.h> //To obtain system time through time_t
void main()
{
int a,i;
time_t t; //t is time type variable now
clrscr();
(unsigned) time(&t); //time(&t) function will initialize time variable t with
system time .
srand(t); //seeds the random number
generator
for(i=0;i<5;i++)
{
a=rand();
printf("%d",a);
}
getch();
Remember that srand() should be
invoked just once prior to the loop and not every time before generating a
random number through rand().In my program I have invoked srand() only once
during toss and after that I have not invoked it in any function.
In my program I have use s=rand()%8+1
; so that I can generate random numbers within range 1-8.
If you still not get the concept
of random numbers than don’t worry I also found some difficulty in starting but
with time all things become clear, just remember above codes.
Any questions regarding to program
please write in comments.
why you take system time in t??
ReplyDeletetime() returns the current time in seconds, elapsed since 00:00:00 GMT, JANUARY 1, 1970. Now to generate different random numbers every time, we have to pass a truly variable value (such as system time) to
Deletesrand() as argument ,which I have already explained above .Now I take system time in t and pass it as argument in srand() and set the random number generator to a new starting point.
how to make single player with difficulty levels also
ReplyDelete