Animal Guessing Game based on Decision Trees.
In this assignment you will implement a decision tree that learns while playing. This assignment is based on N. Howe's Decision Tree assignment.
Your output will look something like this; with the ?>
sequence indicating where the user was asked to type.
Think of an animal.
I'll try to guess it.
Is your animal a Mouse?
?> no
I got it wrong.
Please help me to learn.
What was your animal?
?> Snake
Type a yes or no question that would distinguish between a Crocodile and a Mouse
?> Is it a mammal?
Would you answer yes to this question for the Crocodile?
?> no
Play again?
?> yes
Think of an animal.
I''ll try to guess it.
Is it a mammal?
?> no
Is your animal a Snake?
?> yes
I guessed it!
Play again?
?> no
Your program should try to guess the animal by asking the questions in a stored decision tree, and following the appropriate branch for the answer given by the user. The leaves will contain an animal to guess. If the system guesses wrong, it will ask the user to add a question distinguishing the wrong guess from the correct answer. This question will be used to extend the decision tree.
Using the provided TreeIO
class, you can load
and save
your decision tree to a file,
so that questions added during one session can be saved for later.
Using the provided TextInput
class, you can ask a yes/no question with confirm
and get a line of text from the user with readLine
.
The core adds to 90 points, and there are two 10-point challenges.
There's a Google Form once again for submitting what you accomplished.
It is very important your code compiles and you do not change the directory structure. If you have issues importing the code into Eclipse, let me know and we can fix it together. I plan to grade these as automatically as possible.
This is a guided tree-traversal. The TextInput
object, e.g., input.confirm("question?")
will be of great help to you.
Welcome to the guessing game!
To play, first think of an animal.
Is it a mammal? (y/n): y
Is it a Mouse? (y/n): n
You win!
Do you want to play again? (y/n): y
To play, first think of an animal.
Is it a mammal? (y/n): n
Is it a bird? (y/n): n
Is it a Snake? (y/n): y
I win!
Do you want to play again? (y/n):
This can be done with a loop, OR with recursion.
Welcome to the guessing game!
To play, first think of an animal.
Is it a mammal? (y/n): y
Is it a Mouse? (y/n): n
You win!
...
What animal were you thinking of?
Elephant
What is a question that would be YES for a Mouse but not a Elephant?
Is it really small?
Do you want to play again? (y/n): y
To play, first think of an animal.
Is it a mammal? (y/n): y
Is it really small? (y/n): y
Is it a Mouse? (y/n): y
I win!
Do you want to play again? (y/n): y
To play, first think of an animal.
Is it a mammal? (y/n): y
Is it really small? (y/n): n
Is it a Elephant? (y/n): y
I win!
Do you want to play again? (y/n):
- In order to update the tree, you will need to ask for a new question, and a new animal.
- Add a method with the signature
public String getNotEmptyInput(String question)
to theTextInput
class. It will call thereadLine
method, but make sure they typed something and didn't just accidentally press ENTER. - Once you find the
current
node that the game thought was the answer; you can use two strategies to expand the tree:- You can keep track of the
parent
of that node so you can insert a new node in eitheryes
orno
wherecurrent
used to be. (Harder in the recursive solution. - You can move the animal in
current
to a new node and then turn the original into a question.
- You can keep track of the
Most of the hard work is done for you in the TreeIO
class. Saving and loading a DecisionTree is a recursive algorithm.
- Modify the code to use the provided
knowledge
variable if it cannot find theknowledge.dat
file. To check if the file exists, create a newjava.io.File
object, and callexists
on it; otherwiseTreeIO.load
theknowledge.dat
file into theknowledge variable
. - Modify the code to save the data every time after the user plays the game.
- Modify your playing to return a boolean;
true
if the computer wins, anfalse
if the player wins. If the player wins, the data changes; only save the data to theknowledge.dat
file if it has changed.
If you implemented it with loops, also implement it with recursion. If you implemented it first with recursion, also build the loop based version. You will be able to reuse most of the logic you've designed.
In HTML, you get to define lists as follows:
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
Make a new class that TreeIO.load
the data from the game, and prints it out in the following style; indentation/spacing doesn't matter so much (but is worth bonus points if you get it nice).
Is it a mammal?
<ul>
<li>YES: Is it tiny?
<ul>
<li>YES: ANIMAL: Mouse</li>
<li>NO: ANIMAL: Elephant</li>
</ul>
</li>
<li>NO: Is it a bird?
<ul>
<li>YES: Is it nocturnal?
<ul>
<li>YES: ANIMAL: Owl</li>
<li>NO: ANIMAL: Chicken</li>
</ul>
</li>
<li>NO: ANIMAL: Snake</li>
</ul>
</li>
</ul>
You can then open and look at the knowledge the game has learned in your browser!