theadarshmehta

Hand Cricket Project In Java (BlueJ)

Jun 29th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.05 KB | None | 0 0
  1. // Program written by The Adarsh Mehta
  2. // www.theadarshmehta.in
  3.  
  4. import java.io.*;
  5.  
  6. public class Playing_Hand_Cricket_Against_Computer
  7.  
  8. {
  9.  
  10.     public static void main(String args[])throws IOException
  11.    
  12.     {
  13.        
  14.         InputStreamReader isr=new InputStreamReader(System.in);
  15.         BufferedReader br=new BufferedReader(isr);
  16.        
  17.         String name; // to accept the player's name;
  18.         int aa; // converting the random real number generated by 'rr' into a random whole number run for the computer;
  19.         int ii; // used in for loops;
  20.         int bb; // to accept the player's number;
  21.         double rr; // to generate a random real number;
  22.         int cheat; // to end the game if wrong input is entered; to keep a check whilst the game is running as to whether the game has ended or not;
  23.         int score1; // to store the latest runs scored by the player;
  24.         int score2; // to store the latest runs scored by the computer;
  25.         int c; // used in for loops to create a specific design;
  26.         int choice; // used to decide whether the game needs to be ended or restarted;
  27.         int infinity=1; // used to keep the game running until the player decides to end the game;
  28.        
  29.         /* After referring to all the comments mentioned above,
  30.          * you must be able to understand the working of this program as it is very easy.
  31.          * If you find it complicated,
  32.          * feel free to visit www.theadarshmehta.in
  33.          * and contact The Adarsh Mehta.
  34.          * Have fun!
  35.          */
  36.        
  37.         while(infinity>0)
  38.         {
  39.            
  40.             name=" ";
  41.             aa=0;
  42.             ii=0;
  43.             bb=0;
  44.             rr=0;
  45.             cheat=0;
  46.             score1=0;
  47.             score2=0;
  48.             c=0;
  49.             choice=0;
  50.             infinity=1;
  51.            
  52.             System.out.println("=======================");
  53.             System.out.println("| WELCOME TO THE GAME |");
  54.             System.out.println("=======================");
  55.             System.out.println("");
  56.             System.out.print("Enter Your Name: ");
  57.             name=br.readLine();
  58.            
  59.                 for(c=0;c<=2;c++)
  60.                 {
  61.                     System.out.println("====================================");
  62.                 }
  63.                
  64.                 System.out.println("Hello "+name+",");
  65.                 System.out.println(".");
  66.                 System.out.println("WELCOME TO THE CRICKET-MANIA");
  67.                 System.out.println(".");
  68.                 System.out.println("Instructions:");
  69.                 System.out.println(".");
  70.                 System.out.println("This Game Is Similar To The Hand-Cricket Game Played By Kids.");
  71.                 System.out.println(".");
  72.                 System.out.println("There Is No Limit On The Number Of Balls.");
  73.                 System.out.println(".");
  74.                 System.out.println("You Will First Bat. Enter Any Number From 1 To 10.");
  75.                 System.out.println(".");
  76.                 System.out.println("Enter The Numbers Carefully Without Making Any Mistake.");
  77.                 System.out.println(".");
  78.                 System.out.println("If The Number Entered By The Computer Is Same As The Number Entered By You,");
  79.                 System.out.println("You Will Be Declared As Out.");
  80.                 System.out.println(".");
  81.                 System.out.println("Your Final Score Will Be The Sum Of The Numbers You Entered Before Getting Out.");
  82.                 System.out.println(".");
  83.                 System.out.println("After Getting Out, You Will Have To Bowl.");
  84.                 System.out.println(".");
  85.                 System.out.println("Try To Enter A Number Similar To The Number Entered By The Computer.");
  86.                 System.out.println(".");
  87.                 System.out.println("If The Sum Of Numbers Entered By The Computer Becomes More Than Your Score, It Will Win.");
  88.                 System.out.println(".");
  89.                 System.out.println("But If You Enter A Number Same As The Number Entered By The Computer, You Can Win!");
  90.                 System.out.println(".");
  91.                
  92.                 for(c=0;c<=2;c++)
  93.                 {
  94.                     System.out.println("====================================");
  95.                 }
  96.                
  97.                 System.out.println("YOU ARE BATTING:");
  98.                
  99.                 for(c=0;c<=2;c++) { System.out.println("===================================="); } for(ii=1;ii>0;ii++)
  100.                 {
  101.                     System.out.println("");
  102.                     System.out.print("Enter Your Number: ");
  103.                     bb=Integer.parseInt(br.readLine());
  104.                     rr=((Math.random())*10);
  105.                     rr=rr+1;
  106.                     aa=(int)(rr);
  107.                     System.out.println("");
  108.                     System.out.println("Computer's Number: "+aa);
  109.                     System.out.println("");
  110.                     System.out.println("**********");
  111.                     System.out.println("");
  112.  
  113.                     if(bb==aa)
  114.                     {
  115.                         System.out.println(" ");
  116.                         System.out.println("HOWZATTT!!!!!!");
  117.                         System.out.println(" ");
  118.                         System.out.println("################");
  119.                         System.out.println("################");
  120.                         System.out.println("YOUR FINAL SCORE = "+score1);
  121.                         System.out.println("################");
  122.                         System.out.println("################");
  123.                         System.out.println(" ");
  124.                         break;
  125.                     }
  126.                    
  127.                     else if(bb>0&&bb<=10) { score1=score1+bb; } else if(bb>10||bb<=0)
  128.                     {
  129.                         for(c=0;c<=2;c++)
  130.                         {
  131.                             System.out.println("====================================");
  132.                         }
  133.                         System.out.println("You have either tried to cheat or entered a wrong input. Game Over!");
  134.                         cheat++;
  135.                         break;
  136.                     }
  137.  
  138.                     System.out.println("Your Current Score Is "+score1);
  139.                     System.out.println("");
  140.                     System.out.println("**********");
  141.                 }
  142.                
  143.                 for(c=0;c<=2;c++) { if(cheat>0)
  144.                     {
  145.                         break;
  146.                     }
  147.                     System.out.println("====================================");
  148.                 }
  149.                
  150.                 for(c=0;c<1;c++) { if(cheat>0)
  151.                     {
  152.                         break;
  153.                     }
  154.                     System.out.println("YOU ARE BOWLING:");
  155.                 }
  156.                
  157.                 for(c=0;c<=2;c++) { if(cheat>0)
  158.                     {
  159.                         break;
  160.                     }  
  161.                     System.out.println("====================================");
  162.                 }
  163.                
  164.                 for(ii=1;ii>0;ii++)
  165.                 {
  166.                     if(cheat>0)
  167.                     {
  168.                         break;
  169.                     }
  170.                    
  171.                     System.out.println("");
  172.                     System.out.print("Enter Your Number: ");
  173.                     bb=Integer.parseInt(br.readLine());
  174.                     rr=((Math.random())*10);
  175.                     rr=rr+1;
  176.                     aa=(int)(rr);
  177.                     System.out.println("");
  178.                     System.out.println("Computer's Number: "+aa);
  179.                     System.out.println("");
  180.                     System.out.println("**********");
  181.                     System.out.println("");
  182.                    
  183.                     if(aa==bb)
  184.                     {
  185.                         System.out.println(" ");
  186.                         System.out.println("COMPUTER IS OUTT!!!!");
  187.                         System.out.println(" ");
  188.                         System.out.println("################");
  189.                         System.out.println("################");
  190.                         System.out.println("COMPUTER'S FINAL SCORE = "+score2);
  191.                         System.out.println("################");
  192.                         System.out.println("################");
  193.                         System.out.println(" ");
  194.                         break;
  195.                     }
  196.                    
  197.                     else if(bb>0&&bb<=10) { score2=score2+aa; } else if(bb>10||bb<=0)
  198.                     {
  199.                         for(c=0;c<=2;c++) { System.out.println("===================================="); } System.out.println("You have either tried to cheat or entered a wrong input. Game Over!"); cheat++; break; } if(score2>score1)
  200.                     {
  201.                         System.out.println(" ");
  202.                         System.out.println("THE COMPUTER HAS SCORED MORE THAN YOUU!!!");
  203.                         System.out.println(" ");
  204.                         System.out.println("################");
  205.                         System.out.println("################");
  206.                         System.out.println("COMPUTER'S FINAL SCORE = "+score2);
  207.                         System.out.println("################");
  208.                         System.out.println("################");
  209.                         System.out.println(" ");
  210.                         break;
  211.                     }
  212.                    
  213.                     System.out.println("Computer's Current Score Is "+score2);
  214.                     System.out.println("");
  215.                     System.out.println("**********");
  216.                 }
  217.                
  218.                 for(c=0;c<=2;c++) { if(cheat>0)
  219.                     {
  220.                         break;
  221.                     }
  222.                    
  223.                     System.out.println("====================================");
  224.                 }
  225.                
  226.                 for(ii=0;ii<1;ii++) { if(cheat>0)
  227.                     {
  228.                         break;
  229.                     }
  230.                    
  231.                     System.out.println(" ");
  232.                     System.out.println("################");
  233.                     System.out.println("################");
  234.                     System.out.println("YOUR FINAL SCORE = "+score1);
  235.                     System.out.println("COMPUTER'S FINAL SCORE = "+score2);
  236.                     System.out.println("################");
  237.                     System.out.println("################");
  238.                     System.out.println(" ");
  239.  
  240.                     if((score1)>(score2))
  241.                     {
  242.                         System.out.println("Congratulations "+name+"!! You Have Defeated The Almighty, Computer!!");
  243.                     }
  244.                    
  245.                     else if((score1)<(score2))
  246.                     {
  247.                         System.out.println("Sorry "+name+", But The Computer Has Defeated You..!");
  248.                     }
  249.                    
  250.                     else
  251.                     {
  252.                         System.out.println("It's a Tie..! :(");
  253.                     }
  254.                    
  255.                 }
  256.                
  257.                 for(c=0;c<=2;c++)
  258.                 {
  259.                     System.out.println("====================================");
  260.                 }
  261.            
  262.             System.out.println("");
  263.             System.out.println("Enter 1 To Play This Game Again.");
  264.             System.out.println("");
  265.             System.out.println("Enter 0 Or Any Other Number To Quit.");
  266.             choice=Integer.parseInt(br.readLine());
  267.             System.out.println("");
  268.            
  269.             if(choice==1)
  270.             {
  271.                 System.out.print('\f');
  272.             }
  273.            
  274.             else
  275.             {
  276.                 System.out.print('\f');
  277.                 System.out.println("Good Bye!");
  278.                 break;
  279.             }
  280.            
  281.         }
  282.     }
  283. }
  284.  
  285. // Program written by The Adarsh Mehta
  286. // www.theadarshmehta.in
Add Comment
Please, Sign In to add comment