AnindyaBiswas

vending

Jun 9th, 2022 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.math.*;
  3. public class vending {
  4.     public static void main(String[] Args )
  5.     {
  6.         int c = 1;
  7.         System.out.println("\t<---GAME STARTED--->\n");
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         Vend P1 = new Vend();
  11.         Vend P2 = new Vend();
  12.         System.out.println("\nRULES\n\n1. Players have to enter the no. of candies they want in each round.");
  13.         System.out.println("2. Enter '0' to stop the game.");
  14.         System.out.println("3. Enter '1' to surrender.");
  15.         System.out.println("4. The Status indicator represents the amount of candies using 10 '#' signs(actual no. of candies may not be 10).\n\n");
  16.  
  17.         while(!Vend.isEmpty())
  18.         {
  19.             Vend.Status();
  20.             System.out.println("\n\n\t---Round "+ c++ + "---");
  21.             System.out.print("\nPlayer 1, enter no. of candies : ");
  22.             int p1 = sc.nextInt();
  23.             if(p1 == -1 || p1 == 0)
  24.             {
  25.                 if(Vend.StoporSurr(1, p1, P1.candies_drawn, P2.candies_drawn))
  26.                     break;
  27.                 else
  28.                     continue;
  29.             }
  30.             if(P1.Draw(p1))
  31.                 System.out.print("\t You got it!");
  32.             else
  33.                 System.out.print("\t Better luck next time.");
  34.  
  35.             if(Vend.isEmpty())
  36.                 break;
  37.  
  38.             System.out.print("\nPlayer 2, enter no. of candies : ");
  39.             int p2 = sc.nextInt();
  40.             if(p2 == -1 || p2 == 0)
  41.             {
  42.                 if(Vend.StoporSurr(2, p2, P1.candies_drawn, P2.candies_drawn))
  43.                     break;
  44.                 else
  45.                     continue;
  46.             }
  47.  
  48.             if(P2.Draw(p2))
  49.                 System.out.print("\t You got it!");
  50.             else
  51.                 System.out.print("\t Better luck next time.");
  52.            
  53.        
  54.         System.out.print("\n\nPlayer1 candies = " + P1.candies_drawn+", Player2 candies = "+P2.candies_drawn+",");
  55.  
  56.         }
  57.         Vend.Winner(P1.candies_drawn, P2.candies_drawn);
  58.         P1.finalize();  
  59.     }
  60.      
  61.  
  62. }
  63.  
  64. class Vend
  65. {
  66.     final static int cap;
  67.     private static int n;
  68.     int candies_drawn;
  69.  
  70.     static
  71.     {
  72.         cap = (int)(Math.random()*100 + 1);
  73.         n = cap;
  74.     }
  75.  
  76.     Vend()
  77.     {
  78.         candies_drawn = 0;
  79.     }
  80.  
  81.     boolean Draw(int k)
  82.     {
  83.         if(k!=0 && n % k == 0)
  84.         {
  85.             candies_drawn += k;
  86.             n -= k;
  87.             return true;
  88.         }
  89.         return false;
  90.     }
  91.  
  92.     static boolean isEmpty()
  93.     {
  94.         if(n == 0)
  95.             return true;
  96.         return false;
  97.     }
  98.  
  99.     static void Status()
  100.     {
  101.         System.out.print(" Candy Status  <");
  102.  
  103.         int d = (int)(((double)(cap-n)/cap)*10);
  104.        
  105.         for (int i = 10; i >= 1; i--)
  106.         {
  107.             if(i > d)
  108.                 System.out.print("#");
  109.             else
  110.                 System.out.print(".");
  111.         }
  112.         System.out.print(">");
  113.     }
  114.  
  115.     static void Winner(int p1, int p2)
  116.     {
  117.         System.out.println("\n\n\nFINAL POINTS\n\nPlayer 1 : " + p1 + "\nPlayer 2 : " + p2);
  118.         if(p1 > p2)
  119.             System.out.println("\n\tPlayer 1, WON!!!");
  120.         else if(p2 > p1)
  121.             System.out.println("\n\tPlayer 2, WON!!!");
  122.         else
  123.             System.out.println("\n\tDRAW!");
  124.     }
  125.  
  126.     static boolean StoporSurr(int p, int option, int p1, int p2)
  127.     {
  128.         Scanner sc = new Scanner(System.in);
  129.         char ans;
  130.         switch(option)
  131.         {
  132.             case 0:
  133.             System.out.print("\n\tDoes Player " + (p%2+1) +" agree to STOP the game?(y/n)");
  134.             ans = sc.nextLine().toLowerCase().charAt(0);
  135.             if(ans == 'y')
  136.                 return true;
  137.             break;
  138.  
  139.             case -1:
  140.             System.out.print("\n\tConfirm SURRENDER, Player " + p + " ?(y/n)");
  141.             ans = sc.nextLine().toLowerCase().charAt(0);
  142.             if(ans == 'y')
  143.             {
  144.                 System.out.println("\n\n\nFINAL POINTS\n\nPlayer 1 : " + p1 + "\nPlayer 2 : " + p2);
  145.                 System.out.println("\nPlayer "+ p + ", Surrendered, so ......");
  146.                 System.out.println("\n\tPlayer "+ (p%2+1) +", WON!!!");
  147.                 System.exit(0);
  148.             }
  149.             break;
  150.         }
  151.         return false;
  152.     }
  153.  
  154.     protected void finalize()
  155.     {
  156.         System.out.println("\n\t---END OF GAME---");
  157.         System.gc();
  158.     }
  159. }
Add Comment
Please, Sign In to add comment