Skip to content

Instantly share code, notes, and snippets.

Created April 11, 2015 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/e115cd89a9b3291ee584 to your computer and use it in GitHub Desktop.
Save anonymous/e115cd89a9b3291ee584 to your computer and use it in GitHub Desktop.
Anymall Guesser
import java.util.Scanner;
class Animal{
String Question;
Animal Left;
Animal Right;
Animal (String Question, Animal Left, Animal Right){
this.Question = Question;
this.Left = Left;
this.Right = Right;
}
public void setQuestion (String theQuestion){
Question = theQuestion;
}
public void setLeft (Animal theLeft){
Left = theLeft;
}
public void setRight (Animal theRight){
Right = theRight;
}
public String getQuestion(){
return Question;
}
public Animal getLeft(){
return Left;
}
public Animal getRight(){
return Right;
}
public boolean isTerminal(){
boolean isTerminal = true;
if((Left != null) || (Right != null)){
isTerminal = false;
}
return isTerminal;
}
}
public class AnimalGameTree {
public Scanner kb = new Scanner (System.in);
Animal root;
public AnimalGameTree (){
// root.Question = "Is it a Dog? ";
// root.Left = null;
// root.Right = null;
root = new Animal("Dog",null,null);
}
public boolean Play(){
Animal where = root;
while(!where.isTerminal()){
System.out.print(root.Question);
String Answer = kb.next();
if(Answer.equalsIgnoreCase("yes")){
where = where.Right;
}else{
where = where.Left;
}
}
System.out.println("Think of an animal!");
System.out.print("Are you thinking of a " + where.Question + "? ");
String Ans = kb.next();
if (!Ans.equalsIgnoreCase("yes")){
System.out.print("What is your animal? ");
String Resp = kb.next();
System.out.println("Give me a question that is true about " + Resp + " and false about " + where.Question);
String Question = kb.next();
where.Left = new Animal(Resp, null, null);
where.Right = new Animal(where.getQuestion(), null, null);
where.setQuestion(Question);
}
System.out.println("Thanks for playing!");
System.out.print("Do you want to play again? ");
String Reply = kb.next();
if (!Reply.equalsIgnoreCase("yes")){
return true;
}
return false;
}
public static void main (String [] args){
AnimalGameTree game = new AnimalGameTree();
boolean done = false;
System.out.println("Welcome!");
while (!done){
done = game.Play();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment