A console-based Mastermind-style number game written in Java. The game generates a secret four-digit number and asks the player to identify it using feedback after each guess.
- Java Development Kit (JDK) 25
- Apache Maven
The project is configured with Java 25 as both its Maven compiler source and target version.
- At the start of each round, the game generates a four-digit secret number.
- The secret number is between
1000and9999, so its first digit is not zero. - All four digits in the secret number are different.
- You have up to 10 attempts per round.
- A guess must contain exactly four characters, digits only, and four different digits.
- A bull (
+) is a digit that is correct and in the correct position. - A cow (
-) is a digit that exists in the secret number but is in the wrong position. - The round is won with four bulls. If all 10 attempts are used, the secret number is revealed.
For example, if the secret number is 1234 and the guess is 1524, the result is +2 -1: two digits are in the correct position and one additional digit is present but misplaced.
Clone the repository and enter the project directory:
git clone https://github.com/suleymanibis/mastermind-game-java.git
cd mastermind-game-javaCompile the project with Maven:
mvn compileAfter compilation, start the application with:
java -cp "target\classes" Mastermindjava -cp "target/classes" MastermindThe game prints the number of remaining attempts, prompts for a guess, and displays feedback in the +bulls -cows format. After a round, enter a response beginning with y to play again; any other response exits the game.
When a round is won, the score is the number of attempts remaining. The best score is retained while the application is running.
Run the complete JUnit test suite with:
mvn testThe tests cover validation of length, numeric content, and duplicate digits; evaluation of bulls and cows; and generation of four-digit numbers with unique digits and a non-zero first digit.
src/
├── main/java/
│ ├── Mastermind.java # Application entry point
│ ├── MastermindGame.java # Console game loop
│ ├── NumberGenerator.java # Secret-number generation
│ ├── InputValidator.java # Guess validation
│ ├── GuessEvaluator.java # Bulls-and-cows evaluation
│ └── ... # Result types
└── test/java/ # JUnit 5 tests
No license file is currently included in the repository.