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:- What information do we need to keep track of during the game? Think about the kind of data it is and what type (int, string, list, etc.) you should use for it.
- What are the different graphical components that you need to draw and update?
- What are the basic steps that your program will have to perform?
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.- Write a function called contains that takes as parameters a list and a string and returns True if the string is in the list and False if it is not.
- Write a function called listToString that takes as a parameter a list of strings and returns a single string with everything in the list concatenated together. For example, listToString(["C", "S", "1","1", "0"]) should return the string "CS110".
- Write a function called check that takes as parameters a word and a letter and returns True if the letter is in the word and False otherwise.
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:- dashes where the correct answer will appear (make sure that they are separated enough that you can easily count the number of letters in the word),
- the letters A-Z in a sequence on the screen for the user to pick from (Hint: if you include from string import * at the top of your program, then the variable uppercase will hold all the uppercase letters A-Z as a string),
- some status text asking you to pick a letter (later, this will be updated to say if you already guessed the letter, and otherwise if your guess was in the word or not),
- the list of letters that you have already guessed (leave this empty for now), and
- the number of tries you have left.
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- obtain a mouse click and extract the x and y coordinate of the click, and
- keep looping while (hint, hint) a region of the screen containing one of the letters A-Z has not been clicked.
- When a region with a letter in it has been picked, you have to determine which letter was clicked. You should figure out a formula that converts the x coordinate clicked (i.e., how far to the right or left on the screen the user clicked) into a position in the alphabet.
- Return the letter (as a string) at the above position (again, uppercase holds a list of all the letters A-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:- guesses: the list of guesses so far (initialized to [])
- triesLeft: an integer counting how many tries the player has left (initialized to 6 or 9, depending on how generous you feel)
- current: the list of what the player currently sees, initialized to a list with as many "-"s as there are letters in the word (e.g., if the word is "dog" then the list should be ["-", "-", "-"])
- get the user's guess using the getGuess function from Step 3;
- check if the user has guessed this letter before (use the contains function from Step 1), in which case you should update the status text to say this (using setText);
- otherwise, check if the guessed letter is in the word (use the check function from Step 1), and if it is you should
- update the status text to say this,
- fill in the letters on screen,
- fill in the list current with the guess at the appropriate positions,
- add the letter to the list of guesses, and
- update the list of guesses on-screen (use the listToString function from Step 1 to make guesses into a string);
- and finally if neither of the above is the case (the player guessed incorrectly), you should
- update the status text,
- update the list of guesses,
- update the list of guesses on-screen,
- update the number of tries left, and
- update the number of tries left on-screen.
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:
- hangman.py