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

Beginner Rock, Paper, Scissors in Java

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

Problem

I was curious on what, if anything, I could be doing better. I have been spending the last few days working with Java and learning some basics and wanted to try this challenge and making it two classes. I found the scanner is sometimes strange and doesn't take input the first time.

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

public class rps {
public static void main (String args[]){
int input;
int b = 1;
Scanner sage = new Scanner(System.in);
Random rnd = new Random();
System.out.println("Rock Paper Scissors, by Sage!");
System.out.println("Select 1, 2, 3, for Rock, Paper, Scissors");
//Menu Present, pretty bad still

while (b != 0){
int rock = 1, paper = 2, scissors = 3;
input = sage.nextInt();
int randomNumber = rnd.nextInt(3-1+1)+1;

if(randomNumber == rock){
if(input == rock){
System.out.println("Rock vs. Rock, ITS A TIE!");
} else if(input == paper){
System.out.println("Rock vs. Paper! You win!" );
} else if(input == scissors){
System.out.println("Rock vs. Scissors! You lose!");
} //These blocks establish options if the computer got Rock

else if(randomNumber == paper){
if(input == rock){
System.out.println("Paper vs. Rock! You lose!");
} else if(input == scissors){
System.out.println("Paper vs. Scissors! You win!");
} else if(input == paper){
System.out.println("Paper vs. Paper! Its a tie!");
} //These blocks establish the options if comp. got paper

else if(randomNumber == scissors){
if(input == rock){
System.out.println("Scissors vs. Rock! You win!");
} else if(input == scissors){
System.out.println("Scissors vs. Scissors, ITS A TI

Solution

There are plenty of rock-paper-scissors implementations in java on this site. I'll try to keep my suggestions at a beginner level, though.

First, I should point out that your "play again?" mechanism doesn't actually work: you ask the question, but never do anything with the result. Putting the "play again?" prompt in its own class doesn't make much sense; it can just be a function. That function should return a boolean result — true to play again, false to quit. In main(), the while (b != 0) loop would best be written as a do-while loop, since you want to run the game at least once without asking. b is a poor variable name; playAgain would be much more helpful.

The main lesson that I think you should learn from this exercise is object-oriented thinking. Instead of input and randomNumber, how about thinking about a HumanPlayer and a RandomComputerPlayer, both of which can return a choice of "Rock", "Paper", or "Scissors" when asked to play()? The code would better model the game.

To analyze the outcome, you have enumerated all 9 combinations. Of those, three outcomes are ties, which can easily be detected. It would also be better to group all of the winning outcomes and all of the losing outcomes together.

RockPaperScissors.java

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

public class RockPaperScissors {
    private static boolean playAgain(Scanner scanner) {
        System.out.println("Play again? Y(8), N(9)?");
        switch (scanner.nextInt()) {
        case 8:
            System.out.println("Rock, Paper, Scissors!");
            return true;
        default:
            System.out.println("Thanks for playing!");
            return false;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        RPSPlayer computer = new RandomComputerPlayer(new Random());
        RPSPlayer human = new HumanPlayer(scanner);

        System.out.println("Rock Paper Scissors, by 200_success!");
        do {
            String comp = computer.play();
            String you = human.play();

            System.out.printf("%s vs. %s", comp, you);
            if (you.equals(comp)) {
                System.out.println(", IT'S A TIE!");
            } else if ( ("Rock".equals(you) && "Scissors".equals(comp)) ||
                        ("Scissors".equals(you) && "Paper".equals(comp)) ||
                        ("Paper".equals(you) && "Rock".equals(comp)) ) {
                System.out.println("! You win!");
            } else {
                assert (("Rock".equals(comp) && "Scissors".equals(you)) ||
                        ("Scissors".equals(comp) && "Paper".equals(you)) ||
                        ("Paper".equals(comp) && "Rock".equals(you)));
                System.out.println("! You lose!");
            }
        } while (playAgain(scanner));
    }
}


RPSPlayer.java

public interface RPSPlayer {
    String[] CHOICES = new String[] { "Rock", "Paper", "Scissors" };

    /**
     * Returns one of "Rock", "Paper", or "Scissors".
     */
    String play();
}


HumanPlayer.java

import java.util.Scanner;

public class HumanPlayer implements RPSPlayer {
    private final Scanner scanner;

    public HumanPlayer(Scanner scanner) {
        this.scanner = scanner;
    }

    public String play() {
        System.out.println("Select 1, 2, or 3 for Rock, Paper, Scissors");
        int choice = this.scanner.nextInt();
        // Keeping things simple, not doing any validation here
        return CHOICES[choice - 1];
    }
}


RandomComputerPlayer.java

import java.util.Random;

public class RandomComputerPlayer implements RPSPlayer {
    private final Random random;

    public RandomComputerPlayer(Random random) {
        this.random = random;
    }

    public String play() {
        return CHOICES[this.random.nextInt(CHOICES.length)];
    }
}

Code Snippets

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

public class RockPaperScissors {
    private static boolean playAgain(Scanner scanner) {
        System.out.println("Play again? Y(8), N(9)?");
        switch (scanner.nextInt()) {
        case 8:
            System.out.println("Rock, Paper, Scissors!");
            return true;
        default:
            System.out.println("Thanks for playing!");
            return false;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        RPSPlayer computer = new RandomComputerPlayer(new Random());
        RPSPlayer human = new HumanPlayer(scanner);

        System.out.println("Rock Paper Scissors, by 200_success!");
        do {
            String comp = computer.play();
            String you = human.play();

            System.out.printf("%s vs. %s", comp, you);
            if (you.equals(comp)) {
                System.out.println(", IT'S A TIE!");
            } else if ( ("Rock".equals(you) && "Scissors".equals(comp)) ||
                        ("Scissors".equals(you) && "Paper".equals(comp)) ||
                        ("Paper".equals(you) && "Rock".equals(comp)) ) {
                System.out.println("! You win!");
            } else {
                assert (("Rock".equals(comp) && "Scissors".equals(you)) ||
                        ("Scissors".equals(comp) && "Paper".equals(you)) ||
                        ("Paper".equals(comp) && "Rock".equals(you)));
                System.out.println("! You lose!");
            }
        } while (playAgain(scanner));
    }
}
public interface RPSPlayer {
    String[] CHOICES = new String[] { "Rock", "Paper", "Scissors" };

    /**
     * Returns one of "Rock", "Paper", or "Scissors".
     */
    String play();
}
import java.util.Scanner;

public class HumanPlayer implements RPSPlayer {
    private final Scanner scanner;

    public HumanPlayer(Scanner scanner) {
        this.scanner = scanner;
    }

    public String play() {
        System.out.println("Select 1, 2, or 3 for Rock, Paper, Scissors");
        int choice = this.scanner.nextInt();
        // Keeping things simple, not doing any validation here
        return CHOICES[choice - 1];
    }
}
import java.util.Random;

public class RandomComputerPlayer implements RPSPlayer {
    private final Random random;

    public RandomComputerPlayer(Random random) {
        this.random = random;
    }

    public String play() {
        return CHOICES[this.random.nextInt(CHOICES.length)];
    }
}

Context

StackExchange Code Review Q#133878, answer score: 5

Revisions (0)

No revisions yet.