94.0k views
4 votes
Write this program using an IDE. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. This lab asks you to create a text game Rock Paper Scissors. The rule of the game: There are two players in this game and they can choose Rock, Paper, or Scissors each time. Rock beats Scissors, Paper beats Rock and Scissors beat Paper. Your program needs to prompt a user to choose an action (Rock, Paper, or Scissors) and save their choice. The program randomly chooses an action (Hint: use .nextInt() to generate random integers in the range of [1,3]. 1 indicates Rock, 2 indicates Paper and 3 indicates Scissors.) Then the program determines who is the winner.

When input is R Please select one of [R/P/S]: You chose: Rock I chose: Scissors Rock beats scissors - you win! If the user enters the lowercase, your program can still recognize. When input is p Output should be Please select one of [R/P/S]: You chose: Paper I chose: Scissors Scissors beat paper - you lose! If the user enters invalid input, the program will give an error message and set the default choice as Rock When input is v Output should be Please select one of [R/P/S]: Invalid choice! Defaulting to Rock. I chose: Scissors Rock beats scissors - you win! If the user has the same result as the computer, your program should output: A Tie!

User Delmet
by
7.2k points

1 Answer

4 votes

Answer:

import java.util.Scanner; //to accept input from user

import java.util.Random; //to generate random numbers

public class Main { //class name

public static void main(String[] args) { //main method

System.out.println("Please select one of [R/P/S]: "); //prompts user to select a choice

Scanner scanner = new Scanner(System.in); //reads input choice

String playerChoice = scanner.next().toUpperCase().replace(" ", ""); //converts the choice to upper case and stores it in playerChoice

if (playerChoice.equals("R") || playerChoice.equals("P") || playerChoice.equals("S")) { // if player choice is a valid choice

results(CompChoice(), playerChoice); // calls method to compute result of game

} else { //if player enters invalid choice

System.out.println("\\Invalid choice!"); } } //error message

private static String CompChoice() { //to generate computer choice

Random generator = new Random(); //to generate random numbers

int comp = generator.nextInt(3)+1; //generates 3 random numbers

String compChoice = ""; //to store computer choice

switch (comp) { //checks the random number for corresponding computer choice

case 1: //1 is for Rock

compChoice = "Rock"; //sets compChoice to Rock

break;

case 2: //random number 2 is for Paper

compChoice = "Paper"; //sets compChoice to Paper

break;

case 3: //random number 3 is for Scissors

compChoice = "Scissors"; //sets compChoice to Scissors

break; }

return compChoice; } //returns computer choice

private static void results(String compChoice, String playerChoice) { //method to compute result

if(playerChoice.equals("R")) //if playerChoice is equal to R

{ playerChoice = "Rock"; //stores Rock to playerChoice

System.out.println("You chose: Rock");} //displays player choice

else if(playerChoice.equals("P")) //if playerChoice is equal to P

{ playerChoice = "Paper"; //stores Paper to playerChoice

System.out.println("You chose: Paper");} //displays player choice

else //if playerChoice is equal to S

{ playerChoice="Scissors"; //stores Scissors to playerChoice

System.out.println("You chose: Scissors"); } //displays player choice

System.out.println("I chose: " + compChoice); //displays computer choice

if (compChoice.equals(playerChoice)) { //if compChoice is equal to playerChoice

System.out.println("a tie!"); //its a tie if computer and player choice is the same

System.exit(1); }//exits on tie

boolean youWin = false; //variable to check if player wins

if (compChoice.equals("Rock")) { //if computer choice is Rock

if (playerChoice.equals("Paper")) //if player choice is Paper

{System.out.println("Paper beats Rock.");

youWin = true;} //player wins to youWin is set to true

else if(playerChoice.equals("Scissors")) { //if player choice is Scissors

System.out.println("Rock beats scissors.");

youWin = false;} } //player loses to youWin is set to false

if (compChoice.equals("Paper")) { //if computer choice is Paper

if (playerChoice.equals("Rock")) //if player choice is Rock

{System.out.println("Paper beats Rock.");

youWin = false;} //player loses to youWin is set to false

else if(playerChoice.equals("Scissors")) { //if player choice is Scissors

System.out.println("Scissors beats Paper.");

youWin = true;} } //player wins to youWin is set to true

if (compChoice.equals("Scissors")) { //if computer choice is Scissors

if (playerChoice.equals("Rock")) //if player choice is Rock

{System.out.println("Rock beats Scissors.");

youWin = true;} //player wins to youWin is set to true

else if(playerChoice.equals("Paper")) { //if player choice is Paper

System.out.println("Scissors beats Paper.");

youWin = false;} } //player loses to youWin is set to false

if (youWin) //if player wins means if youWin is true

System.out.println("you win!"); //declare player to be winner

else //if player loses means if youWin is false

System.out.println("you lose!"); } } //declare player to lose

Step-by-step explanation:

The program is well explained in the comments attache with each line of program. The program has three methods. One is the main method to start the game and prompts user to enter a choice and converts the user input choice to upper case. One is CompChoice method that generates three random numbers and uses switch statement to set 1 for Rock, 2 for Paper and 3 for Scissors . One method is results that takes computer choice and player choice as parameters to decide the winner. It uses if else statements to check if choice of user wins from choice of computer or vice versa. It uses a boolean variable youWin which is set to true when player choice beats computer choice otherwise is set to false. At the end if youWin is true then the player is declared winner otherwise not. The screenshot of the program output is attached.

Write this program using an IDE. Comment and style the code according to CS 200 Style-example-1
User EpicPandaForce
by
6.6k points