DEV Community

Cover image for Simple Java Project: Rock, Paper, Scissors Game (with Detailed Explanation)
Sharique Siddiqui
Sharique Siddiqui

Posted on

Simple Java Project: Rock, Paper, Scissors Game (with Detailed Explanation)

The Rock, Paper, Scissors Game is a classic, fun Java project perfect for beginners looking to practice conditionals, user input, random number generation, comparison logic, and loops. This walkthrough will guide you step-by-step through building a console-based version of the game.

What Does This Rock, Paper, Scissors Project Do?

  • Lets the user select rock, paper, or scissors.
  • Randomly generates the computer’s (opponent’s) choice.
  • Determines and displays the winner based on standard rules.
  • Allows the user to play multiple rounds until they choose to quit.

Step 1: Java Concepts You’ll Practice

  • Scanner for user input.
  • Random for computer’s choice.
  • if-else and switch for game logic.
  • Loops for playing again.
  • String comparison for validating and scoring.

Step 2: Import Necessary Classes

java
import java.util.Scanner;
import java.util.Random;
Enter fullscreen mode Exit fullscreen mode

Step 3: Main Program Structure

Create your class and the main method:

java
public class RockPaperScissors {
    public static void main(String[] args) {
        // All the game logic goes here
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Up Scanner and Random

Initialize objects for user input and randomization:

java
Scanner scanner = new Scanner(System.in);
Random random = new Random();
Enter fullscreen mode Exit fullscreen mode

Step 5: Game Loop and Choices

Use a loop to let the player play as many rounds as they want:

java
String[] options = {"rock", "paper", "scissors"};
String playAgain;

do {
    // Game round logic here

    System.out.print("Play again? (yes/no): ");
    playAgain = scanner.nextLine().toLowerCase();
} while (playAgain.equals("yes"));

System.out.println("Thanks for playing!");
scanner.close();
Enter fullscreen mode Exit fullscreen mode

Step 6: Get User and Computer Choices

Prompt the user for their move. Generate the computer's move randomly.

java
System.out.print("Enter your move (rock, paper, or scissors): ");
String userMove = scanner.nextLine().toLowerCase();

// Validate input
while (!userMove.equals("rock") && !userMove.equals("paper") && !userMove.equals("scissors")) {
    System.out.print("Invalid move! Please enter rock, paper, or scissors: ");
    userMove = scanner.nextLine().toLowerCase();
}

// Computer randomly chooses
int compIndex = random.nextInt(3); // 0, 1, or 2
String computerMove = options[compIndex];

System.out.println("Computer chose: " + computerMove);

Enter fullscreen mode Exit fullscreen mode

Step 7: Decide the Winner

Use if-else or switch logic to determine the winner:

java
if (userMove.equals(computerMove)) {
    System.out.println("It's a tie!");
} else if (
    (userMove.equals("rock") && computerMove.equals("scissors")) ||
    (userMove.equals("scissors") && computerMove.equals("paper")) ||
    (userMove.equals("paper") && computerMove.equals("rock"))
) {
    System.out.println("You win!");
} else {
    System.out.println("You lose!");
}

Enter fullscreen mode Exit fullscreen mode

Step 8: Full Java Code — Rock, Paper, Scissors Game

Here’s the complete version, tying it all together:

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

public class RockPaperScissors {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        String[] options = {"rock", "paper", "scissors"};
        String playAgain;

        System.out.println("Welcome to Rock, Paper, Scissors!");

        do {
            System.out.print("Enter your move (rock, paper, or scissors): ");
            String userMove = scanner.nextLine().toLowerCase();

            // Validate input
            while (!userMove.equals("rock") && !userMove.equals("paper") && !userMove.equals("scissors")) {
                System.out.print("Invalid move! Please enter rock, paper, or scissors: ");
                userMove = scanner.nextLine().toLowerCase();
            }

            // Computer randomly chooses
            int compIndex = random.nextInt(3);
            String computerMove = options[compIndex];

            System.out.println("Computer chose: " + computerMove);

            // Decide winner
            if (userMove.equals(computerMove)) {
                System.out.println("It's a tie!");
            } else if (
                (userMove.equals("rock") && computerMove.equals("scissors")) ||
                (userMove.equals("scissors") && computerMove.equals("paper")) ||
                (userMove.equals("paper") && computerMove.equals("rock"))
            ) {
                System.out.println("You win!");
            } else {
                System.out.println("You lose!");
            }

            System.out.print("Play again? (yes/no): ");
            playAgain = scanner.nextLine().toLowerCase();
        } while (playAgain.equals("yes"));

        System.out.println("Thanks for playing!");
        scanner.close();
    }
}

Enter fullscreen mode Exit fullscreen mode

Key Concepts Practiced

  • Input validation: Ensures only valid moves are accepted.
  • Random number generation: Simulates the computer as a real opponent.
  • Nested conditionals: Builds game score logic.
  • Loop control: Lets the user replay, or exit gracefully.
  • Case-insensitive comparison: Handles user input flexibly.

How to Run This Project

1.Copy the code into a file named RockPaperScissors.java.

2.Open a terminal and compile:

text
javac RockPaperScissors.java
Enter fullscreen mode Exit fullscreen mode

3.Run it:

text
java RockPaperScissors
Enter fullscreen mode Exit fullscreen mode

Enter your move and enjoy the game. After each round, type "yes" to play again or "no" to exit.

Ideas for Expansion

  • Add scoring for user and computer (track wins/losses).
  • Limit total rounds and display a summary at the end.
  • Add more moves (extend to "rock, paper, scissors, lizard, spock").
  • Handle invalid inputs more robustly (e.g., catch exceptions).
  • Create a GUI version using JavaFX or Swing.

Building a Rock, Paper, Scissors Game in Java teaches you practical skills—user interaction, basic logic, and fun experimentation. Tweak it as you learn more, and soon you’ll be ready for more complex Java adventures!

Check out the YouTube Playlist for great java developer content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ... : CodenCloud

Top comments (0)