For this lab, you will be implementing a text-based word guessing similar to “hangman”. There are many variants of that game, so make sure to follow the specifications exactly. Read through the whole lab first before getting started. I provide given an overview of how to proceed and some hints near the end.
If you want, you may work with a partner on this lab. If you do, you must both be there whenever you’re working on the lab. Only one of you should submit the assignment, but make sure both your names are in the comment at the top of the file and you add your partner to your Gradescope submission (as described at the end of the assignment).
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. Please find an example below.
>>> insert_letter("a", "__n_n_", "banana")
'_anana'
play_wordgame
, 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 game and continue to run until the
game finishes.and meet these additional specifications:
set
to store the guessed letters.play_wordgame
from within the if __name__ == "__main__"
conditional in the
starter file. play_wordgame
should only be called from within than
conditional.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:
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_wordgame
incrementally, testing each step before moving on
to the next:
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_wordgame.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.
If you worked with a partner, only one person needs to submit to Gradescope, but that person does need to add their partner’s name as shown in Gradescope documentation. Make sure both names are included in the comment at the top of the file.
Features | Points |
---|---|
Four functions implemented as specified | 4 |
All words are read from the file | 1 |
Each time a random word is picked from the words read | 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 to 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 |
Code design and style | 5 |
Creativity points | 2 |
Total | 27 |
Thinking about the example for insert_letter
>>> insert_letter("a", "__n_n_", "banana")
'_anana'
The current “underscored” word, what I would call the “working word”, is a kind of state. What properties of the game are embedded in this state? For example, can we tell if the user has won? How?
There are a number of ways to tackle this creativity option. I will show two
potential approaches below for creating “hangman”-style figures using multi-line strings and the format
method.