Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Apr 2008

    20 questions game

    Hey, all. I'm writing a progam that uses Binary search trees to create a game similar to the 20 questions game. Unfortunately, I'm not quite sure how the code works completely. I think it should probably have recursion in it somewhere as we just got done covering that, but I can't seem to find where. Currently, what I have is a binarytreenode class to create the nodes and then a decisiontree class that is supposed to hide the implementation of the binarytreenode class. The problem comes in that I'm not quite sure how to write the code. Any help even with psuedocode would be helpful and appreciated. I'd like to have it so if the questions is true then it moves to the left child but if it is false then it moves to the right child.

    Currently have,
    class BinaryTreeNode(object):
        def __init__(self, data, left = none, right = none):
            self.data = data
            self.left = left
            self.right = right
    '''Then from here I'm not sure what else to put into the BinaryTreeNode class, it doens't seem like this is all there should be but I can't figure out what else might go in here. Possibly an insert function, but I believe with Add in the decisiontree class that is taken care of'''
    import pickle
    import BinaryTreeNode
    class DecisionTree(object):
        def __init__(self, category):
            outfile = open("database.txt", 'w')
            root = BinaryTreeNode(data, left, right)
            self.root = root
            currnode = self.root
            self.currnode = currnode
            category =
    '''I need some type of cursor, like currnode I think to start at root and then be implemented into a main program to move throughout the bst based on the user's answer. I'm not sure what to do with category or if it is even necessary. Or is it just something that is added by the user and doesn't need a category =?. Also, I know that this program needs to use pickle somewhere, but I'm not quite sure where... I think it is in the Add function."
        def Add(self, question, newcategory, answer):
            newcategory = BinaryTreeNode(data, left, right)
            self.currnode.data = question
            self.currnode.left = answer
            mything = self.currnode
            outfile = open("database.txt", 'w')
            pickle.dump(mything, outfile)
    '''This one I am not sure on, needs to add a question and answer if the user's answer is not in the game so next time they play the game gets better and better. I believe that the loop can be taken care of in the main program to get there, so I think you just create it here, but I don't really know and I'm not sure if this needs to return something. Once again, I don't know what to do with new category. All I could think of was that it creates a new binarytreenode.'''
        def Reset(self):
            return self.currnode = self.root
        ''' resets the cursor back to the top of the BST'''
        def GetCurrent(self):
            return self.current.data
    ''' I believe all it does is supposed to return the currnodes data it might need to print out the current data so in the main program it asks the question to the user'''
        def Movedown(self, direction):
            if direction == left:
                return self.currnode.left
            if direction == right:
                return self.currnode.right
    '''Needs to move the cursor down through the program. I think it just uses the left and right, I'd like to have it so if it's not left or right then it asks for it again. I think its like raiseexception or something but I'm not sure. And I'm not sure if I need the double =='''
        def Atleaf(self, answer):
            if answer == self.currnode.data:
               return true
            if answer == self.currnode.left:
               return true  
               return false
    '''Should return a boolean expression for the current leaf.'''

    import BinaryTreeNode
    import DecisionTree
    def main()
        ''' needs to take a file from a database and use that as a binarytree.  I believe the file is already established in the constructor of decisiontree so it doens't need to be established here'''
        print "Welcome to the 20 questions game.  You pick an object, and I will try to guess what it is.  You can help me get better at the game by giving me more information when I make a mistake."
        print "The more you play, the better I get."
        print "Think of an object and I will try to guess what it is."
       ''' need some type of loop here and I'm not sure how to start the program.'''
       while GetCurrent != none:
           print GetCurrent()
           questionanswer = raw_input
           answer = raw_input("Help me get better, what is your object?")
           Add(question, newcategory, answer)
        ''' I need it to be an if else look probably recursively, but I can't figure out how to do it.  This clearly wont work cause it isn't going to the end of the BST before it says it doesn't know the answer.'''      
        playagain = raw_input("Do you want to play again?")
        if playagain == "yes":
       ''' is there some type of code that closes the program?'''
        ''' need to get this loop so that when the left or right of the currnode is none that it asks, is your object a(n) ""? '''
    Last edited by Jimbo43; 2008-04-14 at 17:08. Reason: trying to get code embedded properly

  2. #2
    Regular Contributor
    Join Date
    Dec 2007

    Re: 20 questions game


    Its a nice idea, you though might now be caught in recursion as you say, you should add the functions that may be missing here, And how about posting the code properly embedded so that we can have a good look at it, although it seems more of symbian stuff to me.

    V 7.0642.0
    Nokia 7610

  3. #3
    Registered User
    Join Date
    Apr 2008

    Re: 20 questions game

    I confirmed it today, I have all the functions here that are needed to do the project they just aren't correct.

Similar Threads

  1. Question. Free memory in Game Loop(J2ME)
    By videador in forum Mobile Java General
    Replies: 5
    Last Post: 2009-08-24, 06:05
  2. Game programming questions
    By DantePink in forum Symbian
    Replies: 8
    Last Post: 2007-10-27, 11:41
  3. Great game Wappo
    By geonofear in forum News and Announcements
    Replies: 2
    Last Post: 2007-01-03, 12:00
  4. Making a game, few specific questions
    By lraoux in forum Symbian
    Replies: 3
    Last Post: 2006-11-23, 10:40
  5. Questions from an Independent Game Developer
    By rorygraves in forum Mobile Java Games
    Replies: 4
    Last Post: 2006-04-19, 19:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts