Question

Hello, I have this programing assignment that I have to do and I am having issues...

Hello, I have this programing assignment that I have to do and I am having issues on how to solve it. The program has to be on Java.

-thank you in advance.

1- The game Paper, Rock, Scissors is played between two people. Each person counts "one, two, three" and on three displays their hands as either flat - Paper, a fist - Rock or two spread fingers - Scissors. The winner is determined by these rules:

Paper covers Rock (Paper wins) Rock smashes Scissors (Rock wins) or Scissors cuts paper (Scissors wins). In the event of players choosing the same item, the game must be replayed until there is a winner.

Recreate the classic game with the user entering their choice and the computer choice generated randomly. Record who wins or if it is a tie replay the game.  Output the results. Try to use several methods to do the actual work. Treat main method like a traffic cop that directs the flow but does not do the computations or output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.


Here is the completed code for this problem.

 Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

 Thanks
===========================================================================

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

public class RPSGame {

    public static void main(String[] args) {

        // welcome message
        System.out.println("Welcome to the classic game of Rock, Scissors and Paper");
        // method that will get a valid response from user and returns back
        String userChoice = getUserChoice();
        // method that returns a random choice imitaiting the other player and returns
        String computerChoice = getComputerChoice();
        // prints the result of the two choices
        displayResult(userChoice, computerChoice);


    }

    private static void displayResult(String userChoice, String computerChoice) {

        System.out.println("You choose: " + userChoice.toUpperCase() + " Computer choose: " + computerChoice.toUpperCase());
        if (userChoice.equalsIgnoreCase(computerChoice)) {
            System.out.println("Its a Tie!");
        } else if (userChoice.equalsIgnoreCase("Paper") && computerChoice.equalsIgnoreCase("Rock")) {
            System.out.println("Congratulations. You win!");
        } else if (userChoice.equalsIgnoreCase("Rock") && computerChoice.equalsIgnoreCase("Scissors")) {
            System.out.println("Congratulations. You win!");
        } else if (userChoice.equalsIgnoreCase("Scissors") && computerChoice.equalsIgnoreCase("Paper")) {
            System.out.println("Congratulations. You win!");
        } else {
            System.out.println("Sorry, Computer wins!");
        }
    }

    private static String getComputerChoice() {
        Random random = new Random();
        int randomNumber = random.nextInt(3) + 1;
        if (randomNumber == 1) return "Rock";
        else if (randomNumber == 2) return "Paper";
        else return "Scissors";
    }

    private static String getUserChoice() {

        Scanner scanner = new Scanner(System.in);
        String choice = "";
        while (true) {

            System.out.println("Enter your choice (Rock, Paper or Scissors): ");
            choice = scanner.nextLine();
            if (choice.equalsIgnoreCase("Rock") || choice.equalsIgnoreCase("Paper") ||
                    choice.equalsIgnoreCase("Scissors")) {
                break;

            } else {
                System.out.println("Sorry, Invalid selection. Please choose either Rock, Scissors or Paper only.");
            }
        }
        return choice.toUpperCase();
    }
}

====================================================================

Add a comment
Know the answer?
Add Answer to:
Hello, I have this programing assignment that I have to do and I am having issues...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python please. I have a working one that doesn't keep track of w/l ratio, it may...

    Python please. I have a working one that doesn't keep track of w/l ratio, it may be helpful to see how others did the entire program and inserted that module. Write a modular program that let the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 thru 3 is generated but do not display the computer choice immediately. Number 1...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock p...

    Help needed in Perl language program! Note : Perl Language Rock, Paper, Scissors (also known by several other names, see http://en.wikipedia.org/wiki/Rock paper scissors) is an extremely popular hand game most often played by children. Often, it is used as a method of selection similar to flipping a coin or throwing dice to randomly select a person for some purpose. Of course, this game is not truly random since a skilled player can often recognize and exploit the non-random behavior of...

  • Please help I am struggling with this and 3*Math.random())+1; and how to use it within the...

    Please help I am struggling with this and 3*Math.random())+1; and how to use it within the program For this project you will write a Java program that will play a simple game of Rock, Paper, Scissors.  If you have never played this game before the rules are simple - each player chooses one of Rock, Paper or Scissors and they reveal their choices simultaneously. • The choice of Rock beats the choice of Scissors ("Rock smashes Scissors") • The choice of...

  • In python: The randrange(min, max) produces a random integer greater than or equal to min and...

    In python: The randrange(min, max) produces a random integer greater than or equal to min and less than max. So randrange(0,3) produces either 0, 1, or 2 Write a program that asks the user to input paper, rock, scissors, or done If they input something else, repeat  the question. If they input 'done', exit the game loop. If they input rock, paper, or scissors, have the computer pick a random choice. Write a function that takes two strings, each 'rock', 'paper',...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets 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 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • please help me fix this. I'm getting it all mixed up in user defined functions. here's...

    please help me fix this. I'm getting it all mixed up in user defined functions. here's the question. This week we'll write a C program that lets the user play the game of Rock, Paper, Scissors against the computer. Your program will call a user-defined function named display_rules that displays the rules of the game. It then proceeds to play the game. To determine the winner, your program will call another user- defined function called determine_winner that takes two integer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT