-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwentyQuestions.java
More file actions
251 lines (221 loc) · 8.18 KB
/
TwentyQuestions.java
File metadata and controls
251 lines (221 loc) · 8.18 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Play the game of 20 questions.
* @author Stina Bridgeman
* @author Max Brodheim
* @author Erika Cardenas
* @author Camera Finn
*/
public class TwentyQuestions {
/**
* Play a game of 20 Questions, and edits the tree if unsuccessful.
*
* @param tree
* the 20 Questions tree
* @param scanner
* the scanner to read user input from
* @preconditions: tree and scanner cannot be null
* @postcondition: the tree is updated to included more information (user
* input is not forgotten)
*/
public static void play ( BinaryTree tree, Scanner scanner ) {
assert tree != null && scanner != null;
Node runner = tree.getRoot();
while ( true ) { // keeps the game running
if ( tree.isLeaf(runner) ) { // guessing an answer
System.out.println("You're thinking of a " + runner.getElement()
+ ", right?");
if ( scanner.nextLine().toLowerCase().equals("yes") ) { // if answer is
// yes
// computer guessed correctly, ends game
System.out.println("I guessed it!");
break;
}
else { // if answer is no
// Gathers information:
System.out.println("What were you thinking of? ");
String actualAns = scanner.nextLine(); // stores the users answer
System.out.println("Enter a question that is 'yes' for " + actualAns
+ " and 'no' for " + runner.getElement()); // queries for question
String newQues = scanner.nextLine(); // stores new question
// manipulate tree to include new info
tree.expandLeaf(runner);
tree.setElement(tree.getLeftChild(runner),actualAns);
tree.setElement(tree.getRightChild(runner),newQues);
tree.swapElements(runner,tree.getRightChild(runner));
break; // ends play loop
}
} else { // asking a question
System.out.println(runner.getElement());
String response = scanner.nextLine().toLowerCase();
if ( response.equals("yes") ) {// if answer is yes
runner = tree.getLeftChild(runner); // restarts loop with appropriate
// response for next node
}
else if ( response.equals("no") ) {// answer is no
runner = tree.getRightChild(runner); // restarts loop with appropriate
// response for next node
} else {
System.out.println("please enter only 'yes' or 'no' "); // handles bad
// input
}
}
}
}
/**
* Save the tree to a file.
*
* @param tree
* tree to save
* @param filename
* name of file to save the tree in
* @param node
* the root of the tree to be saved
* @throws IOException
* if there is an error opening the file or writing the tree
* @precondition: file is writeable, assured through usage of method in main
* @postcondition: writes File filename to store tree data and structure,
* PrintWriter is closed.
*/
public static void save ( BinaryTree tree, String filename )
throws IOException {
PrintWriter output = new PrintWriter(new FileWriter(filename)); // writer
// for
// writing
// to file
save(tree,tree.getRoot(),output);
output.close(); // close output
}
/**
* Uses recursion to save a tree to a file in a specific structure, must be
* used in conjunction with a public method that closes the output stream.
*
* @param tree
* BinaryTree that is read from
* @param node
* current location in the reading process
* @param output
* PrintWriter being used to write to file
* @precondition: node cannot be null, which means tree can't be null. output
* cannot be null.
* @postcondition: file is updated, but the output is not closed.
*/
private static void save ( BinaryTree tree, Node node, PrintWriter output ) {
if ( tree.isInternal(node) ) { // if the node is a question:
output.print("[Q]");
output.println(node.getElement()); // prints appropriate data
save(tree,tree.getLeftChild(node),output); // recurses on children
save(tree,tree.getRightChild(node),output);
} else { // if node is an answer. this is the base case.
output.print("[A]");
output.println(node.getElement());
}
}
/**
* Load the tree from a file.
*
* @param filename
* name of file to load from, with full filepath and file type if appropriate
* @return the loaded tree
* @throws IOException
* if there is an error opening the file or reading the tree
* @precondition: filename must not be null or empty string, and must have file handle.
* @postcondition: creates tree with data from File filename with proper structure.
*/
public static BinaryTree load ( String filename ) throws IOException {
Scanner scanner = new Scanner(new FileReader(filename)); //scanner to read file
BinaryTree tree = new BinaryTree(); //tree to have information added too
tree = loadHelper(tree,tree.getRoot(),scanner); //calls helper
scanner.close(); //closes input
return tree;
}
/**
* helper method for load, used to fill in the BinaryTree with recursion
* @param tree
* BinaryTree being written to
* @param node
* current node in the writing process
* @param scanner
* scanner associated with the correct file, used to read file
* @return tree with data in the correct spot
* @precondition: nothing can be null, however this is assured by usage in load().
* @postcondition: tree with correct information in correct locations
*/
private static BinaryTree loadHelper ( BinaryTree tree, Node node,
Scanner scanner ) {
String next = scanner.nextLine(); //stores the next line
tree.setElement(node,next); //sets the node's element to the stored line
if ( node.getElement().charAt(1) == 'Q' ) { //tests if the node should be internal
tree.expandLeaf(node); //adds two null children
loadHelper(tree,tree.getLeftChild(node),scanner); //recurses for children
loadHelper(tree,tree.getRightChild(node),scanner);
}
//removes the [Q] or [A] from the string, to keep the tree neat and properly formatted
String editted = node.getElement();
editted = editted.substring(3,editted.length());
tree.setElement(node,editted);
return tree;
}
/**
* Print the tree.
*
* @param tree
* the tree to print
*/
public static void print ( BinaryTree tree ) {
print(tree,tree.getRoot(),"");
}
/**
* Print the subtree rooted at the specified node.
*
* @param tree
* the tree to print
* @param node
* the root of the subtree to print
*/
private static void print ( BinaryTree tree, Node node, String indent ) {
System.out.println(indent + node.getElement());
if ( tree.isInternal(node) ) {
print(tree,tree.getLeftChild(node),indent + " ");
print(tree,tree.getRightChild(node),indent + " ");
}
}
public static void main ( String[] args ) {
Scanner scanner = new Scanner(System.in);
BinaryTree gametree = new BinaryTree("duck");
for ( ; true ; ) {
System.out.print("play, print tree, save, load, or quit? [p/t/s/l/q] ");
char choice = scanner.nextLine().charAt(0);
System.out.println();
if ( choice == 'q' ) {
System.out.println("goodbye");
break;
} else if ( choice == 't' ) {
print(gametree);
} else if ( choice == 's' ) {
try {
System.out.print("enter filename to save in: ");
String filename = scanner.nextLine();
save(gametree,filename);
} catch ( IOException e ) {
System.out.println("error saving");
}
} else if ( choice == 'l' ) {
try {
System.out.print("enter filename to load from: ");
String filename = scanner.nextLine();
gametree = load(filename);
} catch ( IOException e ) {
System.out.println("error loading file");
}
} else if ( choice == 'p' ) {
play(gametree,scanner);
}
System.out.println();
}
}
}