//print string of tree in pre order recursively void binary_tree_write(binary_tree* self, FILE* stream){ // string to output text with char *str = NULL; // if it's a leaf if (binary_tree_is_leaf(self)){ str = binary_tree_get_string(self, str); fprintf(stream, "A%s", str); return; } else { // it's not a leaf str = binary_tree_get_string(self, str); fprintf(stream, "Q%s", str); binary_tree_write(binary_tree_get_left(self), stream); binary_tree_write(binary_tree_get_right(self), stream); } }
binary_tree* playRound(FILE* stream, binary_tree* tree){ char answer; //the answer to the question shown binary_tree* root = tree; char* value; char testing[MAX_STRING_SIZE]; while(!binary_tree_is_leaf(tree)){ strcpy(testing, binary_tree_get_string(tree, value)); if(testing[strlen(testing)-1] == '\n'){ testing[strlen(testing)-1] = '\0'; } printf("%s ", testing); rewind(stdin); scanf(" %c", &answer); if(answer == 'y'){ parent = tree; tree = binary_tree_get_right(tree); } else{ parent = tree; tree = binary_tree_get_left(tree); } } if(answer != 'y' && answer != 'n'){ answer = 's'; } char* val; char finalAnswer; //the answer to the question prompted char testing2[MAX_STRING_SIZE]; strcpy(testing2, binary_tree_get_string(tree, val)); if(testing2[strlen(testing2)-1] == '\n'){ testing2[strlen(testing2)-1] = '\0'; } printf("Were you thinking of a %s ? ", testing2); scanf(" %c", &finalAnswer); if(finalAnswer == 'y'){ printf("Great\n"); return root; } else{ printf("Doh! What was the animal? "); char animal[MAX_STRING_SIZE]; char question[MAX_STRING_SIZE]; char correctAnswer[MAX_STRING_SIZE]; char* getValue; char testing3[MAX_STRING_SIZE]; strcpy(testing3, binary_tree_get_string(tree, getValue)); if(testing3[strlen(testing3)-1] == '\n'){ testing3[strlen(testing3)-1] = '\0'; } scanf("%s", animal); //scans stdin for your animal printf("What question separates %s from %s? ", animal, testing3); rewind(stdin); fgets(question, MAX_STRING_SIZE, stdin); //scans stdin for your question that must end in a '?' char printf("What is the correct answer for this animal? "); rewind(stdin); scanf("%s", correctAnswer); //the correctAnswer to your question if(answer == 's'){ parent= NULL; } if(correctAnswer == 'y'){ newTree = binary_tree_create_stt(question, binary_tree_create_s(animal), tree); } else{ newTree = binary_tree_create_stt(question, tree, binary_tree_create_s(animal)); } if(answer == 's'){ return newTree; } else{ if(answer == 'y'){ binary_tree_set_right(parent, newTree); } else{ binary_tree_set_left(parent, newTree); } return root; } } }