1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include<fstream>
using std::fstream;
using std::ios;
class animal
{
public:
char text[128]; //a yes/no question OR an animal name
animal* yes; //pointer to next node if question is answered YES
animal* no; //pointer to next node if question is answered NO
}; //if "yes" and "no" are 0 then "text" is an animal name
// Prototypes
void deallocateTree(animal*);
int main()
{
//initialize a "top" pointer named "animal* top
animal* top;
top = new animal; //set "top" to a newly created node
strcpy(top->text, "Elephant"); //set its text to "elephant"
//pointers to 0
top->yes = 0;
top->no = 0;
//start a "while" loop that runs each cycle of the program
while(true)
{
char play;
cout << endl;
cout << "Would you like to play the guessing game? [Y/N]: "; //invide the user to think of an animal which it will try to guess
cin >> play;
cin.ignore(1000, 10);
if (play == 'N' || play == 'n')
break; //await the user's response, and break out of the loop if he declines
else if (play == 'Y' || play == 'y')
{
animal* p = top; //declare a pointer "p" to traverse the tree, and initialize it to "top"
while(p) //start a loop to traverse the binary tree
{
if(!p->yes)
{
char guess; //ask use if guessed animal is correct. Y/N?
cout << "Is it a " << p->text << "? [Y/N]: "; //print p->text as the guessed animal
cin >> guess;
cin.ignore(1000, 10);
if (guess == 'Y' || guess == 'y') //if guess Y brag and break
{
cout << "I win!" << endl;
break;
}
else if (guess == 'N' || guess == 'n')
{
char yesNo; //yes or no question
char a[128]; //store answer in char a[128] (animal thinking of)
char q[128]; //question in char q[128] (question differentiates)
cout << "No? Which animal were you thinking of?: "; //ask user what animal he was thinking of ANSWER
cin.getline(a, 128);
cout << "What question should I ask to tell the difference?: "; //ask what yes/no question differentiates "p->text" from "a"
cin.getline(q, 128);
cout << "Which answer is correct for that animal? [Y/N]: "; //ask which response is correct for "a" -- yes or no
cin >> yesNo;
cin.ignore(1000, 10);
//create two new nodes, names "y" and "n"
animal* y = new animal;
animal* n = new animal;
//if the correct response for "a" is yes [a[128] (animal thinking of)]
if (yesNo == 'Y' || yesNo == 'y')
{
strcpy(y->text, a); //copy "a" into y->text
strcpy(n->text, p->text); //copy p->text into n->text
}
//else if the correct response is no
else if(yesNo == 'N'|| yesNo == 'n')
{
strcpy(n->text, a); //copy "a" into n-text
strcpy(y->text, p->text); //copy p->text into y->text
}
strcpy(p->text, q); //copy "q" into p->text
//set y->yes, n->yes, y->no, n->no to 0
y->yes = 0;
n->yes = 0;
y->no = 0;
n->no = 0;
p->yes = y; //set p->yes to y
p->no = n; //set p->no to n
break; //break from loop
}
}
else if(p->yes)
{
char yesNo;
cout << p->text << "? [Y/N]: "; //print p->text as a question ask for a yes/no reply
cin >> yesNo; //store in char yesNo
cin.ignore(1000, 10);
if(yesNo == 'Y' || yesNo == 'y')
{
p = p->yes; //if yes set p to p->yes
}
else if (yesNo =='N'|| yesNo=='n')
{
p = p->no; //else if set p to p->no
}
//reclaim memory
else if (yesNo == 'D' || yesNo == 'd')
{
deallocateTree(p->yes);
deallocateTree(p->no);
cout << "Provide an answer to replace the question: ";
cin.getline((char*)p->text, 128);
p->yes = 0;
p->no = 0;
}
else if (yesNo == 'E' || yesNo == 'e')
{
cout << "Enter the new question: ";
cin.getline((char*)p->text, 128);
}
else
{
cout << "Incorrect entry! Try again." << endl;
continue;
}
}
}
}
}
}
void deallocateTree(animal* x)
{
if(x)
{
deallocateTree(x->yes);
deallocateTree(x->no);
delete x;
}
} // end deallocateTree
|