Question

In: Computer Science

Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...

Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and player 2 choices rather than hardcoding the numbers. Make sure to close your Scanner at the end of your program to avoid losing a point. When run, the program should look like the screenshots shown below.

Named constants:
ROCK = 1
PAPER = 2
SCISSORS = 3

Name your Java program RockPaperScissors.java and submit it to Canvas along with the other files specified in the “Grading” section below. Make sure your program compiles before submitting it to avoid losing points.

Example 1:
Player 1 chooses rock.
Player 2 chooses paper.
Player 2 wins!

Solutions

Expert Solution

If you have any doubts, please give me comment....

import java.util.Scanner;

import java.util.Random;

public class RockPaperScissors {

    public static void main(String[] args) {

        final int ROCK = 1;

        final int PAPER = 2;

        final int SCISSORS = 3;

        Scanner scnr = new Scanner(System.in);

        String player2Material = "";

        String player1Material = "";

        int player1Choice = 0;

        int player2Choice = 0;

        boolean invalidChoice = true;

        System.out.println("Play! Enter: 1 (rock), 2 (paper), 3 (scissors)\n");

        while (invalidChoice) {

            invalidChoice = false;

            System.out.print("Player 1 choice: ");

            player1Choice = scnr.nextInt();

            switch (player1Choice) {

            case ROCK:

                player1Material = "rock";

                break;

            case PAPER:

                player1Material = "paper";

                break;

            case SCISSORS:

                player1Material = "scissors";

                break;

            default:

                System.out.println("Invalid choice!");

                invalidChoice = true;

            }

        }

        invalidChoice = true;

        while (invalidChoice) {

            System.out.print("Player-2 choice: ");

            player2Choice = scnr.nextInt();

            invalidChoice = false;

            switch (player2Choice) {

            case ROCK:

                player2Material = "rock";

                break;

            case PAPER:

                player2Material = "paper";

                break;

            case SCISSORS:

                player2Material = "scissors";

                break;

            default:

                System.out.println("Invalid choice!");

                invalidChoice = true;

            }

        }

        System.out.println("\nPlayer 1 chooses "+player1Material+".");

        System.out.println("Player 2 chooses "+player2Material+".");

        if (player2Choice == player1Choice) {

            System.out.println("Tie!");

        } else if ((player2Choice == ROCK && player1Choice == SCISSORS)

                || (player2Choice == PAPER && player1Choice == ROCK)

                || (player2Choice == SCISSORS && player1Choice == PAPER)) {

            System.out.println("Player 2 wins!");

        } else {

            System.out.println("Player 1 wins!");

        }

        System.out.println();

    }

}


Related Solutions

Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player. If it is a draw, no payment is made. The payoff to a player is the amount of money (s)he gets. Represent the situation in a matrix form. Find all the Nash equilibria.
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create...
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create the code so that when the program is run the user will see in the console the following: We are going to play rock paper scissors. Please choose 1 for Rock 2 for Paper or 3 for scissors. The program will have your choice which is the integer-valued typed in by the user and compChoice which will be the randomly generated value of either...
JAVA Remember the childhood game “Rock, Paper, Scissors”? It is a two-players game in which each...
JAVA Remember the childhood game “Rock, Paper, Scissors”? It is a two-players game in which each person simultaneously chooses either rock, paper, or scissors. Rock beats scissors but loses to paper, paper beats rock but loses to scissors, and scissors beats paper but loses to rock. Your program must prompt the player 1 and player 2 to each enter a string for their choice: rock, paper, or scissors. Then appropriately reports if “Player 1 wins”, “Player 2 wins”, or “It...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The...
Rock-Paper-Scissors Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The user inputs the number of rounds which should be positive and odd. This is a free form assignment but structure your code similar to the test cases provided. USING MATLAB ONLY!!! Program Inputs • How many rounds would you like to play (odd rounds only)?: XXX – XXX should be positive and odd. Restart the game if the input is not positive or odd....
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,...
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...
Rock, Paper, Scissors is a two-player game in which each player chooses one of three items....
Rock, Paper, Scissors is a two-player game in which each player chooses one of three items. If both players choose the same item, the game is tied. Otherwise, the rules that determine the winner are: (a) Rock always beats Scissors (Rock crushes Scissors) (b) Scissors always beats Paper (Scissors cut Paper) (c) Paper always beats Rock (Paper covers Rock) Implement function rps() that takes the choice ('R', 'P', or 'S') of player 1 and the choice of player 2, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT