Write a JAVA program that lets the user play the game of Rock, Paper, Scissors a
ID: 3723071 • Letter: W
Question
Write a JAVA 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, the user enters his or her choice of “r”, “p”, or “s” at the keyboard using a menu in a function, called selection, that returns a character.
2. Next, there should be a function, called CP, that generates the computer’s play. A random number in the range of 1 through 3 is generated. (NOTE: You’ll have to do some research on how to create random numbers correctly.) 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. The computer’s choice is returned as a character.
3. After, a function, called W, will determine the winner between the user’s choice vs. the computer’s choice. (NOTE: It will return nothing, but it will take in the user’s choice and the computer’s choice as the two arguments.) The result is selected according to the following rules:
* If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.)
* If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors cuts paper.)
* If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)
* If both players make the same choice, the game ends in a draw and there is no winner.
4. Finally, after a result is selected, there should be a function, called Repeat, in which the player should have the option of playing again. This should return a boolean. Be sure that the program contains at least the four functions mentioned .
Explanation / Answer
Implemented the code as per the requirement. Please comment if you face any problem or if you need any modification.
Code:
======
import java.util.Random;
import java.util.Scanner;
public class RPS {
static Scanner sc=null;
static String ch_user = "", ch_comp = "";
static Random randomNum = new Random();
public static void main(String[] args) {
sc = new Scanner(System.in);
while(true) {
System.out.print("Enter your choice (r/p/s or enter q - to quit): ");
ch_user = sc.nextLine();
ch_comp = cp() + "";
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+ch_comp+" "+ch_user);
w();
if(!doYouWannaPlayAgain()) {
break;
}
}
}
public static char cp() {
int showMe = 1 + randomNum.nextInt(2);
if(showMe==1) {
return 'r';
}
else if(showMe==2) {
return 'p';
}
else {
return 's';
}
}
public static boolean doYouWannaPlayAgain() {
System.out.print("Do you wanna play the game again? (y/n): ");
String ch = sc.nextLine();
if(ch.contains("y")) {
return true;
}
else {
return false;
}
}
public static void w() {
if(ch_user.equalsIgnoreCase(ch_comp)) {
System.out.println("It's draw.. Both chosen the same choice..");
}
else if((ch_comp.equalsIgnoreCase("r") && ch_user.equals("s")) || (ch_comp.equals("s") && ch_user.equalsIgnoreCase("p")) ||
(ch_comp.equalsIgnoreCase("p") && ch_user.equalsIgnoreCase("r"))){
System.out.println("System own the game.. ");
System.out.println("System choice: "+ch_comp);
System.out.println("User choice: "+ch_user);
}
else if((ch_comp.equalsIgnoreCase("s") && ch_user.equals("r")) || (ch_comp.equals("p") && ch_user.equalsIgnoreCase("s")) ||
(ch_comp.equalsIgnoreCase("r") && ch_user.equalsIgnoreCase("p"))){
System.out.println("User own the game.. ");
System.out.println("System choice: "+ch_comp);
System.out.println("User choice: "+ch_user);
}
else if(ch_user.equalsIgnoreCase("q")) {
System.out.println("Thank you for playing the game.. Bye Bye!!!");
System.exit(0);
}
else {
System.out.println("You entered an invalid choice..");
}
}
}
Not able to attach output screens due to lack of time. Please comment if you want screens.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.