Question

In: Computer Science

Java) Write a program to score the rock-paper-scissor game. Each of two users types in either...

Java) Write a program to score the rock-paper-scissor game. Each of two users types in either R, P, S or Q to quit. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or a Tie game. Allow the user to use lowercase as well as uppercase letters. Your program should loop until the user types a 'Q' to quit.

In the sample code, the game is enclosed within a do…while loop that keeps repeating while the user enters a 'Y' in response to the prompt "Do you want to play again? " The program exits if anything other than an upper-case or lower-case 'Y' is entered. For extra credit, add additional code at the end of the program to keep asking "Do you want to play again? " until the user enters either a 'Y' or 'N' and rejects any other response.(NEEDED)

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 char ROCK = 'R';

        final char PAPER = 'P';

        final char SCISSORS = 'S';

        final char QUIT = 'Q';

        Scanner scnr = new Scanner(System.in);

        char choice;

        String player2Material = "";

        String player1Material = "";

        int player1Choice = 0;

        int player2Choice = 0;

        boolean invalidChoice;

        do {

            invalidChoice = true;

            System.out.println("Play!\nEnter: R (rock), P (paper), S (scissors), Q (Quit)\n");

            while (invalidChoice) {

                invalidChoice = false;

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

                player1Choice = scnr.next().toUpperCase().charAt(0);

                switch (player1Choice) {

                case ROCK:

                    player1Material = "rock";

                    break;

                case PAPER:

                    player1Material = "paper";

                    break;

                case SCISSORS:

                    player1Material = "scissors";

                    break;

                case QUIT:

                    System.out.println("Player 1 quit the game");

                    System.exit(0);

                    break;

                default:

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

                    invalidChoice = true;

                }

            }

            invalidChoice = true;

            while (invalidChoice) {

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

                player2Choice = scnr.next().toUpperCase().charAt(0);

                invalidChoice = false;

                switch (player2Choice) {

                case ROCK:

                    player2Material = "rock";

                    break;

                case PAPER:

                    player2Material = "paper";

                    break;

                case SCISSORS:

                    player2Material = "scissors";

                    break;

                case QUIT:

                    System.out.println("Player 2 quit the game");

                    System.exit(0);

                    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.print("\nDo you want to play again? ");

            choice = scnr.next().charAt(0);

        } while (choice == 'Y' || choice == 'y');

    }

}


Related Solutions

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,...
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...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions. Requirements: 1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you...
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...
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...
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...
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 PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to look up Random numbers in Java. First ask the user what language they want to play in. Choice 1 is English, but choice 2 can be whatever real language you want. Your first loop is making sure they enter good input. If there is a tie you don't give them the option to play again. They have to. Outer loop runs until they say...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT