-
Notifications
You must be signed in to change notification settings - Fork 3
/
guess.cxx
136 lines (111 loc) · 3.13 KB
/
guess.cxx
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
//-< GUESS.CXX >-----------------------------------------------------*--------*
// POST++ Version 1.0 (c) 1998 GARRET * ? *
// (Persistent Object Storage) * /\| *
// * / \ *
// Created: 2-Feb-98 K.A. Knizhnik * / [] \ *
// Last update: 2-Feb-98 K.A. Knizhnik * GARRET *
//-------------------------------------------------------------------*--------*
// Sample program: game "Guess an animal"
//-------------------------------------------------------------------*--------*
#include "object.h"
USE_POST_NAMESPACE
#define MAX_STRING_SIZE 256
storage guess_storage("guess.odb");
class tree_node : public object {
public:
tree_node* guess();
static tree_node* what_is_it(tree_node* parent);
CLASSINFO(tree_node, REF(yes) REF(no));
static tree_node* create(tree_node* no, char* question, tree_node* yes) {
return new (self_class, guess_storage, strlen(question))
tree_node(no, question, yes);
}
protected:
tree_node* yes;
tree_node* no;
char question[1];
tree_node(tree_node* no, char* question, tree_node* yes) {
this->no = no;
this->yes = yes;
strcpy(this->question, question);
}
};
void input(char* prompt, char* buf, size_t buf_size)
{
char* p;
do {
printf(prompt);
*buf = '\0';
fgets(buf, buf_size, stdin);
p = buf + strlen(buf);
} while (p <= buf+1);
if (*(p-1) == '\n') {
*--p = '\0';
}
}
bool positive_answer()
{
char buf[16];
return fgets(buf, sizeof buf, stdin) && (*buf == 'y' || *buf == 'Y');
}
tree_node* tree_node::what_is_it(tree_node* parent)
{
char animal[MAX_STRING_SIZE], question[MAX_STRING_SIZE];
input("What is it ?\n", animal, sizeof animal);
input("What is difference from other ?\n", question, sizeof question);
return create(parent, question, create(NULL, animal, NULL));
}
tree_node* tree_node::guess()
{
printf("May be, %s (y/n) ? ", question);
if (positive_answer()) {
if (yes == NULL) {
printf("It was very simple question for me...\n");
} else {
tree_node* clarify = yes->guess();
if (clarify != NULL) {
yes = clarify;
}
}
} else {
if (no == NULL) {
if (yes == NULL) {
return what_is_it(this);
} else {
no = what_is_it(NULL);
}
} else {
tree_node* clarify = no->guess();
if (clarify != NULL) {
no = clarify;
}
}
}
return NULL;
}
REGISTER(tree_node);
int main()
{
if (guess_storage.open()) {
tree_node* root = (tree_node*)guess_storage.get_root_object();
while(true) {
printf("Think of an animal.\nReady (y/n) ? ");
if (!positive_answer()) {
break;
}
if (root == NULL) {
root = tree_node::what_is_it(NULL);
guess_storage.set_root_object(root);
} else {
root->guess();
}
}
printf("End of the game\n");
guess_storage.flush();
guess_storage.close();
return EXIT_SUCCESS;
} else {
printf("Failed to open database\n");
return EXIT_FAILURE;
}
}