MrThoe

4.7.11

Oct 13th, 2022
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. public class RockPaperScissors extends ConsoleProgram
  2. {
  3.     private static final String USER_PLAYER = "User wins!";
  4.     private static final String COMPUTER_PLAYER = "Computer wins!";
  5.     private static final String TIE = "Tie";
  6.      
  7.     /**
  8.      * Function: getWinner
  9.      *
  10.      * Parameters:
  11.      * String user - a string holding the choice that the user player
  12.      * made. Must either be "rock", "paper", or "scissors"
  13.      * String computer - a string holding the choice that the computer
  14.      * player made. Must either be "rock", "paper", or "scissors"
  15.      *
  16.      * Determines the winner based on the rules of rock paper scissors.
  17.      * Rock beats scissors and loses to paper
  18.      * Paper beats rock and loses to scissors
  19.      * Scissors beats paper and loses to rock
  20.      * Identical choices tie
  21.      *
  22.      * Return: a string holding a message describing the winner, either
  23.      * USER_PLAYER, COMPUTER_PLAYER, or TIE
  24.      */
  25.     private String getWinner(String user, String computer)
  26.     {
  27.         if(user.equals(computer))
  28.         {
  29.             return TIE;
  30.         }
  31.         if(user.equals("rock") && computer.equals("paper"))
  32.         {
  33.             return COMPUTER_PLAYER;
  34.         }
  35.         if(user.equals("rock") && computer.equals("scissors"))
  36.         {
  37.             return USER_PLAYER;
  38.         }
  39.         if(user.equals("paper") && computer.equals("rock"))
  40.         {
  41.             return USER_PLAYER;
  42.         }
  43.         if(user.equals("paper") && computer.equals("scissors"))
  44.         {
  45.             return COMPUTER_PLAYER;    
  46.         }
  47.         if(user.equals("scissors") && computer.equals("rock"))
  48.         {
  49.             return COMPUTER_PLAYER;
  50.         }
  51.         return USER_PLAYER;
  52.     }
  53.    
  54.     public void run()
  55.     {
  56.         while(true){
  57.             //Get the user's choice
  58.             String choice = readLine("Enter your choice (rock, paper, or scissors): ");
  59.             choice = choice.toLowerCase();
  60.            
  61.             //End the game if the user entered a blank line
  62.             if(choice.equals("")){
  63.                 break;
  64.             }
  65.            
  66.             //Make sure it was a valid choice
  67.             if(choice.equals("rock") || choice.equals("paper") || choice.equals("scissors")){
  68.                 //Print the user choice
  69.                 System.out.println("User: " + choice);
  70.                
  71.                 //Get the computer random choice
  72.                 int compChoiceNum = Randomizer.nextInt(0,2);
  73.                 String compChoice = "";
  74.                 if (compChoiceNum == 0)
  75.                 {
  76.                     compChoice = "rock";
  77.                 }
  78.                 else if (compChoiceNum == 1)
  79.                 {
  80.                     compChoice = "paper";
  81.                 }
  82.                 else
  83.                 {
  84.                     compChoice = "scissors";
  85.                 }
  86.                
  87.                 //Print the computer choice
  88.                 System.out.println("Computer: " + compChoice);
  89.            
  90.                 //Print the winner
  91.                 System.out.println(getWinner(choice, compChoice));
  92.             }
  93.                
  94.             else{
  95.                 System.out.println(choice + "is not a valid move. Please try again");
  96.             }
  97.         }
  98.        
  99.         //End the game with a nice message
  100.         System.out.println("Thanks for playing!");
  101.     }
  102. }
  103.  
  104.  
  105. /*
  106. //Print the user choice
  107.                 System.out.println("User: " + choice);
  108.            
  109.                 //Get the computer random choice
  110.                 String[] compChoices = new String[]{"rock", "paper", "scissors"};
  111.                 int compChoiceNum = Randomizer.nextInt(0, 2);
  112.                 String compChoice = compChoices[compChoiceNum];
  113.                
  114.                 //Print the computer choice
  115.                 System.out.println("Computer: " + compChoice);
  116.            
  117.                 //Print the winner
  118.                 System.out.println(getWinner(choice, compChoice));
  119.             }
  120. */
Advertisement
Add Comment
Please, Sign In to add comment