Question

In: Computer Science

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 reveals both choices and prints a statement indicating whether the user won, the computer won, or it was a tie. Continue playing until the user chooses to stop by saying either ‘y’ or ‘n’, where ‘y’ means ‘yes continue playing’, and ‘n’ means ‘no stop playing’. Then print the number of user wins, losses, and ties.

Solutions

Expert Solution


import java.util.Random;
import java.util.Scanner;

public class RockScissorPaperGame {

        public static final String ROCK = "ROCK";
        public static final String PAPER = "PAPER";
        public static final String SCISSORS = "SCISSORS";

        public static void main(String args[]) {
                int totalGames = 0, wins = 0, losses = 0,ties=0;
                do {
                        totalGames++;
                        System.out.println("Enter any one of the following inputs: ");
                        System.out.println("ROCK");
                        System.out.println("PAPER");
                        System.out.println("SCISSORS");
                        System.out.println();
                        String playerMove = getPlayerMove().toUpperCase();
                        String computerMove = getComputerMove();

                        if (playerMove.equals(computerMove)) {
                                System.out.println("Its tie !!");
                                ties++;
                        } // if player selects ROCK
                        else if (playerMove.equals(ROCK)) {
                                if (computerMove.equals(PAPER)) {
                                        System.out.println("Computer wins");
                                        losses++;
                                } else {

                                        System.out.println("Player wins");
                                        wins++;
                                }
                        } // if player selects PAPER
                        else if (playerMove.equals(PAPER)) {
                                if (computerMove.equals(SCISSORS)) {
                                        System.out.println("Computer wins");
                                        losses++;
                                } else {
                                        System.out.println("Player wins");
                                        wins++;
                                }
                        } // if player selects SCISSORS
                        else {
                                if (computerMove.equals(ROCK)) {
                                        System.out.println("Computer wins");
                                        losses++;
                                } else {
                                        System.out.println("Player wins");
                                        wins++;
                                }
                        }
                } while (playAgain());
                System.out.println("Total Games: "+totalGames);
                System.out.println("Wons: "+wins);
                System.out.println("Losses: "+losses);
                System.out.println("Ties: "+ties);
        }

        public static String getComputerMove() {
                String computermove;
                Random random = new Random();
                int input = random.nextInt(3) + 1;
                if (input == 1) {
                        computermove = ROCK;
                } else if (input == 2) {
                        computermove = PAPER;
                } else {
                        computermove = SCISSORS;
                }

                System.out.println("Computer Pick is: " + computermove);
                System.out.println();
                return computermove;
        }

        public static String getPlayerMove() {
                Scanner in = new Scanner(System.in);
                String input = in.next();
                String playermove = input.toUpperCase();
                System.out.println("Player move is: " + playermove);
                return playermove;
        }

        public static boolean playAgain() {
                Scanner sc = new Scanner(System.in);
                System.out.print("Do you want to play again? ");
                String userInput = sc.nextLine();
                userInput = userInput.toUpperCase();
                return userInput.charAt(0) == 'Y';
        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

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...
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,...
Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You...
Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the following rewards each time you play the game:  You get $5 if you win  You get $2 if there is a tie  You get $-1 if you lose The computer randomly chooses rock, paper, or scissors in each game. Rather than deciding what to play (rock, paper or scissors) for each individual game, you decide to use the following...
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...
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...
Code the game of Rock, Paper, Scissors between a human player and the computer. You can...
Code the game of Rock, Paper, Scissors between a human player and the computer. You can check out the game on Wikipedia if you are not familiar with it. Create a 4 option menu with the human player choices, plus the option of exiting the game. Randomly determine the computer’s choice (although a great deal of AI research has gone in to determining the best computer move). • Loop the game until the human player exits. • Count the number...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT