Game 20 questions java code




















We define a loadTree method taking in parameter the filename of a raw file containing Decision Tree data. We open the file and we call a buildTree method with the root in parameter and the BufferedReader instance used to read the file line by line. When we read a line, we make the following actions :. Q:Is it white?

Q:Is it a male? Q:Is it yellow? Q:Is it related to the cat family? Q:Can it be all animals? Q:Are you going to click yes for the last question? Q:Can it do everything?

Our UI will have a text area for displaying questions to the user and three buttons :. You can note that our Decision Tree data are centered on animals. So, we ask to the user to think only to an animal. The three buttons are represented via the JButton class. So, we have a Decision Tree implementation, some data and a User Interface. Now, we need to implement interaction with the player during the Twenty Questions game. We add an ActionListener instance to the three buttons for that.

We define three methods :. We assemble our game in a TwentyQuestions class. We define a currentNode property to keep a pointer of our position in the Decision Tree.

This step is implemented in the restart method. When the yes method is called, we know that the user has clicked on yes for the current question or answer displayed. So, we need to follow the yes path of our current node. If the new current node is a question, we display the data contained within it on the screen. Otherwise, we display a message to the user if he thought to the answer present in the current node. The logic is the same for the no method.

At the end, the computer finds our animal and asks us the following question:. If you want to discover some books to learn Java programming, I advice you to read the following article with my selection of the Top 6 Best Books for Java programming:. Your email address will not be published.

It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search. Ever since childhood, I've wondered how the 20Q electronic game worked. You think of an object, thing, or animal e. The device then asks you a series of questions such as:.

For each question, you can answer yes , no , maybe , or unknown. I always imagined it work with immense, nested conditionals if -statements. However, I think that's an unlikely explanation because of its complexity for the programmer. I don't know how 20Q did it specifically, but there is plenty of information on how to implement a game of 20 questions.

There are lots of ways of solving this, but I'll describe one way. These games can implement some sort of decision tree. For an electronic game like 20Q, this tree would be precomputed and fairly easy to traverse.

There are methods for using learning decision trees where the game can accept new objects at the end of it's questions if it's unable to guess what the user is asking. When the questions are a series of yes or no answers, you end up with a binary tree. Each node is a question and the leaves are answers. When questions are answered with unknown or not sure, the child nodes can be combined and their questions asked in series to further cull the possible answers.

Generating the tree is probably the topic of another question. But basically it's choosing questions that split the answers as much as possible. Put the questions that divide the questions most equally near the beginning so that the most number of questions can be culled the fastest. At 20Q. The handheld toy uses the same neural network algorithms.

The neural network picks the questions to ask as well as making guesses. This approach means that the A. Another advantage is that the game will ask questions differently every game even if you are thinking of the same thing. The algorithms and the neural network of the classic english game Animal, Vegetable, Mineral was created in by Robin Burgener.

Here is a quick overview of the code I linked: There are several different answers hard coded into the program. The program looks at the definition of the appropriate category and tracks which animal is most likely the one you are thinking of based on the TRUE or FALSE values and your inputed Yes or No answer to the question.

Robin Burgener, the inventor, completely documented his algorithm in his patent filing. It's ingeniously simple. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. How can I implement a "20 Questions" algorithm? Ask Question. Asked 9 years, 3 months ago.

Active 6 years, 5 months ago. Viewed 21k times.



0コメント

  • 1000 / 1000