-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuesserLearner.java
80 lines (73 loc) · 2.52 KB
/
GuesserLearner.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
import java.io.*;
/**
* Write a description of class GuesserLearner here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GuesserLearner {
public static void main (String [] args) { // check for valid node before createing new tree
MyTree guesserGame;
DecisionTreeNode root = deserializeTree(MyTree.knowledgeBase);
if (root == null) {
guesserGame = new MyTree();
serializeTree(MyTree.knowledgeBase, guesserGame);
} else {
guesserGame = new MyTree(root);
serializeTree(MyTree.knowledgeBase, guesserGame);
}
/*while(true) {
if(UserInterface.playAgain()) {
guesserGame = new MyTree(deserializeTree(MyTree.knowledgeBase));
serializeTree(MyTree.knowledgeBase, guesserGame);
} else {
System.exit(0);
}
}*/
}
public static void serializeTree(File outFile, MyTree newTree) {
try {
OutputStream output = new FileOutputStream(outFile);
OutputStream fileBuffer = new BufferedOutputStream(output);
ObjectOutput objectOutput = new ObjectOutputStream(fileBuffer);
try {
objectOutput.writeObject( newTree.root);
} finally { // change this
objectOutput.close();
}
} catch (IOException e ) {
System.out.println("Unable to serialize object");
e.printStackTrace();
}
}
/**
* Reads the data tree from the file.
*
* @param inFile is the file to read from.
*
* @return the tree root.
*/
public static DecisionTreeNode deserializeTree(File inputFile) {
try {
ObjectInput input = new ObjectInputStream(
new BufferedInputStream( new FileInputStream( inputFile ) )
);
try {
@SuppressWarnings( "unchecked" )
DecisionTreeNode root = (DecisionTreeNode)input.readObject();
return root;
}
finally {
input.close();
}
}
catch (java.io.FileNotFoundException e) { }
catch (ClassNotFoundException ex) {
System.err.println("Unsuccessful deserialization: Class not found. " + ex);
}
catch (IOException ex) {
System.err.println("Unsuccessful deserialization: " + ex);
}
return null;
}
}