@ycmjason/

PleasantCooperativeAutocad

C

No description

fork
loading
Files
  • main.c
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node
{
  char* question;
  char* object;
  struct node* parent;
  struct node* yes_ptr;
  struct node* no_ptr;
}Node;

Node* initialise(Node* p);
Node* play(Node* p);
int is_yes_or_no(char* ans);
Node* createObject(char* obj);
Node* createQuestion(char* quest);
char* getNewObjectName();
char* getNewObjectQuestion(Node* currentNode, char* newObject);
void nodePrint(Node* p);
int isObject(Node* p);
void printObject(Node* p);
void printQuestion(Node* p);
void treePrint(Node* p);
void freeTree(Node* p);


int main(int argc, char* argv[])
{
  Node* root = NULL;
  printf("root at the beginning: **********\n" );
  nodePrint(root);
  root = initialise(root);
  printf("root after initialise: **********\n" );
  nodePrint(root);
  int play_again = 1;

  //game logic
  while(play_again)
  {
    root = play(root);
      printf("root return from this round: **********\n" );
      nodePrint(root);
    char* again = (char*)malloc(5* sizeof(char));
    printf("Would you like to play again? \n");
    fgets(again, 5, stdin);
    if(!is_yes_or_no(again)) {
      play_again = 0;
    }
    free(again);
  }//while

  treePrint(root);
  freeTree(root);

  return 0;
}

//initialise the game
Node* initialise(Node* p)
{
  printf("Game start!!!!! \n");
  printf("---------------------------------------------------- \n");
  p = createObject("A Pangolin");

  return p;
}

Node* play(Node* p)
{
  Node* currentNode = p;
  //Node* root = p;
  int finished = 0;

  printf("OK, please think of something. \n");
  while(!finished)
  {
    //if currentNode is object
    if(isObject(currentNode))
    {
        //if the currentNode is an object
        //make the guess
        char* ans = (char*) malloc(5*sizeof(char));
        printf("Is it %s?\n", currentNode->object);
        fgets(ans,5,stdin);
        printf("Your answer is : %s",ans);
        //if player answer yes, the computer win
        if(is_yes_or_no(ans) == 1)
        {
            printf("Good. That was soooo easy.\n");
        }
        else
        {
            //say user has won and ask object name
            char* newObj = getNewObjectName();
            char newObject[80];
            strcpy(newObject, newObj);
            free(newObj);
            printf("%s \n",newObject);

            //ask user to provide question about the object
            char* newQuest = getNewObjectQuestion(currentNode, newObject);
            char newQuestion[80];
            strcpy(newQuestion, newQuest);
            free(newQuest);
            printf("%s \n",newQuestion);

            //ask user provide an answer for the question about the object
            char* newAns = (char*) malloc(5*sizeof(char));
            printf("What is the answer for %s?\n", newObject);
            fgets(newAns,5,stdin);

            //create two new node for the question and object
            Node* newObjNode = createObject(newObject);
            nodePrint(newObjNode);
            Node* newQuestNode = createQuestion(newQuestion);
            nodePrint(newQuestNode);
            //link the node together
              printf("CurrentNode: ************** \n");
              nodePrint(currentNode);
            Node* temp = currentNode;
              nodePrint(temp);
            Node* tempParent = currentNode->parent;
              nodePrint(tempParent);
            newQuestNode->parent = tempParent;
              nodePrint(newQuestNode->parent);
            currentNode = newQuestNode;
              printf("CurrentNode: ************** \n");
              nodePrint(currentNode);

            if(tempParent != NULL && strcmp((tempParent->yes_ptr)->object,temp->object) == 0)
            {
              tempParent->yes_ptr = currentNode;
            }
            else if(tempParent != NULL && strcmp((tempParent->no_ptr)->object,temp->object) == 0)
            {
              tempParent->no_ptr = currentNode;
            }

            if(is_yes_or_no(newAns))
            {
              currentNode->yes_ptr  = newObjNode;
                nodePrint(currentNode->yes_ptr);
              newObjNode->parent    = newQuestNode;
                nodePrint(newObjNode->parent);
              currentNode->no_ptr   = temp;
                nodePrint(currentNode->no_ptr);
              temp->parent          = newQuestNode;
                nodePrint(temp->parent);
            }
            else
            {
              currentNode->no_ptr   = newObjNode;
              newObjNode->parent    = newQuestNode;
              currentNode->yes_ptr  = temp;
              temp->parent          = newQuestNode;
            }
            free(newAns);

            printf("Thanks.\n");
        }
        free(ans);
        finished = 1;
    }//if currentNode is object
    else
    {
        //for question node,
        //if the player answer yes then move the current Node to the yes_ptr
        //if the player answer no then move the current Node to the no_ptr
        char* newAns = (char*) malloc(5*sizeof(char));
        printf("%s?\n", currentNode->question);
        fgets(newAns,5,stdin);
        if(is_yes_or_no(newAns) == 1)
        {
          currentNode = currentNode->yes_ptr;
        }
        else if(is_yes_or_no(newAns) == 0)
        {
          currentNode = currentNode->no_ptr;
        }
        else
        {
          printf("Please say yes or no! \n");
        }
        free(newAns);
    }//else if currentNode is question
  }//while

    printf("CurrentNode before looping:******\n" );
    nodePrint(currentNode);
  //find the root node
  while(currentNode->parent != NULL)
  {
    currentNode = currentNode->parent;
  }

    printf("CurrentNode after looping:******\n" );
    nodePrint(currentNode);

  return currentNode;
}//play

