HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Rock, Paper, Scissors 3-part

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scissorspaperrockpart

Problem

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;
}

Solution

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  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.

Code Snippets

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++;
            }
        }
    }
}

Context

StackExchange Code Review Q#107558, answer score: 11

Revisions (0)

No revisions yet.