Skip to the content of the web site.

Project M.4: Hangman

Write a game to play hangman. This is where the user attempts to guess a word or phrase by guessing letters of the alphabet. Each time the player correctly guesses a letter that appears in the word or phrase, all instances of that letter are revealed, but if the letter does not appear, this is considered a strike. The player is allowed a maximum of five strikes and on the sixth, the player loses the game. Strikes are recorded pictorially as the gradual appearance of a hanged figure:

Initially, all that is visible are the gallows:

   ___
  |   |
  |
  |
  |
 _|_______
/         \

Then, each time the player guesses a letter that does not appear in the word or phrase, another part of the body appears on the screen, in the order:

  1. head,
  2. torso,
  3. left arm,
  4. right arm,
  5. left leg, and
  6. right leg,

after which, the phrase You lose appears, and the un-guessed word appears:

   ___
  |   |
  |  \o/
  |   |       You lose
  |  / \
 _|_______          T A L L Y - H O
/         \

Play again (y/n)? 

To begin, those letters of the alphabet that have not yet been guessed by the player appear at the bottom. The word

   ___
  |   |
  |
  |
  |
 _|_______          _ _ _ _ _ - _ _
/         \

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Guess a letter: 

When the player chooses a letter, it disappears from the list of available letters and either those letters in the word or phrase matching the letter appear or the player receives another strike. There is no penalty for guessing a non-alphabetic character or accidentally guessing a letter a second time. It should not matter whether a player guesses a letter with lower case or upper case.

Note: When the word or phrase is displayed, there should be a space between all characters in the word or phrase, including spaces between blanks. All non-letters should be immediately revealed. For example, each of the following words or phrases would initially appear as:

	HELLO                _ _ _ _ _
	HELLO WORLD!         _ _ _ _ _   _ _ _ _ _ !
        DON'T DO THAT        _ _ _ ' _   _ _   _ _ _ _
        GOOD BYE             _ _ _ _   _ _ _
        TALLY-HO             _ _ _ _ _ - _ _