Lab 6 will be focused on functions abd modules. We will use modules such as matlplotlib, random, and time to help with building a hangman game!

Purpose and Outcomes

We will practice:

  1. declaring, defining, and changing variables

  2. implementing flow control through while/for loops and if statements,

  3. adhering to function contract information,

  4. manipulating lists, and

  5. graphing using matlib plot

By the end of this lab, you should be able to:

  1. correctly append, delete and extract information in a list,

  2. use basic functionality from matlibplot module, and

  3. understand why import statements are important in a script

Preparation

Design

We will use the following sensors:

  • Buzzer (to indicate a wrong guess)

  • LED (to indicate a correct guess)

  • OLED screen

  • Button (to allow the user to play another round)

Here are the rules/specifications of the game:

  1. The player has 6 chances only to guess the word

  2. The player will guess letter by letter (cannot provide a complete word)

  3. The name of the player will be entered once the game is started. Then, the player is asked to choose a difficulty level (easy, medium, hard)

  4. Based on the choice, a random word from the corresponding choice is chosen

  5. A statement that shows how many letters in the word will be displayed to the player.

  6. The player will start guessing letter by letter as long as they still have chances left to play

  7. If the guess is more than one letter, or if the guess is not a letter, it will be counted as an invalid input. It will not be counted as a missed chance will be allowed to guess again.

  8. If the player correctly guessed that letter earlier, it will not be counted as a missed chance. A message that says "You already guessed that letter. Try again!" will be displayed. This means that the correct guessed letters should be saved. Think about this and how to update it in the design.

  9. If the guess is correct, the letter and its position will be displayed to the user. Think about how to get the position of the guessed letter (you can use a function to do that).

  10. If the guess is incorrect, one chance is taken from the player. At the end of each guess, the progress is summarized to the player, showing how many chances are left and the current guessed word.

  11. If the player runs out of chances, the game should end and the message "Game over! The word was: _"_ should be displayed to the user (indicating the word).

  12. The player will be given 5 seconds to press the button if another round is desired. Otherwise, the game will not be restarted

Having discussed the specifications of the game, let’s get to the design!

  1. Design: create a flowchart of the above game design. Note that the above steps are not standalone if statements. You should think of how to link all the rules together to form the game. There will be while loops, if-elif-else statements.

  2. Functions: familiarize yourself with the following functions. Feel free to look them up. Note in your logbook what would be the output of the following functions:

    1. random.choice(ListOfWords)

    2. letter.isalpha()

    3. ' '.join(ListOfLetters)

Warning
Code submission: Remember to label the code with the corresponding step according to the procedure section.

Procedure

Download Lab6_w24.py and open it within Thonny.

2.1 Create the function def success():

This function will either return True or False. It will return True if the user finally guessed the full word correctly. This means that guessed_word is equal to chosen_word. Given these conditions, what should be the parameters that are passed to this function?

2.2 Part I of the hangman game

Implement the following steps inside the while loop (based on your preparation design):

  • Ask for the player’s name

  • Then, ask the user to choose a difficulty level (easy [0], medium [1], hard [2]).

  • Based on the choice, choose a random word from the corresponding choice

  • Display a statement that shows how many letters are in the word to the player.

  • Define pos_list, which is a list that displays the current guess situation. It starts where all the letters are displayed as "_".

    • It looks like the following:

hangman I

Tip
Note that the number of letters is varied based on the random choice (The implementation should be general!). Therefore, pos_list size will depend on the chosen word.

2.3 Part II of the hangman game

  • Implement the while loop that allows continuing the guessing as long as there are chances left to play

  • Take a guess from the user

  • Implement the following based on the guess:

    • If the guess is more than one letter, or if the guess is not a letter, it will be counted as an invalid input. It will not be counted as a missed chance. A message that says "Invalid Input" will be displayed. The player will guess again.

    • If the player already guessed a correct letter earlier, it will not be counted as a missed chance. A message that says "You already guessed that letter. Try again!" will be displayed.

    • If the guess is correct, the letter and its position will be displayed to the user. pos_list will be updated and displayed to the user (showing the current progress). LED should turn on. guessed_word will be updated as well. It will look like the following:

hangman II

  • If the guess is incorrect, one chance is taken from the player, and the hangman picture will be displayed (the correct level of hangman pic should be displayed). The buzzer will sound as well.

  • If the player finally guessed the whole word correctly (this will be determined by calling the function success()), a message that says "Congratulations!" will be displayed to the user along with the full-guessed word. The round will end. In this case, add an elements to the list History (the name of the player and the number of chances left). Each element of the History list should have the name and corresponding list.

  • At the end of each guess, the progress is summarized to the player, showing how many chances are left and the current guessed word, which is pos_list.

Tip
Note: it is not necessarily that all the listed will be standalone "if" conditions. There should be an if-elif-else flow control. The listed above is just what is expected in each case. Think of how you can design the implementation.

2.4 Part III of the hangman game

  • If the player ran out of chances (the loop terminated because chances = 0). A message that says, "Game over! The word was: _ "_ will be displayed to the user, showing the actual word.

  • A message will be displayed to ask the user to press the button if they want to play again.

  • Implement a sleep of a second or two to prepare If the button is not pressed, end the game. If it is pressed, a new player will enter the information and play the game

2.5 Plot a summary of the results

If the game ended, we would like to see who played the game and how many chances they had upon winning, which represents the dictionary "History".

Tip
Matplotlib is "a Python 2D plotting library which produces publication quality figures. For simple plotting the pyplot module from matplotlib provides a MATLAB-like interface (MATLAB is another scripting language you will become quite familiar with during Engineering). We will use pyplot module to plot our collected data." To use it, you need to import the library as: import matplotlib.pyplot as plt
  • Use Matplotlib functionality within your main script to plot History.

  • Use a bar plot where the x-axis is the names of the players from the history list, and the y-axis is the number of chances left. It will look something like the following if only two players played:

hangman 2.5

Q1: Write the code for all the implementation steps. Highlight or label each block of code by the corresponding step number.

Q2: Include a screenshot of a sample plot you created using your code.

Q3: Write in a bullet-point format, the errors you got while working on the code.

Tip
You can refer to the pyplot tutorial for information on how to plot your data. It can be useful for your project as well!

Testing

Include the testing results in a tabular format. Include all test cases and the corresponding output. Investigate and write in the comments section what you modified to achieve the expected result.