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

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...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's 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 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...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT