CSE142 Spring 2004      Assignment 5

Word Guess Game (also known as Hangman or Hangperson)       Due Dates: see below

In this last assignment, you write a complete program to play a word game.  You will have a chance to use all of the programming concepts you have learned so far this quarter.  A new feature, Java interfaces, will be important.  We return to using the uwcse.graphics package to create a game with appealing visual output.

In the game, essentially Hangman/Hangperson, a player tries to guess a word.  The unguessed letters are represented as a sequence of underscore characters, one per letter. The player guesses one letter at a time. If the letter is in the word, all the underscores representing the letter are replaced with the letter. If the letter is not in the word, it counts as an incorrect guess; traditionally, this is shown as a body part in the gallows. If all the letters in the word are guessed before the body is hung, then the player wins. The body is hung on the seventh incorrect letter guess and the player loses.

Before you get too far into the assignment, make sure you understand how to play the game.  If you have not played it before, find someone  you can play it with.  Once you understand it, test your understanding as follows: find someone who does not already know the game and teach it to them.

There are many implementations of this game on the Web.  Here are two could can try out:   one game   and   another version .   While some versions use phrases, the version you implement for this assignment will have a player guess one word.  When the game session starts, the computer picks a word and the player makes a series of guesses.  After one game is done (i.e., the player has won or lost with a single word), the player has the option of continuing with another game (another word), and so on indefinitely.

In your implementation, you may use the traditional gallows and hung body or some other representation to show missed guesses. For example, you could show weeds growing, a ball falling, the sun setting, etc. This is a time for creativity!  In fact, a small part of the grade for the assignment will be based on visual appeal and creativity.

Whatever representation you choose, the player has seven wrong letter guesses before losing. Correct letter choices aren't factored into losing, but if the player guesses a letter that has already been guessed, that counts the same as guessing an incorrect letter.

Supplied Files

As in previous assignments, there is a directory of files.  All of the .class files and .java in it must be downloaded and copied into the same directory you plan to place your own .java files in.  For convenience, the files are also available in a .zip file (use that if you like; instructions are not provided).  Follow the same general procedures as for the previous assignments.

The supplied .java files should all compile immediately without error, except for one: TestPreTurnIn.java.  This file will not compile until your own Movement I .java files are complete.

A class HangmanDictionary provides access to a list of words to use. Use this in your game as a source of words to be guessed.  In support of the dictionary, some additional .class files are supplied (DelimitedTextReader.class, etc.; these are updates of the version used for the previous assignment, so copy them all again).  There is a large file (linuxwords.txt) of English words that can be used as input to the HangmanDictionary.

The game code that you write is largely specified by Java interfaces: IHangmanWord.java, IHangmanGame.java, and IHangmanSession.java.   Don't modify any of these files as you will not be able to turn in any interface files.  Your classes must compile and run with the original versions.  For each of the interfaces, you will (eventually) create and implement the concrete classes Word, GameController, and SessionController.  Check out the documentation to understand which operations are expected to be in each class.  The project won't make any sense until you have read through all of those interfaces carefully.

Program/Implementation Requirements

The following requirements must be met (given in no particular order).  You may wish to skip this section on first reading, but be sure to come back and study it carefully before writing any code.

Class Interaction Explanation

While you decide on your instance variables for SessionController, GameController, and Word, you may not realize that SessionController and GameController need references for each other. This section explains why. You may wish to skip this section on first reading, but come back to it and study it carefully before writing code for GameController and SessionController.

Because both GameController and SessionController draw the window, objects of both classes need access to the window. The GWindow is an instance variable of one of these two classes. (You decide which class it is based on what it means to be an instance variable. Ask yourself "Is the window associated with the whole session (all the games)?"   and   "Is the window associated with only one game?" The answer to these questions should tell you which owns the Gwindow.)

So that both have access to the window (without having to pass it as a parameter all over the place), each object has a reference to the other, as seen in the diagram. With this organization, a GameController object can use its SessionController reference to display the window.  (See the IGameController documentation for a mention of this where it discusses the constructor).

