A command-line implementation of the classic Mastermind game in Python. This project was originally created as a university assignment and has been rebuilt with modern Python practices, comprehensive testing, and proper software architecture.
Shahin ABDI
- GitHub: shahinabdi
Mastermind is a code-breaking game where one player creates a secret code and another player tries to guess it within a limited number of attempts. After each guess, the code-maker provides feedback about how many colors are correct and in the correct position.
- Command-line interface
- Single player mode against computer
- Computer player mode with basic AI strategy
- Configurable game rules (code length, available colors, attempts)
- Comprehensive feedback after each guess
- Score tracking
mastermind/
├── conftest.py # Pytest configuration for all tests
├── LICENSE # MIT License file
├── README.md # Project documentation
├── requirements.txt # Project dependencies
├── pytest.ini # Pytest settings
├── setup.py # Package installation configuration
├── src/ # Source code root
│ ├── __init__.py # Makes src a Python package
│ ├── game/ # Game logic package
│ │ ├── __init__.py
│ │ ├── config.py # Game configuration settings
│ │ ├── game_controller.py # Main game controller
│ │ ├── mastermind.py # Core game mechanics
│ │ └── players.py # Player implementations
│ ├── interfaces/ # Abstract base classes
│ │ ├── __init__.py
│ │ ├── game_interface.py # Game interface definitions
│ │ └── player_interface.py # Player interface definitions
│ ├── main.py # Application entry point
│ ├── models/ # Data models (if needed)
│ │ └── __init__.py
│ └── utils/ # Utility functions and constants
│ ├── __init__.py
│ └── constants.py # Game constants and enums
├── tests/ # Test suite directory
│ ├── __init__.py # Makes tests a package
│ ├── test_config.py # Tests for game configuration
│ ├── test_game_controller.py # Tests for game controller
│ ├── test_game.py # Tests for core game mechanics
│ ├── test_main.py # Tests for main entry point
│ └── test_players.py # Tests for player implementations
- Python 3.11.5
- pytest for testing
- mypy for type checking
- Coverage.py for test coverage
- Ensure you have Python 3.11 or later installed:
python --version
- Create and activate a virtual environment:
# Create virtual environment
python -m venv venv
# Activate on Unix/macOS
source venv/bin/activate
# Activate on Windows
venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
python src/main.py
-
Choose game mode:
- Play as Human
- Watch Computer Play
-
If playing as human:
- Enter colors as space-separated values (e.g., "R G B Y")
- Available colors: R (Red), G (Green), B (Blue), Y (Yellow), W (White), K (Black)
- Get fedback after each guess:
- Black dot (●): Correct color and position
- White dot (○): Correct color, wrong position
# Run all tests
pytest
# Run tests with coverage
pytest --cov=src
# Run specific test file
pytest tests/test_game_controller.py
# Type checking
mypy src/
# Code formatting
black src/
# Sort imports
isort src/
- Interface-based design with abstract base classes
- MVC-inspired architecture
- Strategy pattern for player implementations
- Factory pattern for game configuration
GameController
: Manages game flow and player interactionMastermindGame
: Implements core game logicPlayer Interface
: Defines player behavior contractGame Interface
: Defines game behavior contract
- Unit tests for all components
- Mock objects for player interaction
- Parametrized tests for game rules
- Integration tests for game flow
- 90%+ test coverage
MIT License
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
- 1.0.0
- Complete rebuild of university project
- Added comprehensive test suite
- Implemented proper architecture
- Added computer player