THE HANGMAN GAME is a Command Line Interface program built as the third Milestone Project using Python.
Users are able to play a game where they can try to guess the hidden word or phrase (depending on the difficulty level selected), one letter at a time. The game ends either when the user runs out of lives or when the correct answer was guessed.
The flowchart for THE HANGMAN GAME was built using Lucidchart.
The flowchart visualizes the separate steps of the game in sequential order.
Once the program starts running the user is asked for a name input which is then followed by another input where the user has the option of reading the game rules and instructions before playing. The third input will require the user to select a difficulty level which will then start the main game loop with either a word or a phrase to be guessed.
The hidden word or phrase will then be visualized with a series of underlineS '_' and any special characters will be given. Every guess is stored and displayed for the user and the correct guesses will uncover the hidden letters in their correct place(s). Every wrong answer will take away a life and draw a part of the stick man.
The game is over either when the correct answer was guessed or if the user runs out of lives and the hangman is complete. Once the game is over the user will be presented with an option to play again and if they do, this will take them back to the difficulty level selection phase.
The user is asked to input a name for the game which is then used to personalize experience with a greeting. The input is checked so that it does not contain any numbers or special characters. An empty string will also throw an 'Invalid input' feedback and the user will be asked to try again.
The user is prompted to make a choice if they want to see the game rules and instructions before playing. The input is checked so that only the given options are accepted and everything else throws a 'Invalid input' error and the user will be asked to try again.
At this stage the user is asked to select difficulty level for the game. They will have 2 options to choose from : easy and hard. Depending on their choice the game will build up for guessing either a random word from a list of approximately 2500 words or a random phrase from a list of approximately 200 idioms.
Once the difficulty level is selected the game board is printed. We first see a banner for the game which helps to set the mood. This is followed by an empty gibbot where the hangman will be drawn and below is the placeholder of the word or phrase to be guessed. For every letter in the word or phrase an underline will be displayed to represent the number of characters the correct answer has. All guesses made by the user (right or wrong) will be stored and displayed to help keep track. Below, the user will have a life count which will show how many attempts are left until the game is over.
- create a more complex design for the game board and banner
- expand the list of phrases for the game so it doesn't become repetitive
- improve the one at a time letter guess to word guesses as well
- add different options for difficulty level choice (like adding medium level or categories)
- add function to count wins and losses for user
- Python - programming language used to build the game
- Heroku - platform used to deploy the game
- Lucidchart - used for creating the flowchart of the programme
- Github - used for creating and storing the repository of the project
- Gitpod - used for creating and trialing the code
- Visual Studio Code - code editor used to create initial version of the project
- PowerShell - command line program used to test partial code correctness and bug solutions
- random - module generates random actions
- platform - module accesses underlying platform's data
- os - module interacts with the underlying operating system
THE HANGMAN GAME was tested throughout development in VS Code, PowerShell, Gitpod and Heroku. Manual testing verified and confirmed the functionality of the steps listed below:
-
Program Start
- program starts without any detected issues
- name input request runs on start-up
- invalid input calls error feedback for user
-
Name input
- input is validated against special characters, numbers and empty spaces
- invalid input calls error feedback for user
-
Game Rules
- option to see the game rules runs after name input
- input is validated to only accept given options
- invalid input calls error feedback for user
-
Difficulty level selection
- difficulty selection runs after game rules option
- input is validated to only accept given options
- valid input pulls random item from the correct list based on difficulty selection
- invalid input calls error feedback for user
-
Game board
- initial empty game board is printed after difficulty selection
-
Letter guess
- input is validated to only accept letters (A-Z)
- correct guesses uncover the letters in their respective place(s)
- invalid input calls error feedback for user
-
Remaining attempts
- remaining attempts are displayed as lives
- wrong guess will decrease the number of lives and draw the respective part of the stick man
-
Used letters tracker
- user guesses are stored and displayed for user
- guess repetition calls error feedback for user
-
End game
- the game ends once correct answer is guessed or the man is hung
- provides winner or looser message feedback for user
-
Play again
- play again option runs correctly when game is over
- game returns to difficulty selection and restarts correctly on affirmative
- otherwise the program stops running while providing a feedback message for user
All python files pass validation through the PEP8 online check with no errors.
While creating the game i have identified and corrected the below bugs:
- Even though every letter was guessed correctly the word builder would not be equal to the randomly choosen word/phrase as it was a list of strings. The bug solution was tested and proved using PowerShell.
while option in ['Y', 'YES']:
while error_count != error_limit:
print_board(build_word, used_letters)
guess = get_user_input(used_letters)
if not check_user_input(guess, random_word, build_word):
error_count += 1
if str(build_word) == random_word: # bug
winner = True
break
while option in ['Y', 'YES']:
while error_count != error_limit:
print_board(build_word, used_letters)
guess = get_user_input(used_letters)
if not check_user_input(guess, random_word, build_word):
error_count += 1
if ''.join(build_word) == random_word: # correction
winner = True
break
- Letter guess input would accept consecutive letters from abc as ascii_letters is a long string with letters (a-Z). The bug solution was tested and proved using PowerShell.
- On new game the board would not reset the gibbot to it's initial empty stage. Converting the two relevant class attributes to lists solved the issue.
def __init__(self, random_word, level):
self.random_word = random_word
self.hang = list(hang) # convert to list
self.hbody = list(hbody) # convert to list
self.build_word = []
self.used_letters = []
self.error_limit = 11
self.error_count = 0
self.winner = False
self.level = level
- Deployment
The project was deployed to Heroku. Deployment steps are:
- Log in to Heroku or Set up an account
- Create a new app from the dashboard
- Enter a unique name and select region
- Click the 'Create app' button
- Select the settings tab from navigation panel
- Click 'Reveal config vars' button
- In the 'Key' field input 'PORT'; in the 'Value' field input '8000'
- Press 'Add' button
- Scroll down and click 'Add buildpack'
- First add 'Python'
- Then add 'node.js'
- Scroll up and select the 'Deploy' tab from navigation menu
- In the 'Deployment method' section select GitHub and connect your repository
- Scroll down and select your preferred deployment type then click 'Deploy branch'
- Cloning the repository
Cloning repository creates a local copy on the computer for the remote GitHub repository.
- On GitHub navigate to the main page of the repository
- Find and click code in the top right corner
- Copy the URL for the repository
- Open any command line where git is installed
- Change the current working directory to the location of your choice
- Type git clone, and then paste copied URL
- Press ENTER to create local clone
- List of words was found via Stack Overflow.
- List of phrases was built from Topcorrect and Literary Devices.
- The flowchart was built using Lucidchart
The website was created for Code Institute's Full Stack Software Development Diploma Course as the third Milestone Project.
Therefore, I would like to take this opportunity to show my appreciation and thanks to my mentor Precious Ijege and the whole of Code Institute for all the help and support received throughout the creation of this project.