Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: Write a program that plays the popular rock-paper-scissor game. If

ID: 670412 • Letter: D

Question

Description:

Write a program that plays the popular rock-paper-scissor game. If you don't

know the rules, please look them up. The program will randomly generate a

number between 0 and 2, inclusive, representing scissor, rock, and paper for the

computer player. Then, the program will prompt the user player to enter a number

between 0 and 2, inclusive.

Example Output:

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. Game is a draw.

Steps:

1.

Prompt the user for a number between 0 and 2. Where scissor(0), rock

(1), and paper (2). Make sure user input is valid!

2.

Generate a random number for the computer player. You will be using

the Random class to generate a random number. To declare a random object

use the following syntax:

Random rnd =

new

Random();

//

Declares

and

initializes

a

Random

Object

int

num = rnd.nextInt(3);

//

Generates

a

number

between

0

and

2

3.

Once you have the user's selected value and the computer's randomly

generated value, properly implement decision making in your code that de-

termines the correct winner or if the game is a draw.

4.

Display who is the winner or display if the game is a draw.

1

Grading Rubric

{

(15 points) Does the program correctly read user input. This includes :

Only accepting valid values. (i.e. 0, 1, 2 ), and excludes everything else.

In the case of invalid input, does the program let the user know? It is OK

if your program exits when it detects invalid input given that you display

a good message explaining why the program is exiting.

{

(7 points) Does the program correctly determine winners based on user's

input and computer's randomly generated number?

{

(8 points) Does the program display to the user who wins or loses or if the

game is a draw.

{

(10 points) Is the program well documented. A well documented program

includes good variables names and commenting of non-obvious code.

{

(5 POINTS EXTRA CREDIT) Modify your program to run rock-paper-

scissors in a best of 3 format, i.e. rst player to win 2 out of 3 games wins.

This must be done with 1 execution of the program. Running the program 3

times does not count.

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissor {

   public static boolean userWins(int user, int computer) {
       if (user == 0 && computer == 0) {
           return false;
       } else if (user == 0 && computer == 1) {
           return false;
       } else if (user == 0 && computer == 2) {
           return true;
       } else if (user == 1 && computer == 0) {
           return true;
       } else if (user == 1 && computer == 1) {
           return false;
       } else if (user == 1 && computer == 2) {
           return false;
       } else if (user == 2 && computer == 0) {
           return false;
       } else if (user == 2 && computer == 1) {
           return true;
       } else if (user == 2 && computer == 2) {
           return false;
       }
       return false;
   }
  
   public static String getString(int num){
       if(num == 0){
           return "scissor";
       }
       else if(num == 1){
           return "rock";
       }
       else{
           return "paper";
       }
   }

   public static void main(String args[]) {
       Scanner in = new Scanner(System.in);
       Random r = new Random();
       int userWin = 0, compWin = 0;
       for(int i = 0; i < 3; ++i){
           int choice;
           while(true) {
               System.out.print("scissor (0), rock (1), paper (2): ");
               choice = in.nextInt();
               if(choice < 0 || choice > 2){
                   System.out.println("Invalid input!");
               }
               else{
                   break;
               }
           }
           int comp = r.nextInt(3);
           if(userWins(choice, comp)){
               System.out.println("The computer is " + getString(comp) + ". You are " + getString(choice) + ". You won.");
               ++userWin;
           }
           else{
               if(choice == comp){
                   System.out.println("The computer is " + getString(comp) + ". You are " + getString(choice) + " too. Game is a draw.");  
               }
               else{
                   System.out.println("The computer is " + getString(comp) + ". You are " + getString(choice) + ". You lost.");
                   ++compWin;
               }
           }
       }
       System.out.println("You Won " + userWin + " times");
       System.out.println("Computer Won " + compWin + " times");
       if(userWin > compWin){
           System.out.println("You Won");
       }
       else if(userWin < compWin){
           System.out.println("You Lost");
       }
       else{
           System.out.println("Match Drawn");
       }
   }
}