Computer Science 111a - Fall Semester,
2003
Homework #10
due: by 5:00 on Dec. 10
Your task in this week's project is to make some changes to the animal
guessing game program, to make it more exciting. When the computer guesses an
animal incorrectly, your program should ask the user what animal she was
thinking of. Your program will then ask the user for a question to distinguish
between her animal and the one you guessed, and it will use this information to
modify its decision tree! The information will then be saved to file, so that
the next time someone plays the game the computer will be even
"smarter". Here are details:
- If the computer guesses an
animal that is not the one the user was thinking of, it should get
information from the user and add it to the tree:
- Ask the user what
animal she was thinking of -- and read in the animal name, which should
be one word (e.g., dog).
- Ask the user for a
question that has the answer yes for her animal and no for
the animal that the computer just guessed -- and read in the question.
When you ask for this, you should print out both animal names, e.g., What
is a question that has the answer yes for an aardvark and no for a skunk.
Note that you should also use the correct article: a or an,
depending on whether the name begins with a consonant or a vowel. How can you print the name of the
animal that the computer guessed?
Well, it is in the question string, and if you search for the
first space from the end of the string (with strrchr), the name
begins at the next position.
- Add this new
information to the tree by:
- Moving the record
from the index i that you're at to index 2i + 1;
- Changing the record
at index i to contain the user's question (be sure to change the
questiontype field); and
- Adding a record at
index 2i with a guess that contains the user's animal name: Is it an
aardvark?You can use strcpy and strcat
to form this question from the strings "Is it
a(n)", the name of the animal, and "?". Be sure to initialize the
questiontype field as well.
- Before ending the program,
copy all of the information in the animal_tree array back out to
the file animal.dat. To do this,
- Open the file for
writing;
- Loop through the array
and, at each index that has an i or g, write the index
number, the letter, and the question into the file. Be sure to format the
file exactly as it was previously, so that the next time you scanf from
the file it will work correctly!
- Close the file.
- For this to work
correctly, you will need to initialize the questiontype field in
every cell of the array to some value other than i or g,
prior to reading the date from animal.dat into the array in the initialize
function.
What to submit. Your program should be in a file called hw10.c.
Submit this homework by the due date with the command submit hw10 hw10.c