CSCI 136: Assignment 7 -- The animals game

Due 4/28/97


Your next program is to implement the game of animals. 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 either read or write files. Thus the demo below on this web page does not support the file operations.



Luckily it turns out to be pretty trivial to turn an applet into an application, and even run the same program as either an application or an applet. All you have to do is add a "static" main method to the applet and add a new class which sets up a frame for the applet to run in.

Put the following method in your applet class (I've specialized it here for the animals game).

  /*
    post:  If program is being run as an application, creates a frame for it to run in.
            Ignored if run as applet.
  */
  public static void main(String[] args)
  {
      // Set up and show frame
    AnimalAppletFrame app = new AnimalAppletFrame("Animals Game");
    app.resize(400,160);
    app.show();
  }
and add a new class at the end of the file containing your applet class:
/*
  This class supplied a frame (window) to be used when the program is run as an application.
*/
class AnimalAppletFrame extends Frame
{
  /*
    Create frame holding the animals applet.
  */
  public AnimalAppletFrame(String frameTitle)
  {
    super(frameTitle);              // Put title on window
    
    // create the animals applet and stick it in the middle of the frame (filling it!)
    AnimalApplet applet = new AnimalApplet();
    applet.setApplication();        // Tell the applet it is being run as an application
    add("Center",applet);
    applet.init();                  // Initialize the applet
    applet.start();                 // Start the applet running
  }

}
Now you will be able run your applet as either an applet or application, depending on what kind of a project you are running (I've set up both for you - Animal.mu for the applet and AnimalApp.mu for the application). The application will handle files, while the applet will not. You will need to create a class to hold the information which will be stored in the animal tree. These object need to be able to tell whether they represent an animal or 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.

I have intentionally provided a bit less code for you with this program than usual. I've provided the code to set up the user interface and the action routine, but the rest is up to you. You should not have much problem in writing the code at this point, but the logic will need very careful thought! To make life easier, you may work in pairs on this project. If you decide to work in a pair with someone in class, you must let me know at the beginning of lab on Wednesday who your partner is. Once you complete the overall design I suggest that you have one person work on the main game, with the other person specializing in the interaction with the files. Here is the code you will need for creating, writing to, and reading from files:

    // create a new output file with name fileName.
  DataOutputStream outFile = new DataOutputStream(new FileOutputStream(fileName));
  char letter = inFile.readChar();    // read character from inFile and store in letter
  String contents = inFile.readUTF(); // read string from inFile and store in contents
  outfile.close();                    // close file

    // create a new input file with name fileName.
  DataInputStream inFile = new DataInputStream(new FileInputStream(fileName));
  outFile.writeChar(letter);          // write character from letter onto outFile
  outFile.writeUTF(contents);         // write string from contents onto outFile
  inFile.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 either with a pre-order or post-order traversal of the tree. While you could write it 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, as 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).

If you would prefer to work by yourself, rather than with a partner, you need not write the part of the program with the file manipulations (though your program will be less interesting to use!)

Be sure to get an early start on this and to do an extremely careful design, as otherwise you will not have much luck on this program!