forked from leagerl1/Akinator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAkinator.cpp
More file actions
57 lines (47 loc) · 1.25 KB
/
Akinator.cpp
File metadata and controls
57 lines (47 loc) · 1.25 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
#include "Akinator.h"
using namespace std;
Akinator:: Akinator(){
root = nullptr;
}
void Akinator:: traverse(){
traverse_helper(root);
}
void Akinator:: traverse_helper(Node* node){
if(ask(node)){
node = node->yes;
traverse_helper(node);
}
else{
node = node->no;
traverse_helper(node);
}
}
bool Akinator:: ask(Node* node){
if(node->yes == nullptr){
cout << "Your person is " << node->question << endl;
exit(0);
}
else{
cout << node->question << " (Answer Yes or No): " << endl;
string ans;
if(cin >> ans)
return (strcasecmp(ans.c_str(),"yes") == 0);
else{
cout << "Invalid input. Try again." << endl;
ask(node);
}
}
}
void Akinator:: insert(bool answer, std::string question, string y, string n){
Node* yesNode = nodeMap[y];
Node* noNode = nodeMap[n];
Node* newNode = new Node(answer,question,yesNode,noNode);
nodeMap.emplace(question, newNode);
}
void Akinator:: insert_answer(bool answer, std::string question, Node* y, Node* n){
Node* newNode = new Node(answer,question,y,n);
nodeMap.emplace(question, newNode);
}
void Akinator::set_root(string start){
root = nodeMap[start];
}