-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathakinator.cpp
More file actions
671 lines (508 loc) · 14.3 KB
/
Copy pathakinator.cpp
File metadata and controls
671 lines (508 loc) · 14.3 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#include "headers/akinator.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "headers/input_output.h"
#include "headers/interface.h"
#include "..\err_codes.h"
#include "headers/UDL.h"
#include "headers/stack.h"
#include <process.h>
#include <time.h>
#define CHECK_FOR_CLOSE(ans, run) \
do \
{ \
if (ans == CLOSE) \
{ \
*run = 0; \
return SUCCESS; \
} \
} while(0)
enum mode
{
USER = 1,
GOD = 2
};
static mode choose_game_mode (int argc, char* argv[]);
static int check_password (char* str);
static char* caesar (const char* str);
static err change_tree (Node* tree, int* run);
static err get_node_info (Node* tree);
static err running(Node* tree, int* run);
static err run_describe (Node* tree);
static err run_guess (Node* tree, int* run);
static err check_back_restart (answer ans, Node** tree, Stack* dont_know = NULL);
static err add_node (Node* tree);
static err get_info (Node* tree, char** left_buf, char** parent_buf);
static err ask (char* data, advertisement* advert = NULL);
static char* make_question (char* data);
static err tree_kill (Node* head);
static err deleteNode (Node* head);
static err treeSearch (Node* head, data_t srch, Node** return_t);
static err make_description (Node* tree, const char* obj, char** description);
static err fill_description (Stack* stk, char** description);
static err make_description__ (Node* tree, const char* obj, Stack* stk, int* found);
static void split_str (char** description);
static void other_subtree (Node** tree, Stack* stk);
static void choose_subtree (Node** tree, Stack* stk);
int main(int argc, char* argv[])
{
FOPEN(read, "rtTree.txt", "rb");
FOPEN(log, "rtTreeLog.txt", "a");
Node* tree = NULL;
CALLOC(tree, Node, 1);
CALLOC(tree->data, char, DATA_LEN + 1);
//CALLOC(tree->picture, char, DATA_LEN + 1);
importTree(read, tree);
fclose(read);
create_window ();
int run = 1;
mode md = choose_game_mode(argc, argv);
while (run == 1)
{
fprintf(log, "\n\n");
fprint_tree(log, tree);
FOPEN(out, "rtTree.txt", "wb");
if (md == GOD)
change_tree(tree, &run);
else
running(tree, &run);
fprint_tree(out, tree);
fclose(out);
}
fclose(log);
tree_kill(tree);
}
mode choose_game_mode (int argc, char* argv[])
{
if(argc == 1)
return USER;
if (argc == 2 && check_password(argv[1]))
return GOD;
else if (argc > 2)
{
printf ("Аргументы не были распознаны\n");
mySleep(1500);
}
return USER;
}
int check_password (char* str)
{
const char* password = "ufxx|twi";
if (strncmp(password, caesar(str), strlen(password)) == 0)
return 1;
return 0;
}
char* caesar (const char* str)
{
char* code = (char*)calloc(strlen(str), sizeof(char));
for (int i = 0; i < strlen(str); i++)
code[i] = str[i] + 5;
return code;
}
err change_tree (Node* tree, int* run)
{
answer ans = ERR;
while (1)
{
ask(tree->data);
ans = check_answer(YNBR);
CHECK_FOR_CLOSE (ans, run);
if (ans == YES)
tree = tree->left;
else if (ans == NO)
tree = tree->right;
if (check_back_restart(ans, &tree) == FAIL)
return SUCCESS;
if (tree->right == NULL && tree->left == NULL)
{
put_answer(tree->data, THINKING);
ans = check_add ();
CHECK_FOR_CLOSE (ans, run);
if (check_back_restart(ans, &tree) == FAIL)
return SUCCESS;
if (ans == ADD)
get_node_info(tree);
}
}
return SUCCESS;
}
err get_node_info (Node* tree)
{
answer ans = ERR;
char* right_buf = 0;
char* left_buf = 0;
CALLOC(left_buf, char, DATA_LEN + 1);
CALLOC(right_buf, char, DATA_LEN + 1);
ans = InputBox(left_buf, "Введите значение узла по направлению ДА", DATA_LEN);
if (ans == ERR)
{
free(left_buf);
return FAIL;
}
ans = InputBox(right_buf, "Введите значение узла по направлению НЕТ", DATA_LEN);
if (ans == ERR)
{
free(left_buf);
free(right_buf);
return FAIL;
}
CALLOC(tree->right, Node, 1);
CALLOC(tree->left, Node, 1);
Node* right = tree->right;
Node* left = tree->left;
CALLOC(right->data, char, DATA_LEN + 1);
CALLOC(left->data, char, DATA_LEN + 1);
right->parent = tree;
left->parent = tree;
strcpy(right->data, right_buf);
strcpy(left->data, left_buf);
free(left_buf);
free(right_buf);
return SUCCESS;
}
err check_back_restart (answer ans, Node** tree, Stack* dont_know)
{
if (ans == BACK)
{
if ((*tree)->parent != NULL)
{
(*tree) = (*tree)->parent;
if (dont_know != NULL)
if (dont_know->data[dont_know->size - 1] == (*tree))
stack_pop(dont_know, NULL);
}
else
return FAIL;
}
else if (ans == RESTART)
{
while ((*tree)->parent != NULL)
(*tree) = (*tree)->parent;
if (dont_know != NULL)
{
stack_dtor(dont_know);
stack_ctor(dont_know, 0);
}
}
return SUCCESS;
}
err running(Node* tree, int* run)
{
answer ans = ERR;
fill_window(BASE);
draw_mode_bt();
ans = choose_mode();
CHECK_FOR_CLOSE (ans, run);
if (ans == GUESS)
{
run_guess(tree, run);
if (*run == 0)
return SUCCESS;
}
else if (ans == DESCR)
run_describe(tree);
else if (ans == SHOW)
{
put_answer ("Вот!", FISH);
mySleep(1000);
draw_tree(tree);
mySleep(5000);
}
put_question((char*)"Играть снова?", BASE);
ans = check_answer(YN);
if (ans == NO)
{
put_answer("Спасибо за игру!", BASE);
*run = 0;
}
CHECK_FOR_CLOSE (ans, run);
return SUCCESS;
}
err run_guess (Node* tree, int* run)
{
Stack dont_know = {};
stack_ctor(&dont_know, 1);
answer ans = ERR;
advertisement advert = {};
find_advert(&advert);
while (tree->right != NULL && tree->left != NULL)
{
ask(tree->data, &advert);
ans = check_answer(YNDN);
CHECK_FOR_CLOSE (ans, run);
if (ans == YES)
tree = tree->left;
else if (ans == NO)
tree = tree->right;
else if (ans == SKIP)
choose_subtree(&tree, &dont_know);
if (check_back_restart(ans, &tree, &dont_know) == FAIL)
return SUCCESS;
while (tree->right == NULL && tree->left == NULL)
{
char* question = make_question(tree->data);
put_question (question, UNDERSTAND);
draw_picture (tree);
free(question);
ans = check_answer(YN);
CHECK_FOR_CLOSE (ans, run);
if (ans == YES)
{
put_answer("От меня ничего не укроется", PROUD);
break;
}
else if (ans == NO)
{
if (dont_know.size > 0)
{
other_subtree(&tree, &dont_know);
}
else
{
// err res = add_node(tree);
// if (res != SUCCESS)
// return SUCCESS;
put_answer("Вам удалось победить меня.", CONFUSED);
break;
}
}
}
}
stack_dtor(&dont_know);
delete_ad(&advert);
mySleep(5000);
return SUCCESS;
}
err run_describe (Node* tree)
{
char* description = 0;
char* srch = 0;
CALLOC(srch, char, DATA_LEN);
//txWaveData_t tam_blya = txWaveLoadWav ("description.wav");
answer ans = InputBox(srch, "Чье описание вы хотите получить ?\n( Пожалуйста, вводите в именительном падеже )", DATA_LEN);
if (ans == ERR)
{
free(srch);
return FAIL;
}
make_description (tree, srch, &description);
put_answer(description, UNDERSTAND, DATA_LEN);
//txWaveOut (tam_blya);
free(description);
free(srch);
mySleep(5000);
return SUCCESS;
}
err tree_kill (Node* head)
{
CHECK_PTR(head);
if (head->left != NULL)
tree_kill(head->left);
if (head->right != NULL)
tree_kill(head->right);
free(head->data);
free(head->picture);
free(head);
return SUCCESS;
}
char* make_question (char* data)
{
char* question = (char*)calloc(strlen(data) + strlen("Это ?"), sizeof(char));
strcpy(question, "Это ");
strcat(question, data);
strcat(question, " ?");
return question;
}
err ask (char* data, advertisement* advert)
{
char* qst = 0;
CALLOC(qst, char, (strlen("Ваш объект ") + strlen(data) + 2000));
strcpy(qst, "Ваш объект ");
strcpy(qst + strlen("Ваш объект "), data);
for (int i = 1; i < strlen("Ваш объект ") + strlen(data) + 1; i++)
qst[i] = (char)tolower(qst[i]);
put_question (qst, THINKING);
free(qst);
if (advert != NULL)
_beginthread (draw_advert, 0, advert);
return SUCCESS;
}
err add_node (Node* tree)
{
char* left_buf = 0;
char* parent_buf = 0;
err res = get_info(tree, &left_buf, &parent_buf);
if (res != SUCCESS)
return res;
CALLOC(tree->right, Node, 1);
CALLOC(tree->left, Node, 1);
Node* right = tree->right;
Node* left = tree->left;
right->parent = tree;
left->parent = tree;
CALLOC(right->data, char, DATA_LEN + 1);
CALLOC(left->data, char, DATA_LEN + 1);
strcpy(left->data, left_buf);
strcpy(right->data, tree->data);
strcpy(tree->data, parent_buf);
free(left_buf);
free(parent_buf);
return SUCCESS;
}
err get_info (Node* tree, char** left_buf, char** parent_buf)
{
answer ans = ERR;
CALLOC(*left_buf, char, DATA_LEN + 1);
ans = InputBox(*left_buf, "Кого или что вы имели в виду?", DATA_LEN);
if (ans == ERR)
{
free(*left_buf);
return FAIL;
}
char* what_different = 0;
CALLOC(what_different, char, (strlen("Чем отличается от ?") + strlen(*left_buf) + strlen(tree->data)));
sprintf(what_different, "Чем %s отличается от %s ?", *left_buf, tree->data);
CALLOC(*parent_buf, char, DATA_LEN + 1);
ans = InputBox(*parent_buf, what_different, DATA_LEN);
if (ans == ERR)
{
free(*parent_buf);
free(*left_buf);
return FAIL;
}
strcat(*parent_buf, "?");
return SUCCESS;
}
void other_subtree (Node** tree, Stack* stk)
{
stack_pop(stk, tree);
if ((*tree)->st == left)
{
*tree = (*tree)->right;
}
else
{
*tree = (*tree)->left;
}
return;
}
void choose_subtree (Node** tree, Stack* stk)
{
srand ((unsigned)time(NULL));
if (rand() % 2 == 0)
(*tree)->st = left;
else
(*tree)->st = right;
stack_push(stk, tree);
if ((*tree)->st == left)
{
*tree = (*tree)->left;
}
else
{
*tree = (*tree)->right;
}
return;
}
err make_description (Node* tree, const char* obj, char** description)
{
CHECK_PTR(tree);
CHECK_PTR(obj);
Stack stk = {};
stack_ctor(&stk, 1);
int found = 0;
err res = make_description__(tree, obj, &stk, &found);
if (res == SUCCESS)
{
fill_description(&stk, description);
}
else
{
const char* descr_temp = "Не нашел ничего по вашему запросу(((";
*description = (char*)calloc(strlen(descr_temp), sizeof(char));
strcpy(*description, descr_temp);
}
split_str(description);
stack_dtor(&stk);
*description = (char*)realloc(*description, strlen(*description));
return res;
}
err fill_description (Stack* stk, char** description)
{
int descr_size = 0;
for (size_t i = 0; i < stk->size; i++)
descr_size += ((int)strlen(stk->data[i]->data) + 5);
CALLOC(*(description), char, descr_size);
for (size_t i = 0; i < stk->size - 1; i++)
{
if ((stk->data[i])->right == stk->data[i + 1])
strcat(*(description), "не ");
strcat(*(description), stk->data[i]->data);
if (i == (stk->size - 3))
{
strcat(*(description), "и");
}
strcat(*(description), " ");
}
size_t cnt = 0;
(*description)[0] = (char)toupper((*description)[0]);
for (int i = 1; i < descr_size; i++)
{
if ((*description)[i] == '?')
{
cnt++;
if (cnt == stk->size)
(*description)[i] = '.';
else if (cnt == (stk->size - 2))
(*description)[i] = ' ';
else
(*description)[i] = ',';
}
(*description)[i] = (char)tolower((*description)[i]);
}
return SUCCESS;
}
err make_description__ (Node* tree, const char* obj, Stack* stk, int* found)
{
CHECK_PTR(tree);
if (tree->left != NULL)
{
stack_push(stk, &tree);
make_description__(tree->left, obj, stk, found);
if (*found == 1)
return SUCCESS;
stack_pop(stk, NULL);
}
if (tree->right != NULL)
{
stack_push(stk, &tree);
make_description__(tree->right, obj, stk, found);
if (*found == 1)
return SUCCESS;
stack_pop(stk, NULL);
}
if (strcmp(tree->data, obj) == 0)
{
*found = 1;
stack_push(stk, &tree);
return SUCCESS;
}
return FAIL;
}
void split_str (char** description)
{
int line_len = DATA_LEN;
int ds_len = (int)strlen(*description);
int lines = ds_len / line_len + 1;
char* ptr = 0;
for (int l = 1; l < lines; l++)
{
ptr = *description + (l * line_len);
while (*ptr != ' ')
ptr--;
*ptr = '\n';
}
return;
}