CSCI 220 Homework Assignment #6

Assigned Date: Tuesday, November 11, 2003 
Due Date: Friday, November 21, 2003 
Due Time: Noon 

Program file names to be submitted: Hangman.java, HangmanDriver.java, Dictionary.java

Skills Developed: Object-oriented programming, creating and calling methods, program design.

Specification:

In the game of Hangman, a player tries to guess a secret word. With each wrong guess, the stick figure of a hanging man is partially drawn. When six wrong guesses have been made, the figure is complete, and the player loses the game.

Part 1:  

Write methods to draw the hanging man as he looks after each guess. In other words, you should draw six pictures, with each picture incorporating the previous one. (Hint: Start with a DrawHat() method, then let the DrawHead() method call it, etc.) Each method should call the method defined before it.)

Part 2:  

Have your program allow the user to play the game of Hangman for a specific word stored as an array of characters. Design your program so that the word could easily be changed without having to change any code - other than the statement declaring the word.

(Hint: use two parallel arrays: one to store the word and another to store the portion of the word that has been guessed.)

After each guess, you should show as much of the word as has been guessed and the appropriate drawing.

Notes:

  1. You should modularize your code appropriately.
  2. User guess should not be case-sensitive. That is, if they user enters 'A' or 'a' it should be a good guess if the word contains any occurrences of the letter 'a'.
  3. If the user guesses the same wrong letter over and over, count each as a wrong guess and terminate the game after six wrong guesses.
  4. If the user keeps guessing the same correct letter, output a warning only; do not count each as a wrong guess.

Design:  

For this program, implement the following methods and use them in your solution for playing the game of hangman. Your main method will drive the game playing by calling the methods below (some of which will call each other, as needed). (You may add additional methods, but do not eliminate any of those described below.)

The following are methods for drawing the body in pieces; each generates the hanged man up to that part: drawHead() calls drawHat() before drawing the head, so the result is a head wearing a hat, etc. You can design your own six drawing methods, rather than using those 6 described next as long as each calls the others before it, as described in the Part 1 above.

public void drawHat()

public void drawHead()

public void drawNeck()

public void drawTorso()

public void drawLegs()

public void drawFeet()

The following methods are useful for controlling the game.

public char [] initializeWord() 

This initializes the "secret word" for playing the game.  It returns a reference to a character array.  The caller should save the returned value in a character array reference variable (e.g., char word[]; ... word = initializeWord(); )  

public char getUserInput()

This prompts for user's guess, verify that guess is valid, by calling routine isInputValid()

public boolean isInputValid(char input)

This verifies that input provided by user is a letter.

public boolean isGuessCorrect(char guess, char secretWord[], char guessedWord[])

This checks if user's guess is correct - is this letter in the secret word? If so, it updates the guessed word.

public void drawHangman(int whichWrongGuess)

This draws hangman if guess was not correct. Method drawHangman() calls appropriate drawing method (above) based on number of wrong guesses thus far.

public boolean isGameOver(int wrongGuesses, char secretWord[], char guessedWord[])

This checks if game is now over. Game is over if 6 wrong guesses have been entered or if the word has been guessed correctly.

Part 3:  

Extend the program that plays Hangman by adding the class Dictionary.  This class encapsulates a list of words. The Hangman program can ask a Dictionary object to give it a new word by calling method getWord(). This method accepts no parameters and returns a pointer to an array of characters.

A Dictionary object should store its words in an array of String type.  This array should be initialized through the default constructor.  A Dictionary object should also include a toString() method to return all the words in the dictionary (Hint: Use a loop to create a single concatenated String.)

Include these words into every Dictionary object.  

Method getWord() should randomly return one of these words every time it is called.  (Hint: Use Math.floor( Math.random()*MAX) to get a random number between 0 and MAX-1.)