Rock, Paper, Scissors (Java Swing)
Overview
Welcome to your next Java Swing project!
Your task is to build a desktop application that allows a user to play Rock, Paper, Scissors against the computer.
The application must keep track of:
- The player's current win streak
- The player's best streak (high score)
- The computer's choice
- The player's choice
- The result of each round
The high score must persist between application launches by saving it to a JSON file.
This project is designed to introduce you to:
- Object-Oriented Programming
- Java Swing
- Event Handling
- Layout Managers
- Enums
- Random Number Generation
- File I/O
- JSON Serialization
- Separating UI code from business logic
Learning Objectives
By completing this project, you should be able to:
- Create a Swing application
- Create and position Swing components
- Respond to button clicks using ActionListeners
- Generate random values
- Use of Enum to represent game choices
- Extract logic into reusable methods
- Separate business logic from user interface code
- Backend thinking
- Frontend thinking
- Save and load data from a file
- Database thinking
- Design a small application using multiple classes
Functional Requirements
Game Choices
The player must be able to choose:
- Rock
- Paper
- Scissors The computer must randomly choose one of these options when the user clicks a button.
Determining the Winner
The standard Rock, Paper, Scissors rules apply:
| Player | Computer | Result |
|---|---|---|
| Rock | Scissors | Win |
| Rock | Paper | Loss |
| Paper | Rock | Win |
| Paper | Scissors | Loss |
| Scissors | Paper | Win |
| Scissors | Rock | Loss |
| Same Choice | Same Choice | Tie |
Current Streak
The application must track consecutive wins.
Win
Increase current streak by 1.
Tie
Current streak remains unchanged.
Loss
If current streak is greater than the high score:
- Update the high score
Then:
- Reset current streak to 0
High Score
The application must remember the highest streak achieved.
This value must:
- Load when the application starts
- Save when a new high score is achieved
- Persist between application launches
Store the value inside a JSON file.
{
"highScore": 12
}
User Interface Requirements
Your application should contain three major sections.
Top Section
Current Streak: 0
Best Streak: 0
Center Section
Display game information.
Computer Picked: Rock
You Picked: Paper
Result: You Win!
Bottom Section
Provide three buttons:
[ Rock ] [ Paper ] [ Scissors ]
Suggested Layout
+--------------------------------+
| Current Streak: 3 |
| Best Streak: 8 |
+--------------------------------+
| |
| Computer Picked: Rock |
| You Picked: Paper |
| Result: You Win! |
| |
+--------------------------------+
| [Rock] | [Paper] | [Scissors] |
+--------------------------------+
You may choose whichever Layout Manager you feel is most appropriate.
Research your options before starting.
- BorderLayout
- GridLayout
- BoxLayout
- GridBagLayout
Technical Requirements
Requirement 1: Enum
public enum Choice {
ROCK,
PAPER,
SCISSORS
}
Requirement 2: Random Computer Choice
Generate a random choice for the computer. Hint:
Random random = new Random();
There should be an equal chance for each choice.
Requirement 3: Multiple Classes
Do not place everything inside a single class.
Think about responsibilities.
Possible structure:
Choice.java
GameResult.java
GameEngine.java
HighScoreService.java
MainFrame.java
Application.java
You are free to organize differently.
Requirement 4: Separation of Concerns
Try to separate:
UI
Responsible for:
- Buttons
- Labels
- Windows
Business Logic
Responsible for:
- Winner calculation
- Streak updates
- Computer choices
Persistence
Responsible for:
- Loading high score
- Saving high score
Bonus Challenges
If you finish early, try one or more of these:
Bonus 1
Display:
Wins
Losses
Ties
Bonus 2
Add a match history panel. Example:
You: Rock
CPU: Scissors
Result: Win
Bonus 3
Add a Reset High Score button.
Bonus 4
Add keyboard shortcuts. Example:
R = Rock
P = Paper
S = Scissors
Bonus 5
Add custom styling. Examples:
- Different fonts
- Padding
- Borders
- Colors