CSCI 136 Assignment 7


Your next program is to implement the game of animals (which you can think of as a specialized game of "Twenty Questions"). The computer will ask you questions until it is ready to guess your answer. If it is correct, it should gloat, otherwise it will ask you for information to help it learn your answer. In particular, it will ask you to tell it the animal you were thinking of, a question that will help it distinguish between the new animal and the one it guessed, and what the correct answer is for the new animal.

To make the game more interesting to play, your program should be able to save a game to a file and also to read in an old game from a file. This will allow the user to continue a game that was played earlier. Unfortunately, Java applets cannot read or write files. Thus the demo below on this web page does not support the file operations.



Luckily there is no problem in using files in an application!

You will need to create a class to hold the information that will be stored in the animal tree. These objects need to be able to tell whether they represent an animal or a question and should be able to return its contents.

This program uses a CardLayout manager. It can be imagined as holding a list of panels, only one of which shows at a time. They are added with the command add(name,myPanel), where name is a string which can be used to bring up the panel. If the name of the layout manager is cardManager, then the panel with name myName can be brought to the top (and shown) with the command cardManager.show(this,myName). The manager remembers the order the panels were inserted, so commands like cardManager.first(this), cardManager.last(this), and cardManager.next(this) also can be used to select among the panels.

One minor complication is that you will likely be calling these methods from inside "listener" inner classes of the frame. If you write just cardManager.first(this) it will refer to the this of the inner class. To get it to call the correct "this", write cardManager.first(AnimalApp.this).

The rest of the design is up to you. The design for this program will require careful thought. There are many components here: the GUI, the class to describe nodes in the game tree, the listeners, the code to handle the actions of the game, and the code to handle the file operations. You should have no trouble writing the code for the individual pieces. However, it will take some time to develop an elegant overall design. We will discuss writing to and reading from files in class. If you would like to get started on your design before that discussion, here is some code that you will need. While this is more than adequate in helping you to formulate your design, it is not the entire story on file I/O. You will need to hear one or two more points in class before doing a successful implementation.

  // create a new input file with name fileName.
DataInputStream inFile = new DataInputStream(new FileInputStream(fileName));
char letter = inFile.readChar();   // read char from inFile and store in letter
string contents = inFile.readUTF(); // read string from inFile and store in contents
inFile.close();                    // close file

  // create a new output file with name fileName.
DataOutputStream outFile = new DataOutputStream(new FileOutputStream(fileName));
outFile.writeChar(letter);          // write character from letter onto outFile
outFile.writeUTF(contents);         // write string from contents onto outFile
outFile.close();                    // close file
When you write to the file you will need to include a one character tag for each string to indicate whether it is a question or answer. I suggest writing 'Q' before each question and 'A' before each answer. It is easiest to read the file back if you write the file with a pre-order traversal of the tree. (Post-order is a bit more difficult, but not bad.) While you could write the file using the iterators of the BinaryTree class, it is just as easy to write your own recursive version. To read the file in and recreate the binary tree you will need to write a recursive routine. The trick is to "believe" in recursion; if you do, it will be pretty easy. One helpful hint: at the end of writing a subtree, be sure to bring the cursor back to the root of the subtree (which is generally not the root of the entire tree).

Goals

The major goal of this lab is to give you practice in creating and traversing trees. A second goal is to give you an opportunity to work with files. A last (but very important) goal is to give you the opportunity to do a design of a relatively large program.

What to hand in

Design

For this lab you should begin by writing up a very thorough design. Your design will count for 10% of the final grade, so be sure to do a good job.

As mentioned in class, I will be collecting designs at the beginning of the lab session and commenting upon them during lab. I will return graded designs by the end of the lab session. This is a significant departure from the design review approach we've been following this semester.

I've been asked, given the scope of this assignment, whether it would be possible to divide the design over two weeks. In general, it is important to consider all facets of a program when coming up with a design. The choice of data structures, for example, needs to be made by considering all the functionality required. Fortunately, however, this lab can be divided nicely into two design phases.

The core design - In this phase you should design the GUI, the class to describe nodes in the game tree, the listeners, and the code to handle the actions of the game.

File handling - In the second phase, you should design the code to handle the file operations.

You can turn in the entire design during the first lab session, if you'd like, or you can turn in the first phase followed by the second phase in the following week.

The Program

When you have completed your program, place all of your code in a folder and drop it off in the CSCI 136 drop-off folder. Your project should include all necessary classes to run.

The lab is due on Sunday, April 23 at 11:59 pm for those in Wednesday labs; it is due on Monday, April 24 at 11:59 pm for those in the Thursday lab. Note that this is a two-week lab assignment. It will therefore be worth twice the usual lab amount.