Tune-up

For the tune-up, draw (on paper, by hand) sketches of how your window will look in the finished program. Look at this sample window layout to see what information must be present in your display.  Sketch a sequence of pictures starting with player has zero wrong guesses, then one wrong guess, up to seven wrong guesses. Show what the window will look like if the player wins (the winning message) or if the player loses (the losing message and correct word).

Answer the following questions in a file called tuneup.txt to show you understand the methods provided and the classes you will write.  You will have to refer to the documentation to answer many of them.

  1. Among the files you should have downloaded is one called TestSetup.java.  Does it compile without any syntax errors?  If not, something is wrong.  Find out what and fix it.  Note that this file does not need to execute -- it just has to compile.
  2. Browse the documentation for the Hangman Dictionary.  In brief (11 words or less), what kind of file can be used as a dictionary? 
  3. In the interactions pane, what can you type in to create a new instance of a dictionary (one line of Java code)?  If you are asked for a file, click Cancel.  (If you can't create this object, then it is likely that your installation of the .class files is not correct.  Figure it out before continuing.)
  4. Use an appropriate method to retrieve the first word from the dictionary. What is it?
  5. Retrieve the next word. What is it?
  6. Create another dictionary instance; this time, point it toward some textfile (one whose contents you are well aware of).  What file did you use?  What are the first two dictionary words?  Do they in fact occur in the text file?
  7. The Word.java class that you write must use the IHangmanWord interface.  What is the magic incantation that the Word class will need to indicate that it is implementing the IHangmanWord interface?  (Hint: the import statement is not involved!)
  8. The documentation for IHangmanWord mentions a constructor.  But notice that this information is given in the general comments at the beginning of the file.  You might expect to find one or more constructors with separate documentation, as with practically every other class we've seen (go back and check!).  Why isn't there here?
  9. When you have finished implementing your program, which method of which class will read one guessed letter from the user?  Which interface specifies this?
  10. When the program is finished, which method of which class will display the game status on the screen?  Which interface specifies this?
  11. When the program is finished, which method of which class checks to see if a guessed character is correct?  Which interface specifies this?
  12. In the interactions pane, enter
         'a' - 'a'
         'b' - 'a'
         'c' - 'a'
         'y' - 'a'
         'z' - 'a'
    What values do you get? Now enter a short sequence of code:
         char ch = 'e';
         int n = ch - 'a';
    What is the value of n? Repeat this exercise a few times. What do you think happens if you repeat this experiment with all the lowercase letters? How about using uppercase letters; would it work the same way?
  13. A number of methods of class String may be helpful in addition to methods you already know.   What can you write in DrJava to do the following:
           Given a string, get its first character.
           Given a string, get its last character.
  14. At various points in the program you will need to work with characters.  The wrapper class Character has a number of methods that can make your life easier.  Experiment with them. How can you do each of these tasks (in one or two lines of Java in the Interactions pane):
           Given a char variable,  how do you make a Character object containing the same value?  
           Given a Character object, how can you get a char out of it? 
           Given a char value, how can you convert it to lowercase? 
           Given a char value, how can you convert it to a String?

Movement #1

Electronic due Wednesday, May 19 at 10pm (Word.java, GameController.java, SessionController.java, and tuneup.txt). Paperwork including
  • your drawings from the tune-up
  • electronic turn-in receipt
  • a screen snapshot of your window after the player wins (but in a game where the player got three wrong letter guesses before winning)
  • a screen snapshot of your window after the player loses (see instructions below for getting a screen snapshot).

  • is due in class the next day.   (Remember one turn-in per team.)   Be sure to put your quiz section on all papers.

    Write all of the Word class. This includes methods as described by the given interface. Figure out what your instance variables will be (don't forget to document them) and write any additional methods needed.

    Write stubs for the other classes:   SessionController.java  and   GameController.java.   A stub is a method without a body, but enough syntax so it compiles. For example:
         public int getStatus() {
              return 0;
         }
    or
         public void display() {
         }

    After creating your Word class, test it out in the DrJava Interactions pane.  Create a Word object. Verify that your class is correct by trying out all the methods. Guess correct letters. Guess incorrect letters. Do you handle a guess of a non-letter character? You may need to write temporary display method(s) to see what is currently stored, to see if your methods work as expected.

    When you turn in your classes, they must all compile.  Except for Word, they do not need to operate correctly, but they must compile without errors. Screen snapshots of your window need not be complete, but you need to show progress through each movement.

    How to you get a "screen snapshot"?  Here is one way, in Windows.  If it doesn't work, or you are not using Windows, ask for advice on the Message Board. 

    First, get the image or window you want to capture fully visible on the screen. 

    Locate the button on your keyboard marked Print Screen. 

    Press ALT and PrintScreen together. 

    This action may seem to do nothing, but actually it places an image of the screen on the Windows Clipboard. 

    Next, open a program which knows how to process image files and which also knows how to print.  There are many of these, including Word, Powerpoint, Paint, FrontPage, and others.  Paste (from the clipboard) into the program.   You should see your screen image.  Print it.

    Movement #2

    Electronic due Monday, May 24 at 10pm (Word.java, GameController.java, and SessionController.java). Paperwork including
  • electronic turn-in receipt
  • a screen snapshot of your window after the player wins (but in a game where the player got three wrong letter guesses before winning)
  • a screen snapshot of your window after the player loses (see above for hints on getting a snapshot)

  • is due in the next class day.   (Remember one turn-in per team.)   Be sure to put your quiz section on all papers.

    The goal at this step is to be able to start the program and play one game fully and correctly.  To do this, complete the GameController class, that is, write the full final version of all of its methods.  Also implement improved versions of the methods of the SessionController class.  These versions don't have to be the full final versions, but they should be enough so that your program can run and play at least one game correctly.  You may find that some methods can be left as stubs for the time being.

    Continue working on screen output, making what you display in the window closer to completion. While your window may not display perfect graphics, all information that pertains to the game should be clear to the player. Display the total number of games played (might be just one game!) and the number of games won and lost.

    Movement #3

    Final electronic due Thursday, May 27 at 10pm (Word.java, GameController.java, and SessionController.java, and movement3.txt). Recall from the program requirements that your final receipt is to have one array, ArrayList, and Iterator declaration highlighted. Write clearly which was used in large print beside the highlighted code. Paperwork including
  • electronic turn-in receipt
  • a screen snapshot of your window after the player wins (but in a game where the player got three wrong letter guesses before winning)
  • a screen snapshot of your window after the player loses

  • is due in class the next day.   (Remember one turn-in per team.)   Be sure to put your quiz section on all papers.

    Complete all classes, to the point where all requirements of the assignment are met:   correctness, practice, programming requirements, graphics, etc.

    Discuss the following questions with you partner and answer them in a file called movement3.txt.

    1. Pick a place where you chose to use an array. Briefly, where is it used and what for? Was that a good choice? Explain why or why not.
    2. Pick a place where you chose to use an ArrayList. Briefly, where is it used and what for? Was that a good choice? Explain why or why not.
    3. Why do you think having one game controlled in a class and having the session controlled in another class is good design?
    4. Think of an addition to the game playing that would give it your own personal touch.
    Turn in your individual cheers/jeers paragraph as well.   Be sure to put your quiz section on your paper.

    Cheers/Jeers

    On your own (separate from your partner) write a short paragraph about your pair-programming experience.

          What worked well, what didn't?
          How well was the work shared between you?
          Was there anything particularly challenging about this assignment?   If so, what?
     
    Thinking back over the quarter, what general comments do you have about pair programming? For example, what did you especially like, especially dislike, what do you wish had been different, etc.

     Hand in this write-up at the same time as the other paperwork for the assignment.