예제 #1
0
// helper function for create method that does the work
binary_tree* binary_tree_create_f_help(FILE* stream){

  // line to read the text with
  char *line = NULL;
  char hold[MAX_STRING_SIZE];
  line = fgets(hold, MAX_STRING_SIZE, stream);

  // if the text is not null
  if (line != NULL){
  // get the first character
  char first = *line;
  // get the rest of the line (trim out Q or A)
  char *rest = (line + 1);

  // if it's a question
  if (first == 'Q'){
    binary_tree *left = binary_tree_create_f_help(stream);
    binary_tree *right = binary_tree_create_f_help(stream);
    binary_tree *root = binary_tree_create_stt(rest, left, right);
    return root;
  } else if (first == 'A'){
    binary_tree *leaf = binary_tree_create_s(rest);
    return leaf;
  } else {
      return NULL;
    }
  } else
    return NULL;
}
예제 #2
0
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;
    }
  }
}