STEP 1: VISUALIZATION
As ‘Hangman’ is a fairly intuitive game, you should be able to come up with a simple algorithm quickly. Note that there are numerous ways to approach the problem, and yours may differ from mine. Have fun with it by adding variations.
For this challenge, this is the way that I would like my ‘Hangman’ game to work:
2. Loop: Guess an alphabet. Is the alphabet part of the random word?
- If yes, update the remaining missing letters required
- If no, increase the ‘HANGMAN’ countdown
3. End condition
- End if entire word has been found
- End if ‘HANGMAN’ countdown has ended
STEP 2: ALGORITHM
Up to this point, you should already have enough information to come up with an algorithm of your own. In case you run into any trouble, here is a visual flowchart to help you:
STEP 3: IMPLEMENTATION
Once you have a clear idea on how to build the game, you can begin coding!
For this challenge, instead of providing a pseudo-code like I did with the previous activity, I have provided a template with extensive comments below. The comments provide step-by-step assistance for you to code each line as you go. When you are done, run the codes to test if they work.
The model answer can be found on my Github page.
If you encounter any difficulties along the way, or have ideas on how to improve the game, leave us a comment below!