CompSci 100E, Fall 2006, Animal/Twenty-questions

Introduction

The game of Twenty Questions is sometimes amusing, especially if the domain of questions is one you find interesting. For this assignment you'll write a program that permits the user to play a dynamic version of twenty questions --- dynamic in the sense that the user can add new questions, save the game, then replay the game with the new questions.

At first your program will read textfiles, but the GUI will be modified to read properly-formatted files over the web.

Snarf the code via Eclipse/Ambient using Animal.

Playing the Game

Initially the program is run from the main method in the GameMain.java class. Until you complete the model, however, the game won't be complete. When it is, the screenshots below illustrate how the game will be played. Before you can play, you'll need to load a file representing a game. Use the File menu for this and navigate to something like animal.txt or books.txt for playing two different kinds of game (this isn't really a game).

Here's the progression of a series of questions
start Answering the first question in a game where the user is thinking of a chicken.
start So it has feathers and lives in a barnyard, what's next?
start Wow, the computer guessed my animal!
start The computer wins! (by guessing your animal)


Here's a different sequence of user-responses, where the user is thinking of a lion. These responses show how the user is prompted for a new animal and a question to differentiate the new animal from the incorrect one guessed/chosen by the computer.

start Answering the second question in a game where the user is thinking of a lion (and already said "no" to having feathers).
start So it is a mammal, but doesn't have stripes
start No stripes, and now it doesn't hop
start Nope, not an elephant.
start I'm thinking of a lion
start And here's a differentiating question for elephant/lion

At this point if we play a new game the question "Does it have tusks" will be incorporated into the tree. The user can play more, and ultimately decide to save the game/tree to a file for playing again. Saving a file is a menu-option which the view relays to the model's method write which takes a FileWriter parameter and writes a tree to this file so that the game can be played again by reading from the file. The user chooses to save a game from the File menu as shown below.

filedialog


The Program You Write

You must write a program that prompts the user for the name of a file, reads that file into a tree, plays a game, and allows the user to add new information to the tree as the game is played. The user should be able to play more than once during a run of the program. The user should be able to save the tree to a file specified by the user.

Most of the control aspects of the program are already incorporated into the class AnimalGameViewer that is similar to previous IView implementations we've seen in other programs. You'll need to implement the AnimalGameModel class so that it works together with the view similarly to the other MVC programs we've done this semester.

As shown in the screen shots above, the user is prompted for a filename in which the tree/game will be saved. The default dialog box for choosing a filename specifies the name of the file the user originally entered, but in the run above a different name is used. It's a good idea to use a different name for the file until you know your program works.

The Model

Your model class will need to create a tree for playing a game. You should use a static inner class for the Tree/Node, similarly to what's done in the TreeReader class.

Recall that in the MVC architecture we're using communication from the View comes to the model via the process method. In this program the parameter to process is a string representing the user's response to a question, e.g., 'yes' or 'no'.

The model class communicates back-and-forth with the view to update the state of the game, e.g., to move down the tree and then restart from the root of the tree for a new game. The model should maintain as an object invariant a reference/pointer to the current node in the tree whose question the user answers using the view. For example, this current node is initially the root. Depending on the user's response to the first question (and to subsequent questions) the current node is updated to be the left- or right-child according to the user anwering 'yes' or 'no'.

In the model you start with, the instance field myCurrent is not a tree node, it's a String. This won't work, you'll need to make it a tree node, the type String simply allows the program to compile, but it won't work in your final program, you need a tree node.

In the process of playing a game, myCurrent will eventually reach a leaf node. Then you'll need to maintain more state in the model than simply the current node so that you can determine which part of adding/editing new information the user is responding to. This part of a game requires at least three phases:

You'll need to keep state in the model, and update this state, to determine which of these phases the user responds to. You'll need to store state in the model since the view will simply report something like "Is it a lion" to the model's process method. That method will need to differentiate this response from "no", from "Does it have tusks" in the final phase of adding a new node to the tree.

The AnimalGameModel class has some private helper methods you may want to use or modify in this final part of a game. These methods include the following.

Model>View Communication

You may recall that the model communicates with an IView object by displaying a message using messageViews, displaying an error using showViewsError (in the animal game the error is tagged as informative rather than an error in the dialog that pops up), and displaying any other information using notifyViews. This last method takes any Collection as a parameter, typically this is an ArrayList of strings constituting a message to display to the user. The AnimalGameModel class you start with has some of the state needed for this so that the ArrayList can be re-used in the communication process.

Input/Output

The input to the program will be any file in the schema below which is the pre-order traversal of a tree representing the guessing game. Note that in every such tree all non-leaf/internal nodes have two children.

  Question
  Yes Answer (left subtree)
  No  Answer (right subtree)

Questions are identified by the three-character string #Q: at the beginning of a line, lines that do not begin with the string #Q: represent answers/animals.

For the run above, the sample file animal.txt is reproduced below.



    #Q:Does it have feathers
    #Q:Does it live in a barnyard
    Is it a chicken
    #Q:is it wise
    Is it an owl
    #Q:does it gobble
    Is it a turkey
    #Q:does it say "Nevermore!"
    Is it a raven
    Is it an eagle
    #Q:Is it a mammal
    #Q:does it have stripes
    Is it a tiger
    #Q:does it hop
    Is it a kangaroo
    Is it an elephant
    Is it a gila monster

The tree that corresponds to this file is shown below. You'll need to construct a tree like this in the program you write. Note that yes answers in the tree are shown as left/blue arrows, no answers are right/red arrows. The leaf nodes are shown only with the animal information rather than the entire question, e.g., Is it an elephant

Reading a pre-order tree

The code in TreeReader.java shows how to read a tree from the tree's pre-order traversal. The program writes a tree to standard out (System.out). For example, here's a run on the data file treedata.txt.

   in order traversal
   
   dingo
   fox
   giraffe
   platypus
   racoon
   rhinoceros
   tasmanian devil
   tiger
   warthog
      
   pre order traversal
   
   platypus
   fox
   dingo
   giraffe
   tiger
   rhinoceros
   raccoon
   tasmanian devil
   warthog

Note that the format of the data file used above is similar (though not identical) to what you'll be writing for the twenty questions program.


What to Submit

This assignment is worth 25 points, the breakdown is as follows:

functionality points
read file into tree 4
play game/add info to tree 7
save file properly 4
coding style/classes 4
data file created 3
README 3

You must also submit a README file in which you list all the people with whom you collaborated, and the TAs/UTAs you consulted with. You should include an estimated of how long you spent on the program and what your thoughts are about the assignment.

You must create a sample data file whose name indicates something about its contents. The questions should be yes/no questions about any topic, e.g., animals, elements from the periodic table, courses at Duke, and so on.

Submit via eclipse using animal