-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestiontree.cpp
More file actions
299 lines (256 loc) · 7.31 KB
/
Copy pathquestiontree.cpp
File metadata and controls
299 lines (256 loc) · 7.31 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "questiontree.h"
#include <sstream>
#include <fstream>
#include <climits>
#include <cstring>
#include <string>
#include "strlib.h"
#include <iostream>
#include "questionnode.h"
#include "ui.h"
/*
* Hi, I am devansh sharma!
* This project has the entire 20 questions game
*/
//Constructor
QuestionTree::QuestionTree()
{
root = new QuestionNode ("A:computer");
lost = 0;
won = 0;
}
//destrcutor
QuestionTree::~QuestionTree()
{
//calling a helper function that frees up memory for the tree
destructorHelper(root);
lost = 0;
won = 0;
}
//a recursive helper function that deletes everything in the tree
void QuestionTree::destructorHelper (QuestionNode* curr)
{
if(curr != nullptr)
{
destructorHelper (curr->yes);
destructorHelper (curr->no);
curr = nullptr;
delete curr;
}
}
//counting the number of answers in a tree
int QuestionTree::countAnswers() const
{
QuestionNode* curr = root;
int number = 0;
//calling a helper function that counts the answers in a parameter recursively
countAnswersHelper(curr, number);
return number;
}
void QuestionTree::countAnswersHelper(QuestionNode* node, int& number) const
{
if (node != nullptr)
{
string line = node->data;
//if the node begins with A, then it is an answer
if (line[0] == 'A')
{
number++;
}
countAnswersHelper(node->yes, number);
countAnswersHelper(node->no, number);
}
}
//counting the number of answers in a tree
int QuestionTree::countQuestions() const
{
QuestionNode* curr = root;
int number = 0;
//using a recursive helper function to count the questions of a tree in the parameter
countQuestionsHelper(curr, number);
return number;
}
//helper function for counting questions
void QuestionTree::countQuestionsHelper(QuestionNode* node, int& number) const
{
if (node != nullptr)
{
//if the node has children, then it is a question
if (node->yes != nullptr && node->no != nullptr) number++;
countQuestionsHelper(node->yes, number);
countQuestionsHelper(node->no, number);
}
}
//returning the number of games lost
int QuestionTree::gamesLost() const
{
int x = lost;
return x;
}
//returning the number of games won
int QuestionTree::gamesWon() const
{
int x = won;
return x;
}
//function to merge the data of a new file into the existing tree
void QuestionTree::mergeData(UI& ui, istream& input)
{
//new tree that will have the new file's questions and answers
QuestionNode* new_tree = new QuestionNode ();
//helper function that reads the new file into using the provided input stream
readDataHelper(input, new_tree);
//helper function that merges the new tree and the existing one using a question
mergeDataHelper(ui, root, new_tree);
}
void QuestionTree::mergeDataHelper (UI& ui, QuestionNode* original, QuestionNode* new_tree)
{
string ques = ui.readLine("Type a Y/N question to distinguish my data from your file:");
bool ans = ui.readBoolean("And what is the answer for the new data?");
//new question node that will connect the two files
QuestionNode* replacement = new QuestionNode ("Q:" + ques);
replacement->yes = new QuestionNode();
replacement->no = new QuestionNode();
if (ans)
{
replacement->yes = new_tree;
replacement->no = original;
}
else
{
replacement->yes = original;
replacement->no = new_tree;
}
//making the new question node as the root
root = replacement;
}
//main thing - playing the game
bool QuestionTree::playGame(UI& ui)
{
QuestionNode* curr = root;
//helper function that does the work
return playGameHelper(ui, curr);
}
bool QuestionTree::playGameHelper(UI& ui, QuestionNode* node)
{
bool r = false;
string line = node->data;
//asking the question if the node is a question, then going to the yes or no children depending on the response
if (line[0] == 'Q')
{
bool b = ui.readBoolean(line.substr(2));
if (b)
{
r = playGameHelper (ui, node->yes);
}
else
{
r = playGameHelper (ui, node->no);
}
}
//checking the answer if the node is an answer
else
{
bool b = ui.readBoolean("Are you thinking of: " + line.substr(2) + '?');
if (b)
{
won++;
r = true;
ui.print("Hooray, I win!");
}
else
{
lost++;
r = false;
//helper function that adds the new item to a node, and updates the tree if the computer loses
node_changer(node, line, ui);
}
}
return r;
}
void QuestionTree::node_changer (QuestionNode* node, string line, UI& ui)
{
string item = ui.readLine("Drat! I lost. What was your object? ");
string ques = ui.readLine("Type a Y/N question to distinguish " + item + " from " + line.substr(2) + ":");
bool ans = ui.readBoolean("And what is the answer for " + item + " ?");
//adjusting the node as per the new object and the question
node->data = "Q:" + ques;
node->yes = new QuestionNode ();
node->no = new QuestionNode ();
if (ans)
{
node->yes->data = "A:" + item;
node->no->data = line;
}
else
{
node->no->data = "A:" + item;
node->yes->data = line;
}
}
//finding the answer range
void QuestionTree::answerRange(int& minDepth, int& maxDepth) const
{
QuestionNode* curr = root;
maxDepth = 0; //temporary
minDepth = INT_MAX; //temporary
//using a recursive helper function that uses a variable to ascertain what is the level of the
//node being explored
answerRangeHelper(minDepth, maxDepth, 0, curr);
}
void QuestionTree::answerRangeHelper(int& minDepth, int& maxDepth, int l, QuestionNode* node) const
{
if (node != nullptr)
{
l++;
//finding the answer's level and comparing it to the existing min and max values
if (node->data [0] == 'A')
{
if (l > maxDepth) maxDepth = l;
if (l < minDepth) minDepth = l;
}
answerRangeHelper(minDepth, maxDepth, l, node->yes);
answerRangeHelper(minDepth, maxDepth, l, node->no);
}
}
//reading the data of a file
void QuestionTree::readData(istream& input)
{
//clearing the exisiting tree
destructorHelper(root);
root = new QuestionNode ();
readDataHelper(input, root);
}
void QuestionTree::readDataHelper(istream& input, QuestionNode* node)
{
string line;
if (getline(input, line))
{
//reading the line into the node's data
node->data = line;
//in case the node is a question
if (line[0] == 'Q')
{
node->yes = new QuestionNode ();
node->no = new QuestionNode ();
readDataHelper(input, node->yes);
readDataHelper(input, node->no);
}
}
}
//data that writes the present tree to a file
void QuestionTree::writeData(ostream& output)
{
QuestionNode* curr = root;
writeDataHelper(output, curr);
}
//helper function that recurisvely does the job
void QuestionTree::writeDataHelper(ostream& output, QuestionNode* node)
{
if (node!=nullptr)
{
output << node->data << endl;
writeDataHelper(output, node->yes);
writeDataHelper(output, node->no);
}
}