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.
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...
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...
Background: In the game “Rock, Paper, and Scissors,” two players simultaneously say (or display a hand...
Background: In the game “Rock, Paper, and Scissors,” two players simultaneously say (or display a hand symbol representing) either “rock,” “paper,” or “scissors.” The winner is the one whose choice wins over the other. The rules are: paper wins over (wraps) rock, rock wins over (breaks) scissors, and scissors wins over (cuts) paper. Assignment: A customer wants you to design and implement a program (RPS.py) for playing the game of “Rock, Paper, Scissors” for a single player against the computer....
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT