determining the winner in a rock paper scissor game
Categories:
Mastering Rock, Paper, Scissors: A Java Implementation Guide

Learn how to programmatically determine the winner in a Rock, Paper, Scissors game using Java, covering game logic, input handling, and clear method design.
Rock, Paper, Scissors is a classic hand game, often used as a simple decision-making method. Implementing this game in code is an excellent way to practice conditional logic and method design. This article will guide you through creating a Java program that can accurately determine the winner of a single round based on two player choices.
Understanding the Game Rules
Before diving into code, it's crucial to formalize the rules of Rock, Paper, Scissors. There are three possible moves, and each move defeats one other move while being defeated by another. The rules are as follows:
- Rock crushes Scissors
- Paper covers Rock
- Scissors cuts Paper
If both players choose the same move, the round is a tie. We'll represent these moves using an enum
in Java for clarity and type safety.
flowchart TD A[Player 1 Choice] --> B{Player 2 Choice?} B -- Rock --> C{P1: Rock} B -- Paper --> D{P1: Paper} B -- Scissors --> E{P1: Scissors} C -- Rock --> F[Tie] C -- Paper --> G[Player 2 Wins] C -- Scissors --> H[Player 1 Wins] D -- Rock --> I[Player 1 Wins] D -- Paper --> J[Tie] D -- Scissors --> K[Player 2 Wins] E -- Rock --> L[Player 2 Wins] E -- Paper --> M[Player 1 Wins] E -- Scissors --> N[Tie] F,G,H,I,J,K,L,M,N --> O[End Game]
Decision flow for determining the winner in Rock, Paper, Scissors.
Designing the determineWinner
Method
The core of our game logic will reside in a method responsible for taking two player choices and returning the outcome. We'll define an enum
for the possible moves and another for the game's outcome. This approach makes the code more readable and less prone to errors compared to using raw strings or integers.
public enum Move {
ROCK,
PAPER,
SCISSORS;
// Optional: Add a method to check what this move beats
public boolean beats(Move other) {
if (this == other) return false; // Cannot beat itself
return switch (this) {
case ROCK -> other == SCISSORS;
case PAPER -> other == ROCK;
case SCISSORS -> other == PAPER;
};
}
}
public enum Outcome {
PLAYER1_WINS,
PLAYER2_WINS,
TIE
}
public class RockPaperScissors {
public static Outcome determineWinner(Move player1Move, Move player2Move) {
if (player1Move == player2Move) {
return Outcome.TIE;
} else if (player1Move.beats(player2Move)) {
return Outcome.PLAYER1_WINS;
} else {
return Outcome.PLAYER2_WINS;
}
}
public static void main(String[] args) {
// Example Usage:
System.out.println("Rock vs Scissors: " + determineWinner(Move.ROCK, Move.SCISSORS)); // PLAYER1_WINS
System.out.println("Paper vs Rock: " + determineWinner(Move.PAPER, Move.ROCK)); // PLAYER1_WINS
System.out.println("Scissors vs Paper: " + determineWinner(Move.SCISSORS, Move.PAPER)); // PLAYER1_WINS
System.out.println("Rock vs Paper: " + determineWinner(Move.ROCK, Move.PAPER)); // PLAYER2_WINS
System.out.println("Rock vs Rock: " + determineWinner(Move.ROCK, Move.ROCK)); // TIE
}
}
Java code for Move
and Outcome
enums and the determineWinner
method.
beats
method within the Move
enum encapsulates the winning logic, making determineWinner
cleaner.Extending the Game: Input and User Interaction
While the determineWinner
method is the core logic, a complete game would involve taking input from players. This could be done via console input, a GUI, or even random generation for a computer opponent. The beauty of separating the logic into a dedicated method is that it can be easily integrated into any input mechanism.
import java.util.Scanner;
public class RockPaperScissorsInteractive {
// ... (Move and Outcome enums and determineWinner method from above) ...
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Rock, Paper, Scissors!");
System.out.println("Enter your move (ROCK, PAPER, SCISSORS):");
Move player1Move = null;
while (player1Move == null) {
try {
String input = scanner.nextLine().toUpperCase();
player1Move = Move.valueOf(input);
} catch (IllegalArgumentException e) {
System.out.println("Invalid move. Please enter ROCK, PAPER, or SCISSORS.");
}
}
// For simplicity, let's have Player 2 choose randomly
Move[] moves = Move.values();
Move player2Move = moves[(int) (Math.random() * moves.length)];
System.out.println("Player 1 chose: " + player1Move);
System.out.println("Player 2 chose: " + player2Move);
Outcome outcome = determineWinner(player1Move, player2Move);
System.out.println("Result: " + outcome);
scanner.close();
}
}
Example of integrating user input and random computer choice with the determineWinner
method.