Hangman
Description
Hangman is a command-line word-guessing game implemented in Python. Players guess a hidden word one letter at a time before running out of attempts. The game supports multiple difficulty levels, saved games, and a multiplayer challenge mode.
This project demonstrates object-oriented design, input validation, persistence
with pickle, modular architecture, and automated testing with pytest.
Demo
Features
- Three difficulty levels with different word lengths
- Single-player and multiplayer modes
- Game saving and loading in single-player mode
- Automatic deletion of completed saved games
- Input validation and error handling
- Automatic display of numbers, spaces, and punctuation in hidden words
- Automated tests with
pytest - Interactive command-line gameplay
Installation
To install the game with uv:
uv tool install hangman-p
This installs the game in an isolated environment and makes the hangman-p
command available on your PATH.
You can also try the game without permanently installing it:
uvx --from hangman-p hangman-p
To install it into your active Python environment with pip:
python -m pip install hangman-p
Usage
Hangman is an interactive terminal game. After installation, start it with:
hangman-p
You can also run it as a Python module:
python -m hangman_p
How to play
Welcome to Hangman, the no-nonsense game. Be smart, then you live. If not, you'll have to die by hanging.
The main menu gives you four options:
- Press
Por enterplayto start a game. - Press
Ior enterinstructionto read the instructions. - Press
Lor enterloadto continue a saved game. - Press
Qor enterquitto leave the game.
When you start a game, choose a difficulty level and either play against the computer or challenge another player. The hidden word is represented by underscores, with one underscore for each letter. Numbers, spaces, and punctuation are displayed automatically.
Guess one letter at a time. A correct guess reveals every matching position in the word. You win by revealing all the letters within the available number of attempts. Failure means death by hanging.
Multiplayer note
When the challenger enters the secret word, the input is masked so the guessing player cannot see it.
Extra commands
| Command | Action |
|---|---|
:c |
Reveal the secret word. This is a cheat, so use it wisely. |
:h or history |
Display your correct and incorrect guesses. |
:q or quit |
Quit the current game. In single-player mode, you can save before leaving. |
Project structure
project.py # Compatibility wrapper
hangman_p/project.py # Packaged CLI entry point
hangman_p/level.py # Difficulty configuration
hangman_p/game_engine.py # Core gameplay logic
hangman_p/dictionary.txt # Word list for single-player mode
hangman_p/game_persistence.py # Saved-game handling
pyproject.toml # Package metadata and dependencies
test_project.py # Automated tests
Design decisions
- Encapsulated game state: I selected an object-oriented design so each game engine can own and manage changing state, including the hidden word, guesses, remaining attempts, and player information.
- Object persistence: I chose
pickleto serialize complete game objects and restore them when a player continues a saved game. - Reusable input validation: I extracted the validation logic into the
reusable
cli-input-validatorpackage instead of duplicating it throughout the game. - Testable terminal input: I used dependency injection where the game needs
a custom input function, such as masked input, and
pytestmonkeypatching to test interactive input and output without requiring a live terminal session.
Project history
Hangman began as my CS50P final project and has since evolved into an installable, tested CLI package published on PyPI. You can watch the original project demo.
Contributing
Contributions are welcome. You do not need to build or publish distribution files to contribute.
-
Fork the repository on GitHub and clone your fork.
-
Create a branch for your change:
git switch -c feature/your-change
-
Install the project dependencies and run the game locally:
uv sync uv run hangman-p
-
Run the tests before submitting your change:
uv run --with pytest pytest
-
Commit your changes, push your branch, and open a pull request against the
mainbranch of this repository.
Author
Mayowa Pitan
Enjoy your Hangman experience and try not to be hanged.
Good luck!
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hangman_p-0.1.7.tar.gz.
File metadata
- Download URL: hangman_p-0.1.7.tar.gz
- Upload date:
- Size: 139.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a52297528f5c796ba5714d1697914f813035795b9982209c9ac7f254a85559b
|
|
| MD5 |
38feee43023914ec5a26a7638b9dcf25
|
|
| BLAKE2b-256 |
165dc82a143616816cf574c7c193268e65ee2b616744b7b5450649da87660dbe
|
Provenance
The following attestation bundles were made for hangman_p-0.1.7.tar.gz:
Publisher:
release.yml on VanPaitin/hangman-p
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hangman_p-0.1.7.tar.gz -
Subject digest:
3a52297528f5c796ba5714d1697914f813035795b9982209c9ac7f254a85559b - Sigstore transparency entry: 2647301628
- Sigstore integration time:
-
Permalink:
VanPaitin/hangman-p@d01e75f634fc3eaac80065daa10cc03337472da6 -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/VanPaitin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d01e75f634fc3eaac80065daa10cc03337472da6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file hangman_p-0.1.7-py3-none-any.whl.
File metadata
- Download URL: hangman_p-0.1.7-py3-none-any.whl
- Upload date:
- Size: 138.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b37de0cdcf726c587da66bcbb6992d7de84ad93531517a65988681b044a36cf
|
|
| MD5 |
f512a74caae35d62ad0d372b7d9f1e77
|
|
| BLAKE2b-256 |
7768257d9780480b822ac99c80aa09cd465a7adc2a3fe693464e7d487f637c4b
|
Provenance
The following attestation bundles were made for hangman_p-0.1.7-py3-none-any.whl:
Publisher:
release.yml on VanPaitin/hangman-p
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hangman_p-0.1.7-py3-none-any.whl -
Subject digest:
0b37de0cdcf726c587da66bcbb6992d7de84ad93531517a65988681b044a36cf - Sigstore transparency entry: 2647301633
- Sigstore integration time:
-
Permalink:
VanPaitin/hangman-p@d01e75f634fc3eaac80065daa10cc03337472da6 -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/VanPaitin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d01e75f634fc3eaac80065daa10cc03337472da6 -
Trigger Event:
push
-
Statement type: