Java CLI Challenge - Higher or Lower
Overview
Your task is to create a command-line (CLI) game in Java called Higher or Lower.
The application will generate a random number between 0 and 100 and ask the player whether the next number generated will be higher or lower than the current number.
The game should keep running until the player chooses to quit.
The goal of this project is to help you practice working with:
- Classes and Objects
- Enums
- User Input
- Loops and Conditionals
- File I/O
- JSON Serialization
- Exception Handling
- Input Validation
- Basic Application Design
- Random Number Generation
Learning Objectives
By completing this project, you should be able to:
- Create and organize multiple Java classes
- Read input from the console
- Validate user input
- Generate random numbers
- Persist application data to disk
- Load data when the application starts
- Serialize and deserialize JSON using Jackson Databind
- Handle exceptions gracefully
- Separate responsibilities between classes
Technical Requirements
Your solution must:
- Be written in Java 25
- Use Jackson Databind for JSON serialization
- Run entirely in the terminal/console
- Persist player data using a JSON file
- Validate all user input
- Never crash because of invalid input
- Use object-oriented design
Functional Requirements
First Launch
When the application is launched for the first time:
- Check if a file named
player.jsonexists. - If the file does not exist:
- Ask the user for a nickname.
- Create a new player profile.
- Save the profile to
player.json. Example:
Welcome!
Please enter your nickname:
> MidnightCoder
Returning Player
If player.json already exists:
- Load the player profile from disk.
- Display a greeting using the stored nickname.
Example:
Welcome back, MidnightCoder!
Gameplay
- Generate a random number between 0 and 100.
- Display the current number.
- Ask the user whether the next number will be:
(H) Higher
(L) Lower
(Q) Quit
- Generate a second number.
Important Rule
The second number must never be equal to the current number. If the generated number matches the current number:
- Generate a new number.
- Continue until a different number is produced.
Example Round
Current Number: 47
Will the next number be:
(H) Higher
(L) Lower
(Q) Quit
> H
Next Number: 61
Correct!
Win/Loss Tracking
A correct guess counts as:
Wins +1
An incorrect guess counts as:
Losses +1
Statistics must be updated immediately and saved to disk.
Streak Tracking
Track the player's current streak.
Example
Correct!
Current Streak: 4
If the player guesses incorrectly:
Incorrect!
Current Streak reset to 0.
The application must also track the player's:
Best Streak
If the current streak exceeds the previous best streak, update it.
Quitting
The player should be able to quit at any time.
Example:
Thanks for playing!
Wins: 25
Losses: 12
Best Streak: 8
Player Data
The player profile must contain:
{
"nickname": "MidnightCoder",
"wins": 0,
"losses": 0,
"bestStreak": 0
}
This data should be stored in:
player.json
using Jackson Databind.
Suggested Project Structure
You are free to organize your project however you like.
One possible design might include:
Application
Player
PlayerService
GameService
RandomNumberGenerator
Think carefully about what responsibility each class should have.
Avoid putting all logic into a single class.
Hints
Enum
Consider using an enum for player choices.
Example values:
HIGHER
LOWER
QUIT
Input Validation
Users will enter invalid data.
Examples:
banana
123
?
Your application should detect invalid input and ask again.
Saving Data
Consider saving the player's profile whenever:
- A win occurs
- A loss occurs
- The best streak changes
- The application exits
This helps prevent lost progress.
Constraints
You may NOT:
- Use a graphical user interface (GUI)
- Ignore invalid user input
- Hardcode player information
Bonus Challenges
Choose one or more of the following:
Bonus #1 - Statistics Screen
Allow players to view their statistics at any time.
Examples:
- Total Wins
- Total Losses
- Win Percentage
- Best Streak
Bonus #2 - Main Menu
Create a menu system.
Example:
1. Play Game
2. View Statistics
3. Exit
Bonus #3 - Multiple Player Profiles
Allow players to create and load multiple profiles.
Examples:
profiles/
alice.json
bob.json
charlie.json
Bonus #4 - Game History
Track previous rounds and save them to a file.
Example information:
- Previous Number
- Next Number
- Player Guess
- Result
Bonus #5 - Colored Console Output
Use ANSI escape codes to display:
- Green for correct guesses
- Red for incorrect guesses
- Yellow for warnings or invalid input