Advertisement
SenpaiZero

ROCK PAPER SCISSOR

Feb 29th, 2024
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class rock_paper_scissor
  5. {
  6.     private int winsToWinTheMatch;
  7.     public rock_paper_scissor(int winsToWinTheMatch)
  8.     {
  9.         this.winsToWinTheMatch = winsToWinTheMatch;
  10.     }
  11.     public void start()
  12.     {
  13.         System.out.println("WELCOME TO ROCK, PAPER AND SCISSOR!");
  14.         gameplay();
  15.     }
  16.    
  17.     private void gameplay()
  18.     {
  19.         int score = 0, enemyScore = 0, enemyChoice, userChoice;
  20.         Random random = new Random();
  21.         Scanner scanner = new Scanner(System.in);
  22.        
  23.         do
  24.         {
  25.             enemyChoice = random.nextInt(1, 4);
  26.             System.out.println("Please Choose \n[1] ROCK\n[2] Paper\n[3] SCISSOR\n");
  27.             System.out.print("Enter: ");
  28.             String choice = scanner.nextLine();
  29.             String winner = checkWinner(getWeapon(Integer.valueOf(choice)), getWeapon(Integer.valueOf(enemyChoice)));
  30.             if(winner.equals("PLAYER"))
  31.             {
  32.                 System.out.println("Player is the winner");
  33.                 score++;
  34.             }
  35.             else if(winner.equals("ENEMY"))
  36.             {
  37.                 System.out.println("Enemy is the winner");
  38.                 enemyScore++;
  39.             }
  40.             else {
  41.                 System.out.println("TIE");
  42.             }
  43.            
  44.             System.out.println("\nCURENT SCORE ");
  45.             System.out.println("Player: " + score);
  46.             System.out.println("Enemy: " + enemyScore);
  47.             System.out.println("==========================================\n");
  48.            
  49.             if(winsToWinTheMatch <= score)
  50.             {
  51.                 System.out.println("PLAYER IS THE WINNER OF THE MATCH");
  52.                 break;
  53.             }
  54.             else if(winsToWinTheMatch <= enemyScore)
  55.             {
  56.                 System.out.println("ENEMY IS THE WINNER OF THE MATCH");
  57.                 break;
  58.             }
  59.         } while(true);
  60.     }
  61.    
  62.     private String getWeapon(int weapon)
  63.     {
  64.         if(weapon == 1)
  65.             return "ROCK";
  66.         else if(weapon == 2)
  67.             return "PAPER";
  68.         else if(weapon == 3)
  69.             return "SCISSOR";
  70.         else
  71.             return "ERROR";
  72.     }
  73.     private String checkWinner(String playerChoice, String enemyChoice)
  74.     {
  75.         if(playerChoice.equalsIgnoreCase(enemyChoice))
  76.             return "TIE";
  77.         else if(playerChoice.equals("ROCK") && enemyChoice.equals("PAPER"))
  78.             return "ENEMY";
  79.         else if(playerChoice.equals("SCISSOR") && enemyChoice.equals("ROCK"))
  80.             return "ENEMY";
  81.         else if(playerChoice.equals("PAPER") && enemyChoice.equals("SCISSOR"))
  82.             return "ENEMY";
  83.         else
  84.             return "PLAYER";
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement