Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a guessing game called “Does It Have Legs?”, in which you think of an ani

ID: 3662537 • Letter: C

Question

Create a guessing game called “Does It Have Legs?”, in which you think of an animal
and the computer tries to guess what it is. The game continually becomes “smarter”
as you play because you teach it how to become more specific with its questions.
For example, a typical game session might proceed as follows:
Computer: Does the animal you are thinking of have legs?
Player: Yes.
Computer: Is it a dog?
Player: Yes.
Computer: Yay! I win. Do you want to play again?
Player: Yes.
Computer: Does the animal you are thinking of have legs?
Player: Yes.
Computer: Is it a dog?
Player: No.
Computer: I give up. What was your animal?
Player: Horse.
Computer: Type a question for which the answer is Yes for Dog but No for Horse.
Player: Does it live in the house?
Computer: Do you want to play again?
Player: Yes.
Computer: Does the animal you are thinking of have legs?
Player: Yes.

Computer: Does it live in the house?
Player: No.
Computer: Is it a Horse?
Player: No.
Computer: I give up. What was your animal?
Player: Cow.
Computer: Type a question for which the answer is Yes for Horse but No for Cow.
Player: Do people ride it?
Computer: Do you want to play again?
Player: Yes.
Computer: Does the animal you are thinking of have legs?
Player: Yes.
Computer: Does it live in the house?
Player: No.
Computer: Do people ride it?
Player: No.
Computer: Is it a Cow?
Player: Yes.
Computer: Yay! I win. Do you want to play again?
Player: No.
(Hint: You might want to look up “binary trees” on the Internet as a method to
organize this application.)
Save the application as DoesItHaveLegs.java.

Explanation / Answer

As per given problem statement i have written the methods to guess the animal type by asking questions from the user. To Implement this we need to use the trees concept. Used the Binary Tree so that it can traverse the both the sides of the legs..see the below code ..

import java.util.*;

import java.io.*;

public void animalGame()

{

treeNoderoot=new treeNode("cat",0,0,0);

treeNode *current=root;

System.out.println("Let's play guess the animal.");

while(current!=0){

System.out.println("current.value");

if(answer())

current = current.leftChild;

else

current = current.rightChild;}

else {

System.out.println("I know. Is it a " + current.value + "?");

if(answer())

System.out.println("I won.");

else{

learnNewAnimal(current);

}

System.out.println("Try again?");

if(answer())

current = root;

else

return;}

}

}

public void animalGame()

{

treeNoderoot=new treeNode("snake",0,0,0);

treeNode *current=root;

System.out.println("Let's play guess the animal.");

while(current!=0){

System.out.println("Does it have leg");

if(answer())

current = current.rightChild;

else

current = current.leftChild;}

else {

System.out.println("I know. Is it a " + current.value + "?");

if(answer())

System.out.println("I won.");

else{

learnNewAnimal(current);

}

System.out.println("Try again?");

if(answer())

current = root;

else

return;}

}

}

public void animalGame()

{

treeNoderoot=new treeNode("earthworm",0,0,0);

treeNode *current=root;

System.out.println("Think of an animal and I will guess it.");

while(current!=0){

System.out.println("Does it have legs?");

if(answer())

current = current.rightChild;

else

current = current.leftChild;}

else {

System.out.println("Is it a snake?");

if(answer())

System.out.println("I won.");

else{

learnNewAnimal(current);

}

System.out.println("I give up what is it?");
string ew = scan.nextstring();

System.out.println("Please type a question whose answer is yes for an earthworm and no for a snake:);
if(answer())

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Does it live underground?);
if(answer())

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Try again?");

if(answer())

current = root;

else

return;}

}

}
public void animalGame()

{

treeNoderoot=new treeNode("fish",0,0,0);

treeNode *current=root;

System.out.println("Think of an animal and I will guess it.");

while(current!=0){

System.out.println("Does it have legs?");

if(answer())

current = current.rightChild;

else

current = current.leftChild;}

else {

System.out.println("Is it a snake?");

if(answer())

System.out.println("I won.");

else{

learnNewAnimal(current);

}

System.out.println("I give up what is it?");
string fish = scan.nextstring();

System.out.println("Please type a question whose answer is yes for an fish and no for a snake:);
if(answer())

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Does it live in water?);
if(answer())

current = current.rightChild;

else

current = current.leftChild;}

System.out.println("Try again?");

if(answer())

current = root;

else

return;}

}

}

public boolean answer(){

while(1){

string ans;

ans = Simple().readLine();

if(ans|0|=='Y)

return true;

else if(ans|0|=='N')

return false;

System.out.println("please answer yes or no. (Y/N)");

}

}

public void learnNewAnimal(treeNode current) {

string currentAnimal - current.value;

System.out.println("What is your animal?");

strint newAnimal;

newAnimal = SimpleIO.readLine()

System.out.println("What is a Y/N question that I can use to tell a" + current.value + "from a " + newAnimal + "?");

string newQuestion;

treeNodenode1 = new treeNode(newAnimal, current, 0,0);

treeNodenode2 = new treeNode(currentAnimal, current, 0,0);

newQuestion = SimpleIO.readLine();

System.out.println("For a " + newAnimal + " is the answer Y/N?");

if(answer()){

Current.leftChild = node1;

Current.rightChild = node2;

}

else{

Current.leftChild = node2;

Current.rightChild = node1;

}

Current.value = newQuestion;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote