Taken from Haskell Programming From First Principles (aka The Haskell Book) Chapter 13
In case anyone is not familiar with the game, Hangman is a game with 2 (in this case) player. The first player picks a word at random and the second has to guess that word letter-by-letter. Typically game also involves drawing an empty gallows pole and each incorrect guess adds a body part to the gallows starting at the head, then body, one arm, then the other and so on until there is second player has guessed the word (win) or all the body parts have been drawn (lose). The second player can make as many correct guesses as is needed to fully 'expose' the word; each correct guess exposes a part of the target word.
___
| |
|
|
|
___|___
word: _ _ _ _ _
___
| |
O |
|
|
___|___
word: _ _ _ _ _
___
| |
O |
|
|
___|___
word: _ _ e _ e
Write a basic hangman game using the word list provided. For each game pick a word from the list at random; there are all sorts of words so you might want to specify a min/max length.
- Pick a word at random
- Ask the player for a letter
- Tell the player whether the guess was correct or not
- Display the current game state to the player
- A letter can only be guessed once
- The player has a fixed number of incorrect guesses. That number represents the number body parts; usually 6 but I can rember playing games as a kid that also included feet and hands so the number was 10.
- You don't need to draw the gallows or the person...but you can.
- Have fun!
Current puzzle is: _ _ _ _ _ Guessed so far:
Guess a letter:
Your guess was: r
This character wasn't in the word, try again.
Current puzzle is: _ _ _ _ _ Guessed so far: r
Guess a letter:
Your guess was: a
This character was in the word, filling in the word accordingly
Current puzzle is: _ _ _ _ a Guessed so far: aer
Guess a letter: