A classic color-based code-breaking game built in Python with intelligent feedback system
About • Installation • How to Play • Features
Mastermind is an interactive command-line game where players attempt to crack a secret 4-color code within 10 guesses. The game provides strategic feedback after each guess, indicating correct positions and correct colors in wrong positions. Built with Python's random module and featuring robust input validation.
|
|
# Requires Python 3.6 or later
python --versionClone & Run:
# Clone the repository
git clone https://github.com/yourusername/mastermind-game.git
cd mastermind-game
# Run the game
python mastermind.pyOr download and run directly:
python mastermind.py- Objective: Guess the secret 4-color code within 10 attempts
- Color Options: Choose from 6 colors (numbered 0-5)
- Feedback System:
2= Correct color in correct position1= Correct color in wrong position
- Win Condition: Get all four colors in the correct positions
[2, 2, 2, 2]
The secret code has been chosen. You have 10 tries to guess the code.
-----------------------------
Make a guess of four colors:
0 - red
1 - orange
2 - yellow
3 - green
4 - blue
5 - purple
-----------------------------
Guess color: 0
Guess color: 1
Guess color: 2
Guess color: 3
-----------------------------
Your guess is:
['red', 'orange', 'yellow', 'green']
Your clue is: [1, 2]
You have 9 guesses left
| Clue | Meaning | Example |
|---|---|---|
[2, 2, 2, 2] |
All correct - You win! | Code: [red, blue, green, yellow]Guess: [red, blue, green, yellow] ✅ |
[2, 2, 1, 1] |
2 correct positions, 2 correct colors wrong positions | Code: [red, blue, green, yellow]Guess: [red, blue, yellow, green] |
[1, 1, 1] |
3 correct colors, all in wrong positions | Code: [red, blue, green, yellow]Guess: [blue, yellow, red, orange] |
[] |
No correct colors | Code: [red, blue, green, yellow]Guess: [orange, purple, orange, purple] ❌ |
┌──────────────────────────────┐
│ Main Game Loop │
├──────────────────────────────┤
│ generateCode() │ → Creates random 4-color code
│ getGuess() │ → Gets player input with validation
│ CheckGuess(guess, code) │ → Evaluates guess & returns clues
│ main(seedValue) │ → Controls game flow & replay logic
└──────────────────────────────┘🔵 generateCode()
# Generates a random 4-color code
# Uses random.randint() for index selection
# Returns: List of 4 color strings🔵 getGuess()
# Prompts player for 4 color choices (0-5)
# Validates input with try-except blocks
# Handles both invalid numbers and non-numeric input
# Returns: List of 4 color strings**🔵 CheckGuess(guess, co