CSCI 203 Project #3

Animal Guessing (Trees)

 

Purpose: This project will give you practice working with binary trees.

 

Description: You are to implement a program to play “Guess an Animal.”  Your program should construct a tree with a string at each node.  This tree stores the knowledge that your program currently knows.  At leaf nodes, the strings should contain the names of animals, while interior nodes should contain yes/no questions.  The program should start at the root and ask the question stored at the root.  If the user answers the question no, the program traverses the left branch of the tree, while if the user answers the question yes, the program traverses the right branch of the tree.  Your program will end up at a leaf node, and when it does, it should ask the user if the animal named there is the one he is thinking of.  If it isn’t, your program should ask the user for a question that distinguishes the leaf node animal from the animal that the user was thinking of.  This question should have a yes answer for his animal, and no for the animal currently at the leaf node.  Your program should then store the question at this leaf node, and make the animal at that leaf node the left leaf node for the new question node, and the user’s animal, the animal at the right leaf node.

 

After finishing a round, your program should ask the user if he wants to play again, and if he does, should repeat the process.  If the user says he wants to exit, your program should print out the number of animals that it guessed correctly, and the number of new animals that the program learned.  (Optional Extra Credit) It should then write out the tree to a file and exit.

 

When your program is run, (Optional Extra Credit) it should read a file that contains the tree that was saved, and use this as its original animal knowledge.  For the first run, this file will be empty.

 

Here is some sample output:

 

What is your name?

Foo Bar

Hi, Foo Bar, think of an animal and I will try to guess it.

Press the g key to go on when you have thought of one...g

Is it a penguin? (y/n) n

I give up--what is your animal?

giraffe

Please enter a yes/no question that when answered yes distinguishes a giraffe from a penguin.

Does it have a long neck?

Thank you...would you like to try another round? (y/n) y

Think of an animal and I will try to guess it.

Press the g key to go on when you have thought of one...g

Does it have a long neck? (y/n) n

Is it a penguin? (y/n) y

Hey, I got it!

Would you like to try another round? (y/n) n

Okay, thanks...I learned 1 new animal, and guessed 1 animal correctly.

 

Turnin: You should turn in a zipped file containing your source file(s), the data file you used for the test, and a text file containing a screen snapshot of the output of your program running on the data file.  You should also include a README file that describes any special or unique features of your program, as well as instructions as to how to compile and run it.

 

Hints:  Start early…you are implementing a tree class with strings as the data field nodes.  While this program should be easier than the previous ones, it will still take a lot of time to write!