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

Let's play Rock, Paper, Scissors

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

Problem

I have written my first rock, paper, scissors project recently. I had to use a lot of if statements and System.out.println(); commands in the program. I am wondering what ways I could optimize my program to contain less lines of code/run faster. Are there ways to get the job done using more practical coding techniques like utilizing classes in this program?

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

public class Application {

public static void main(String[] args) {

System.out.println("Welcome to Rock, Paper, Scissors; Let's Play!");

Scanner playerName = new Scanner(System.in);
Scanner roundsToWin = new Scanner(System.in);

System.out.println("Please enter your name: ");
String text = playerName.nextLine();

System.out.println("Hello, " + text + "\nHow many rounds would you like to play?");
int rounds = roundsToWin.nextInt();

int value = 0;
int player = 0;
int computer = 0;

do {
// System.out.println("Current rounds played: " + value);

String[] rockPaperScissors = {"Rock", "Paper", "Scissors"};
Random random = new Random();

Scanner playerChoice = new Scanner(System.in);
System.out.println("Please enter Rock, Paper or Scissors: \nCapitilization Matters!");
String choice = playerChoice.nextLine();
System.out.println();

int select = random.nextInt(rockPaperScissors.length);

System.out.println("Computer selection: " + rockPaperScissors[select]);
System.out.println("Your selection: " + choice);
System.out.println();

if (choice.equals(rockPaperScissors[select])) {
System.out.println("It is a Tie");
}
else {
if(choice.equals("Rock")) {
if(rockPaperScissors[select].equals(rockPaperScissors[1])) {
System.out.println("Pape

Solution

First up, I read carefully your question and found a string 'I have a lot of if statements'.

Perfomance Issue

Unnecessary if checks

Lets look deeply in your code.

if (choice.equals("Rock")) {
    // some code
}
if (choice.equals("Paper")) {
    // some code
}
if (choice.equals("Scissors")) {
    // some code
}


Using only if we will have (it's not a Java code, it's just an example).

choice = "Rock" => 3 check
choice = "Paper" => 3 checks
choice = "Scissors" => 3 checks


In all cases you will have 3 checks.

Solution

From if to if/else if.

if (choice.equals("Rock")) {
    // some code
} else if(choice.equals("Paper")) {
   // some code
} else if(choice.equals("Scissors")) {
  // some code
}


Using if/else if we will have (it's not a Java code, it's just an example).

choice = "Rock" => 1 check
choice = "Paper" => 2 checks
choice = "Scissors" => 3 checks


It's better than previous.

Memory Issue

You don't need to create new variables in each new round of your game. Create once at the beginning.

String[] rockPaperScissors = {"Rock", "Paper", "Scissors"};
Scanner playerChoice = new Scanner(System.in);
Random random = new Random();

do {
   // your code
} while(your_condition);


You don't need 2 scanners for reading user input. You can use one.

Instead of:

Scanner playerName = new Scanner(System.in);
Scanner roundsToWin = new Scanner(System.in);


You can use:

Scanner sc = new Scanner(System.in);
String playerName = sc.nextLine();
int rounds = sc.nextInt();


UPDATE

From if/else if to switch.
Sometimes it's difficult to read a lot of if/else statements in the code. We can replace last if/else if statement with switch to improve readability. Also replace string with enum.

public enum GameChoice {
   Rock,
   Paperm,
   Scissors
}

switch (choice) {
   case GameChoice.Rock:
      //some code
      break;
   case GameChoice.Paper:
      // some code
      break;
   case GameChoice.Scissors:
      // some code
      break;
   default:
      // some code
      break;
}


Bug

There is a probability when (computer == player), but you don't handle it.

if (computer > player) {
   System.out.println("You Lose! Computer Wins");
} else if (computer == player) {
   System.out.println("It is a draw");
} else {
   System.out.println("Congratulations " + text + ", You won!");
}


Naming

Please, provide more intuitive name for variables, because when I first looked in your code I didn't understand variables names: computer and player variables.

computer can be replaced with computerScore

player can be replaced with playerScore

value can be replaced with roundIndex

text can be replaced with playerName

Design Issue

DRY ideology

Do not repeat your self

Look carefully in your code and find a part that repeat.

1) 2 times print the score of the game

System.out.println(playerName + ": " + player);
System.out.println("Computer: " + computer);
System.out.println();


Move this part to a separate function.

private static void printScore(String playerName, int playerScore, int computerScore) {
   System.out.println(playerName + ": " + playerScore);
   System.out.println("Computer:" + computerScore);
   System.out.println();
}


Break big function into small several functions

Do One Thing and Do It Well

1) Function for printing result of the game

private static void printGameResult(String playerName, int playerScore, int computerScore) {
   System.out.println("Final Score!");
   printScore(playerName, playerScore, computerScore);

   if (computerScore > playerScore) {
      System.out.println("You Lose! Computer Wins");
    } else if (computerScore == playerScore) {
      System.out.println("It is a draw");
    } else {
      System.out.println("Congratulations " + playerName + ", You won!");
    }
}

Code Snippets

if (choice.equals("Rock")) {
    // some code
}
if (choice.equals("Paper")) {
    // some code
}
if (choice.equals("Scissors")) {
    // some code
}
choice = "Rock" => 3 check
choice = "Paper" => 3 checks
choice = "Scissors" => 3 checks
if (choice.equals("Rock")) {
    // some code
} else if(choice.equals("Paper")) {
   // some code
} else if(choice.equals("Scissors")) {
  // some code
}
choice = "Rock" => 1 check
choice = "Paper" => 2 checks
choice = "Scissors" => 3 checks
String[] rockPaperScissors = {"Rock", "Paper", "Scissors"};
Scanner playerChoice = new Scanner(System.in);
Random random = new Random();

do {
   // your code
} while(your_condition);

Context

StackExchange Code Review Q#129888, answer score: 8

Revisions (0)

No revisions yet.