CS 312 - Project 8
Practice with: File input, problem decomposition, arrays, graphics, class constants, psuedo-random number generation with the Random class

For this project, you will write a game of Hangman using arrays. You will allow the user to guess letters, and you will represent which letters have been guessed in an array.

This assignment is NOT a pair programming assignment. You must complete this project alone. Before you begin this assignment, review the project guidelines on the CS 312 assignments page.

For this assignment, you are limited to Java language features in chapters 1 through 7 in our textbook.

As for all CS 312 projects, your file comment header should look like this:
*  author: <Your Name Here>
*  date:  <Submission Date>
*  CS 312 Assignment 8
*  On my honor, <Your Name>,   this programming assignment is my work, and mine alone.
*  EID: <Your EID>
*  Section: <Unique Number>, <Thursday discussion time>
* <Brief Description - what does the program do?>
* Slip Days I am using on this project: <Your Slip Days>
* Slip Days I have used this semester: <Your Total, including for this project>

Game Description:
In Hangman, there are two players. One player thinks of a word, and the other tries to guess it by guessing letters. If the guessed letter is in the word, the word is produced with all guessed letters in the correct position. Unguessed letters are replaced with dashes. Whenever an incorrect letter is guessed, one of six parts of a man's body are drawn (in this order: the head, the torso, two arms, two legs).  Once the second player has made six incorrect guesses, the game is over. Alternately, the game is also over once the second player has correctly guessed all letters in the word.

Program Details:
Your program will randomly pick a word from the file dictionary.txt for the user to guess. The first line of dictionary.txt is the number of words in the file, and after that, each line contains a single dictionary word. For example, dictionary.txt could look like this:

As you see in this example, all words will consist of lower-case letters only. As your program's user is guessing letters, (s)he may guess lower or upper case letters. You will use a one-dimensional array of size 26 to track which letters have been guessed.

Your program will prompt the user to guess a letter in the word, until either the entire hangman (six body parts) has been drawn on a DrawingPanel object, or the user has correctly guessed all letters in the word. If the user guesses a letter more than once, a message indicating that the letter has already been guessed is displayed (if the letter does not occur in the word, a part of the hangman is only drawn the first time the letter is guessed). Note that a guessed letter may occur more than once in the word.

The dimensions of your DrawingPanel should be 100x150.

Sample Run(in blue below): (The word that was chosen by the program is antelope.) The user's inputs are underlined in the sample output below.

Welcome to Hangman!

You are guessing a word of length 8.

Guess a letter: h
Oops! Letter h is not there. Adding to hangman...

Guess a letter: a
Good guess! The word is:

Guess a letter: p
Good guess! The word is:

Guess a letter: r
Oops! Letter r is not there. Adding to hangman...

Guess a letter: x
Oops! Letter x is not there. Adding to hangman...

Guess a letter: u
Oops! Letter u is not there. Adding to hangman...

Guess a letter: c
Oops! Letter c is not there. Adding to hangman...

Guess a letter: r
You have already guessed r.

Guess a letter: v
Oops! Letter v is not there. Adding to hangman...

Game Over!

After each incorrect guess, your program will draw another body part of the hangman. Draw the hangman's body on a DrawingPanel object. When all six parts have been drawn, the hangman will look like this:

hangman image

When an incorrect letter is guessed, draw the next body part. The body parts are drawn in this order: head, torso, left arm, right arm, left leg, right leg.


You will use conversions between char and int so that entry 0 in your array tracks whether letter a has been guessed, entry 1 tracks whether b has been guessed, etc.

You must include the following methods:

1. void draw(int part, Graphics g): This method draws the next body part on the hangman. Use integers 0 through 5 to represent the six parts. Class constants should represent these integers so that they do not appear as "magic numbers" in your program. E.g.,
        public static final int HEAD = 0;

2. String pickWord(String f): f represents the filename, so when you call this method, the argument will be "dictionary.txt". This method returns a randomly chosen word from the file.

3. void playGame(String word): In this method, you will create your array, create your DrawingPanel and graphics objects, and read all inputs from the user. I suggest that you use a boolean flag as your while loop condition. Once the game is over, either because the entire hangman has been printed or the word has been completely guessed, this method will display "Game Over!". See the sample output above for more details.

4. boolean printGuess(int[]  letters, String word): This method takes the array which represents the letters which have been guessed and the word that the user is trying to guess. The method displays to the console a String which represents the word, with guessed letters appearing in the correct position(s), and unguessed letters replaced with dashes. (See sample output above for more detail). This method returns a boolean that is true if there are still unguessed letters in the word, and false otherwise. While I have indicated that the first parameter should be an int array, it may be a boolean array instead (your choice).

Your main() method should not directly perform any file I/O or read any input from the user. It should call pickWord() to choose a word from the dictionary.txt file, and call playGame().

Stylistic Guidelines:

You must:
1. Use class constants to represent the parts of the hangman, as described above.

2. Use whitespace, meaningful variable and method names, and comments to make your program readable.

3. Comments should be included at the beginning of your program, on each method, and on complex sections of code. Use descriptive, concise comments.

4. Avoid redundant code. Someone who is reading your program should be able to easily follow the sub-problems that each method solves.

5. Use parameters to pass information from one method to another. Global variables are NOT allowed.

Did you remember to:
Complete this project on your own?
Use meaningful variable and method names?
Add comments that describe each method, and each block of code inside a method that carries out a complex operation?
Add whitespace (blank lines) to improve readability of your code?
Submit the file Hangman.java by the due date?
Email the graders (Lisa, Hang, Monique and Daniel) after program submission, if you use slip days for this project?