Question

In: Computer Science

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 is a tie.” Your program should work for both lower case and uppercase letters. If the user string is not rock, scissors or paper the program will print “Wrong choice”.

Sample Run: User input is in Bold.

Player 1: Choose rock, scissors, or paper:

rock

Player 2: Choose rock, scissors, or paper:

paper

Player 2 wins.

Player 1: Choose rock, scissors, or paper:

scissors

Player 2: Choose rock, scissors, or paper:

rock

Player 2 wins.

Player 1: Choose rock, scissors, or paper:

PAPER

Player 2: Choose rock, scissors, or paper:

Rock

Player 1 wins.

Player 1: Choose rock, scissors, or paper:

rock

Player 2: Choose rock, scissors, or paper:

pen

Wrong choice!

Solutions

Expert Solution

import java.util.*;
public class Test {
   public static int result(String player1, String player2){
       if(player1.equalsIgnoreCase("rock") && player2.equalsIgnoreCase("rock"))
           return 0;
       else if(player1.equalsIgnoreCase("rock") && player2.equalsIgnoreCase("scissors"))
           return 1;
       else if(player1.equalsIgnoreCase("rock") && player2.equalsIgnoreCase("paper"))
           return 2;
       else if(player1.equalsIgnoreCase("scissors") && player2.equalsIgnoreCase("rock"))
           return 2;
       else if(player1.equalsIgnoreCase("scissors") && player2.equalsIgnoreCase("scissors"))
           return 0;
       else if(player1.equalsIgnoreCase("scissors") && player2.equalsIgnoreCase("paper"))
           return 1;
       else if(player1.equalsIgnoreCase("paper") && player2.equalsIgnoreCase("rock"))
           return 1;
       else if(player1.equalsIgnoreCase("paper") && player2.equalsIgnoreCase("scissors"))
           return 2;
       else if(player1.equalsIgnoreCase("paper") && player2.equalsIgnoreCase("paper"))
           return 0;
       return -1;
   }
   public static void main(String[] args) {
       String player1,player2;
       Scanner sc=new Scanner(System.in);
       System.out.println("Player 1: Choose rock, scissors, or paper:");
       player1=sc.nextLine();
       System.out.println("Player 2: Choose rock, scissors, or paper:");
       player2=sc.nextLine();
       if(result(player1,player2)==1){
           System.out.println("Player 1 wins");
       } else if(result(player1,player2)==2){
           System.out.println("Player 2 wins");
       } else if(result(player1,player2)==0){
           System.out.println("It is a tie");
       } else {
           System.out.println("Wrong choice!");
       }
   }
}


Related Solutions

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...
Background: In the game “Rock, Paper, and Scissors,” two players simultaneously say (or display a hand...
Background: In the game “Rock, Paper, and Scissors,” two players simultaneously say (or display a hand symbol representing) either “rock,” “paper,” or “scissors.” The winner is the one whose choice wins over the other. The rules are: paper wins over (wraps) rock, rock wins over (breaks) scissors, and scissors wins over (cuts) paper. Assignment: A customer wants you to design and implement a program (RPS.py) for playing the game of “Rock, Paper, Scissors” for a single player against the computer....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player. If it is a draw, no payment is made. The payoff to a player is the amount of money (s)he gets. Represent the situation in a matrix form. Find all the Nash equilibria.
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player...
Game Description: The popular rock-paper-scissors game is usually played between two people in which each player simultaneously chooses either a rock or a paper or scissors (usually with an outstretched hand). The rule of the game is simple: rock crushes scissors, scissors cut paper, and paper wraps rock. If both the players choose the same object, then it ends in a tie. Problem Description: You have to play the rock-paper-scissors game against the computer for 100 times. You receive the...
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...
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,...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT