CS21 Lab 5: more advanced functions

Written portion: due in class Thursday, March 1 or Friday, March 2

Programming portion: due Saturday, March 3, before midnight


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.

Programming Tips

Topics for this assignment


1. Stack Diagram

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).


2. Hangman

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"

Helper Functions

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:

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-

Main Function

Your main() function should utilize the above functions to do the bulk of the work.
The general outline of the program is as follows:

  1. Pick a secret word from the dictionary. We have provided a function to do this for you. Simply import the words library and call the getRandomWord() method:
import words
def main():
  secretWord = words.getRandomWord()
  1. Print out a welcome message explaining the rules of the game.

  2. 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).

  3. Until the user runs out of whammies, or has correctly predicted the secret word, you will need to repeatedly:

Note that each of these sub-steps should correspond to one of the functions you implemented above.

  1. Print the end game message. If the user revealed the entire secret word with whammies remaining, congratulate them on their victory. If the user ran out of whammies, then notify them that they lost and reveal the correct answer.

Strategy and Tips

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.

3. Answer the Questionnaire

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.


Turning in Your Labs

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.