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.
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
| Answering the first question in a game where the user is thinking of a chicken. |
| So it has feathers and lives in a barnyard, what's next? |
| Wow, the computer guessed my animal! |
| 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.
| Answering the second question in a game where the user is thinking of a lion (and already said "no" to having feathers). |
| So it is a mammal, but doesn't have stripes |
| No stripes, and now it doesn't hop |
| Nope, not an elephant. |
| I'm thinking of a lion |
| 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.
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.
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:
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.
askQuestion() --- which shows the user the current
question.
newAnimal() --- which is part of the three-phase
prompt-for-new information aspect of the program described
above.
write() --- which is started and will help save the tree
to a file (you'll need to complete this). To write the file you'll need
to read the API for java.io.FileWriter, in particular
you'll
only need to use the the write method that accepts
a String parameter -- be sure to terminate each string with
a newline character, e.g., something like
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.
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
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.
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