-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuestionTree.java
114 lines (95 loc) · 3.88 KB
/
QuestionTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// February 28, 2017
// The QuestionTree class creates a game similar to twenty questions.
// If the user chooses to read in the questions/answers from a file,
// then the questions and answers will be added to the game. If not, then
// the user is asked if an object is his/her guess. If the object is not
// the what the user guessed, then the user is asked to add a new question
// and their object to the game. Once the user is done playing, all of the
// newly added questions and answers provided by the user will be read back
// into the text file to expand the number of questions and answers accessible
// to the game.
import java.util.*;
import java.io.*;
public class QuestionTree {
private Scanner console;
private QuestionNode superRoot;
// Constructs a new game with one initial answer.
public QuestionTree() {
console = new Scanner(System.in);
superRoot = new QuestionNode("computer");
}
public void read(Scanner input) {
superRoot = readHelper(input, superRoot);
}
private QuestionNode readHelper(Scanner input, QuestionNode root) {
String nodeType = input.nextLine();
String text = input.nextLine();
if (nodeType.equals("A:")) {
return new QuestionNode(text);
}
root = new QuestionNode(text);
root.left = readHelper(input, root.left);
root.right = readHelper(input, root.right);
return root;
}
public void write(PrintStream output) {
writeHelper(output, superRoot);
}
private void writeHelper(PrintStream output, QuestionNode currentRoot) {
if (currentRoot.left == null && currentRoot.right == null) {
output.println("A:");
output.println(currentRoot.data);
} else {
output.println("Q:");
output.println(currentRoot.data);
writeHelper(output, currentRoot.left);
writeHelper(output, currentRoot.right);
}
}
public void askQuestions() {
superRoot = askQuestionsHelper(superRoot);
}
private QuestionNode askQuestionsHelper(QuestionNode root) {
if (root.left == null && root.right == null) {
System.out.print("Would your object happen to be ");
boolean userObject = yesTo(root.data + "?");
if (userObject) {
System.out.println("Great, I got it right!");
} else {
System.out.print("What is the name of your object? ");
String userAnswer = console.nextLine().toLowerCase();
System.out.println("Please give me a yes/no question that");
System.out.println("distinguishes between your object");
System.out.print("and mine--> ");
String userQuestion = console.nextLine();
boolean yesOrNoAnswer = yesTo("And what is the answer for your object?");
root = addUserObject(root, userAnswer, userQuestion, yesOrNoAnswer);
}
} else {
boolean userResponse = yesTo(root.data);
if (userResponse) {
root.left = askQuestionsHelper(root.left);
} else {
root.right = askQuestionsHelper(root.right);
}
}
return root;
}
private QuestionNode addUserObject(QuestionNode root, String userAnswer, String userQuestion,
boolean yesOrNoAnswer) {
if (yesOrNoAnswer) {
return new QuestionNode(userQuestion, new QuestionNode(userAnswer), root);
}
return new QuestionNode(userQuestion, root, new QuestionNode(userAnswer));
}
public boolean yesTo(String prompt) {
System.out.print(prompt + " (y/n)? ");
String response = console.nextLine().trim().toLowerCase();
while (!response.equals("y") && !response.equals("n")) {
System.out.println("Please answer y or n.");
System.out.print(prompt + " (y/n)?");
response = console.nextLine().trim().toLowerCase();
}
return response.equals("y");
}
}