Question

In: Computer Science

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 is `R` because rock beats scissors, S&S is S, and R&S -> R

One more example is if the user inputs: `(((R&P)&(P&S))&R)` the output is `R` because (((R&P)&(P&S))&R --> (((P)&(S))&R --> (S)&R --> R

Solutions

Expert Solution

Hello There,Thanks for Asking to Answer.Here we can see that we have to use the Java Regex for pattern matching as well as for checking the input. In this answer i am providing you the basic format of the above question.

Code:-

import java.util.*;
import java.io.*;
import java.util.regex.*;
class myclass{
public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the First Input:-");
    String a = sc.nextLine();
    String aa = new String(a.toLowerCase());
    System.out.println("Enter the Second Input:-");
    String b = sc.nextLine();
    String bb = new String(b.toLowerCase());
    if(aa.matches("(.*)rock(.*)") && bb.matches("(.*)scissors(.*)") || bb.matches("(.*)rock(.*)") && aa.matches("(.*)scissors(.*)")){
       System.out.println("The output is R because Rock beats Scissors");
    }else if(aa.matches("(.*)paper(.*)") && bb.matches("(.*)rock(.*)") || bb.matches("(.*)paper(.*)") && aa.matches("(.*)rock(.*)")){
       System.out.println("The output is Paper because Rock beats Rock");
    }if(aa.matches("(.*)scissors(.*)") && bb.matches("(.*)paper(.*)") || bb.matches("(.*)scissors(.*)") && aa.matches("(.*)paper(.*)")){
       System.out.println("The output is S because Scissors beats Paper");
    }else if(a.equals(b)){
       System.out.println("both inputs are same");
    }
}
}

I Hope it fufiled Your Requirements.However,if you need that this answer need some modification or there is any error in the code then do let me know in the Comments.

Happy Learning!!!.


Related Solutions

write a python script for rock scissors paper game
write a python script for rock scissors paper game
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,...
Kim and Kanye are playing a single game of Rock-Paper-Scissors. They have complete information. The winner...
Kim and Kanye are playing a single game of Rock-Paper-Scissors. They have complete information. The winner must pay the loser $1,000. If they make the same choice they each get nothing. What is the Nash equilibrium for the game?
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...
Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
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...
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