Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- *
- * @author
- * Section 2
- */
- public class RockPaperScissors{
- //these are class constants (all methods in the class can see them)
- static final int ROCK = 1, PAPER = 2, SCISSORS = 3, YOU_WIN = 4, COMP_WIN = 5, DRAW = 0;
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String userInput;
- int yourScore=0, compScore=0;
- int gameResult;
- System.out.println("========================");
- System.out.println("ROCK PAPER SCISSORS GAME");
- System.out.println("========================");
- System.out.println("\nHello! Let's get started!");
- do {
- //calling the playGame method
- gameResult = playGame(scan);
- if (gameResult == YOU_WIN)
- yourScore++;
- else if(gameResult == COMP_WIN)
- compScore++;
- printScore(yourScore,compScore);
- System.out.println(" You : Comp");
- System.out.println("Score: -----------");
- System.out.println(" | "+yourScore+" : "+compScore+" |");
- System.out.print("Do you want to play some more? (y or n): ");
- userInput = scan.next();
- } while (userInput.equalsIgnoreCase("y") || userInput.equalsIgnoreCase("yes"));
- System.out.println("========GAME OVER=======");
- System.out.println(" FINAL SCORE:");
- printScore(yourScore,compScore);
- }
- /**
- This method "plays" the game.
- The input parameter is the Scanner object created in main()
- The method return the int result of the game.
- */
- private static int playGame(Scanner scan) {
- int yourChoice, compChoice;
- //1. Prompt the user and use scan to read her input (R, P, or S)
- System.out.println("Rock, Paper, or Scissors? (R,P,S):");
- String usermove = scan.nextLine();
- //2. Use a while loop to continue asking for user input if it is not
- // one of the three above ("r", "p", or "s", ignoring case)
- while(!usermove.equalsIgnoreCase("r") || !usermove.equalsIgnoreCase("p") || !usermove.equalsIgnoreCase("s"));
- System.out.println(usermove+" Is not suported. Try again");
- usermove = scan.nextLine();
- //3. Once the user input is satisfactory
- //print out the choice (e.g. "You chose Rock!"),
- //translate it to one of the int constants (ROCK, PAPER, or SCISSORS),
- //and assign to yourChoice variable (e.g. yourChoice = ROCK)
- if(usermove.equalsIgnoreCase("R")){
- System.out.println("You chose Rock!");
- yourChoice= ROCK;
- }
- else if (usermove.equalsIgnoreCase("P")){
- System.out.println("You chose Paper!");
- yourChoice= PAPER;
- }
- else if (usermove.equalsIgnoreCase("S")){
- System.out.println("You chose Scissors!");
- yourChoice= SCISSORS;
- }
- //4. Use Math.random() to randomly generate computer's choice
- //Remember, you need to split the range between 0 and 1 into
- // three equal intervals and if the random number is in one of them
- // assign a corresponding constant to compChoice.
- //Print out computer's choice as well.
- double x= Math.random();
- if (x<.33){
- System.out.println("Computer Chose Rock!");
- compChoice= ROCK;
- }
- else if (x>.66){
- System.out.println("Computer Chose Paper!");
- compChoice= PAPER;
- }
- else{
- System.out.println("Computer Chose Scissors!");
- compChoice= SCISSORS;
- }
- //Call the method whoWins using yourChoice and compChoice as parameters-----------
- int gameResult = whoWins(result);
- //return the game result to main()------------------------------------------------
- return gameResult;
- }
- /**
- This method takes two int parameters yourChoice and compChoice,
- and returns the result of the game.
- It will return YOU_WIN if you win, COMP_WIN if computer wins
- and DRAW if it is a draw. (Remember, these are constants defined in this class)
- In addition, the method prints out the corresponding description of
- the result of the game e.g. "It's a draw!!",
- or "Scissors cut Paper! You WIN!".
- or "Rock is covered by Paper! You lose :(", and so on.
- */
- private static int whoWins(int yourChoice, int compChoice){
- int result;
- if(yourChoice==ROCK && compChoice==SCISSORS){
- System.out.println("Rock smashes Scissors! You WIN!");
- result= YOU_WIN;
- }
- else if(yourChoice==ROCK && compChoice==PAPER){
- System.out.println("Paper wraps Rock! You LOSE!");
- result= COMP_WIN;
- }
- else if(yourChoice==ROCK && compChoice==ROCK){
- System.out.println("Rock hits Rock! It's a DRAW!");
- result= DRAW;
- }
- if(yourChoice==PAPER && compChoice==SCISSORS){
- System.out.println("Scissors gets cut! You LOSE!");
- result= COMP_WIN;
- }
- else if(yourChoice==PAPER && compChoice==PAPER){
- System.out.println("Both Papers fold! It's a DRAW!");
- result= DRAW;
- }
- else if (yourChoice==PAPER && compChoice==ROCK){
- System.out.println("Paper wraps Rock! You LOSE!");
- result= YOU_WIN;
- }
- if(yourChoice==SCISSORS && compChoice==SCISSORS){
- System.out.println("Both SCissors fold! It's a DRAW!");
- result= DRAW;
- }
- else if(yourChoice==SCISSORS && compChoice==PAPER){
- System.out.println("Scissors cuts Paper! You WIN!");
- result= YOU_WIN;
- }
- else if(yourChoice==SCISSORS && compChoice==ROCK){
- System.out.println("Rock smashes Scissors! You LOSE!");
- result= COMP_WIN;
- }
- return result;
- }
- /**
- This method takes in yourScore and compScore as parameters and prints
- them out in a formatted way, as shown in the project description.
- */
- private static void printScore(int yourScore, int compScore){
- System.out.println(" You : Comp");
- System.out.println("Score: -----------");
- System.out.println(" | "+yourScore+" : "+compScore+" |");
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement