6
\$\begingroup\$

Write a program that lets a user play "Rock, Paper, Scissors" against the computer. The program should ask the user to choose one of the three choices, and then the computer randomly picks one (without knowing what the user has chosen). For this problem, the user should be asked to enter an integer: 0 for rock, 1 for paper, or 2 for scissors. Rock beats scissors, scissors beats paper, paper beats rock. The program should say who wins, and then keep playing until someone (the user or the computer) has won 10 rounds. The computer needs to keep track of the current score and should also display it before each round.


import java.util.Scanner;
public class rock_paper
{

public static void main(String args[])
{
    int count_win=0;
    int user_choice;
    String choice=" ";
    int num_comp_wins=0;
    int num_user_wins=0;
    while(num_comp_wins!=10||num_user_wins!=10)
    {
    /* Read user input and determine choice */

    System.out.println("\n Let us play Rock Paper Scissors \n");
    System.out.println("\n Enter your choice 0 for rock, 1 for paper, or 2 for scissors \n");
    Scanner keyboard = new Scanner(System.in);
    user_choice=keyboard.nextInt();
    System.out.println("\n Your choice was \n"+user_choice);
    if(user_choice==0)
    {
        choice="rock";
    }
    else if(user_choice==1)
    {
        choice="paper";
    }
    else if(user_choice==2)
    {
        choice="scissor";
    }
    else
    {
        System.out.println("Incorrect choice");
        choice=null;
    }
    System.out.println(choice);

    /* Randomly generate computer input */

    Random rand = new Random();
    int computer_choice = rand.nextInt(3) + 1;
    String choice1="";
    if(computer_choice==0)
    {
        choice1="rock";
    }
    else if(computer_choice==1)
    {
    choice1="paper";
    }
    else if(computer_choice==2)
    {
    choice1="scissor";
    }
    else
    {
        System.out.println("Incorrect choice");
        choice1=null;
    }
    System.out.println(choice1);
   /* Compare user and computer response*/
    if (choice.equalsIgnoreCase("rock")) {
    if (choice1.equalsIgnoreCase("scissor"))
    {
        System.out.println("\nThe user wins!");
        num_user_wins++;
    }
    else if (choice1.equalsIgnoreCase("paper"))
    {
        System.out.println("\nThe computer wins!");
        num_comp_wins++;
    }
    else
        System.out.println("Tie");
}
else if (choice.equalsIgnoreCase("paper"))
{
    if (choice1.equalsIgnoreCase("scissor"))
    {
        System.out.println("\nThe computer wins!");
        num_comp_wins++;
    }
    else if (choice1.equalsIgnoreCase("rock"))
    {
        System.out.println("\nThe user wins!");
        num_user_wins++;
    }
    else
         System.out.println("Tie");
}
else if (choice.equalsIgnoreCase("scissor"))
{
    if (choice1.equalsIgnoreCase("rock"))
    {
        System.out.println("\nThe computer wins!");
        num_comp_wins++;
    }
    else if (choice1.equalsIgnoreCase("paper"))
    {
        System.out.println("\nThe user wins!");
        num_user_wins++;
    }
    else
         System.out.println("Tie");
}

}


}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Have you learned about arrays yet? Have you learned about switch statements yet? That will help guide any answers. \$\endgroup\$ – Eric Stein Oct 14 '15 at 18:08
  • \$\begingroup\$ Next: Rock Paper Scissors Lizard Spock? :-) \$\endgroup\$ – Mathieu Guindon Oct 14 '15 at 18:11
10
\$\begingroup\$

Naming

Java classes start with a capital letter and do not use underscores, so rock_paper should be RockPaper. Java uses camelCase, not snake_case, for all variable names except constants (static finals), so user_choice should be userChoice.

Spacing

The convention in java is to include whitespace before and after operators such as +, =, != or ==, so count_win=0 should be countWin = 0. Curly braces should be on the same line as an if or else.

Variable Declarations

You declare a new Random() inside your loop, so you might wind up making 19 Random objects, when you only need one. Declare it outside the loop. Conversely, you declare user_choice and computer_choice outside the loop, but they're only used inside. They should have the smallest possible scope.

Branching

You're branching (using if conditional statements) a lot to look up the name of a numeric selection. If you've learned about arrays, you could use a String[] of choices to simplify that lookup. There are a ton more branches to determine who wins. You only need one comparison to determine if there's a tie, and then only three specific checks to see if the user wins. Otherwise, the computer wins.

Making all those changes might look something like this:

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

public final class RockPaperScissors {

    private static final String[] CHOICES = { "rock", "paper", "scissors" };

    public static void main(final String[] argv) {
        final Random random = new Random();
        final Scanner keyboard = new Scanner(System.in);

        int computerWins = 0;
        int userWins = 0;

        System.out.println("\n Let us play Rock Paper Scissors \n");
        while (computerWins <= 10 && userWins <= 10) {

            /* Read user input and determine choice */    
            System.out.println("\n Enter your choice 0 for rock, 1 for paper, or 2 for scissors \n");
            final int userChoice = keyboard.nextInt();
            if (userChoice < 0 || userChoice > 2) {
                System.out.println("Incorrect choice");
                continue;
            } else {
                System.out.println("\n Your choice was \n" + CHOICES[userChoice]);
            }

            /* Randomly generate computer input */
            final int computerChoice = random.nextInt(3) + 1;
            System.out.println("\n The computer choice was \n" + CHOICES[computerChoice]);

            if (userChoice == computerChoice) {
                System.out.println("Tie");
                continue;
            }

            if (userChoice == 0 && computerChoice == 1
                    || userChoice == 1 && computerChoice == 2
                    || userChoice == 2 && computerChoice == 0) {
                System.out.println("\nThe user wins!");
                userWins++;
            } else {
                System.out.println("\nThe computer wins!");
                computerWins++;
            }
        }
    }
}

Review Note: I intentionally don't close the Scanner because I'm assuming that hasn't been covered yet.

\$\endgroup\$
  • \$\begingroup\$ You could add a note that by using continue related to the incorrect choice, you are also avoiding a NULL exception which the OP has in the code. Good work! \$\endgroup\$ – holroy Oct 14 '15 at 19:32
3
\$\begingroup\$

I'm going to suggest you use a two dimension array look up to determine the win lose matrix.

//-1 = user loses
// 0 = tie 
// 1 = user wins
int[][] winArray = new int[
            [0, -1, 1], 
            [1,  0,-1], 
            [-1, 1, 0]
      ]; 

int userMove;      //get from user input
int computerMove;  //randomly generated

int result = winArray[userMove, computerMove]
//now determine what happens if the user wins/loses/ties

Much simpler.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.