Guessing Game – v1.0

This assignment was pretty exciting for me. We were asked to create a 20 Questions-like game, where the program could guess what object you were thinking of, using a binary tree.


Binary Trees

My teacher introduced me to a new concept called a binary tree. A binary tree consists of nodes, and each node contains a left and right child. For example, to the right, node A has node B as its left child and node C as its right child. This data structure is infinitely expandable and can hold a wide variety of data types.

Saving & Reading Data

This was definitely the toughest part of the project - saving the data in a plain text file. Using recursion, I was able to output the binary nodes relatively easily, using Preorder. This means that the nodes are printed out in this order: parent, left, right. For example, if you had "A" as the parent node, and "B" and "C" as its left and right nodes respectively, the output would be ABC. I used this method to print to a text file using Java's BufferedWriter.

Loading a tree from that outputted string was the toughest part of the project for me. First I tried to load each deleting the line from the file after I accessed it. That way, the top line of the file would always be the name of the node that the method is accessing. However, loading the file into an ArrayList of Strings gave me greater flexibility, as I can easily remove the first entry of the list.

Guessing

Creating this part was the easiest part of the project. The program asks a question based on the first node, then takes in user input (yes/no). If the answer is yes, the program moves on to the right node, and to the left node if the answer is no. Then the process is repeated, until the program reaches a node that has no children. If the node has no children, the program has narrowed its objects down enough to make a guess.

If the guess is correct, then, hooray! The program worked as intended. If the guess is incorrect, it will ask the user what object he/she was thinking of. A follow-up question requests the user to input a difference between this new object, and the object that the program guessed.

This whole process actually makes the Guessing Game a learning program. If it does not guess your answer, it asks the user for input and learns about new objects. Then, it will save the data to a file (using the system I mentioned earlier). The next time the user runs the game, it will be able to guess the object that the user taught it.

Room For Improvement

I believe that this project would benefit greatly from a GUI. Although it could not draw the entire binary tree due to its theoretically infinite size, the program could show, say, the next two or three levels down from the node that it is currently at. This would help the user see what the program is thinking.


This project was exciting for me, as it's my first program that actually learns from a user's input. I'm excited to continue to create programs that can learn and improve on their own.

Comments

Popular updates

Half Moon Project Part 2 - More Accuracy

NumberVision – Machine Learning Application

Machine Learning & Half Moon Project