Create a guessing game called \"Does It Have Legs?\" in which you think of an an
ID: 3631610 • 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: 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 and 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
Explanation / Answer
import java.util.*;
import java.io.*;
public void animalGame()
{
treeNode<string>root=new treeNode<string>("cat",0,0,0);
treeNode<string> *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 bool 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<string> 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;
treeNode<string>node1 = new treeNode<string>(newAnimal, current, 0,0);
treeNode<string>node2 = new treeNode<string>(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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.