This is a simple Rock Paper Scissors game implemented in Java.
- Download Gradle if you don't use it.
- Download git if you don't have it.
- Use Java 17 or higher. You can use SDK-Man to manage your Java versions.
Build the project:
./gradlew clean build
Run the tests:
./gradlew test
To start the game, go to Main.java
and run the file.
You will be asked to enter the number of rounds you want to play. After that, you will be asked to enter your choice. You can choose between Rock, Paper, and Scissors. The computer will randomly choose one of the three options. The winner will be decided on the standard rules.
The game is implemented using the Strategy design pattern. SOLID principles are followed as closely as possible. I've provided the explanation for some core classes below.
MoveStrategy
is the interface that defines a contract for the different move strategies. It supports Open-Closed
Principle without modifying existing code.
UserMoveStrategy
is the concrete implementation of the MoveStrategy
interface. It handles only user input. This
follows Single Responsibility Principle and Interface Segregation Principle.
RandomMoveStrategy
is the concrete implementation of the MoveStrategy
interface. It handles only random moves. This
follows Single Responsibility Principle and Interface Segregation Principle. This is currently being as CPU based
Strategy.
MoveRules
Defines rules for the game. It supports Open-Closed Principle without modifying existing code. It also
follows Single Responsibility Principle.
GameResultService
: only deals with the game result. It follows Single Responsibility Principle.
GameRulesService
: only deals with the game rules. It follows Single Responsibility Principle.
UserInputService
: only deals with user input. It follows Single Responsibility Principle and Interface Segregation
Principle. There can be multiple userInput types like. FileInput.
Game
is the main class that orchestrates the flow of the game. It manages rounds, uses MoveStrategy
to make moves,
and lets GameResultService
to give the results.
It doesn't do anything else than orchestrating the flow of the game. The decisions of the game are made by the
GameRulesService
. Thus, it follows Single Responsibility Principle.
It follows Open-Closed Principle. This is because it can be extended without modifying the existing code. It follows Dependency Inversion Principle because it doesn't need to know the concrete implementations of the strategies.