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

Does my Rock Paper Scissors game look good?

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

Problem

I'm doing some homework and am just curious if everything I've done looks good and/or if you'd suggest I modify something to keep with "javaese."

```
import java.util.Scanner;
import java.util.Random;
public class rockpaperscissors
{
public static void main (String[] args)
{
int cChoice; // variable for computers choice (R/P/S)
int pChoice = 0; // Holds converted choice (R=1, P=2, S=3)
int cScore = 0, pScore = 0, tie = 0, rounds = 0; // Initialised variables for score keeping (c = computer, p = player) plus keeps track of number of rounds played
String loop="yes"; // Starts our loop
Scanner input = new Scanner(System.in); // Creates scanner object
Random rgen = new Random();
System.out.println("Hello, for this exercise we're going to be playing everyone's favourite game, Rock-Paper-Scissors!");

while (loop.equals("yes")) // This loop keeps our game going only while our string.loop is equal to 'yes'
{
cChoice=rgen.nextInt(3)+1;
System.out.println("Please make your selection: R - Rock. P - Paper. S - Scissors");
String hInput = input.nextLine(); // variable for players choice (R/P/S)
String hChoice = hInput.toUpperCase(); //Converts to Upper case in case user did not
if (hChoice.equals("R") || hChoice.equals("P") || hChoice.equals("S")) // Ensures player has entered the correct choice for the game to continue
{
System.out.println("");

if (hChoice.equals("R")) // This converts pChoice to the numeric value for "Rock"
{
pChoice = 1;
}
if (hChoice.equals("P")) // This converts pChoice to the numeric value for "Rock"
{
pChoice = 2;
}
if (hChoice.equals("S")) // This converts pChoice to the numeric value for "Rock"
{

Solution

For your perusal, I took the liberty to clean up and simplify your code a bit.

public class RockPaperScissors
{
  static int cScore, pScore, tie, rounds;

  enum RPS {
    R("Rock", "S"), P("Paper", "R"), S("Scissors", "P");
    private final String beats, name;
    RPS(String name, String beats) { this.beats = beats; this.name = name; }
    int compare(RPS other) { return other == this? 0 : other == valueOf(beats)? 1 : -1; }
    String fullName() { return name; }
  }

  public static void main (String[] args) {
    final Scanner input = new Scanner(System.in);
    final Random rgen = new Random();
    System.out.println("Hello, for this exercise we're going to be playing everyone's favourite game, Rock-Paper-Scissors!");
    while (true) {
      final RPS playerChoice, compChoice = RPS.values()[rgen.nextInt(3)];
      System.out.println("Please make your selection: R - Rock. P - Paper. S - Scissors");
      try { playerChoice = RPS.valueOf(input.nextLine().toUpperCase()); }
      catch (Exception e) {
        System.out.println(
            "Sorry, you didn't pick Rock, Paper, or Scissors. The game will end now.\n" +
            "Here are the final scores after " + rounds +" rounds:\nYou\tComputer\tTies\n" +
            " "+ pScore +"\t   " + cScore + "\t\t " + tie);
        return;
      }
      System.out.println("\nComputer picked " + compChoice.fullName() + "!");
      switch (playerChoice.compare(compChoice)) {
      case 0:
        System.out.println("Tie Game!\n");
        tie++;
        break;
      case -1:
        System.out.println(compChoice.fullName() + " beats " + playerChoice.fullName() + "!");
        System.out.println("**Computer Wins!**\n");
        cScore++;
        break;
      case 1:
        System.out.println(playerChoice.fullName() + " beats " + compChoice.fullName() + "!");
        System.out.println("**Player Wins!**\n");
        pScore++;
      }
      rounds++;
    }
  }
}

Code Snippets

public class RockPaperScissors
{
  static int cScore, pScore, tie, rounds;

  enum RPS {
    R("Rock", "S"), P("Paper", "R"), S("Scissors", "P");
    private final String beats, name;
    RPS(String name, String beats) { this.beats = beats; this.name = name; }
    int compare(RPS other) { return other == this? 0 : other == valueOf(beats)? 1 : -1; }
    String fullName() { return name; }
  }

  public static void main (String[] args) {
    final Scanner input = new Scanner(System.in);
    final Random rgen = new Random();
    System.out.println("Hello, for this exercise we're going to be playing everyone's favourite game, Rock-Paper-Scissors!");
    while (true) {
      final RPS playerChoice, compChoice = RPS.values()[rgen.nextInt(3)];
      System.out.println("Please make your selection: R - Rock. P - Paper. S - Scissors");
      try { playerChoice = RPS.valueOf(input.nextLine().toUpperCase()); }
      catch (Exception e) {
        System.out.println(
            "Sorry, you didn't pick Rock, Paper, or Scissors. The game will end now.\n" +
            "Here are the final scores after " + rounds +" rounds:\nYou\tComputer\tTies\n" +
            " "+ pScore +"\t   " + cScore + "\t\t " + tie);
        return;
      }
      System.out.println("\nComputer picked " + compChoice.fullName() + "!");
      switch (playerChoice.compare(compChoice)) {
      case 0:
        System.out.println("Tie Game!\n");
        tie++;
        break;
      case -1:
        System.out.println(compChoice.fullName() + " beats " + playerChoice.fullName() + "!");
        System.out.println("**Computer Wins!**\n");
        cScore++;
        break;
      case 1:
        System.out.println(playerChoice.fullName() + " beats " + compChoice.fullName() + "!");
        System.out.println("**Player Wins!**\n");
        pScore++;
      }
      rounds++;
    }
  }
}

Context

StackExchange Code Review Q#11444, answer score: 8

Revisions (0)

No revisions yet.