simple rock paper scisscors game in java code example

Example 1: rock paper scissors 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)];
    }
}

Example 2: rock paper scissors 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];
    }
}

Tags:

Misc Example