Make sure all programs are saved to your cs21/labs/05
directory. Files outside this directory will not be graded.
$ update21
$ cd ~/cs21/labs/05/
Note that there is a written portion to this lab that is due in class.
If you are in Prof. Meeden’s section, your solution is due Thursday, March 1.
If you are in either Prof. Fontes’s or Prof. Soni’s section, it is due Friday, March 2.
The first part of the lab involves you tracing a program with functions and showing the resulting stack that the function calls generate. This is excellent practice for what will almost certainly be a quiz question! Download the PDF, print it out, and turn it in at the start of class on either Thursday (Meeden) or Friday (Fontes/Soni).
In hangman.py
, you will implement a (somewhat simplified) version of the popular Hangman guessing game. In this game, one player chooses a word for the other player to guess. The player then guesses letters, one at a time, in an effort to figure out what the secret word is. If they are correct, the letter is revealed in the word. If they are wrong, they get closer to losing.
In our version, the word will be chosen randomly from a dictionary of words that we provide. The user then tries to guess the word, and has a maximum of 6 incorrect guesses before they lose. We will refer to the correct answer as the secret word since the user cannot see it and the user’s ongoing guess to be the visible word as this is what the user knows. Also, instead of “hanging a man”, we’ll choose the more pacifist option of collecting “whammies” for incorrect guesses. 6 whammies means you lose the game.
Here is an example run of the game (there are more runs at the bottom of this document):
$ python3 hangman.py
Welcome to hangman. Choose letters to uncover the secret word.
Beware: 6 incorrect choices means you lose!
There are 6 letters in the word
********************
Current guess:
------
You have 6 whammies remaining
Enter letter: s
********************
Current guess:
s-----
You have 6 whammies remaining
Enter letter: X
Illegal choice; enter letter: 1
Illegal choice; enter letter: aa
Illegal choice; enter letter: x
Whammy! x is not in the word
********************
Current guess:
s-----
You have 5 whammies remaining
Enter letter: c
********************
Current guess:
s-cc--
You have 5 whammies remaining
Enter letter: r
********************
Current guess:
s-cc-r
You have 5 whammies remaining
Enter letter: l
Whammy! l is not in the word
********************
Current guess:
s-cc-r
You have 4 whammies remaining
Enter letter: o
********************
Current guess:
socc-r
You have 4 whammies remaining
Enter letter: e
Congratulations, you guessed soccer with only 2 whammies!
In order to implement the hangman game we will be using a string to represent the secret word and a list of characters to represent the visible word that reveals the user’s correct guesses so far. We cannot use a string to represent the visible version of the word because strings are immutable in python, while lists are mutable.
You can convert between a list of characters and strings:
ls = list("blah") # gives list of characters ['b','l','a','h']
st = "".join(["b", "l", "a", "h"]) # st is the string "blah"
You are required to use functions for this lab, and they must match the specifications given here. You should read this all the way through and only start programming when you have a good understanding of how main()
will use each of the other functions:
getLetter()
: this function takes in no parameters and returns a string that must be a single lowercase alphabetic character. It asks the user to input a letter, and repeats the question until the user gives a single character that is from a
to z
(lower case only). This is the only function that will interact with the user.
printStatus(visibleWord, whammiesLeft)
: this function prints the game status to the screen. It takes in the visible version of the word as well as the remaining whammies before the game ends. There is no return value. The message includes the current guessing status (i.e., the partially visible word with '-'
’s for unguessed letters) and the number of whammies before the player loses the game. For example, if you have had 2 incorrect guesses (out of 6), and you have revealed only the letter 'r'
in "swarthmore"
, you would print:
Current guess:
---r----r-
You have 4 whammies remaining
Note that printing out the visible word is non-trivial since it is a list and not a string. If you just use print(visibleWord)
in the example above, you will get:
['-','-','-','r','-','-','-','-','r','-']
You can convert the list to a string in one of 2 ways: * Use an accumulator to generate the full string * Use the join
operator:
>>> visibleWord = ['-','-','-','r','-','-','-','-','r','-']
>>> print("".join(visibleWord))
---r----r-
scoreLetter(letter, secretWord, visibleWord)
- this function’s parameters are the guessed letter, the secret word (a string), and the user’s visible word (a list of character strings). If the letter is in the secret word, you can reveal the letter visible to the user. For example, if the user guesses 's'
and currently has revealed ['-','-','-','r','-','-','-','-','r','-']
("---r----r-"
) for the word "swarthmore"
, you would modify the visible word to be ['s','-','-','r','-','-','-','-','r','-']
("s--r----r-"
). The function returns a boolean – True
if the letter is in the secret word and False
otherwise. A side effect of this function is that visibleWord
will be modified, so you do not need to explicitly return the list. There may be multiple instances of a letter (e.g., 'r'
in "Swarthmore"
) and all of them will be revealed at once if the user guesses the letter.Your main()
function should utilize the above functions to do the bulk of the work.
The general outline of the program is as follows:
words
library and call the getRandomWord()
method:import words
def main():
secretWord = words.getRandomWord()
Print out a welcome message explaining the rules of the game.
Initialize the current state of the visible word. Since no characters are revealed at the start of the game, the status of the visible word will be a dash "-"
for each letter. Since strings are immutable, you must store this as a list. For example, if the word is "mint"
, the initial visible version will be ['-','-','-','-']
(a list of 4 strings that are a dash).
Note that each of these sub-steps should correspond to one of the functions you implemented above.
It is very important that you think carefully about how each of your functions is being used. Each of the steps in the main method that we have outlined should only need 1 or 2 lines of code – the functions handle most of the complexity.
Additionally, it is incredibly important that you use incremental development. The 5-point outline is a good starting point - you should complete one step at a time, thoroughly test it to see if your program works, and only move on after getting it to work. The ninjas and lab instructors will ask you to backtrack and do incremental development if you do not follow this strategy.
If you try to implement the whole program at once, you will waste a lot of time and energy trying to fix your program.
We have not defined what should happen if the user guesses a letter that they have previously entered. We will not test for this condition, but you are free to think of a reaction. One option is to treat it as a normal guess (re-guessing an incorrect letter results in another whammy, while re-guessing a correct letter reveals nothing new but does not receive a penalty). Or, as a challenge, think of how you could keep track of previous guesses and force the user to only enter in new guesses.
Here is an example where the user loses the game:
$ python3 hangman.py
Welcome to hangman. Choose letters to uncover the secret word.
Beware: 6 incorrect choices means you lose!
There are 4 letters in the word
********************
Current guess:
----
You have 6 whammies remaining
Enter letter: a
Whammy! a is not in the word
********************
Current guess:
----
You have 5 whammies remaining
Enter letter: e
Whammy! e is not in the word
********************
Current guess:
----
You have 4 whammies remaining
Enter letter: i
********************
Current guess:
-i--
You have 4 whammies remaining
Enter letter: o
Whammy! o is not in the word
********************
Current guess:
-i--
You have 3 whammies remaining
Enter letter: u
Whammy! u is not in the word
********************
Current guess:
-i--
You have 2 whammies remaining
Enter letter: r
Whammy! r is not in the word
********************
Current guess:
-i--
You have 1 whammies remaining
Enter letter: t
********************
Current guess:
-i-t
You have 1 whammies remaining
Enter letter: n
********************
Current guess:
-int
You have 1 whammies remaining
Enter letter: l
Whammy! l is not in the word
Sorry, you lose.
The word is mint.
Each lab has a short questionnaire at the end. Please edit the QUESTIONS-05.txt
file in your cs21/labs/05
directory and answer the questions in that file.
Don’t forget to run handin21
to turn in your lab files! You may run handin21
as many times as you want. Each time it will turn in any new work. We recommend running handin21
after you complete each program or after you complete significant work on any one program.