theadarshmehta

Soham's Hand Cricket Java Program

Jul 13th, 2017
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. class HandCricket
  3. {
  4.     static Scanner x= new Scanner(System.in);
  5.     public static void Cricket()
  6.     {
  7.         int p1, p12, total = 0, total2 = 0, control = 0, control2 = 0;
  8.         double cpud, cpud2;
  9.         int cpu, cpu2;
  10.  
  11.         System.out.println("You have selected the option for Hand Cricket. It is a very popular game among kids in India. Rules of the games are as follows");
  12.         System.out.println("This is a player vs computer game. Both will get a chance to bat. You have to enter a number between 1 and 6. At the same time the computer will generate a number.");
  13.         System.out.println("If the number you enter is the same as the number generated by the computer then you will be declared out. If not then the number will be added to your total score.");
  14.         System.out.println("This will be repeated until you get out. Once you get out the same process is repeated but you will be bowling. Whoever manages to score more runs wins.");
  15.         System.out.println("Any number larger than 6 or smaller than 1 will not be considered and runs will not be counted while batting or bowling");
  16.         System.out.println("Begin by entering a number.");
  17.  
  18.         do
  19.         {
  20.             p1 = x.nextInt();
  21.             cpud = Math.random()*(6-1)+1;
  22.             cpu = (int)Math.rint(cpud);
  23.  
  24.             if( p1>=6 || p1<1 )
  25.             {
  26.                 System.out.println("Not a number between 1 and 6. Please try again.");
  27.             }
  28.  
  29.             else
  30.             {
  31.                 System.out.println("You- " + p1 +". Computer- " + cpu);
  32.                 if( p1 == cpu )
  33.                 {
  34.                     System.out.println("You are out");
  35.                     control++;
  36.                 }
  37.  
  38.                 else
  39.                     total+= p1;
  40.             }
  41.  
  42.         }while(control == 0);
  43.  
  44.         System.out.println("Your total score is = " +total);
  45.         System.out.println("Your chance to bowl");
  46.  
  47.         do
  48.         {
  49.             p12 = x.nextInt();
  50.             cpud2 = Math.random()*(6-1)+1;
  51.             cpu2 = (int)Math.rint(cpud2);
  52.  
  53.             System.out.println("You- " + p12 +". Computer- " + cpu2);
  54.  
  55.             if( p12>=6 || p12<1 )
  56.             {
  57.                 System.out.println("Not a number between 1 and 6. Please try again.");
  58.             }
  59.  
  60.             else
  61.             {
  62.                 if( p12 == cpu2 )
  63.                 {
  64.                     System.out.println("Computer is out");
  65.                     control2++;
  66.                 }
  67.  
  68.                 else
  69.                     total2+= cpu2;
  70.             }
  71.  
  72.             if(total2 > total)
  73.                 break;
  74.  
  75.         }while(control2 == 0);
  76.  
  77.         System.out.println("Computers total score is = " +total2);
  78.  
  79.         if(total > total2)
  80.             System.out.println("You win");
  81.  
  82.         else if(total < total2)
  83.             System.out.println("You lose");
  84.  
  85.         else
  86.             System.out.println("It's a draw");
  87.     }
  88. }
RAW Paste Data Copied