-
-
Save samuelcouch/e831bf7ba55a377e8b59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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