In: Computer Science
In java, implement an animal guessing game. Start with a "Decision Tree", but present the leaves as “Is it a X?” If it wasn't, ask the user what the animal was, and ask for a question that is true for that animal but false for X. For example,
Is it a mammal? Y Does it have stripes? N Is it a pig? N I give up. What is it? A hamster Please give me a question that is true for a hamster and false for a pig. Is it small and cuddly?
In this way, the program learns additional facts.
Once complete, add to the program by writing the tree to a file when the program exits. Load the file when the program starts again.
BinaryTree.java
/**
* This class creates a Binary Tree that hold Nodes filled with a
String.
* *
*/
public class BinaryTree {
/**
* Default Constructor, sets root and current to
null.
*/
BinaryTree(){
root = null;
current = null;
}
/**
* Inserts String info into Binary Tree based on String
Move using a private
* recursive function.
* @param info Value to be inserted into Binary
Tree
* @param move Value that is considered in order to
properly place info
*/
void Insert(String info, String move){
root = InsertItem(root, info,
move);
}
/**
* Prints out the Binary Tree using private recursive
function
*/
void Print(){
PrintTree(root);
}
/**
* Evaluates whether or not Binary Tree is empty or
not
* @return trueOrfalse True if root equals null
*/
boolean isEmpty(){
if (root == null){
return
true;
}
else{
return false;
}
}
/**
* Evaluates whether or not current is located at a
ending leaf/child node
* @return trueOrfalse True if current has to nodes to
travel to
*/
boolean isAtEnd(){
if ((current!= null) &&
(current.left == null && current.right == null)){
return
true;
}
else{
return
false;
}
}
/**
* Returns the value of the current node
* @return current.data The string located in the
current node.
*/
String getCurrentData(){
return current.data;
}
/**
* Moves current pointer to the following right node,
signaling a YES
*/
void moveCurrentYes(){
current = current.right;
}
/**
* Moves current pointer to the following left node,
signaling a NO
*/
void moveCurrentNo(){
current = current.left;
}
/**
* Moves current to root, the starting point.
*/
void setCurrentToStart(){
current = root;
}
//Recursive Functions
/**
* Inserts String value into the appropriate
node.
* If newRoot is not fill then this method will fill it
recursively
* @param newRoot node that is considered for
insertion
* @param value String to be inserted into new
Node
* @param move String that determines where to place
other String values
* @return newRoot The node that was inserted into the
Binary Tree
*/
private Node InsertItem(Node newRoot, String value,
String move){
if (newRoot == null)
{
newRoot = new
Node(value);
//
System.out.println("ROOT INSERTED");
return
newRoot;
}
else if (move == "Y"){
// System.out.println("YES INSERTED");
newRoot.right =
InsertItem(newRoot.right, value, move);
}
else{
//
System.out.println("NO INSERTED");
newRoot.left =
InsertItem(newRoot.left, value, move);
}
return newRoot;
}
/**
* Prints the Binary Trees values in a preorder
traversal
* @param root Starting node of the tree
*/
private void PrintTree(Node root){
if (root != null){
System.out.println(root.data);
PrintTree(root.left);
PrintTree(root.right);
}
}
//Data Members
protected Node root; //Starting Point of Binary
Tree
protected Node current; //Current position
}
BinaryTree.java
import java.util.*;
/**
* This class creates a "Bot" that operates as the programs voice
and prompter.
* It creates a new Binary Tree and manipulates it using methods
belonging to the BinaryTree class.
* This "Bot" will ask the questions and insert answers into a
Binary Tree.
* It will keep guessing until it gives up or reaches a correct
answer.
* @
*
*/
public class Bot{
Scanner in = new Scanner(System.in); //Open input
stream
/**
* Default Constructor, creates a new Binary Tree
*/
Bot(){
GuessTree = new BinaryTree();
}
/**
* This method asks the user questions and will try to
guess what the animal is.
*/
void askQuestion(){
String animal;
String question;
char YorN;
if (GuessTree.isEmpty()){
/*TREE IS
EMPTY*/
giveUp();
//System.out.println(root.data);
}
else if
(GuessTree.isAtEnd()){
/*AT END
NODE*/
//System.out.println("END");
animal =
GuessTree.current.data;
question = "Is
it " + AorAn(animal) + " " + animal + "?";
//DoWhile loop
to check character entered and allow user to re-enter valid
arguments
do{
System.out.println(question);
YorN =
in.nextLine().toUpperCase().charAt(0);
if (YorN ==
'Y'){
System.out.println("I win!");
GuessTree.setCurrentToStart();
}
else if (YorN ==
'N'){
giveUp();
}
else{
System.out.println("Invalid Character. Try
Again.");
}}while(YorN !=
'Y' && YorN != 'N');
}
else{
/*ASKING
QUESTION*/
//System.out.println("ASKING");
question =
GuessTree.current.data;//Question Node
//DoWhile loop
to check character entered and allow user to re-enter valid
arguments
do{
System.out.println(question);
YorN =
in.nextLine().toUpperCase().charAt(0);
if (YorN ==
'Y'){
GuessTree.moveCurrentYes();
askQuestion();
}
else if (YorN ==
'N'){
if(GuessTree.current.left == null){
giveUp();
}
else{
GuessTree.moveCurrentNo();
askQuestion();
}
}
else{
System.out.println("Invalid Character. Try
Again.");
}
}while(YorN != 'Y' && YorN
!= 'N');
}
}
/**
* If the Bot cannot guess the correct animal, it will
give up and ask
* the user to input the animal and a question that
describes it.
*/
void giveUp(){
String oldAnimal; //guessed
Animal
String animal; //new Animal
String question; //Concatenated
String
String newQuestion; //new
Question
System.out.println("I give up. What
is it? ");
animal = in.nextLine();
question = "What question would
tell me it is "
+ AorAn(animal) + " " +
animal + "?";
System.out.println(question);
newQuestion = in.nextLine();
/*IF BINARY TREE IS EMPTY*/
if (GuessTree.isEmpty()){
//moves new
question to the right
GuessTree.Insert(newQuestion, "Y");
//moves new
animal to the right
GuessTree.Insert(animal, "Y");
GuessTree.setCurrentToStart();
}
/*IF CURRENT POINTER IS AT STARTING
NODE
*AND USER ENTERS NO FOR FIRST
QUESTION*/
else if( GuessTree.current.data ==
GuessTree.root.data){
//moves new
question to the left
GuessTree.Insert(newQuestion, "N");
//set current
pointer to the new question node
GuessTree.moveCurrentNo();
//moves new
animal to the right of new question
GuessTree.current.right = new Node(animal);
GuessTree.setCurrentToStart();
}
else{
/*IF NODE IS A
QUESTION*/
if(GuessTree.current.data.contains("?")){
//Create new left node
GuessTree.current.left = new
Node(newQuestion);
//Create new left.right node
GuessTree.current.left.right = new
Node(animal);
GuessTree.setCurrentToStart();
}
/*IF GUESSED
ANIMAL IS WRONG*/
else{
//oldAnimal is the animal guess by the Bot
oldAnimal = GuessTree.current.data;
//the new question replaces the oldAnimal
node
GuessTree.current.data = newQuestion;
//the oldAnimal is pushed to the left
GuessTree.current.left = new
Node(oldAnimal);
//the new correct animal placed to the
right
GuessTree.current.right = new
Node(animal);
GuessTree.setCurrentToStart();
}
}
// GuessTree.Print();
}
/**
* This method places correct grammatical structure, a
or an, in front of an animal string.
* @param animal The animal that needs to be
analyzed
* @return aOran an if the animal's first letter begins
with a vowel, a for anything else
*/
String AorAn(String animal){
Character firstLetter;
//Array of Vowels
ArrayList<Character> vowels =
new ArrayList<Character>();
vowels.add('A');
vowels.add('E');
vowels.add('I');
vowels.add('O');
vowels.add('U');
firstLetter =
animal.toUpperCase().charAt(0);
if
(vowels.contains(firstLetter)){
return
"an";
}
else{
return
"a";
}
}
BinaryTree GuessTree; //Binary Tree for storing
questions and answers
}
GameMain.java
import java.util.*;
/**
* This is the main class that instantiates the Bot class and begins
the guessing game.
* It will ask the user if he or she has thought of an animal.
* Also it will ask the user to play again after he or she finishes
a game.
* @
*
*/
public class GameMain {
/**
* Main that begins the guessing game
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method
stub
Bot GuessBot = new Bot();
char charChoice;//Y or N to start
game
char charEnd;//Y or N to play
again
Scanner in = new
Scanner(System.in);
System.out.println("Hello! I will
do my best to guess your animal!");
do{//DoWhile loop to check
character entered and allow user to re-enter valid arguments
System.out.println("Ok. Think of an
animal. Ready? ('y' to continue)");
charChoice =
in.nextLine().toUpperCase().charAt(0);
if (charChoice == 'Y'){
do{//Ask
questions
GuessBot.askQuestion();
do{//DoWhile
loop to check character entered and allow user to re-enter valid
arguments
System.out.println("Would you like to play again? (y/n)");
charEnd =
in.nextLine().toUpperCase().charAt(0);
if (charEnd !=
'Y' && charEnd != 'N'){
System.out.println("Invalid Character.");
}}while(charEnd
!= 'Y' && charEnd != 'N');
}while(charEnd
!= 'N');
}
else if(charChoice == 'N'){
System.out.println("I'll wait.");
}
else{
System.out.println("Invalid Character. Try Again.");
}
}while(charChoice != 'Y');
in.close(); //Closes input
stream
}
}
Node.java
/**
*
*/
/**
* This class creates a Node that holds a String and left,right
nodes.
* @author
*/
public class Node {
/**
* Default Constructor, sets left and right nodes to
null
*/
Node(){
left = null;
right = null;
}
/**
* Non-default Constuctor, sets left and right nodes to
null and sets String data to info
* @param info String value
*/
Node(String info){
left = null;
right = null;
data = info;
}
protected Node left;
protected Node right;
protected String data;
}