Programming Assignment #4, CPS 100E, Fall 1996:
Guessing Games/Animal House (24 points)

Due Date: Early bonus: Tuesday, November 19 midnight (5 points)
Final Due Date: Monday, November 25, midnight

This assignment will provide practice with trees, files, reading from directories, and (optionally) inheritance.

Table of Contents

[ Introduction | Input/Output | Coding | Grading | Submitting | Extra Credit ]

(A Makefile and sample input files are accessible in ~ola/cps100e/animal on the acpub system. Be sure to create a subdirectory animal for this problem and to set the permissions for access by prof/uta/ta by typing fs setacl animal ola:cps100e read.)

Files accessible are:

(note: missing functions for the class GuessGame will prevent the code provided from compiling.)

Introduction

The game of Twenty Questions can be 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. For example, a brief run of a simple version of the program is shown below. Note that new questions are added during the run reflecting new "knowledge" in the program.

prompt> guess
file: animal.dat
Does it have feathers [yes/no] yes
Does it live in a barnyard [yes/no] yes
Is it a chicken [yes/no] yes
I win!!!

play again? [y/n] y
Does it have feathers [yes/no] no
Is it a mammal [yes/no] yes
does it have stripes [yes/no] no
Is it a elephant [yes/no] no
I give up: what were you thinking of? kangaroo
please type a question
that has a yes answer for a kangaroo
and a no answer for a elephant: does it hop

play again? [y/n] y
Does it have feathers [yes/no] no
Is it a mammal [yes/no] yes
does it have stripes [yes/no] no
does it hop [yes/no] yes
Is it a kangaroo [yes/no] yes
I win!!!

play again? [y/n] n

back to table of contents

Input/Output

The input to the program will be any file in the format below.

  Question
  Yes Answer (subtree)
  No  Answer (subtree)

In particular, questions are identified by the string #Q: at the beginning, so that the information used in the sample run above is given below.

   #Q:Does it have feathers
   #Q:Does it live in a barnyard
   chicken
   #Q:is it wise
   owl
   #Q:does it say "Nevermore!"
   crow
   #Q:does it gobble
   turkey
   eagle
   #Q:Is it a mammal
   #Q:does it have stripes
   tiger
   #Q:does it hop
   kangaroo
   elephant
   gila monster
The code below will read an input file in this format and build a tree (passed back via reference parameter root).

void GuessGame::BuildTree(istream & input,TreeNode * & root) // precondition: input open for reading, properly formed // postcondition: root points to a tree formed from contents of input { string s; if (getline(input,s)) // input succeeded? { if (s.substr(0,3) == "#Q:") // add a question { root = new TreeNode(s.substr(3,s.length())); BuildTree(input,root->left); BuildTree(input,root->right); } else { root = new TreeNode(s); // add an answer } } else { cerr << "error: no input" << endl; root = 0; } }

back to table of contents

The Program

You should write a program that prompts for an input file (but see extra credit for an option to display all input files). The program should build a "guessing" tree and use this to allow the user to play a game. As shown above, if the program doesn't guess correctly, the user should be able to add a new animal (or new person, or new food, or new whatever) and a new question that will then be added to the tree.

The user should be allowed to play several games, or to read in a new file and play with new data. When a new file is read in, or when the user quits, the current tree should be written to a file in a format that it can be re-read later. The user should be prompted for the name of the output file; ideally the file from which the information was read will serve as a default filename.

Extra Credit Input

( this is worth 5 points)

For extra credit you should use the DirEntry and DirStream classes described in the Tapestry book and used in the function LoadDirectory from Lab 4 (in the file usepix.cc) to show the user all files that end with .dat. The first line of a .dat file can begin with #C: to signify a comment describing the contents of the file. For example:

  #C:Animals -- mammals, birds, reptiles, etc.
  
  #C:Duke Courses --- hard courses, easy courses, etc.

The user should be shown the names of each .dat file and a description of the contents of the file whenever a new file name for reading a tree is entered. back to table of contents


Coding Requirements

You should write a class GuessGame, part of which is provided to you (see the files guess.h) and guess.cc. You'll need to write several member functions, both private and public. You may decide it's worthwhile to design and implement other classes. If you need help with a Makefile for this, just ask. You'll need to extend the relatively simple playgame.cc that's provided for you.

back to table of contents


Grading Standards

This assignment is worth 24 points. Points will be awarded as follows:
Behavior Points
Plays Game 4
Writes File/Reads File 4
Adds new information to tree 4
User-interface (good prompts, robust, easy to read/play) 4
Coding Style (class, comments, etc.) 4
README 2
Sample Data Files 2
back to table of contents

Submission

You should create a README file for this and all assignments. All README files should include your name as well as the name(s) of anyone with whom you collaborated on the assignment and the amount of time you spent.

To submit your assignment, type:

submit100e animal README animal.cc animal.h playgame.cc Makefile ... Be sure to submit all source files (all .h and .cc files). You must also submit at least one data file you've created that can be used with the program. Extra credit will be awarded for droll and amusing data files, please keep the data files you submit "clean".

Extra Credit

For extra credit, use an abstract base class for nodes of the tree, with concrete classes for leaf nodes and internal nodes. Your code should then take advantage of these intelligent nodes "knowing" how to ask questions about themselves. We'll discuss inheritance in class on Thursday November 14.

To submit the extra credit assignment, type:

submit100e animal.xtra README ..........