//check the answer from user is yes or no
//return 1 if answer yes
//return 0 if answer no
int is_yes_or_no(char* ans)
{
  if( (strncmp(ans,"y",1) == 0) || (strncmp(ans,"Y",1) == 0) || (strncmp(ans,"yes",3) == 0) || (strncmp(ans,"Yes",3) == 0))
    return 1;
  else if((strncmp(ans,"n",1) == 0) || (strncmp(ans,"No",1) == 0) || (strncmp(ans,"no",2) == 0) || (strncmp(ans,"No",2) == 0))
    return 0;
  else
    return -1;
}

//create an object node
Node* createObject(char* obj)
{
  Node* p = (Node*) malloc(sizeof(Node));
  p->object   = obj;
  p->question = NULL;
  p->parent   = NULL;
  p->yes_ptr  = NULL;
  p->no_ptr   = NULL;

  printf("Object [%s] created. \n",obj );

  return p;
}

//create a question node
Node* createQuestion(char* quest)
{
  Node* p = (Node*) malloc(sizeof(Node));
  p->question = quest;
  p->object   = NULL;
  p->parent   = NULL;
  p->yes_ptr  = NULL;
  p->no_ptr   = NULL;

  printf("Question [%s] created. \n",quest );
  return p;
}

char* getNewObjectName()
{
  char* newObj = (char*) malloc(80*sizeof(char));
  printf("Oh. Well you win then ... What were you thinking of? \n");
  fgets(newObj,80,stdin);
  char* newObject = (char*)malloc(80* sizeof(char));
  sscanf(newObj,"%[^\n]", newObject);

  free(newObj);

  return newObject;
}

char* getNewObjectQuestion(Node* currentNode, char* newObject)
{
  char* newQuest = (char*) malloc(80*sizeof(char));
  printf("Please give me a question about %s, so I can tell the difference between %s and %s.\n", newObject, newObject, currentNode->object);
  fgets(newQuest,80,stdin);
  char* newQuestion = (char*)malloc(80* sizeof(char));
  sscanf(newQuest,"%[^\n]", newQuestion);

  free(newQuest);

  return newQuestion;
}


//print a node
void nodePrint(Node* p)
{
  if(p == NULL)
  {
    printf("The node is null. \n");
  }
  else if(isObject(p))
  {
    printf("This is an object. \n\n");
    printObject(p);
    printf("---------------------------------------------------- \n");
  }
  else
  {
    printf("This is a question. \n\n");
    printQuestion(p);
    printf("---------------------------------------------------- \n");
  }
}

//check if the node is an object or a question,
//return 1 if it is an Object,
//return 0 if it is a question
int isObject(Node* p)
{
  //the yes_ptr and no_ptr are both empty for an object node
  if( p->object != NULL && p->question == NULL && p->yes_ptr == NULL && p->no_ptr == NULL)
    return 1;
  else
    return 0;
}

//print an object node
void printObject(Node* p)
{
  char* obj = p->object;
  char* quest = p->question;
  char* par = NULL;
  if(p->parent != NULL)
  {
    par = (p->parent)->question;
  }
  printf("Parent  : %s \n\n", par);
  printf("Object  : %s \n", obj);
  printf("Question: %s \n", quest);


}

//print a question node
void printQuestion(Node* p)
{
  char* obj = p->object;
  char* quest = p->question;
  char* par = NULL;
  if(p->parent != NULL)
  {
    par = (p->parent)->question;
  }
  char* yes = NULL;
  char* no = NULL;

  if(p->yes_ptr != NULL && isObject(p->yes_ptr))        yes = (p->yes_ptr)->object;
  else if(p->yes_ptr != NULL && !isObject(p->yes_ptr))  yes = (p->yes_ptr)->question;

  if(p->no_ptr != NULL && isObject(p->no_ptr))         no = (p->no_ptr)->object;
  else if(p->no_ptr != NULL && !isObject(p->no_ptr))   no = (p->no_ptr)->question;

  printf("Parent  : %s \n\n", par);
  printf("Object  : %s \n", obj);
  printf("Question: %s \n", quest);
  printf("Yes     : %s \n", yes);
  printf("No      : %s \n", no);
}

//print out the binary tree
void treePrint(Node* p)
{
  if(p == NULL)           return;
  else if(isObject(p))    nodePrint(p);
  else
  {
    nodePrint(p);
    treePrint(p->yes_ptr);
    treePrint(p->no_ptr);
  }
}

//free the binary tree
void freeTree(Node* p)
{
  if(p == NULL)           return;
  else if(isObject(p))    free(p);
  else
  {
    Node* temp1 = p->yes_ptr;
    Node* temp2 = p->no_ptr;
    free(p);
    freeTree(temp1);
    freeTree(temp2);
  }
}