Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.math.*;
- public class vending {
- public static void main(String[] Args )
- {
- int c = 1;
- System.out.println("\t<---GAME STARTED--->\n");
- Scanner sc = new Scanner(System.in);
- Vend P1 = new Vend();
- Vend P2 = new Vend();
- System.out.println("\nRULES\n\n1. Players have to enter the no. of candies they want in each round.");
- System.out.println("2. Enter '0' to stop the game.");
- System.out.println("3. Enter '1' to surrender.");
- 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");
- while(!Vend.isEmpty())
- {
- Vend.Status();
- System.out.println("\n\n\t---Round "+ c++ + "---");
- System.out.print("\nPlayer 1, enter no. of candies : ");
- int p1 = sc.nextInt();
- if(p1 == -1 || p1 == 0)
- {
- if(Vend.StoporSurr(1, p1, P1.candies_drawn, P2.candies_drawn))
- break;
- else
- continue;
- }
- if(P1.Draw(p1))
- System.out.print("\t You got it!");
- else
- System.out.print("\t Better luck next time.");
- if(Vend.isEmpty())
- break;
- System.out.print("\nPlayer 2, enter no. of candies : ");
- int p2 = sc.nextInt();
- if(p2 == -1 || p2 == 0)
- {
- if(Vend.StoporSurr(2, p2, P1.candies_drawn, P2.candies_drawn))
- break;
- else
- continue;
- }
- if(P2.Draw(p2))
- System.out.print("\t You got it!");
- else
- System.out.print("\t Better luck next time.");
- System.out.print("\n\nPlayer1 candies = " + P1.candies_drawn+", Player2 candies = "+P2.candies_drawn+",");
- }
- Vend.Winner(P1.candies_drawn, P2.candies_drawn);
- P1.finalize();
- }
- }
- class Vend
- {
- final static int cap;
- private static int n;
- int candies_drawn;
- static
- {
- cap = (int)(Math.random()*100 + 1);
- n = cap;
- }
- Vend()
- {
- candies_drawn = 0;
- }
- boolean Draw(int k)
- {
- if(k!=0 && n % k == 0)
- {
- candies_drawn += k;
- n -= k;
- return true;
- }
- return false;
- }
- static boolean isEmpty()
- {
- if(n == 0)
- return true;
- return false;
- }
- static void Status()
- {
- System.out.print(" Candy Status <");
- int d = (int)(((double)(cap-n)/cap)*10);
- for (int i = 10; i >= 1; i--)
- {
- if(i > d)
- System.out.print("#");
- else
- System.out.print(".");
- }
- System.out.print(">");
- }
- static void Winner(int p1, int p2)
- {
- System.out.println("\n\n\nFINAL POINTS\n\nPlayer 1 : " + p1 + "\nPlayer 2 : " + p2);
- if(p1 > p2)
- System.out.println("\n\tPlayer 1, WON!!!");
- else if(p2 > p1)
- System.out.println("\n\tPlayer 2, WON!!!");
- else
- System.out.println("\n\tDRAW!");
- }
- static boolean StoporSurr(int p, int option, int p1, int p2)
- {
- Scanner sc = new Scanner(System.in);
- char ans;
- switch(option)
- {
- case 0:
- System.out.print("\n\tDoes Player " + (p%2+1) +" agree to STOP the game?(y/n)");
- ans = sc.nextLine().toLowerCase().charAt(0);
- if(ans == 'y')
- return true;
- break;
- case -1:
- System.out.print("\n\tConfirm SURRENDER, Player " + p + " ?(y/n)");
- ans = sc.nextLine().toLowerCase().charAt(0);
- if(ans == 'y')
- {
- System.out.println("\n\n\nFINAL POINTS\n\nPlayer 1 : " + p1 + "\nPlayer 2 : " + p2);
- System.out.println("\nPlayer "+ p + ", Surrendered, so ......");
- System.out.println("\n\tPlayer "+ (p%2+1) +", WON!!!");
- System.exit(0);
- }
- break;
- }
- return false;
- }
- protected void finalize()
- {
- System.out.println("\n\t---END OF GAME---");
- System.gc();
- }
- }
Add Comment
Please, Sign In to add comment