130k views
1 vote
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs: scissor (0), rock (1), paper (2): 1

The computer is scissor. You are rock. You won scissor (0), rock (1), paper (2): 2
The computer is paper. You are paper too. It is a draw

User Titlacauan
by
5.0k points

1 Answer

5 votes

Answer:

Here is the JAVA program:

import java.util.Scanner; //used to take input from user

public class RockPaperScissor{

public static void main(String[] args) { //start of main function

Scanner input = new Scanner(System.in); // creates Scanner class object

String[] choice = {"Scissor","Rock","Paper"}; // stores choices

System.out.print("scissor (0), rock (1), paper (2), quit (3): "); //prompts user to enter a choice 0 for scissor 1 for rock and 2 for paper and 3 to exit

int userChoice = input.nextInt(); //reads user's choice

while (userChoice != 3) {// loop iterates until user enters 3 to quit

if ( userChoice>=0 && userChoice<3) { //checks if use enters choice from 0 to 2 (for each option)

int compChoice = (int)(Math.random() * 2); //generate random number between 0 to 2

System.out.print("The computer is " + choice[compChoice]); //prints computer choice from rock paper scissor

System.out.print(" You are " + choice[userChoice]); //prints user choice from rock paper scissor

winner(compChoice,userChoice); } //calls winner method to declare winner

System.out.print("scissor (0), rock (1), paper (2) quit (3): "); //prompts user to enter a choice

userChoice = input.nextInt(); } } //reads choice from user

public static void winner(int computer, int player){//method to declare winner

boolean win = false; // boolean type variable initialized to false

if (computer == player) // if choice of computer is same as that of user

System.out.println(" It is a draw"); //prints this message if there is a tie

else }}//user loses

Step-by-step explanation:

The program prompts user to enter a choice. The choice is 0 for scissor, 1 for rock, 2 for paper and 3 to quit.

The while loop continues to execute until the user enters 3 to quit the program. Inside the while loop, the if condition checks if the user enters a valid choice that is from 0 to 2. For computer choice, the program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper using Math.random() method from the range 0 to 2.

Then the function winner() is called to declare the winner of the game. The winner method takes two arguments i.e computer choice and player choice. Lets say user enters 0 (scissors). The variable win has the following options in order to declare the winner. If anyone of these evaluate to true then the user wins otherwise the user loses.

win = (player == 0 && computer == 2) || (player == 1 && computer == 0) ||

(player == 2 && computer == 1);

In the above statement the first part is (player == 0 && computer == 2) means if player choice is 0 (scissor) and computer's choice is 2 (randomly generated) which represents paper then this means scissors cuts paper so user wins.

The second part (player == 1 && computer == 0) means if player choice is 1 (rock) and computer's choice is 0 (randomly generated) which represents scissor then this means rock crushes scissors so user wins.

The third part of the above statement (player == 2 && computer == 1) means if player choice is 2 (paper) and computer's choice is 1 (randomly generated) which represents rock then this means paper covers rock so user wins.

if (wins) means if any of the above mentioned conditions in win holds true then the user wins else user loses.

The screenshot of program output is attached.

(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper-example-1
User TheCrzyMan
by
4.8k points