Created
August 18, 2016 22:45
-
-
Save ryandhubbard/f44fb476d990af5b7bee507624b89b19 to your computer and use it in GitHub Desktop.
Java KnowledgeTree
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
Animal | |
dog |
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.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class KnowledgeTree { | |
private String topicofthegame; | |
private Node treebase; | |
public KnowledgeTree(BufferedReader file) throws IOException { | |
topicofthegame = file.readLine(); | |
treebase = readTree(file); | |
} | |
private static BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); | |
private static Node readTree(BufferedReader inputfile) throws IOException { | |
boolean isQuestion = false; | |
String contents = inputfile.readLine(); | |
if (isQuestion) { | |
Node No = readTree(inputfile); | |
Node Yes = readTree(inputfile); | |
return new Node(contents, No, Yes); | |
} | |
else | |
return new Node(contents); | |
} | |
public void startTree() throws IOException { | |
Node currentsubject = treebase; | |
System.out.println("\nThink of an " + topicofthegame+" and I will guess"); | |
if (! promptUserYesorNo("Did you think of an " + topicofthegame)) | |
return; | |
while (currentsubject.Question()) { | |
if (promptUserYesorNo(currentsubject.getQuestion())) | |
currentsubject = currentsubject.answerYes(); | |
else | |
currentsubject = currentsubject.answerNo(); | |
} | |
if (! promptUserYesorNo("Is it a " + currentsubject.getGuess())) { | |
String AnswerfromUser, QuestionfromUser; | |
System.out.print("What type of " + topicofthegame + " Did you guess: "); | |
AnswerfromUser = userInput.readLine(); | |
System.out.println("Please type a question whose answer is Yes for an " + AnswerfromUser + " and No for a " + currentsubject.getGuess()); | |
QuestionfromUser = userInput.readLine(); | |
if (promptUserYesorNo("Is " + AnswerfromUser + " Your answer?")) | |
currentsubject.Question(QuestionfromUser, new Node(currentsubject.getGuess()), new Node(AnswerfromUser)); | |
else | |
currentsubject.Question(QuestionfromUser, new Node(AnswerfromUser), new Node(currentsubject.getGuess())); | |
} | |
} | |
private static boolean promptUserYesorNo(String question) throws IOException { | |
String answer; | |
do { | |
System.out.print(question + "? "); | |
answer = userInput.readLine(); | |
if (answer.equals("yes".substring(0, answer.length()))) | |
return true; | |
else if (answer.equals("no".substring(0, answer.length()))) | |
return false; | |
else | |
System.out.println("Please type only yes or no"); | |
} | |
while (true); | |
} | |
public static void main(String[] args) throws IOException { | |
String filename = "P7Test.txt"; | |
BufferedReader Subjectinput = new BufferedReader(new FileReader(filename)); | |
KnowledgeTree Tree = new KnowledgeTree(Subjectinput); | |
Subjectinput.close(); | |
do { | |
Tree.startTree(); | |
} | |
while (promptUserYesorNo("Do you want to continue")); | |
System.out.println("Thank you and Goodbye"); | |
System.exit(0); | |
} | |
} |
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
public class Node { | |
private Node leftyesChild, rightnoChild; | |
private String contents; | |
private boolean Question; | |
void Question(String question, Node No, Node Yes) | |
{ | |
leftyesChild = Yes; | |
rightnoChild = No; | |
Question = true; | |
contents = question; | |
} | |
Node(String question, Node No, Node Yes) | |
{ | |
this.leftyesChild = Yes; | |
this.rightnoChild = No; | |
Question = true; | |
contents = question; | |
} | |
Node(String guess) | |
{ | |
leftyesChild = null; | |
rightnoChild = null; | |
Question = false; | |
contents = guess; | |
} | |
boolean Question() | |
{ | |
return Question; | |
} | |
String getQuestion() | |
{ | |
return contents; | |
} | |
Node answerYes() | |
{ | |
return leftyesChild; | |
} | |
Node answerNo() | |
{ | |
return rightnoChild; | |
} | |
String getGuess() | |
{ | |
return contents; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment