2017-11-28 3 views
0

このプログラムは、ゲームをプレイするにつれて良くなる推測ゲームです(ここではいくつかのメソッドは含まれていません)。基本的に構造体でできたバイナリツリーです。ツリーの最後にある構造体だけが推測されます。他のすべての構造体は質問です。私がゲームをするたびに、それはうまく始まります。しかし、3回目にゲームをプレイすると、ツリー内のすべての構造体の質問が1つの質問に変わり、なぜ理解できないのですか(正しい推測で終了するため、ツリーの全体構造はうまくいくようです) 。私は本当に面倒な方法でコード化し、私はそれをお詫び申し上げます。しかし、私はこれを修正する方法についての助けは本当に感謝します。構造体に格納されている値が変化していると思う理由がない場合

struct node 
{ 
    char *guess; 
    char *question; 
    struct node *yes_ptr; 
    struct node *no_ptr; 
}; 
struct node *createObjectNode(char *guess) 
{ 
    struct node *newNode = malloc(sizeof(struct node)); 
    newNode->guess = guess; 
    return newNode; 
} 

int response(char *str) 
{ 
    char yesOrNo[5]; 
    printf("%s\n>", str); 
    fgets(yesOrNo, 5, stdin); 
    int i = strlen(yesOrNo)-1; 
    yesOrNo[i] = '\0'; 
    if(strcmp(yesOrNo, "yes") == 0) 
    { 
    return 1; 
    } 
    if(strcmp(yesOrNo, "no") == 0) 
    { 
    return 0; 
    } 
    return 0; 
} 
int main(int argc, char *argv[]) 
{ 

    struct node *root; 
    char guess[20]; 
    char question[120]; 
    char buffer[120]; 
    root = createObjectNode("pangolin"); 
    if(response("Shall we play a game?") == 1) 
    { 
    snprintf(buffer, sizeof(buffer), "Is it a %s?", getGuess(root)); 
    if(response(buffer) == 1) 
    { 
     printf("I win\n"); 
    } 
    else 
    { 
     printf("What were you thinking of?\n>"); 
     fgets(guess, 50, stdin); 
     int i = strlen(guess)-1; 
     if(guess[ i ] == '\n') 
     guess[i] = '\0'; 
     printf("Please give me a question about a %s,so I can tell the difference between a %s and a ", 
     guess, guess); 
     printf("%s\n>",getGuess(root)); 
     fgets(question, 50, stdin); 
     i = strlen(question)-1; 
     if(question[ i ] == '\n') 
     question[i] = '\0'; 
     snprintf(buffer, sizeof(buffer), "What is the answer for %s?", guess); 
     if(response(buffer) == 1) 
     { 
     struct node *child1 = createObjectNode(guess); 
     struct node *child2 = createObjectNode(getGuess(root)); 
     child1->yes_ptr = NULL; 
     child2->no_ptr = NULL; 
     child1->question = NULL; 
     child2->question = NULL; 
     root->guess = NULL; 
     root->question = question; 
     root->yes_ptr = child1; 
     root->no_ptr = child2;  
     } 
     else 
     { 
     struct node *child1 = createObjectNode(guess); 
     struct node *child2 = createObjectNode(getGuess(root)); 
     child1->yes_ptr = NULL; 
     child2->no_ptr = NULL; 
     child1->question = NULL; 
     child2->question = NULL; 
     root->guess = NULL; 
     root->question = question; 
     root->yes_ptr = child2; 
     root->no_ptr = child1; 
     } 
     printTree(root); 

    } 
    } 

    while(1) 
    { 
    char guess2[20]; 
    char question2[120]; 
    char buffer2[120]; 
    struct node *thisNode = malloc(sizeof(struct node)); 
    thisNode->question = root->question; 
    thisNode->yes_ptr = root->yes_ptr; 
    thisNode->no_ptr = root->no_ptr; 
    if(response("Shall we play again?")) 
    { 
     while(thisNode->yes_ptr != NULL && thisNode->no_ptr != NULL && !thisNode->guess) 
     { 
     if(response(thisNode->question) == 1) 
     { 
      thisNode = thisNode->yes_ptr; 
     } 
     else 
     { 
      thisNode = thisNode->no_ptr; 
     } 
     } 
     snprintf(buffer2, sizeof(buffer2), "Is it a %s?", getGuess(thisNode)); 
     if(response(buffer2) == 1) 
     { 
     printf("I win\n"); 
     break; 
     } 
     else 
     { 
     printf("What were you thinking of?\n>"); 
     fgets(guess2, 10, stdin); 
     printf("Please give me a question about a %s,so I can tell the difference between a %s and a %s\n>", 
       guess2, guess2, getGuess(thisNode)); 
     int i = strlen(guess2)-1; 
     guess2[i] = '\0'; 
     fgets(question2, 120, stdin); 
     i = strlen(question2)-1; 
     if(question2[i] == '\n') 
      question2[i] = '\0'; 
     snprintf(buffer2, sizeof(buffer2), "What is the answer for %s?", guess2); 
     if(response(buffer2) == 1) 
     { 
      struct node *child1 = createObjectNode(guess2); 
      struct node *child2 = createObjectNode(getGuess(thisNode)); 
      child1->yes_ptr = NULL; 
      child2->no_ptr = NULL; 
      child1->question = NULL; 
      child2->question = NULL; 
      thisNode->guess = NULL; 
      thisNode->question = question2; 
      thisNode->yes_ptr = child1; 
      thisNode->no_ptr = child2; 
     } 
     else 
     { 
      struct node *child1 = createObjectNode(guess2); 
      struct node *child2 = createObjectNode(getGuess(thisNode)); 
      child1->yes_ptr = NULL; 
      child2->no_ptr = NULL; 
      child1->question = NULL; 
      child2->question = NULL; 
      thisNode->guess = NULL; 
      thisNode->question = question2; 
      thisNode->yes_ptr = child2; 
      thisNode->no_ptr = child1; 
     } 
     } 
     printTree(root); 

    } 

    } 
    return 0; 
} 

答えて

2

問題は、ノードの->questionquestion2を保存することです。これは、関数mainのローカルに割り当てられたフィールドであり、ポインターに変換されても常に同じメモリを指します。 「あなたに質問してください...」と答えると、配列question2[]の内容が変更され、すべての->questionポインターが新しいテキストを指すようになりました。独立して文字列を格納するために

は、あなたは、メモリの新しい部分にmallocにあなたが質問とそのメモリへquestion2strcpy内容を保存したいすべての時間を必要とします。そうすれば、毎回新しい独立したポインタを得ることができます。

関連する問題