Write a program that plays with the user the game

Assignment Help Computer Engineering
Reference no: EM131936577

Binary Tree Assignment

Write a program that plays with the user the game commonly known as "Twenty Questions". However, in this version of the game, the program will not be limited to asking twenty questions. Instead, it may ask any number of questions. Also the program will learn and improve from playing each game.

At the start of each game, the program will ask the user to think of an object for the program to guess. Then, using its database, it will ask the user a number of yes/no questions one at a time. At the end of the question/answer session, it will display its guess for the user object. It will ask the user if its guess is correct. If the guess is correct, it will display a self praising sentence such as "I must say I am very smart". If the guess was wrong, for the purpose of improving its performance in the future, it will ask the user for the correct object. It will also ask the user to provide it with a question whose yes answer will be the user correct object and no answer will be the program's wrong guess. It will then update its database with the information received from the user for future use.

Then it will ask the user if he/she will like to play another game. If the user response affirmatively, it will repeat the procedure described above
in the last paragraph. Otherwise, the program will end.

Database

The program will use a flat file database for keeping questions and answers. It will use these questions and answers in interacting with the user. The flat database will be a text file named knowledge.txt. Initially the file will contain the three lines below: (Manually prepare this file using a text editor and save it in a folder accessible from the program).

Is it a non-living object?

Elephant

Stone

In the file, each line will contain either a question or an answer. Each question will be differentiable from the answer in that it will always end with a question mark "?". An answer will never end with a question mark. The above file contains one question component and two answer components.

The question and answer components are stored in the file in such a way that on reading the file, an in memory binary tree can be constructed with each node containing either the question or the answer component. An answer component will always be stored in a leaf node of the tree and a question component will be stored in a non-leaf node of the tree. For example, when the program reads the above file, it will construct the following binary tree:

Is it a non-living object?
| |
| |
Elephant Stone
Algorithm

• At the start of the program, the program will read the contents of the file and construct a binary tree containing the question and answer components of the file. It will use this tree to interact with the user.

• Once the user has thought of an object, the program will display to the user the contents of the root node as the first question and solicit user's yes/no answer.

• If the user's answer is "no", it will traverse the tree using the left link. Otherwise, it will traverse the tree using the right link.

• If the new node contains a question, it will present this question to the user and solicits a yes/no answer.

• Again if the answer is "no", it will traverse the tree using the left link. Otherwise, it will traverse the tree using right link.

• It will repeat this process, till it arrives at a leaf node.

• It will read the contents of the leaf node and present it to the user as its final guess. (A leaf node will always contain a guess/answer).

• If its guess is correct, it will print a self praising message such as "I must say I am very smart".

• If its guess is wrong, it will first ask the user to provide the correct answer and then it will ask the user to provide a question such that the "yes" answer to the question is user's correct object and "no" answer is program's wrong guess.

• It will update the tree to include the above information received from the user as below:

From the current leaf node that provided the program's guess, it will create two child nodes. It will copy the contents of the current node into its left child node; it will copy user's correct answer into its right child node; and it will copy the user supplied question in the current node.

• Then it will ask the user if the user would like to play another game.
• If the user responds affirmatively, it will repeat the above process.
• Otherwise, it will save the updated binary tree in the database file for future use and exit.

Storing/restoring Tree

//For using the method below, make sure that the last
//character in each question is a question mark i.e. ?
//For the methods below to work,
// the last character of every question must be a question mark.
//The methods below for storing a tree to a file is a modified pre-order traversal.

struct NodeType;
typedef NodeType * NodePtr;
struct NodeType
{
string item;
NodePtr llink;
NodePtr rlink;
};
void store()
{
//open a file object for output
ofstream outFile ("tree.txt", ios::out);
//assume root is the root of the tree;
rstore(root, outFile);
outFile.close ( );
}
void rstore(NodePtr nodep, ofstream & outFile)
{
//item below is a string being written to the file.
//For a question, the string ends with a question mark (?).
//For an answer,
//it ends with a non question mark, say with a period (.).
//use pre-order traversal to save the tree.
if (nodep!=NULL)
{
OutFileitem
rstore(nodep->llink,outFile);
rstore(nodep->rlink,outFile);
}
}
//The methods below can be used for restoring tree
//from a file prepared by the above methods. by using pre-order traversal.
void restore( )
{
//open a file object for input
ifstream inFile ("tree.txt", ios::in);
//assume root is the root of the tree. It may contain NULL at this time.
rrestore(root, inFile);
inFile.close ( );
}
rrestore(NodePtr &nodep, ofstream & inFile)
{
//use pre-order traversal for restoring the tree.
//After reading a line, and creating a node for it, ask if this is a question?
//If it is a question, make a recursive call to create and fill the next node.
//If it is not a question (i.e. it is an answer), do not make the recursive call
//because there is no node below it.
//read a line from the file.
char buf[120];
inFile.getline(buf,120);
//create a node.
//link this node to the node/root above it.
// The root or left/right link of the node above is passed to this method.
nodep = new NodeType;
//fill the node.
nodep->item = buf;
nodep->llink = NULL;
nodep->rlink = NULL:
//if this is a question, make the recursive call
//because there is still a node below it.
if (nodep->item[nodep->item.length( )-1]=='?')
{
rrestore(nodep->llink, inFile);
rrestore(nodep->rlink, inFile);
}
}

Testing

• Make your own data for testing.
• Keep testing till tree becomes at least 7 levels deep.

Submit

• Submit console input/output dialog for at least one correct and one incorrect answer.

• After the tree is at least 7 levels deep, submit a copy of the output file.

Reference no: EM131936577

Questions Cloud

General partners and davidson the limited : Bellamy, Carlisle, and Davidson formed a limited partnership. Bellamy and Carlisle were the general partners and Davidson the limited partner.
Calculate the increase or decrease in bond value : Assume that coupon interest payments are made semiannually and that par value is $1,000 for both bonds. Calculate the values of Bond A and Bond B.
How much money will his daughter have : How much must you invest at 10% interest in order to see your investment grow to $30,000 in 12 years?
Shielded from liability by the firm : Backus sued the firm and Holden personally, but the latter claimed he was shielded from liability by the firm. Is Holden correct?
Write a program that plays with the user the game : Write a program that plays with the user the game commonly known as Twenty Questions. Also the program will learn and improve from playing each game.
Create ever smaller arrays of items : Create ever smaller arrays of items and then merge them to create a final, fully sorted array. In this challenge, you will have two sorted arrays
Sought judgment against them personally : Jacobsen and Kelly agreed to form an LLC. They filled out the appropriate paperwork and mailed it with their check to the secretary of state's office.
What impact would this change have on the assets return : If the market increased by 12% and the return increased by exactly the same amount, what would the new beta for this asset be?
Percent annual interest rate : If you are 30 today, how much must you save each year to meet your retirement goal if you plan to retire at 65?

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd