// Arup Guha
// 6/11/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
 ***/

import java.util.*;
import java.io.*;

public class sixquestions {

	public static void main(String[] args) throws Exception {

		// Open the input file.
		Scanner fin = new Scanner(new File("siucf2018.txt"));

		// Get the tree size.
		String strSize = fin.nextLine();
		int size = Integer.parseInt(strSize);

		// Now we know how big this is.
		String[] data = new String[2*size];
		data[0] = strSize;

		// Read in the rest of the data.
		for (int i=1; i<data.length; i++)
			data[i] = fin.nextLine();
		fin.close();

		// Form the whole tree.
		node root = new node(data, 0, data.length-1);

		Scanner stdin = new Scanner(System.in);

		// Keep on playing...
		while (true) {

			// See if they want to go again.
			System.out.println("Would you like to play six questions?");
			String ans = stdin.next();

			// Time to get out.
			if (ans.toLowerCase().charAt(0) == 'n') break;

			// Start at the root.
			node cur = root;
			while (true) {

				// We made it to a leaf node, we figured out who it is.
				if (cur.isLeaf()) {
					System.out.println("Aha, I've figured it out! You must be "+cur+".");
					break;
				}

				// If we're here, it's a question for the user. Get their answer.
				System.out.println(cur);
				ans = stdin.next();

				// Our file is stored with all no's to the left and all yes's to the right.
				if (ans.toLowerCase().charAt(0) == 'n')
					cur = cur.getLeft();
				else
					cur = cur.getRight();
			}
		}
	}
}

class node {

	private String item;
	private node left;
	private node right;

	public node(String[] list, int start, int end) {

		// The way I do it, we always want to do this.
		item = list[start];

		// Leaf node.
		if (Integer.parseInt(list[start]) == 1) {
			item = list[end];
			left = null;
			right = null;
		}

		// We have trees on both sides.
		else {

			// Extract out size of left subtree.
			int leftSize = Integer.parseInt(list[start+2]);

			// Each node has 2 items stored for it in the array.
			int midIndex = start + 1 + 2*leftSize;

			// Assign the question.
			item = list[start+1];

			// Recursively form left and right subtrees.
			left = new node(list, start+2, midIndex);
			right = new node(list, midIndex+1, end);
		}
	}

	// Returns true iff this node is a leaf node.
	public boolean isLeaf() {
		return left == null && right == null;
	}

	// Returns the String stored in this node.
	public String toString() {
		return item;
	}

	// Returns the left child.
	public node getLeft() {
		return left;
	}

	// Returns the right child.
	public node getRight() {
		return right;
	}
}