HAND CRICKET GAME JAVASCRIPT
by:- Anshuman Pattnaik
import java.util.*;
class Hc //product of www.knowtechnology.online
{ //Hc=Hand Cricket
static int ch,n,scrb,np,nc;
static char choose;
void org()
{
Scanner sc= new Scanner(System.in);
char ch;
System.out.print("Enter 'T' to toss ");
ch=sc.next().charAt(0);
choose=ch;
if(ch=='t'||ch=='T')
{
double r;
r=Math.random();
if(r>=0.1&&r<=0.5)
{
this.ch=0;
System.out.println("you get batting!");
//0=batting
}
else
{
this.ch=1; //1=balling
System.out.println("you get balling!");
}
}
else
System.out.println("False Entry!");
}
void input()
{
Scanner sc= new Scanner(System.in);
scrb=0;
System.out.print("Enter no of balls you want to play:-");
n=sc.nextInt();
System.out.println("You mean "+(n/6)+" overs");
np=1;
nc=1;
}
int player()
{
Scanner sc=new Scanner(System.in);
int n;
System.out.print("You'r turn:-");
n=sc.nextInt();
return n;
}
int computer()
{
double r;
int val;
r=Math.random();
if(r>=0.1&&r<0.2)
val=1;
else if(r>=0.2&&r<0.3)
val=2;
else if(r>=0.3&&r<0.4)
val=3;
else if(r>=0.4&&r<0.5)
val=4;
else if(r>=0.5&&r<0.6)
val=5;
else
val=6;
return val;
}
void control(int ip,int ic)
{
if(ch==0)
{
if(ip!=ic)
{
scrb=scrb+ip;
}
else
{
np--;
}
}
else if(ch==1)
{
if(ip!=ic)
{
scrb=scrb+ic;
}
else
{
nc--;
}
}
}
void display()
{
if(ch==0)
{
System.out.println("Your Total Score:- "+scrb);
}
else if(ch==1)
{
System.out.println("Computer Total Score:- "+scrb);
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Hc mm = new Hc();
mm.org();
mm.input();
int i,ip,ic;
if(choose=='t'||choose=='T')
{
//main loop
for(i=1;i<=n;i++)
{
if(np>0)
{
ip=mm.player();
ic=mm.computer();
System.out.println("Computer's Turn:- "+ic);
mm.control(ip,ic);
}
else
{
break;
}
}
if(I<n)
{
if(ch==0)
{
System.out.println("Sorry you are out!");
System.out.println("Your Total Score:- "+scrb);
System.out.println("Overs left:- "+(n-(i/6)));
}
else if(ch==1)
{
System.out.println("Well done! Computer is out!");
System.out.println("Computer Total Score:- "+scrb);
System.out.println("Overs left:- "+(n-(i/6)));
}
}
else
{
mm.display();
}
}
System.out.println("Thank You! by Anshuman Pattnaik");
}
}
OUTPUT:-


0 Comments
Please follow the moderation policy before writing annotations