For this lab, you will implement a text-based version of hangman. There are many variants of this game, so make sure to follow the specifications exactly. You can also play a demo version of the program you will be implementing. See the prelab for details on how to do this.
Read through the whole lab first before getting started. We provide an overview of how to proceed and some hints near the end.
When the program starts it reads a file containing a list of possible words. Download a text file with CS words (recall right click on the link and select “Save link as…” to download). Remember, this file needs to be in the same directory as your program.
After reading the file, the program randomly selects one of these words as the word that the user is trying to guess and then displays the following:
-----------------
Guessed letters:
Incorrect guesses left: *******
Word: _______
This display has three main components:
The program runs by repeatedly prompting the user for the next letter. When the user enters a letter, there are three possible scenarios:
The following transcript shows all three of these different situations happening as the game is being played:
-----------------
Guessed letters:
Incorrect guesses left: *******
Word: ____
Guess a letter: e
-----------------
Guessed letters: E
Incorrect guesses left: *******
Word: _e__
Guess a letter: a
-----------------
Guessed letters: A E
Incorrect guesses left: *******
Word: _ea_
Guess a letter: r
-----------------
Guessed letters: A R E
Incorrect guesses left: ******
Word: _ea_
Guess a letter: s
-----------------
Guessed letters: A S R E
Incorrect guesses left: *****
Word: _ea_
Guess a letter: a
Letter already guessed!
Guess a letter: s
Letter already guessed!
Guess a letter: t
-----------------
Guessed letters: A S R E T
Incorrect guesses left: ****
Word: _ea_
Guess a letter:
The game ends when either the user runs out of guesses, in which case the word is shown:
The word was: heap
Better luck next time!
or if the user fills in all of the underscores in the word, then the user wins and the number of incorrect guesses is printed out:
Guessed letters: A H S R E
Incorrect guesses left: *****
Word: hea_
Guess a letter: p
You win!
The word was: heap
You guessed it with 2 incorrect guesses
Besides following the game specification described above, your program MUST meet the following requirements:
Your program must contain at least the following four functions:
read_words
, which has a filename as a parameter and returns a list of the
words in the file (you can assume the file contains one word per line).set_to_string
, which takes a set as a parameter and returns the letters in
the set as a string, with each letter capitalized and separated by a space.insert_letter
, which has three string parameters: a letter, the current
underscored word and the word the user is trying to guess. This function
should return a new version of the underscored word with all occurrences of
the letter filled in based on the occurrences of the letter in the word to be
guessed. See an example below.
>>> insert_letter("a", "__n_n_", "banana")
'_anana'
play_hangman
, which has two parameters, the filename that contains the
candidate words and the number of incorrect guesses the player gets. This
function should then initiate the hangman game and continue to run until the
game finishes.and meet these additional specifications:
play_hangman
from within the if __name__ == "__main__"
conditional in the
starter file. The play_hangman
function should only be called from within that
conditional.There are many different ways to implement your program, but here is one suggestion:
read_words
function and make sure that it correctly reads in
the words in the file to a list. Don’t forget to remove the newline
characters at the end of each line when reading in the words.set_to_string
function. This should be very similar to the
function you wrote for the prelab. Recall that sets are iterable, so you can
iterate over the items in a set just like you can with a list.insert_letter
function. There are many ways to implement
this function, but one way is to build up the new string letter by letter
(like we did for the encryption functions). For each letter in the new
underscored word you’re creating, you either just copy what was in the
underscored word before, or, if the letter appears in the word to be guessed
at that position, you copy the correct letter.Implement play_hangman
incrementally, testing each step before moving on
to the next:
You may earn up to 2 creativity points on this assignment by adding improvements to your program. If you do, include in your comment at the top of your program what you added. Here are some ideas that you might consider:
|-----|
| O
| \|/
| |
| / \
Make sure that your program is properly commented:
In addition, make sure that you’ve used good coding style (including meaningful variable names, constants where relevant, vertical white space, etc.).
Submit your program via Gradescope. Your program program file must be named lab6_hangman.py. You can submit multiple times, with only the most recent submission (before the due date) graded. Note that the tests performed by Gradescope are limited. Passing all of the visible tests does not guarantee that your submission correctly satisfies all of the requirements of the assignment.
Feature | Points |
---|---|
Four functions implemented as specified | 4 |
All words are read from the file | 1 |
Each time a random word is picked from the file | 1 |
Initial underscored word shown correctly | 1 |
Each guess is inserted appropriately in underscored word | 2 |
Guessed letters correctly displayed | 1 |
Guessed letters are updated appropriately | 1 |
Guessed letters uses a set | 1 |
Repeated requests from user on repeated letter | 2 |
Incorrect guesses updated appropriately | 2 |
Game ends correctly on win | 1 |
Game ends correctly on lose | 1 |
Handles lower and uppercase letters | 1 |
Program starts automatically on run | 1 |
Comments, style | 3 |
Creativity points | 2 |
Total | 25 |