Hangman is a traditional children's game, typically played with words. For this assignment you'll program the computer so that it plays a clever version of the game, some might say diabolically clever version. Some might say it's a cheating or evil version of hangman. The evil adj ective was applied in a 2011 Nifty Assignment, but a form of the game dubbed cheating is described on this website. For this program you'll leverage the power of dictionaries and a greedy algorithm to program the computer to play a game in which the computer changes its mind, by changing its secret word so that the new secret word is consistent with previous guesses, but harder (perhaps) to guess.
When the game starts, the computer pick a word at random as the secret word, in this case the word curries has been chosen. The player has no misses and no letters of the secret word are shown. Since print-debugging is on, the computer displays the secret word and the number of possibilities for the secret word -- in this case all 7,359 seven-letter words in the computer's lexicon are possible. In the output below the italicized lines would normally not be printed as part of the game
Welcome to (Clever) Hangman: (secret word: curries ) # words possible: 7359 Progress: _ _ _ _ _ _ _ letters missed: guess a letter: i i not in secret word (secret word: tresses ) # words possible: 4048 Progress: _ _ _ _ _ _ _ letters missed: i guess a letter: e you guessed a letter correctly! (secret word: waffles ) # words possible: 969 Progress: _ _ _ _ _ e _ letters missed: i guess a letter: a a not in secret word (secret word: toppled ) # words possible: 455 Progress: _ _ _ _ _ e _ letters missed: i a guess a letter: o o not in secret word (secret word: jumbled ) # words possible: 159 Progress: _ _ _ _ _ e _ letters missed: i a o guess a letter: u you guessed a letter correctly! (secret word: rumbled ) # words possible: 76 Progress: _ u _ _ _ e _ letters missed: i a o guess a letter: r r not in secret word (secret word: tumbled ) # words possible: 46 Progress: _ u _ _ _ e _ letters missed: i a r o guess a letter: t t not in secret word (secret word: bundles ) # words possible: 41 Progress: _ u _ _ _ e _ letters missed: i a r t o guess a letter: s s not in secret word (secret word: buckled ) # words possible: 20 Progress: _ u _ _ _ e _ letters missed: a i o s r t guess a letter: d you guessed a letter correctly! (secret word: lunched ) # words possible: 15 Progress: _ u _ _ _ e d letters missed: a i o s r t guess a letter: n n not in secret word (secret word: bumbled ) # words possible: 9 Progress: _ u _ _ _ e d letters missed: a i o n s r t guess a letter: m you guessed a letter correctly! (secret word: jumbled ) # words possible: 4 Progress: _ u m _ _ e d letters missed: a i o n s r t guess a letter: b you guessed a letter correctly! (secret word: humbled ) # words possible: 3 Progress: _ u m b _ e d letters missed: a i o n s r t guess a letter: l you guessed a letter correctly! (secret word: fumbled ) # words possible: 3 Progress: _ u m b l e d letters missed: a i o n s r t guess a letter: f f not in secret word You are hung! word was jumbled |
Although the user guesses 'i', which is in the computer's secret word,
the computer switches secret words so that 'i' is not in the word, and
the user has one miss.
As the game progresses the user is shown guessing a letter in the secret word, but for the first time on the third guess --- the secret word waffles is the letter revealed as part of the word. As the game progresses the user eventually gets eight misses and loses.
|
Here's part of a game with more print-debugging on. This shows how the computer chooses a new secret word. The user alread has five misses, an explanation follows the flow of the game.
Game Output | Explanation |
---|---|
(secret word: jumpy ) # words possible: 87 Progress: _ _ _ _ _ letters missed: a i s e o guess a letter: u __u__ 32 _u___ 47 _____ 8 ------- you guessed a letter correctly! (secret word: bulky ) # words possible: 47 Progress: _ u _ _ _ letters missed: a i s e o guess a letter: b _u___ 33 bu___ 14 ------- b not in secret word (secret word: lucky ) # words possible: 33 Progress: _ u _ _ _ letters missed: a b e i o s guess a letter: c cu___ 2 _uc__ 2 _u___ 21 _u_c_ 8 ------- c not in secret word (secret word: funny ) # words possible: 21 Progress: _ u _ _ _ letters missed: a c b e i o s guess a letter: |
When the computer indicates its secret word is
jumpy the user has already guessed and missed five letters:
a,i,s,e,o. When the user guesses the letter 'u', the
computer
determines that there are 32 words with 'u' as the third letter, 47 words
with 'u' as the second letter and 8 words that do not
contain a 'u'. None of these 47+32+8 = 97 words contains
either an a,i,s,e, or o. The computer picks one of the
words with 'u' as the second letter because there are more
of these words than the other possibilities.
When the user guesses 'b', there are 33 words that do not contain a 'b' at all (but have 'u' as the second letter) and 14 words that start with 'bu'. No words have a 'b' in positions 3-5 or those templates would have been shown. The computer chooses 'lucky' as its secret word because there are more words without a 'b' than with a 'b' (with 'u' as the second letter.) The user then guesses 'c'. There are 21 words that do not contain a 'c' and, for example, 8 words with 'c' as the fourth letter---one such word might be 'lunch', for example. Since there are more words without a 'c' than with a 'c' in any specific location the computer chooses 'funny' as its secret word. This process continues with the computer's secret word always consistent with the player's guesses and the displayed word-template. |
Details and a deeper explanation of how to write the program can be found in the howto.
Use cleverhang to submit the program.