CIS 180: Object-Oriented Programming I
Programming Project #4

Word Challenge

Due by November 11, 2003

Objectives

The objectives of this project are:

Problem Statement

For this project you are asked to complete the design and implementation of a word game. Word Challenge is a two player game. Before play begins the players must agree on the length of the words that they will use. The longer the words, the more challenging the game! Play begins when player one types a secret word. Player two tries to guess the word by typing a guess and then pressing "Enter" in the guess field. Each time player two guesses, the computer tells how many letters of the guess match letters in the secret word, and of those that match, how many are in the correct position. The object of the game is to use these clues to guess the word in as few tries as possible. The computer keeps score (number of guesses). After player two guesses the secret word, the game can be reset. Now it is player two's turn to enter a secret word, and player one guesses. The lower score wins.

You can play an applet version of the game here:

In an actual game the word and every guess must be a real word that could be found in a dictionary. But for purposes of explaining the rules and testing your code, we will find it useful to use arbitrary strings.

Pay careful attention to how the application reports the number of matches and number in the correct position when the word or the guess (or both) has more than one letter with the same value. Each letter can be part of only one match, and a match in the correct possition takes precedence over matches in incorrect positions. For example, if the word is abcba and the guess is cacde, there are two matches and one is in the correct position. The 'a' in the guess matches one of the 'a's of the word, and the second 'c' of the guess matches the 'c' in the word.

Design

Some of the work has already been done for you. Since we are writing an application (instead of an applet) the WordChallenge class extends the Frame class instead of the Applet class. A Frame is a top level application window. The frame contains several other user interface objects. There are TextField's for entering the secret word and the guesses. There is a reset Button, and there are a number of labels for displaying text. You will need to change the text of the Label's called matches, correct, and score as the program runs to display the appropriate information.

The starting code you are given creates all of the user interface objects and arranges them within the frame. Your job is to complete the design and implementation so that the application functions properly when the words and guesses are entered.

Think carefully about the algorithm for reporting the the number of matches, and the number in the correct position. You may find it useful to create StringBuffer's containing the word and the guess. StringBuffer's have all of the methods of String's plus methods such as setCharAt for changing the values of characters within the string. This allows you to "cross out" a letter that has already been used in a match by changing it to a character that is not a letter, say '#'.

  1. In order to handle guesses being entered and the reset button being clicked you will have to implement the ActionListener interface. What method is required by that interface?
  2. Can you use additional methods to further decompose and simplify the problem?
  3. Draw a UML class diagram of the WordChallenge class, showing all of the instance variables and methods, including their accessibility (public or private) and data types
  4. Develop a written specification for the WordChallenge class based on your UML diagram. The specification should include a detailed algorithm for counting the matches, and the matches in the correct positions. This will become a comment at the beginning of your source file.
  5. Create an Eclipse project, and implement and test the specification you developed in the previous step.

Implementation

Test your program after each of the following steps!
  1. Add the initial comment block (purpose of your program, your name, lab section, etc) to the starting code.
  2. Add the declaration that WordChallenge implements the ActionListener interface, and add a stub for the method required by the interface.
  3. Add stubs for any addtional methods of your design.
  4. Add code to the constructor method to register as an event listener with each the guess TextField and with the reset button.
  5. Add the code to handle guesses. At first, only report the score and the number of matches. When that is working, add code to report the number of matches in the correct positions.
  6. Add code to handle the reset button.

Optional

You may have noticed some additional features of the sample solution on this page. Once a guess has been entered, the word cannot be changed until the reset button is clicked. When the word is entered, '*'s are displayed, but when it is guessed correctly, it is revealed in the secret word text field. You are not required to implement these features, but you are free to do so.

What to turn in

When your assignment is complete, add some comments to the beginning of the java source code explaining any problems you encountered in completing the assignment and describing any bugs in your solution. Undocumented bugs are worse than documented bugs. If you have undocumented bugs we will assume that you did an inadequate job of testing your code, and you will lose additional points.

To submit your homework, follow the link from the homework page to the homework submission form.

There will be a 10% penalty for assignments received after the due date. Assignments will not be accepted more than one week past the due date.

You're done. Great work!