-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
242 lines (229 loc) · 7.24 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <sys/stat.h>
using namespace std;
struct Node
{
string ans;
struct Node* left;
struct Node* right;
};
Node *newnode, *root , *start;
void question(Node* root, Node* prev);
void Deserialize(Node*& root, fstream& file);
void updatetree(Node* fin, Node*& prefin);
void finalquestion(Node* root, Node* prev);
void dot(int n);
void gameReady();
void gameReady()
{
system("color 8F");
cout<<"Description:\n\tThis is C++ simple based on the concept of Artificial Intelligence. The intelligence of this game \nwill be increased as we play it again more number of times.";
cout<<"\n\n\nRULES:\n\t1. Think of an any animal, bird, reptile or something...\n\t2. The AI will guess your animal, reply with \"yes\" or \"no\" only.";
cout<<"\n\t[NOTE:] You can also reply with \"Yes\", \"YES\", \"y\", \"Y\", \"No\", \"NO\", \"n\", \"N\"";
cout<<"\n\n\tKIND REQUEST TO ALL, PLEASE DON'T EVER REPLY WRONGLY AND TRY TO SEE WHAT HAPPENS WHEN COMPUTER FAILS,\nYOUR EACH ANSWER WILL TEACH THE COMPUTER, DON'T TEACH IT WRONG\n\n\n\n\t\t\t\t\t\t***THANK YOU***\n\n\n";
cout<<"\t\t\t\t\tBUILT BY St.Joseph's Cseians"<<endl<<"\t\t\t\t\t\t#Viola\n\t\t\t\t\t\t#Hemashree\n\t\t\t\t\t\t#Lakshmi\n\t\t\t\t\t\t#Yogeswaran\n\t\t\t\t\t\t#Sanjay Prashadh\n\t\t\t\t\t\t#Sathvik\n"<<endl;
system("pause");
}
void Serialize (Node*& root, fstream& file)
{
if(root == NULL)
file<<"#"<<endl;
else
{
file<<root->ans<<endl;
Serialize(root->left,file);
Serialize(root->right,file);
}
}
void Deserialize (Node*& root,fstream& file)
{
string str;
if(!file.eof())
{
getline(file,str);
cin.clear();
}
else
return;
if(str!="#")
{
root = new Node;
root->ans = str;
Deserialize(root->left,file);
Deserialize(root->right,file);
}
else
{
root = NULL;
}
}
void preorder(Node *root)
{
if(root == NULL)
return;
else
{
cout<<root->ans<<endl;
preorder(root->left);
preorder(root->right);
}
}
void updatetree(Node* fin, Node*& prefin)
{
string ans,qn;
system("color DF");
cout<<"\n\n\t\t\t\t\t****I ACCEPT MY DEFEAT****"<<endl;
cout<<"What's your animal?"<<"\t";
cin.ignore();
getline(cin,ans);
cout<<"Now, type a characteristic question for your animal which return your animal "<<ans<<" for yes, and return "<<fin->ans<<" for no"<<endl;
getline(cin,qn);
Node *ansnode, *qnnode;
ansnode = new Node;
ansnode->ans = ans;
ansnode->left = NULL;
ansnode->right = NULL;
qnnode = new Node;
qnnode->ans = qn;
qnnode->left = ansnode;
qnnode->right = fin;
if(prefin->left == fin)
{
prefin->left = qnnode;
}
else if(prefin->right == fin)
{
prefin->right = qnnode;
}
system("color 0A");
fstream file;
file.open("Data.txt",ios::out);
Serialize(start,file);
cout<<"Updated successfully"<<endl<<endl<<endl;
string reply;
cout<<"Do you want to play again?";
cin>>reply;
if(reply=="yes" || reply=="YES" || reply=="Yes" || reply=="y" || reply=="Y")
{
cout<<"Rebooting the system";
dot(2);
system("CLS");
question(start,NULL);
}
else if(reply=="no" || reply=="NO" || reply=="No" || reply=="n" || reply=="N")
{
system("color 09");
cout<<"Thanks for playing with me, i think we had a good time. See you soon. Bye bye"<<endl;
for(int i=0;i<20;i++)
{
if(i==10)
cout<<"\t\t\t\t\tBUILT BY St.Joseph's Cseians"<<endl<<"\t\t\t\t\t\t#Viola\n\t\t\t\t\t\t#Hemashree\n\t\t\t\t\t\t#Lakshmi\n\t\t\t\t\t\t#Yogeswaran\n\t\t\t\t\t\t#Sanjay Prashadh\n\t\t\t\t\t\t#Sathvik\n"<<endl;
else
cout<<endl;
}
}
system("pause");
}
void finalquestion(Node* root, Node* prev)
{
string reply;
cout<<"Is your animal '"<<root->ans<<"' ?"<<"\t";
system("color 4F");
cin>>reply;
if(reply=="yes" || reply=="YES" || reply=="Yes" || reply=="y" || reply=="Y")
{
system("color AF");
cout<<"\n\n\t\t\t\tYAAAAAAAAAAAAAAAAAAAH!!! I found out, Huraaaaay"<<endl;
cout<<"\t\t\t\t\t Thanks for playing"<<endl<<"\n\nDo you want to play again?";
cin>>reply;
if(reply=="yes" || reply=="YES" || reply=="Yes" || reply=="y" || reply=="Y")
{
cout<<"Rebooting the system";
dot(2);
system("CLS");
question(start,prev);
}
else if(reply=="no" || reply=="NO" || reply=="No" || reply=="n" || reply=="N")
{
system("color 09");
cout<<"Thanks for playing with me, i think we had a good time. See you soon. Bye bye"<<endl;
for(int i=0;i<20;i++)
{
if(i==10)
cout<<"\t\t\t\t\tBUILT BY St.Joseph's Cseians"<<endl<<"\t\t\t\t\t\t#Viola\n\t\t\t\t\t\t#Hemashree\n\t\t\t\t\t\t#Lakshmi\n\t\t\t\t\t\t#Yogeswaran\n\t\t\t\t\t\t#Sanjay Prashadh\n\t\t\t\t\t\t#Sathvik\n"<<endl;
else
cout<<endl;
}
}
}
else if(reply=="no" || reply=="NO" || reply=="No" || reply=="n" || reply=="N")
updatetree(root, prev);
else
{
cout<<"Sorry, please enter a valid answer Yes or No"<<endl;
finalquestion(root,prev);
}
}
void question(Node* root, Node* prev)
{
string reply;
system("color 0A");
if(root->left!=NULL && root->right!=NULL)
{
cout<<root->ans<<endl;
cin>>reply;
if(reply=="yes" || reply=="YES" || reply=="Yes" || reply=="y" || reply=="Y")
question(root->left,root);
else if(reply=="no" || reply=="NO" || reply=="No" || reply=="n" || reply=="N")
question(root->right,root);
else
{
cout<<"Sorry, please enter a valid answer Yes or No"<<endl;
question(root,prev);
}
}
else
{
finalquestion(root,prev);
}
}
void dot(int n)
{
for(int i=0;i<=n;i++)
{
cout<<".";
Sleep(900);
}
cout<<endl;
}
int main()
{
fstream file;
struct stat buffer;
system("color 0A");
int present = stat("Data.txt",&buffer);
if(present == 0)
{
file.open("Data.txt",ios::in);
Deserialize(start,file);
file.close();
gameReady();
cout<<"Initiating the GAME";
dot(3);
system("CLS");
question(start,NULL);
file.open("Data.txt",ios::out);
Serialize(start,file);
file.close();
}
else
{
system("color 4F");
cout<<"FILE NOT FOUND, Please ensure the file \"Data.txt\" is present in the current directory"<<endl;
}
return 0;
}