CS 110
Foundations of Computing through Digital Media

Denison

CS 110 Spring 2012 

Homework06: The Game of Hangman

Due: Thursday, March 29th at class time


In Homework 06, you will create the game Hangman (see here for the rules of the game).

Step 0: Planning your program

Spend a little time thinking about what components you need to design the Hangman game. In particular, think about the following questions:

Step 1: Writing some helpful functions

We need some helpful functions for the hangman game. In a file called hangman.py write the following functions. Test each of these functions thoroughly and make sure they work correctly before proceeding.

Step 2: The graphical interface

You can design your own graphical interface (choose some nice colors and position the components aesthetically). Be sure to definitely include the following: Draw all these components on a GraphWin and make sure they all appear properly before continuing.

Step 3: Getting the player's guess

You next want a way to get the player's guess based on where they clicked on the screen. To do this, write a function called getGuess that takes as a parameter the GraphWin variable (so that you can get mouse clicks within this function) and returns a letter. The body of the function should You can test your function out by calling it (directly after your graphics code above) and printing the result to see if the correct letter is being returned. Test this several times to make sure that you are getting all the letters correctly (especially A and Z).

Step 4: Putting it all together

We are now ready to put the Hangman game together. Create a function called hangman that takes as a parameter a word that the player has to guess. Move all your graphics (but not the function) from Step 2 into this function. Next, initialize the following variables: Now we have to create the main game loop. This loop should continue while the complete word hasn't been guessed (use contains from Step 1 to see if current still has any "-"s) and the player still has some guesses left. Inside the loop you should

Finally, you should check at the end if the player guessed the word correctly and congratulate them by updating the status text, or if they didn't guess it then give them the correct answer. Test your program using several different words.

Extra credit 1

Along with displaying the number of guesses left, also draw the hangman itself using graphical components. (See here for the different stages of the hangman drawing.)

Extra credit 2

Modify your program to allow not just words, but also longer phrases (like on Wheel of Fortune). You should still have dashes for all the letters, but you should fill in all the spaces and punctuation (apostrophes, commas, periods) on the clue.

Extra credit 3

Modify your program to use a text file filled with words, one per line, and your program woud read in this file, placing each word into a list, and then the program could randomly pick one word from the list and run your top-level hangman program with the randomly selected word. This could be placed in a loop with an 'Again' option like we did for rock/paper/sissors.

Submitting your lab

Make sure that all your work for this lab is in a folder called lab06. Copy this folder into your folder on the shared drives by the deadline (class time, Tuesday March 29).

Please make sure that your lab06 directory has the following file: