# Arup Guha
# 6/12/2018
# A program that implements the six questions activity from the 2018 Introductory Session for SI@UCF.
# You must have siucf2018.txt in the same directory as sixquestions.class for this to work.
# The program asks you yes/no questions and eventually figures out who you are!

'''
Input File Format:
   Each node is listed on two lines. First line is # of nodes in its subtree, including it,
   and the second line is the String for that node (either a question or the person).
   Nodes must be listed in the pre-order ordering of the question tree where left = no, right = yes
'''

def main():
    
    # Open the input file.
    myFile = open("siucf2019.txt", "r")

    # Store sizes of each subtree and the strings for each subtree.
    sizes = []
    strs = []

    # Add the root node.
    sizes.append(int(myFile.readline()))
    tmp = myFile.readline()
    strs.append(tmp[:len(tmp)-1])
    
    # Read in the rest of the nodes.
    for i in range(sizes[0]-1):
        sizes.append(int(myFile.readline()))
        tmp = myFile.readline()
        strs.append(tmp[:len(tmp)-1])
        
    # Process queries
    while True:

        # See if they want to play.
        ans = input("Do you want to play six questions?\n")
        if ans[0] == 'N' or ans[0] == 'n':
            break

        # Start my search at the root.
        curNode = 0

        # Go down the tree.
        while True:

            # Leaf node check.
            if sizes[curNode] == 1:
                print("Aha, I've figured it out! You must be "+strs[curNode]+".")
                break
            
            # This is the question at this node.
            print(strs[curNode])
            ans = input("")

            # Number of nodes to the left.
            leftSize = sizes[curNode+1]

            # Left is just the next node.
            if ans[0] == 'N' or ans[0] == 'n':
                curNode = curNode + 1

            # Here is where right goes.
            else:
                curNode = curNode + 1 + leftSize

# Start it.
main()
