CSCI 220 Homework Assignment #7

Assigned Date: Monday, April 7, 2003 
Due Date: Friday, April 18, 2003 
Due Time: Noon 

Updated: Thursday, April 10, 2003 11:25 AM

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

Skills Developed: 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 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 drawLeftLeg()

public void drawRightLeg()

The following methods are useful for controlling the game.

<Updated April 10, 2003>  This initializes "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(); )  

<Updated April 10, 2003>  public char [] initializeWord() 

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

public char getUserInput()

This verifies that input provided by user is a letter.

public boolean isInputValid(char input)

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

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

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

public void drawHangman(int whichWrongGuess)

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.

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

Bonus Part:  

Have your program randomly select a word from a list of (at least 20) stored words.