Question

JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There...

JAVA

Beginnings of a paper-rock-scissors game.

Paper-rock-scissors is a game often used to make decisions. There are two players. Each player secretly chooses either, paper, rock or scissors. At the count of three, each player reveals what they have chosen.

The winner of the game is determined this way:

paper beats rock (because paper covers rock)

rock beats scissors (because rock crushes scissors)

scissors beat paper (because scissors cut paper)

If the two players choose the same item, it is a tie.

Write a program to play five hands of this game. The computer will play against the human user.

Ask the user to enter his/her choice and the computer will randomly pick a choice. Then the program will determine who wins or if it is a tie and print a message to the screen. After a hand is played. Print who wins the hand. After five hands, print how many each player

(computer and human) won.

Here is sample output:

Enter your choice (P, R, or S) (user enters S)

The computer chose rock, you chose scissors so the computer won!

Use rhe Random methods we used in lecture:

Import java.util.* Random rng = new Random( ); //to get an int in range of 0 through 4: num = rng.nextInt(5);

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

JAVA PROGRAM:

import java.util.*;
class Game
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int i,scoreU=0,scoreS=0;
for(i=0;i<5;i++)
{
System.out.print("User:");
String a=sc.nextLine();
if(a.equals("P")||a.equals("S")||a.equals("R"))
{
Random r=new Random();
int n=r.nextInt(5);
n%=3;
if(n==0)
{
System.out.println("System:P");
}
else if(n==1)
{
System.out.println("System:S");
}
else if(n==2)
{
System.out.println("System:R");
}
if((a.equals("P")&&n==2)||(a.equals("S")&&n==0)||(a.equals("R")&&n==1))
{
scoreU++;
}
else if((a.equals("P")&&n==1)||(a.equals("S")&&n==2)||(a.equals("R")&&n==0))
{
scoreS++;
}
}
else
{
System.out.println("Wrong Choice");
}
}
System.out.println("User Score:"+scoreU);
System.out.println("System Score:"+scoreS);
if(scoreU>scoreS)
{
System.out.println("Winner is User");
}
else if(scoreU<scoreS)
{
System.out.println("Winner is System");
}
else
{
System.out.println("Match Tie");
}
};
};
SAMPLE INPUT OUTPUT:

Hand 1
User:R
System:R
Hand 2
User:S
System:P
Hand 3
User:P
System:S
Hand 4
User:S
System:P
Hand 5
User:R
System:S
User Score:3
System Score:1
Winner is User

Add a comment
Know the answer?
Add Answer to:
JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There...
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
  • 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...

  • Write a program in python that lets the user play the game Rock, Paper, Scissors against...

    Write a program in python that lets the user play the game Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

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

  • Python Language: Please see program listed below of a game called: Rock, Paper, Scissors. Please add...

    Python Language: Please see program listed below of a game called: Rock, Paper, Scissors. Please add an exception, for example if the user inputs something else other than rock, paper or scissors, the exception will print message: "Enter only rock, paper, or scissors". Also add option to keep playing the game, for example: print: "Would you like to play again? Enter Y or N". If the user says Y, the game will repeat, if N, the game will end and...

  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets 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 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

    C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets 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 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

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

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