“A Simple Hangman” — #LearnPythonThroughProjects: Series 12
Hey there Python rockstar! Welcome to another interesting Project. It’s that time of the week, put on your Coding armour , let’s go conquer another task.
THE HANGMAN GAME:
One of those classic word guessing games played between two or more players. One player sets or defines a word or list of words, and the other player(s) have to guess correctly, given a certain number of trials. It is quick, easy, educational and most times requires only a piece of paper and the ability to spell correctly. It could also be played with a computer(as the one which will be built in this article). For every wrongly guessed word, a life or trial in the game is lost and a “hanging-man” begins to appear, piece by piece. The aim is to solve the puzzle and guess the correct word(s)/phrase before the hangman dies.
This classic game has gained so much popularity, that just like most games, there are a couple of tips, tricks, and strategies used to win.
RULES OF THE GAME:
In order to be able to deploy this game into a code-based solution, we need to first understand the rules around it- How does it work? Only then can we efficiently translate to code.
“Coding is only a tool. First understand the problem, then use the tool”.
- An individual sets a word or list of words.
- A player takes an initial guess at a letter that might start or be contained in the word or list of words.
- If the letter is contained in the word/list of words, the player gains a point and takes another turn guessing a letter.
- If the letter is not contained in the word, the player losses a try(tries)/point, and a part of the hangman appears.
- For every wrongly guessed letter, the hangman appears bit by bit until the complete image is drawn.
- Similarly, for every correctly guessed letter, the letters are placed on the screen until the word is completed and the player wins.
CODING HANGMAN IN PYTHON
So the idea in this series is to first create a very simple version and then in the upcoming series update and improve the functionality. Just as with the previous series. We would not be using the image display here, just a simple logic control on how the game works.
In this project, I would not make use of user-defined modules, however, I encourage you to experiment with that as well and find another way of building this. Take a step back to go through all the concepts learnt so far and apply them to creating something unique. This particular project can get tricky and complicated, hence we are going to start with a mini version first.
STEPS:
- Import the random module.
- Define a function ‘welcome()’.
- Define a variable name in the ‘welcome’ function to get the user’s/player’s name. Use a string method to capture the user’s name in a sentence case.
- Create a decision-making process, to check that the user only enters alphabets and not numbers as a name.
See the screenshot below for steps 1-4
5. Define another function ‘play_again()’. Add a docstrings.
6. Add a decsion-making process
7. Define another function ‘get_word()’, for generating random words for the user to guess. Add a docstring.
8. Define another function ‘game_run()’. Add a docstring if desired
9. Call the ‘welcome function’ inside the ‘game_run()’ function’ to get the game running.
10. Define an ‘alphabet’ variable inside the function
11. Set a word variable(for guessed word) to the ‘get_word()’ function for a random word to be chosen.
12. Initiate an empty list for guessed letter
13. Initiate a tries variable for number of tries by the user
14. Set initial guess to ‘False’.
15. Print an empty line
16. Print a guess hint for the user for number of letters contained in the word
See image below for steps 8–16:
17. Initate a while Loop inside the game_run() function, and create decisions, taking into consideration if the user decides to enter just a single letter or the full word.
18. Also a create decisions for if user inputs a wrong entry, and if user inputs letters and it is not equal to the total number of letters in the word to guess.
19. Deduct tries each time user fails to guess incorrectly.
20. Initiate ‘play_again()’ function if user wishes to continue , at the end of the ‘game_run’ function.
See image below for step 17–21
21. Save and run the program
Do not forget, to get the program running, you have to call the function name. Call the ‘game_run()’ function to execute the full program.
Program run:
Great! It works fine.
There are very few newly introduced concepts here. I will explain them below:
“Pause, take a moment, think about the game, think about how it would proceed without code, then think about the flow . Remember that coding is only a tool.”
- Python string method .isalpha(): This is a string method which returns True if the all the characters in a string/variable is actually a string, and returns False otherwise, i.e if any of the string contains a numerica character alognside a string such as ‘num12’, it would return False. In the program, it was used to make sure the name entered by the user should only contained string characters and not numbers as a game name.
- Dosctrings: Docstrings are literals used in python inside a function, a class or a block of code, as some sort of information about what that particular sets of code does. For example, in the program, it was used to provide information on the functions and their purposes.
The rest of the concepts have previously been explored in past series. Find the link below and embedded here.
Generally, the logic of the game coded here goes as follows:
- A welcome function is defined, where a user enters a game name and the game name is saved in a variable. A condition is set to ensure the entry by the user contains only letters and not numbers. The welcome message displayed is personalized and the user is informed that the word to be guessed will be set by the computer.
- A new function is defined where options are made available if the user wishes to play the game again. In this function , if the response is yes, the game is initiated again, with a random word.
- A function is defined for generating the word which the user is to guess, and this is done using the random module imported at the beginning of the program, from a list of predefined words.
- Finally, a the function controlling the game is defined, with certain variables such as the user’s guesses and number of tries, and using a set of conditions and decisions, taking into consideration possible occurrences, such as :
- The user enters a non-alphabet
- The user enters a word twice or has guessed that letter before
- The user guesses a letter that is not in the proposed word.
- The user tries to give a shot at guessing the entire word at a go but fails.
- The user guesses correctly
- The user runs out of tries.
Final Note 📝 :
Thank you for tuning in. Do look forward to upcoming series, we are gradually moving towards “Automation”. As we keep adding, conquering and acquiring more skills, we should look forward to solving a problem with it. Can you think any?
See you in the next series!
Link to the Github repository have been included below , as well as other useful resources.
SOCIAL MEDIA CONNECT
LinkedIn: https://www/linkedin.com/in/aminah-mardiyyah-rufa-i/
Twitter: @diyyah92