Compsci 101, Fall 2017,
Smarty Hangman

Due: Thursday, November 9 by 11:59pm

10 points

See the howto page for details on starting this project, code, and so on. The page here describes in broad strokes what this assignment is about.



The Doobie Brothers I Cheat the Hangman

Hangman is a traditional children's game, typically played with words. For this assignment you'll program the computer so that it plays a smarty version of the game, some might say diabolically smarty version. Some might say it's a cheating or evil version of hangman. 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.

From the Computer's Viewpoint

Here is the idea of how the game works.

The output below has print-debugging enabled that shows how the computer is "playing" a game of smarty-hangman. The secret word is shown before each guess the human player makes. The number of words that could be the secret word is also shown. The human player doesn't know the secret word, she's trying to guess it.

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 (Smarty) Hangman:

(secret word: curries ) # words possible:  7359
Progress:  _ _ _ _ _ _ _
letters not guessed: abcdefghijklmnopqrstuvwxyz
guess a letter:  i
i  not in secret word

(secret word: tresses ) # words possible:  4048
Progress:  _ _ _ _ _ _ _
letters not guessed:  abcdefghjklmnopqrstuvwxyz
guess a letter:  e
you guessed a letter correctly!

(secret word: waffles ) # words possible:  969
Progress:  _ _ _ _ _ e _
letters not guessed:  abcdfghjklmnopqrstuvwxyz
guess a letter:  a
a  not in secret word

(secret word: toppled ) # words possible:  455
Progress:  _ _ _ _ _ e _
letters not guessed:  bcdfghjklmnopqrstuvwxyz
guess a letter:  o
o  not in secret word

(secret word: jumbled ) # words possible:  159
Progress:  _ _ _ _ _ e _
letters not guessed:  bcdfghjklmnpqrstuvwxyz
guess a letter:  u
you guessed a letter correctly!

(secret word: rumbled ) # words possible:  76
Progress:  _ u _ _ _ e _
letters not guessed:  bcdfghjklmnpqrstvwxyz
guess a letter:  r
r  not in secret word

(secret word: tumbled ) # words possible:  46
Progress:  _ u _ _ _ e _
letters not guessed:  bcdfghjklmnpqstvwxyz
guess a letter:  t
t  not in secret word

(secret word: bundles ) # words possible:  41
Progress:  _ u _ _ _ e _
letters not guessed:  bcdfghjklmnpqsvwxyz
guess a letter:  s
s  not in secret word

(secret word: buckled ) # words possible:  20
Progress:  _ u _ _ _ e _
letters not guessed:  bcdfghjklmnpqvwxyz
guess a letter:  d
you guessed a letter correctly!

(secret word: lunched ) # words possible:  15
Progress:  _ u _ _ _ e d
letters not guessed:  bcfghjklmnpqvwxyz
guess a letter:  n
n  not in secret word

(secret word: bumbled ) # words possible:  9
Progress:  _ u _ _ _ e d
letters not guessed:  bcfghjklmpqvwxyz
guess a letter:  m
you guessed a letter correctly!

(secret word: jumbled ) # words possible:  4
Progress:  _ u m _ _ e d
letters not guessed:  bcfghjklpqvwxyz
guess a letter:  b
you guessed a letter correctly!

(secret word: humbled ) # words possible:  3
Progress:  _ u m b _ e d
letters not guessed:  cfghjklpqvwxyz
guess a letter:  l
you guessed a letter correctly!

(secret word: fumbled ) # words possible:  3
Progress:  _ u m b l e d
letters not guessed:  cfghjkpqvwxyz
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.

hungimage

Here's part of a game with more print-debugging on. This shows how the computer chooses a new secret word. The user already has five misses, an explanation follows the flow of the game. (NOTE THIS IS NOT EXACTLY HOW YOUR OUTPUT WILL BE).

Game Output Explanation
(secret word: jumpy ) # words possible:  87
Progress:  _ _ _ _ _
letters not guessed:  bcdfghjklmnpqrtuvwxyz  
guess a letter:  u
Dictionary of Categories and # words:
__u__ 32
_u___ 47
_____ 8
-------
you guessed a letter correctly!
(secret word: bulky ) # words possible:  47
Progress:  _ u _ _ _
letters not guessed:  bcdfghjklmnpqrtvwxyz  
guess a letter:  b
Dictionary of Categories and # words:
_u___ 33
bu___ 14
-------
b  not in secret word
(secret word: lucky ) # words possible:  33
Progress:  _ u _ _ _
letters not guessed:  cdfghjklmnpqrtvwxyz  
guess a letter:  c
Dictionary of Categories and # words:
cu___ 2
_uc__ 2
_u___ 21
_u_c_ 8
-------
c  not in secret word
(secret word: funny ) # words possible:  21
Progress:  _ u _ _ _
letters not guessed:  dfghjklmnpqrtvwxyz  
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.

The Program You Write

Write a program that plays (cheating) smarty hangman. You should use your hangman assignment code from a previous assignment as a start.

Note the run examples on this page and the howto may have slightly different output. Be sure to follow the requirements below.

Details and a deeper explanation of how to write the program can be found in the howto.

Here are three sample runs:

Requirements

Smarty Hangman with guessing a word

  1. Write a program to play the "smarty" version of hangman for guessing words in which the program cheats by making it as difficult as possible to win by categorizing all the possible words based on the letter guessed, and then continuing with the largest category of words and choosing a new secret word from that category.
  2. You must write all your code in PlaySmartyHangman.py . (Make a COPY of your hangman program from assignment 5).
  3. You should create a testing mode and a playing mode. Ask the user which mode to play. Playing mode is the actual game. Testing mode is the game but showing extra information such as the secret word and whatever else may be helpful for you in getting the program to work.

    For example, here is how the game might start.

    Welcome to the Hangman game. 
    Do you want to play the game (g) or show the testing mode(t)?
    Type g or t: 
    
    

  4. The testing mode is a regular game, but it shows more information. The testing mode must show

    Feel free to show other information in this mode that might be helpful in debugging.

    Each item printed in testing mode that is not part of the playing mode needs to be identified, such as "Secret word is: baloney", "Dictionary of categories is: ", etc.

  5. The requirements for Assignment 5 should still be met. By starting with your completed code from assignment 5, you've already met these. Those requirements were:

    1. The user should have the choice of deciding the length of the word and the number of misses allowed.

    2. The secret word generated should be of length 3 or greater.

    3. The user can guess a letter or can guess the word. To guess the word the user should enter "+" and then they should be asked to guess the word. They don't get any more chances when they guess the word.

    4. After each letter guess, you should show
      1. the correct blanks and guesses for the secret word
      2. the number of incorrect letter misses left
      3. letters that have NOT been guessed in alphabetical order. For example, if they have only guessed 'a' and 'e', then show the string "bcdfghijklmnopqrstuvwxyz".
      4. If the user guesses a letter again by mistake, you should
        • tell the user they have already guessed the letter
        • count that as a miss

    5. You should print a message at the end of the game telling them one of three things:
      • They guessed the correct word.
      • They guessed the wrong word, and tell them what the correct word was.
      • They ran out of guesses, and tell them what the correct word was.

  6. Your program must consist of functions and function calls. You may use one global variable in your program as a DEBUG flag. See the howto on how to set this up.

  7. Your program must be reasonably robust in the face of user errors, but don't worry too much about that.

  8. You must have a comment for each function.

    If you write any additional functions, such as a helper function, then these functions must have comments describing the purpose of the function.

What to Submit

Please submit the following:

Submit your code to the folder assign7-smartyhangman using eclipse/ambient or the websubmit.