Advertisement
BojidarDosev

rocl-papaer-scissors *updated*

Jan 29th, 2025 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Main { public static void main(String[] args) {
  5.  
  6.     Scanner scanner = new Scanner(System.in);
  7.     //create place for saving the number of wins
  8.     int userWins = 0;
  9.     int compWins = 0;
  10.     int numGames = 0;
  11.     boolean gameContinue = false;
  12.     boolean correctAnswer = false;
  13.  
  14.  
  15.     String inputContinue;
  16.     Random rand = new Random();
  17.     System.out.println("Do you want to play best of *number* games: Yes/ No? ");
  18.     String answer = scanner.nextLine();
  19.  
  20.     while(!gameContinue){
  21.  
  22.         if (answer.equals("Yes") || answer.equals("yes")) {
  23.             System.out.println("Choose the number of wins needed");
  24.             numGames = Integer.parseInt(scanner.nextLine());
  25.             System.out.println("Playing best of " + numGames + "!");
  26.         }
  27.         else if (answer.equals("No") || answer.equals("no")) {
  28.             numGames = 1;
  29.         }
  30.         else{
  31.             System.out.println("Choose a correct answer!");
  32.             gameContinue = true;
  33.         }
  34.  
  35.         while (numGames > 0) {
  36.             if(answer.equals("Yes") || answer.equals("yes")) {
  37.                 if(numGames == 1) {
  38.                     System.out.println("Last game!");
  39.                 }
  40.                 else{
  41.                     System.out.println("Number of games remaining: " + numGames);
  42.                 }
  43.             }
  44.  
  45.             System.out.println("Choose [r]ock, [p]aper or [s]cissors: ");
  46.             String input = scanner.nextLine();
  47.             rand = new Random();
  48.             int rand_int = rand.nextInt(3);
  49.             String compInput = switch (rand_int) {
  50.                 case 0 -> "Rock";
  51.                 case 1 -> "Paper";
  52.                 case 2 -> "Scissors";
  53.                 default -> "";
  54.             };
  55.             System.out.println("The computer chose " + compInput + ".");
  56.  
  57.             String combinedInput = input.toLowerCase() + "-" + compInput.toLowerCase();
  58.             switch (combinedInput) {
  59.                 case "paper-scissors":
  60.                 case "rock-paper":
  61.                 case "scissors-rock":
  62.                     System.out.println("You lose.");
  63.                     compWins++;
  64.                     break;
  65.  
  66.                 case "paper-rock":
  67.                 case "rock-scissors":
  68.                 case "scissors-paper":
  69.                     System.out.println("You win.");
  70.                     userWins++;
  71.                     break;
  72.  
  73.                 default:
  74.                     System.out.println("The game was a draw.");
  75.                     compWins++;
  76.                     userWins++;
  77.                     break;
  78.             }
  79.             numGames--;
  80.         }
  81.  
  82.         if (answer.equals("Yes") || answer.equals("yes") || answer.equals("No") || answer.equals("no")) {
  83.             System.out.println("Final score:");
  84.             System.out.println( userWins + " : " + compWins);
  85.             System.out.println("Do you want to play again? :");
  86.             inputContinue = scanner.nextLine();
  87.             if(inputContinue.equals("No") || inputContinue.equals("no")){
  88.                 gameContinue = true;
  89.             }
  90.         }
  91.     }
  92.  }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement