Question

In: Computer Science

Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...

Write a Java program that plays the game Rock, Paper, Scissors.

The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied.

Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins, losses and ties by the player.

Run your program and show at least 2 examples of playing the game before quitting. For example, the output might look like:

Choose: (0=Rock, 1=Paper, 2=Scissors): 1

You chose Paper

Computer chose Rock

You win

Play again? (y/n): y

Choose: (0=Rock, 1=Paper, 2=Scissors): 0

You chose Rock

Computer chose Scissors

You win

Play again? (y/n): n

You won 2 times

You lost 0 times

You tied 0 times

Solutions

Expert Solution


import java.io.*;
import java.util.*;

public class RockPaperScissor

{
  
  
// global named constants for game choices
static final int ROCK = 1;
static final int PAPER = 2;
static final int SCISSORS = 3;
  
// global names constants for game outcomes
static final int PLAYER1_WINS = 11;
static final int PLAYER2_WINS = 12;
static final int DRAW = 3;
  
// global named constant for error condition
static final int ERROR = -1;
/**
* 1. Get human player's choice
* 2. Get computer player's (random) choice
* 3. Check human player's choice
* 4. Check computer player's choice
* 5. Announce winner
*/
public static void main(String[] args)
{
   char ch = 'y';
   int won = 0, tied = 0, lost = 0;
   do{
Scanner scan = new Scanner(System.in);
PrintStream output = System.out;
int player1, player2;
// get player 1 input as 1 (rock), 2 (paper or 3 (scissors)
output.print("Choose 1 (rock), 2 (paper), or 3 (scissors): ");
player1 = scan.nextInt();
  
if(!(player1 >=1 && player1<=3)){
   System.out.println("Invalid Choice, Aborting");
   System.exit(0);
}
  
// echo human player's choice
System.out.print(" You chose ");
if (player1 == ROCK) { System.out.println("rock"); }
else if (player1 == PAPER) { System.out.println("paper"); }
else { System.out.println("scissors"); }
// now computer picks one randomly
output.println("Now Computer choose one ...");
Random r = new Random();
player2 = r.nextInt(3) + 1;

System.out.print(" Computer choose ");
if (player2 == ROCK) { System.out.println("rock"); }
else if (player2 == PAPER) { System.out.println("paper"); }
else { System.out.println("scissors"); }
  
  
int result ;
       if (player1 == ROCK) {
           result = rockChoice(player2);
       } else if (player1 == PAPER) {
           result = paperChoice(player2);
       } else {
           result = scissorsChoice(player2);
       }
      
       if(result==3) {
           System.out.println("\nTHERE IS DRAW");
           ++tied;
       }else if(result==11) {
           System.out.println("\nYOU WIN");
           ++won;
       }else {
           System.out.println("\nCOMPUTER WIN");
           ++lost;
       }
      
       System.out.print("Play again? (y/n): ");
       ch = scan.next().charAt(0);
   }while(ch =='y' || ch=='Y');
  
  
   System.out.println("You won "+ won + " times");
   System.out.println("You lost "+ lost + " times");
   System.out.println("You tied "+ tied + " times");
  
  
} // end main
  
  
public static int rockChoice(int choice){
   if(!(choice >=1 && choice<=3)){
    System.out.println("Invalid Choice, Aborting");
    return ERROR;
}else if(choice==1){
   return 3;
} else if(choice==2){
   return 12;
   } else
       return 11;
}
  
  
public static int paperChoice(int choice){
    if(!(choice >=1 && choice<=3)){
   System.out.println("Invalid Choice, Aborting");
   return ERROR;
}else if(choice==1){
    return 11;
} else if(choice==2){
    return 3;
    } else
        return 12;
}
  
  
  
  
public static int scissorsChoice(int choice){
    if(!(choice >=1 && choice<=3)){
   System.out.println("Invalid Choice, Aborting");
   return ERROR;
}else if(choice==1){
    return 12;
} else if(choice==2){
    return 11;
    } else
        return 3;
}
  
  
  
  
} // end class

======================================================================
SEE OUTPUT


Thanks, PLEASE COMMENT if there is any concern.


========================================================================
Removed and Modified to make it little bit Simpler


import java.io.*;
import java.util.*;
public class RockPaperScissor {

   // global named constants for game choices
   static final int ROCK = 1;
   static final int PAPER = 2;
   static final int SCISSORS = 3;

   public static void main(String[] args) {
       char ch = 'y';
       int won = 0, tied = 0, lost = 0;
       do {
           Scanner scan = new Scanner(System.in);
           PrintStream output = System.out;
           int player1, player2;
           // get player 1 input as 1 (rock), 2 (paper or 3 (scissors)
           output.print("Choose 1 (rock), 2 (paper), or 3 (scissors): ");
           player1 = scan.nextInt();

           // echo human player's choice
           System.out.print(" You chose ");
           if (player1 == ROCK) {
               System.out.println("rock");
           } else if (player1 == PAPER) {
               System.out.println("paper");
           } else {
               System.out.println("scissors");
           }
           // now computer picks one randomly
           output.println("Now Computer choose one ...");
           Random r = new Random();
           player2 = r.nextInt(3) + 1;

           System.out.print(" Computer choose ");
           if (player2 == ROCK) {
               System.out.println("rock");
           } else if (player2 == PAPER) {
               System.out.println("paper");
           } else {
               System.out.println("scissors");
           }

           int result;
           if (player1 == ROCK) {
               result = rockChoice(player2);
           } else if (player1 == PAPER) {
               result = paperChoice(player2);
           } else {
               result = scissorsChoice(player2);
           }

           if (result == 3) {
               System.out.println("\nTHERE IS DRAW");
               ++tied;
           } else if (result == 11) {
               System.out.println("\nYOU WIN");
               ++won;
           } else {
               System.out.println("\nCOMPUTER WIN");
               ++lost;
           }

           System.out.print("Play again? (y/n): ");
           ch = scan.next().charAt(0);
       } while (ch == 'y' || ch == 'Y');

       System.out.println("You won " + won + " times");
       System.out.println("You lost " + lost + " times");
       System.out.println("You tied " + tied + " times");

   } // end main

   public static int rockChoice(int choice) {
          
       if (choice == 1) {
           return 3;
       } else if (choice == 2) {
           return 12;
       } else
           return 11;
   }

   public static int paperChoice(int choice) {
       if (choice == 1) {
           return 11;
       } else if (choice == 2) {
           return 3;
       } else
           return 12;
   }

   public static int scissorsChoice(int choice) {
       if (choice == 1) {
           return 12;
       } else if (choice == 2) {
           return 11;
       } else
           return 3;
   }

} // end class





Check the Simpler code above, Let me know if there is any concern.


Related Solutions

JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When...
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then prompt for the user’s selection. At that point, the program...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then seek for the user’s selection (using your choice of an object...
Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming...
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming 1) Develop a function that prompts the user to enter their choice (1=Rock 2=Paper 3=Scissors) Return either a 1, 2, or 3 depending on the value the user has entered Do not continue the program until the user has entered a valid choice of 1, 2, 3 2) Develop a function that generates the computer player's choice Return either a 1, 2, or 3...
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT