Program Assignment #4 (20 points)

Due: Thursday, May 3

Write a program that plays an interactive game of Hangman with its user. In general, the rules of Hangman are as follows:

· The object of the game is for the user to guess a secret word, given the number of letters in the word. The number of letters is presented as a series of dashes or underscores, like this: _ _ _ _ _ _

· At each turn, the user guesses one letter. If the guess is correct, the letter is displayed in the correct position for the user to observe, like this: M _ _ M _ _

· If the guess is incorrect, the user’s number of chances to “live” is decreased. Initially, the user has 6 chances. If all 6 chances are exhausted before the user guesses all the letters correctly, the user loses the game. If the user guesses all the letters, making the correct word, the user wins.

Program requirements:

· You are required to use methods and parameter passing appropriately. Each separate task should be done by a separate method

· Use a two-dimensional array of type char to hold a library of 20 guessable words, Tip: You will find it much easier to work with the array if all of your words are the same length! You should initialize your library array upon declaration.

· Use one-dimensional arrays of type char to store individual words. Do not use strings or string library methods for this program.

· One of your methods should be responsible for choosing a different word from the library each time a game is to be played. This method should be written so as to ensure that no single word is used twice until all words have been used at least once. Words should be chosen at random, so that the next time the program is run, different words might be chosen.

· The user should be able to play as many games as s/he wishes to during a single run of the program.

· The program should keep track of the user’s incorrect guesses. The user should not be penalized for making the same wrong guess twice. Instead, the program should inform the user that s/he has already tried that letter.

Extra credit options (worth 10 points each):

· Instead of hard-coding a library of words into your program, read words from a dictionary file containing a word list. For this option, provide a dictionary file along with your source code when you turn in the program. For an additional 5 extra credit points, make it possible to choose from several different dictionaries, each of which contains words of a different length; for example, one might contain 6-letter words, another 8, and so forth. Provide 2 example files if you choose this option.

· Add a graphical user interface (GUI) that draws the hangman figure and changes the drawing each time the user guesses a wrong letter


Sample program run: (with user responses)

Your word: _ _ _ _ _ _ You have 6 guess(es) left. Your guess: E

Your word: _ _ _ _ _ _ You have 5 guess(es) left. Your guess: S

Your word: _ _ _ _ _ _ You have 4 guess(es) left. Your guess: M

Your word: M _ _ M _ _ You have 4 guess(es) left. Your guess: O

Your word: M _ _ M _ _ You have 3 guess(es) left. Your guess: E

You already guessed E. Try again.

Your word: M _ _ M _ _ You have 3 guess(es) left. Your guess: Y

Your word: M _ _ M _ _ You have 2 guess(es) left. Your guess: N

Your word: M _ _ M _ _ You have 1 guess(es) left. Your guess: R

Your word: M _ R M _ R You have 1 guess(es) left. Your guess: I

Sorry, you’re dead. The word was MURMUR

Play again? (Y/N): Y

Your word: _ _ _ _ _ _ You have 6 guess(es) left. Your guess: A

Your word: A _ _ _ _ _ You have 6 guess(es) left. Your guess: O

Your word: A _ _ _ _ _ You have 5 guess(es) left. Your guess: I

Your word: A _ _ I _ _ You have 5 guess(es) left. Your guess: E

Your word: A _ _ I _ _ You have 4 guess(es) left. Your guess: S

Your word: A _ _ I _ S You have 4 guess(es) left. Your guess: N

Your word: A N _ I _ S You have 4 guess(es) left. Your guess: D

Your word: A N _ I _ S You have 3 guess(es) left. Your guess: C

Your word: A N _ I _ S You have 2 guess(es) left. Your guess: L

Your word: A N _ I L S You have 2 guess(es) left. Your guess: V

Your word: A N V I L S You got it! Congratulations, you live!

Play again? (Y/N): N

Goodbye!

Computer Science I Spring 2007

Sheller Page 1