Binary Tree Game

Apr 28, 2010 at 3:59pm
I need to create a binary tree game where the program asks the user a question and the users response is yes or no. From there the program will either ask another question or will guess the answer. If the program's guess is correct, then it will prompt the user that it was right and asks to play again. If the program is wrong, then it will ask the user what the correct answer is and then ask for a question that can be put back into the program for it to use in the next game.
I'm having trouble writing the parent node and going from there. I have all the default information in my program, I just need to write the game part and I'm having trouble writing it.
Can someone please help...
Apr 29, 2010 at 10:27am
Hmmm... Ok here's what you probably want to do...

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
struct Node {
     String question;
     Node *left;
     Node *right;
};

class BT {
    private:
         Node *root;
         Node *at;

         void insert(Node *const node,const String &question);
         // deleting etc...

         void guess(void);
    public:
         BT(void);
         void insert(// you'll have to think about the interface how you want to insert the items...);
         // Either construct the nodes outside of BT... or return with the insert function the 
         // newly created Node...
         void ask(void);
         void reset(void); // set at to root
};

BT::BT : root(0) {}

void BT::ask(void) {
    std::cout<<at->question<<"\n";
    //now get the input
    //cin>>something
    //Evaluate it and according to the answer set 
    //the at node to left or right;
    if (something) {
         at = at->left;
    } else {
         at = at->right;
    }
    // Algorithm for whether the PC wants to guess or not...
}

//etc 



Hope that helps
Apr 29, 2010 at 10:28am
You could also set the at as a static variable in ask...
Topic archived. No new replies allowed.