-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessing_game.cpp
389 lines (363 loc) · 11.2 KB
/
Guessing_game.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <assert.h>
//rlink= derecha= SI & llink= izquierda= NO
//info del tree es el key de las listas
//tree para guardar el orden de las respuestas
//dos doublinklist para guardar palabras y preguntas
using namespace std;
//************************************************************
// TEMPLATE PARA LISTA DOBLE
//************************************************************
template <class Type>
struct nodeType
{
Type info;
nodeType<Type>* next;
nodeType<Type>* back;
int key;
};
template <class Type>
class doublyLinkedList
{
public:
bool isEmptyList() const {
return (first == NULL);
}
void destroy() {
nodeType<Type> *temp; //pointer to delete the node
while (first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
last = NULL;
count = 0;
}
void printOne(int printItem) const {
nodeType<Type> *current;
current = search(printItem);
cout<< current->info;
return;
}
int length() const {
return count;
}
Type front() const {
assert(first != NULL);
return first->info;
}
Type back() const {
assert(last != NULL);
return last->info;
}
void saveTo_File() {
ofstream Out_file;
Out_file.open("Data.txt",std::ios_base::app);
nodeType<Type> *temp = first;
while (temp != NULL)
{
Out_file << temp->info<< endl;
temp = temp->next;
}
Out_file<< "*****"<<endl;
Out_file.close();
}
nodeType<Type>* search(const int searchItem) const {
bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first;
while (current != NULL && !found)
if (current->key == searchItem)
found = true;
else
current = current->next;
return current;
}
void insert(const Type& insertItem) {
nodeType<Type> *newNode; //pointer to create a node
newNode = new nodeType<Type>; //create the node
newNode->info = insertItem; //store the new item in the node
newNode->next = NULL;
newNode->back = NULL;
newNode->key = count; //Empieza en 0
if (first == NULL) //if list is empty, newNode is the only node
{
first = newNode;
last = newNode;
count++;
}
else //insert at the end
{
last->next = newNode;
newNode->back = last;
last = newNode;
count++;
}
}
void deleteNode(const int deleteItem) {
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == NULL)
cout << "Cannot delete from an empty list." << endl;
else if (first->key == deleteItem)
{
current = first;
first = first->next;
if (first != NULL)
first->back = NULL;
else
last = NULL;
count--;
delete current;
}
else
{
found = false;
current = first;
while (current != NULL && !found)
if (current->key == deleteItem)
found = true;
else
current = current->next;
if (found)
{
trailCurrent = current->back;
trailCurrent->next = current->next;
if (current->next != NULL)
current->next->back = trailCurrent;
if (current == last)
last = trailCurrent;
count--;
delete current;
}
else
cout << "The item to be deleted is not in list."<< endl;
}
}
doublyLinkedList() {
first = NULL;
last = NULL;
count = 0;
}
~doublyLinkedList() {
nodeType<Type> *temp; //pointer to delete the node
while (first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
delete first;
}
protected:
int count;
nodeType<Type>* first; //pointer to the first node
nodeType<Type>* last; //pointer to the last node
};
//************************************************************
// TEMPLATE PARA TREE
//************************************************************
template <class elemType>
struct binaryTreeNode
{
elemType info;
binaryTreeNode<elemType> *llink;
binaryTreeNode<elemType> *rlink;
};
template <class elemType>
class binaryTreeType {
public:
bool isEmpty() const {
return (root == NULL);
}
int treeHeight() const {
binaryTreeNode<elemType> *p = root;
if (p == NULL)
return 0;
else
return 1 + max(height(p->llink), height(p->rlink));
}
int max(int x, int y) const {
if (x >= y)
return x;
else
return y;
}
void destroyTree() {
destroy(root);
}
void destroy(binaryTreeNode<elemType>* &p) {
if (p != NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p = NULL;
}
}
void insert(const elemType& insertItem, binaryTreeNode<elemType>* &p, int lORr) {
binaryTreeNode<elemType> *newNode; //pointer to create the node
newNode = new binaryTreeNode<elemType>;
assert(newNode != NULL);
newNode->info = insertItem;
newNode->llink = NULL;
newNode->rlink = NULL;
if (p == NULL) { //si esta vacio
root = newNode;
nodeAmount++;
p = newNode;
return;
}
else if (lORr == 0) {
p->llink = newNode;
nodeAmount++;
return;
}
else if (lORr == 1){
//Esta pregunta tiene dos respuestas
binaryTreeNode<elemType> *OldNode; //pointer to create the node
OldNode = new binaryTreeNode<elemType>;
assert(OldNode != NULL);
OldNode->info = p->info;
OldNode->llink = p->llink;
OldNode->rlink = p->rlink;
p->rlink = newNode; //Como tiene rlink, solo pregunta
newNode->llink = OldNode;
nodeAmount++;
return;
}
}
binaryTreeType() {
root = NULL;
nodeAmount = 0;
}
~binaryTreeType(){
destroyTree();
}
binaryTreeNode<elemType> *root;
int nodeAmount;
};
//************************************************************
// FUNCIONES ADICIONALES
//************************************************************
int convertirRespuesta(string entrada) {
int respuestaNum;
if (entrada == "si"||entrada == "Si")
respuestaNum = 1;
else if (entrada == "SI"||entrada == "S")
respuestaNum = 1;
else if (entrada =="s"||entrada =="1")
respuestaNum = 1;
else
respuestaNum = 0;
return respuestaNum;
}
string Instrucciones() {
string mensaje = "\tINSTRUCCIONES: Usted debera pensar en un pais y responder a las \n\t"
"preguntas con si(1) o no(0) para asi yo tratar de adivinar en que\n\t"
"pais esta pensando. \n\t"
"En caso de que no lo conozca, me lo aprendo para usarlo en una \n\t"
"proxima ocasion!\n";
return mensaje;
}
string TextoAniadir () {
string texto = "Ups! Parece que no conozco ese pais!\n"
"Pofavor, introduzca el nombre del pais (Recuerda la mayuscula!),\n"
"y luego una pregunta que lo distinga (Recuerda el simbol *?*)\n";
return texto;
}
//************************************************************
// MAIN
//************************************************************
int main() {
binaryTreeType<int> arbolDe_Paises;
binaryTreeNode<int>* current = arbolDe_Paises.root;
doublyLinkedList<string> listaDe_Palabras;
doublyLinkedList<string> listaDe_Preguntas;
int rORl = 0;
int elementKey = listaDe_Palabras.length();
listaDe_Palabras.insert("Puerto Rico");
listaDe_Preguntas.insert("Vivimos en ese pais?");
arbolDe_Paises.insert(elementKey, current, rORl);
elementKey = listaDe_Palabras.length();
listaDe_Palabras.insert("Estados Unidos");
listaDe_Preguntas.insert("Somos territorio de esa nacion?");
arbolDe_Paises.insert(elementKey, current, rORl);
string entrada;
cout<< "Hola! Binvenido al juego de adivinanzas!\n"
"Deseas ver las instrucciones? Si(1)||No(0)\n";
cin>> entrada;
int respuesta = convertirRespuesta(entrada);
if (respuesta) {
cout<< Instrucciones();
}
cout<< "\tComencemos!\n\n";
int continuar;
int repetir;
binaryTreeNode<int>* previous;
do {
current = arbolDe_Paises.root;
continuar = 1;
do {
rORl = 0;
listaDe_Preguntas.printOne(current->info);
cout<< "\nSi(1)||No(0)\n";
cin>> entrada;
respuesta = convertirRespuesta(entrada);
if (!respuesta) {
previous = current;
current = current->llink;
}
else if (respuesta && current->rlink == NULL) {
cout<< "Es el pais: ";
listaDe_Palabras.printOne(current->info);
cout<< "\nSi(1)||No(0)\n";
cin>> entrada;
respuesta = convertirRespuesta(entrada);
if (respuesta) {
cout<< "Que divertido!\nQuieres jugar de nuevo? Si(1)||No(0)\n";
cin>> entrada;
repetir = convertirRespuesta(entrada);
continuar = 0;
}
else {
previous = current;
current = NULL;
rORl = 1;
}
}
else if (respuesta && current->rlink != NULL){
previous = current;
current = current->rlink;
}
if (current == NULL) {
cout<< TextoAniadir();
elementKey = listaDe_Palabras.length();
cin.get();
getline(cin, entrada, '\n');
listaDe_Palabras.insert(entrada);
getline(cin, entrada, '\n');
listaDe_Preguntas.insert(entrada);
arbolDe_Paises.insert(elementKey,previous,rORl);
cout<< "\nQue divertido!\nQuieres jugar de nuevo? Si(1)||No(0)\n";
cin>> entrada;
repetir = convertirRespuesta(entrada);
continuar = 0;
}
}while (continuar);
cout<< "\n*******************************************************\n"
"*******************************************************\n\n";
} while (repetir);
ofstream Out_file;
Out_file.open("Data.txt");
Out_file.close();
listaDe_Palabras.saveTo_File();
listaDe_Preguntas.saveTo_File();
cout<< "\nAdios!\n";
return 0;
}