Hangman is a classic two-player guessing game, played on pencil and paper, that most of us probably learned in elementary school. One player thinks of a “secret” word, and the other player’s goal is to try to determine the secret word by guessing its letters one at a time.
Your main objective in this assignment is to write a computer program that will play a game of Hangman with you. Your program will come up with the secret word and keep track of your guesses, and you, or anyone playing your game on the computer, will be the one guessing the word.
The rules for our game of Hangman are as follows. The computer will first choose a random secret word that is at least four letters long from a lexicon of the 4000 most frequently used English words. It shows the word first as blanks (underscores or dashes), with one blank for each letter of the word. Then play proceeds as follows:
- The player chooses a letter of the alphabet and indicates it to the computer as his or her
- If the secret word contains the letter, all occurrences of the letter in the word are
- Otherwise, the letter is written in the list of incorrect guesses, and an additional part of the stickman is
- If all letters of the secret word are revealed, the game is won. If the gallows and all parts of the man have been drawn, the game is lost. Otherwise, play continues with step
Our Hangman game will allow the human player a total of eight guesses to determine the secret word. Each incorrect guess will result in the drawing of the following parts, in this order: head, torso, left arm, right arm, left leg, right leg, gallows, face. The game is lost once the face is drawn. A game in progress is shown in Figure 1.
These first two problems in the individual component of this as- signment are designed to help you get familiar with reading a list of words from a file, performing computations on them, and drawing simple things to a window on your screen. You can use the exact same programming environment you set up for previous assign- ments to complete this assignment. Create and submit a separate Python program for each problem.
Problem 1: Word Lookup
Write a Python program that will read the provided lexicon1 file and perform a lookup for a user-specified word in the lexicon. The lexicon file, cpsc231-lexicon.txt, is a text file that contains the 4000 without definitions for the words. most frequent words used in contemporary American English,2 with one word written per line in order of their frequency of use. All words are written lowercase except for the word “I”.
Your program should first indicate at what rank position the query word appears in the list, or that the query word is not contained in the lexicon. Then it should output, in alphabetical order and without a lexicon of most common words in Canadian English. duplicates, a list of all the letters that occur in the query word.
Inputs: A lexicon file name specified as the first command line argu- ment, and a query word as the second command line argument. For example, you might run your program with the following invocation:
$ python3 my-p1.py cpsc231-lexicon.txt color
Outputs: An indication of what frequency rank the query word ap- pears in the lexicon, or that it was not found in the provided file. Then print a list of all letters contained within the query word, in alphabetical order. For example, your program’s output may look like those shown on the right.
Problem 2: Analog Clock
The time module in Python provides a convenient means for your program to ask for your computer’s current local time. When you call the localtime() function in the time module, it gives you back an aggregate data structure3 with named fields that tell you what time it is. For example, you can obtain the hour, minute, and second for the current time as follows:
time_aggregate = time.localtime() hour = time_aggregate.tm_hour
minute = time_aggregate.tm_min second = time_aggregate.tm_sec
Use this information to create a program that draws an analog clock displaying the time (hour, minute, and second) at which your program is run. Your clock should have a circular face with the nu- merals 1–12 to indicate the hour. It should also have an hour hand that is shorter and distinct in appearance from the minute hand, which is in turn shorter and distinct from the second hand. Apart from these basic requirements, you may make your clock appear as you like.
It may be neat to animate the clock as specified in Creative Exer- cise 1.5.32 of your text, though you are not required to do so for this problem. Sorry, doing this is not enough for a bonus either!
Outputs: An analog clock with hour, minute, and second hands showing the current time, drawn in a window on your screen. It should look similar to the one shown on the right.
We encourage you to work with a partner to complete the remainder of this assignment. You may choose your own partner, but remember that you must work with a different partner for each assignment in this class! If you are solving these problems as a pair, one submission is sufficient for both students.
Problem 3: Console Hangman
Your first goal for the paired component is to create an interactive game of Hangman that can be played entirely through the console window. Your program will play the role of “executioner”, selecting the secret word, keeping track of letter guesses, and detecting when the game is won or lost.
Since this is a console version of the game, you won’t be able to draw the stickman and gallows,4 so it will suffice to keep track of the number of guesses remaining before the game is lost. At minimum, your program must be able to perform the following functions:
- Select a random secret word that contains at least four letters from the provided
- Display the number of guesses remaining. The player starts the game with 8 guesses
- Prompt the human player for successive letter guesses through the terminal until the game is “text art” or “ascii art”, and you’re free to do so if you like, but you’ll be making a graphical version of the game in the next problem anyway…
- If a guess is correct, reveal all occurrences of that letter in the appropriate positions in the secret word, keeping the other letters hidden as underscore (_) or dash (-)
- If the secret word does not contain the letter guessed, add the letter to the list of incorrect guesses and deduct one from the guesses
- If the letter has been guessed before, inform the player and ask for a new
- Detect when the game is either won or lost, display a message accordingly, and reveal the secret word at the end.
This might also be a good time to remind you of the things you learned early on with Karel the Robot. You’ll notice the form of this problem is very similar to what you already did in Assignment #2, except that we’re no longer asking for specific intermediate steps. It’s completely up to you to decide how you want to organize your pro- gram, but we suggest you try using top-down design and stepwise refinement to break it up into sub-problems. Identify the tasks that need to be completed and write functions for them if you can.
Inputs: The name of the lexicon file specified as a command line argument. For example, you may run your program as follows:
$ python3 my-p3.py cpsc231-lexicon.txt
Outputs: A guess-by-guess transcript of the game unfolding as you play Console Hangman with your computer program.
本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！
E-mail: [email protected] 微信:dmxyzl003