تم الحل ✓
categoryبرمجة وتطوير البرمجيات
schoolبكالوريوس
event_available2026-07-15
السؤال
Transcribed Image Text:
In this exercise you will implement the Hangman Game.
You should try to implement your program in two phases. The first phase, you will implement a regular Hangman Game; and the second phase,
you will implement a hangman game where the computer is trying to cheat by not choosing one fixed word at the beginning of the game but
keep track of a list of available words and use a special method to dodge the correct guesses as often as it can.
Phase 1: The Regular Hangman Game.
The hangman game is a paper and pencil word game for two players. One player (the computer program) thinks of a word/phrase/sentence and
the other player (the human) tries to guess it by suggesting letters with a certain number of wrong guesses allowed. Each round the human
makes a guess, and if the letter is in the word, it will be revealed; otherwise it will be counted towards the wrong guesses. If the human guesses
the whole word without reaching the maximum number of wrong guesses, the human wins; otherwise the human loses.
To run this game, we start the program with the following command line arguments:
$ python3 hangman.py w1.txt 87
w1.txt is a text file that contains one word per line, you may assume that each word is completely made from lower case letters a-z
8 is the length of the word the computer needs to come up with. The program picks one word of the right length from the w1.txt file indicated
by the previous command line argument. If there is no word of the right length, the program reports an error and terminates. You may assume
that there will be at most one word of a given length in the file for phase1.
7 is the number of wrong guesses allowed. When the human gets to 7 wrong guesses, the game stops and the human loses.
Here are some sample runs of the program. The first two we guessed the word correctly; the last we failed.
Here are some sample runs of the program. The first two we guessed the word correctly; the last we failed.
5 python3 hangman.py wl. Ext 87
missed letters: (7 chances left)
Enter your guess: e
missed letters: e (6 chances left)
Enter your guess:
missed letters: ea (5 chances left)
Enter your guess:
missed letters: e a (5 chances left)
Enter your guess:
_i_in_
missed letters: e a (5 chances left)
Enter your guess:
Ling
missed letters: ea (S chances left)
Enter your guess: E
ing
missed letters: eat (4 chonces left)
Enter your guess:
ings
missed letters: e a t (4 chances left)
Enter your guess: d
iddings
missed letters: e a t (4 chances Left)
Enter your guess: R
iddings
missed letters: e atk (3 chances left)
Enter your guess: 8
You guessed the word: biddings
S python3 hongnan.py wl.txt 6
$ python3 hangmon.py w1, txt 64
missed letters: (4 chances left)
Enter your guess: 5
b_bb_
missed letters: (4 chances left).
Enter your guess: a
bobb
missed letters: (4 chances left)
Enter your guess: I
babbl
missed letters: (4 chances left)
Enter your guess:
babbl
missed letters: 1 (3 chances left)
Enter your guess:
You guessed the word: babble
$ python3 hangmon, wl, txt 62
missed letters: (2 chances left)
Enter your guess:
missed letters: (2 chances left)
Enter your guess:
missed letters: i C1 chances left)
Enter your guess:
You lost after 2 wrong guesses.
Phase 2: The Wicked Hangman Game.
While in the regular Hangman game, the program chooses one word at the beginning of the game and responds to your guesses based on that
one word, in the Wicked Hangman Game, the computer program does not choose a single word. It starts with a list of words (for example all
words with 8 letters from the dictionary). When the user guesses a letter, the program groups the existing words into different groups based on
what positions it will reveal for each word with the guessed letter. For example, this is the output of plaing the Wicked Hangman Game with the
debug-printing on to reveal the internals of how the game is proceeding.
BeBug-printing on to reveal the internals of how the game is proceeding.
$ python3 hangman.py words, txt 3:5 debug
908 words left.
missed letters: (5 chances left)
Enter your guess: a
00:1
00:3
a_a:8
Lo:27
0:56
La:185
:628
628 words left,
missed letters: a (4 chances left)
Enter your guess:
nni
n_n:1
:9
:24
n:52
:541
541 words left.
missed letters: an (3 chances left)
Enter your guess: e
ee:1
e_e:6
Lee:14
21
0:43
195
:361
361 words Teft.
missed letters: ane (2 chances left)
Enter your guess:
Lt:3
L:3
1:22
t:37
296
296 words left.
t:37
296
296 words left.
missed letters anet (1 chances left)
Enter your guess:
00:1
00:1
00:7
0:9
0:27
0.194
<-167
You lost after 5 wrong guesses.
As you can see in the sample above, the blue highlighted output is debugging information that shows how the computer program is cheating by
not keeping one word but many words as potential candidates. When the user give a guess, it groups all the candidates into different groups
depending on the pattern it will reveal for the given word. And it always chooses the group with the most suitable words remaining. In the
extreme example above, it happens to be the case that for any guess given la n e t o), the largest group is the group that doesn't have a, orn, or
e or t or o. Here is another example of running the wicked hangman game for words of length 19 in the dictionary.
$ python3 hangmon.py words.txt 19 10 debug
16 words left.
missed letters:
(10 chonces left)
Enter your guess a
a
a
0.0
a
2 words left.
missed letters: a (9 chances left)
Enter your guess: e
eeeee:1
ee
eee1
1 words left.
ce eee
missed letters: a (9 chances left)
Enter your guess: n
e en ene e:1
1 words left.
e en ene e
missed letters: a chances left)
Enter your guess: s
eens enesses: 1
1 words left.
e ens enesses
missed letters: a ( chances left)
Enter your guess: c
eens enesses:
1 words left,
ceens enesses
missed letters: o (9 chances left)
Enter your guess: 1
eensi enesses:
words left,
eenstenesses
missed letters: a chances left)
Enter your guess: o
co e ens enesses 1
1 words left.
co e ensi enesses
missed letters: a ( chances left)
Enter your guess:
con e ensi enesses 11
1 words left,
come ens enesses
missed letters: a (9 chances left)
Enter your guess: p
comp.e.ensi enesses: 1
1 words left.
comp.e.ensi enesses:1)
1 words left.
comp e ensi enesses
missed letters: a (9 chances left)
Enter your guess: P
compre enst enesses:1
1 words left,
compre ensi enesses
missed letters: a (9 chances left)
Enter your guess: h
comprehensi,enesses 1
1 words left.
comprehensi enesses
missed letters: a C9 chances left)
Enter your guess: v
comprehensivenesses:1
You guessed the word: comprehensivenesses
As you can see in the above example, the wicked hangman game could quickly be reduced to the regular hangman game if we didn't have too
many words to start with. Only the first step the computer can cheat its way by choosing a group with two words that does not have letter 'a
and after that it soon had to be settled with only one word that has a consistent response to all the letters guessing. This is very different from
the example with many three letter words earlier. The program can choose words that does not have 'a' or 'n' or 'e' or 't' or 'o' and still have many
words to choose from.
Your job is to write a wicked hangman engine that plays the game with a set of words to start with, and for each round of the user's guess,
instead of giving response based on one pre-chosen word, you analysis groupings of possible reponses for all the remaining words, choose the
grouping with the most words, and continue with the game.
Implementation Details for Hangman.
Command line arguments and output for each round of the game:
5 python3 hangnon, by w
missed letters: chances left)
Enter your guess:
After the hangman.py, the command line contains the following part: a file name with dictionary words; the length of the letters we will be
guessing; the number of maximum misses allowed.
For each round of the game:
Implementation Details for Hangman
Command line arguments and output for each round of the game:
$ python3 hangmon.py w1.txt Z
missed letters: (7 chances left)
Enter your guess:
After the hangman.py, the command line contains the following part: a file name with dictionary words; the length of the letters we will be
guessing; the number of maximum misses allowed.
For each round of the game
you will print out a pattern like below (or later with revealed letters at specific location)
о
⚫ you will print out the list of missed letters so far in a single whitespace sepatarted list. You will also print out how many chances left like
below:
o missed letters: (7 chances left)
Notice here you print out "missed letters:" with a trailing space and you print " (n chances left)" with a starting space to separate the two
parts from the letter list. So if there is no letters you will have two white spaces here.
you will prompt the user to enter their guess. You may assume a single letter will be entered here.
o Enter your guess:
you will print an empty line before next round.
if after this round you total missed letter reached the limit set at the command line; or if you have guessed every letter in the word correctly.
the game terminates. And it prints out either of the two messages below:
o You guessed the word: biddings
o You lost after 5 wrong guesses.
Tie breakers when choosing a group after a guess in wicked hangman:
1. Choose the group with the most words matching the pattern.
2. If there are more than one such group, choose the one with the most 's (or with the least revealed positions)
For example we chose the last pattern with all 's out of these two patterns with the most words in the previous example:
a.
3. If there are more than one such group after 2, choose the pattern that is largest in alphabetical order.
For example we chose the last pattern because it is alphabetically larger when two patterns have the same number of reveals and the same
number of words remaining:
ee:1
3. If there are more than one such group after 2, choose the pattern that is largest in alphabetical order.
For example we chose the last pattern because it is alphabetically larger when two patterns have the same number of reveals and the same
number of words remaining:
e_e_e_e_e:1
eeeee_1
Debug mode printing in wicked hangman:
As you can see in the wicked hangman program example, there is more info printed in debug mode (when you supply an extra command line
argument regardless of its value). I have provided a debug print function that will only print if we have an extra command line argument. And
you should use it in appropriate places to print out the following debug info in specific format to pass some of the gradescope's autograder
tests.
In debug mode, before the game starts it will print out how many words left for the program to choose from that are consistent to current
pattern and all the reponses given so far.
296 words left.
missed letters: anet (1 chances left)
Enter your guess: o
0.0:1
00:1
00:7
0:9
017
0194
:167
Then after the user gives a new letter guess, you will print out all the possible groupings of different responses to the letter o and how many
words belong to that grouping. So for example, out of the 296 words left, there are 167 words that don't have o. There are 94 with a single o in
the middle and so on...
This list of groupings should be printed out from the smallest grouping to the largest grouping, with the tie breaker defined as earlier (with fewer
's first and then in alphabetical order of the pattern). So the last one printed will be the pattern of choice for the next round.
123
2
3
4
5
from sys import argv
def debug_print(s):
if len(argv) > 4:
print(s)
6 def run():
7
8
return
9
10
11
if
name == '__main__':
run()
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.