Question

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 Scissors beats the choice of Paper ("Scissors cut Paper")
• The choice of Paper beats Rock ("Paper covers rock")
• If both players choose the same item, they tie
to extract the character from the string. One of the PowerPoint slides in section 2 showed this.
• Get a random number (value 1, 2 or 3) in the following manner:
o int compVal = (int) (3*Math.random()) + 1; • You may use the letters themselves to represent the R, P, S values or you may change
them to integers 1, 2, or 3. Your choice, but remember that you have to deal with both upper and lower case characters. (Check out the to.Upper method for assistance if you need it.)
• You could use the R, P or S that the user enters and the 1, 2 or 3 the computer generates
directly if you want to. That may be the easiest way to write it, but don’t forget to make sure to use a consistent mapping between the letters and numbers or you will be really confused.
Sample Output:
Rock, Paper, Scissors-Enter a choice R/P/S: r We both chose the same item-try again
Rock, Paper, Scissors-Enter a choice R/P/S: r I chose Paper and you chose Rock: Paper covers Rock, so I win
Rock, Paper, Scissors-Enter a choice R/P/S: r
I chose Scissors and you chose Rock: Rock breaks Scissors, so you win Rock, Paper, Scissors-Enter a choice R/P/S: p
Your program will generate a random choice of Rock, Paper or Scissors.  It should then prompt the user for their choice.  It should report both choices and indicate who wins the game.  In addition, it should check the user's input and if the user inputs an invalid choice it should display an error message and end. Hints:
• Remember that to input a char value you read it as a string and use the charAt method

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Java code is given below:

import java.util.Scanner;

public class RPS {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String user,my;
        char userChoice, myChoice;
        while(true) {
            /**
             * Generating random number and get corresponding character(R/P/S)
             */
            int compVal = (int) (3*Math.random()) + 1;
            if(compVal == 1)
                myChoice = 'R';
            else if(compVal == 2)
                myChoice = 'P';
            else
                myChoice = 'S';
            /**
             * Input user's choice
             */
            System.out.print("Rock, Paper, Scissors-Enter a choice R/P/S: ");
            user = scan.nextLine();
            userChoice = Character.toUpperCase(user.charAt(0));
            /**
             * Print error message on Invalid choice 
             */
            if(userChoice != 'R' && userChoice != 'P' && userChoice != 'S')
            {
                System.out.println("Invalid Choice!");
                break;
            }
            /**
             *  Comparing and printing results
             */
            if(myChoice == userChoice)
                System.out.println("We both chose the same item-try again");
            else if(myChoice == 'R' && userChoice == 'P') {
                System.out.println("I chose Rock and you chose Paper: Paper covers Rock, so You win");
            }
            else if(myChoice == 'P' && userChoice == 'R') {
                System.out.println("I chose Paper and you chose Rock: Paper covers Rock, so I win");
            }
            else if(myChoice == 'R' && userChoice == 'S') {
                System.out.println("I chose Rock and you chose Scissors: Rock breaks Scissors, so I win");
            }
            else if(myChoice == 'S' && userChoice == 'R') {
                System.out.println("I chose Scissors and you chose Rock: Rock breaks Scissors, so you win");
            }
            else if(myChoice == 'P' && userChoice == 'S') {
                System.out.println("I chose Paper and you chose Scissors: Scissors cut Paper, so you win");
            }
            else if(myChoice == 'S' && userChoice == 'P') {
                System.out.println("I chose Scissors and you chose Paper: Scissors cut Paper, so I win");
            }
        }
    }
}

Sample Output:

Screenshot of java code is given below:

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
Please help I am struggling with this and 3*Math.random())+1; and how to use it within the...
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
  • 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...

  • Use Dev C++ for program and include all of the following. Im lost. Make sure the...

    Use Dev C++ for program and include all of the following. Im lost. Make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”. Ask the user how many times he/she wants to...

  • 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...

  • 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...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • 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...

  • 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...

  • Write a program in the Codio programming environment that allows you to play the game of...

    Write a program in the Codio programming environment that allows you to play the game of Rock / Paper / Scissors against the computer. Within the Codio starting project you will find starter code as well as tests to run at each stage. There are three stages to the program, as illustrated below. You must pass the tests at each stage before continuing in to the next stage.  We may rerun all tests within Codio before grading your program. Please see...

  • 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 language This assignment is to write a program to score the paper-rock-scissors game AGAIN. Each...

    c language This assignment is to write a program to score the paper-rock-scissors game AGAIN. Each of two players enters either P, R, or S. 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 “Draw, nobody wins”. The primary differences between this and previous two versions are described as below: (1) You MUST use a function to get the input value for the player’s...

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