Video Demo: https://youtu.be/9O5YFeWEpbk
This project is a simple implementation of the classic game "Hangman" in Python. The game is designed to be run in the command line and offers a fun and interactive way to guess words letter by letter.
In this version of Hangman, a player tries to guess a word by suggesting letters within a certain number of guesses. Each wrong guess brings the hangman closer to being fully drawn and the game closer to an end. The game uses a list of words imported from a file and ASCII art to visually represent the hangman's stages.
- The game selects a random word from a predefined list of words.
- The player guesses one letter at a time.
- If the guessed letter is in the word, the game reveals its position(s) in the word.
- If the guessed letter is not in the word, one life is deducted and part of the hangman is drawn.
- The game continues until either the player guesses the word correctly or runs out of lives.
- Random word selection from an external file.
- Dynamic ASCII art to represent the hangman.
- Input validation to ensure that only single letters are accepted as guesses.
- Case-insensitive guessing.
- Python 3.x
- External files:
words.txt
for the word list andhangman.txt
for the ASCII art stages.
- Ensure Python 3.x is installed on your system.
- Clone this repository or download the source code.
- Place
words.txt
andhangman.txt
in the same directory asproject.py
. - Run the game using the command
python project.py
.
project.py
: Contains the main game logic and functions.words.txt
: A text file containing possible words to guess.hangman.txt
: A text file containing the ASCII art for the hangman stages.lose.txt
: A text file containing the ASCII art for the game over - lose.win.txt
: A text file containing the ASCII art for the game over - win.test_project.py
: Contains tests for key functions of the game, executable with pytest.
This project includes unit tests for the words_import
, hangman_import
, and print_hangman_word
functions, ensuring that key components of the game behave as expected under various conditions. Here's a brief overview of what each test verifies:
-
test_words_import
: Checks if thewords_import
function successfully reads from thewords.txt
file, returns a list, and ensures that the list contains strings representing words. This test ensures that the game has a valid word list to choose from. -
test_hangman_import
: Ensures that thehangman_import
function properly loads the ASCII art representations of the hangman's stages fromhangman.txt
into a dictionary, validating the structure and content of this dictionary. This is crucial for visually representing the game's progress. -
test_print_hangman_word
: Verifies theprint_hangman_word
function's ability to correctly print the current state of the word being guessed, with letters either revealed or represented as underscores based on the player's guesses. This test captures and examines the function's output to ensure it matches the expected format.
To run these tests, ensure you have pytest
installed in your environment:
pip install pytest
Then, execute the following command in the terminal in your project's root directory:
pytest test_project.py
Pytest will automatically discover and run the tests defined in test_project.py, providing feedback on their success or failure. This testing step is crucial for maintaining the game's functionality and making it robust against errors.
- Name: Tomislav Dukez
- GitHub: tomdu3
- EdX: tomdu3
- Location: London, UK
- Date: 3 April 2024
- ASCII Art for hangman stages inspired by Invent with Python
- Word list sourced from Hangman Words
Enjoy playing Hangman and feel free to contribute or suggest improvements!