Perl Quiz-of-the-Week #17

Marco Baringer <[email protected]> Wed, 26 May 2004 07:47:45 -0400
Newsgroups gmane.comp.lang.perl.qotw.quiz-of-the-week
Message-ID <[email protected]>
IMPORTANT: Please do not post solutions, hints, or other spoilers
        until at least 60 hours after the date of this message.
        Thanks.

IMPORTANTE: Por favor, no enviéis soluciones, pistas, o cualquier otra
        cosa que pueda echar a perder la resolución del problema hasta
        que hayan pasado por lo menos 60 horas desde el envío de este
        mensaje. Gracias.

IMPORTANT: S'il vous plaît, attendez au minimum 60 heures après la
        date de ce message avant de poster solutions, indices ou autres
        révélations. Merci.

WICHTIG: Bitte schicken Sie keine Lösungen, Tipps oder Hinweise für
        diese Aufgabe vor Ablauf von 60 Stunden nach dem Datum dieser
        Mail. Danke.

BELANGRIJK: Stuur aub geen oplossingen, hints of andere tips in de
        eerste 60 uur na het verzendingstijdstip van dit
        bericht. Waarvoor dank.

VNIMANIE: Pozhalujsta ne shlite reshenija, nameki na reshenija, i
        voobshe lyubye podskazki v techenie po krajnej mere 60 chasov
        ot daty etogo soobshenija.  Spasibo.

Qing3 Zhu4Yi4: Qing3 Ning2 Deng3Dao4 Jie1Dao4 Ben3 Xin4Xi2 Zhi1Hou4 60
        Xiao3Shi2, Zai4 Fa1Biao3 Jie3Da2, Ti2Shi4, Huo4 Qi2Ta1 Hui4
        Xie4Lou4 Da2An4 De5 Jian4Yi4.  Xie4Xie4.




When I was in elementary school I wasted many an hour playing hangman
with my friends. 

The Game of Hangman
--------------------

The goal of the game is to guess a word with a certain (limited)
number of guesses. If we fail the "man" gets "hanged," if we succeed
he is set free. (We're not going to discuss the lesson's of life or
justice this game teaches to the 8 year olds who play it regularly).

The game starts out with one person (not the player) choosing a
"mystery" word at random and telling the player how many letters the
mystery word contains. The player then guesses letters, one at a time,
and the mystery word's letters are filled in until a) the entire word
is filled in, or b) the maximum number of guesses are reached and the
the player loses (man is hanged).

Write a perl program which lets the user play hangman. The program
should take the following arguments: 

        1) the dictionary file to use 
        2) the maximum number of guesses to give the player. 

The program must then chose a mystery word from the dictionary file
and print out as many underscores ("_") as there are letters in the
mystery word. The program will then read letters from the user one at
a time. After each guess the program must print the word with properly
guessed letters filled in. If the word has been guessed (all the
letters making up the word have been guessed) then the program must
print "LIFE!" and exit. If the word is not guessed before the maximum
number of guesses is reached then the program must print "DEATH!" and
exit.

Example interaction:

        % ./hangman /usr/share/dict 5
                ___
        c
                ___
        m
                m__
        d
                m__
        a
                ma_
        n
                LIFE!
        $ ./hangman /usr/share/dict 3
                ___
        c
                ___
        m
                m__
        d
                DEATH!
        %


NOTES
- -----

1) The dictionary file will contain one word per line and use only
   7-bit ASCII characters. It may contain randomly generated
   words. The dictionary will contain only words longer than 1
   character. The size of the dictionary may be very large.  See

        http://perl.plover.com/qotw/words/

   for sample word lists.

2) The dictionary file used for the test (or the program for
   generating it) will be made available along with the write-up.

3) If a letter appears more than once in the mystery word, all
   occurrences of that letter must be filled in. So, if the word is
   'bokonon' and the player guesses 'o' the output must be '_o_o_o_'.

-Marco