import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Assignment1 extends AMVsetup implements Serializable {
private static final long serialVersionUID = 1L;
static class Node implements Serializable {
private static final long serialVersionUID = 6265978352799255699L;
public String question;
public Node True;
public String name;
public Node False;
public int parentID;
public int ID;
public char fromWhichBranch;
public char questionAnswer;
public void branch() {
if(question != null) {
input = terminal.readString(question + "\n");
if (input.contains("true"))
True.branch();
else
False.branch();
}
else if(question == null) {
input = terminal.readString("Is it a/an " + name + "?\n");
if(input.contains("true"))
{ terminal.println("I win!"); }
else {
terminal.println("I give up.\n");
newName = terminal.readString("What were you thinking of?\n");
newQuestion = terminal.readString("What is a question that you " + "would answer no to " + newName + ", but " + "yes to " + name + "?\n");
True = new Node();
True.name = name;
False = new Node();
False.name = newName;
name = null;
question = newQuestion;
terminal.println(newName + " has been added to the game.");
}
}
}
}
public static void main(String[] args) throws Exception {
terminal.println("Welcome to my AMV Game\n");
readFile();
Node first = new Node();
first.question = "Is it an animal?";
first.True = new Node();
first.True.name = "Elephant";
first.False = new Node();
first.False.question = "Is it a Vegetable?";
first.False.True = new Node();
first.False.True.name = "Carrot";
first.False.False = new Node();
first.False.False.name = "Coal";
do {
first.branch();
end = terminal.readString("Quit or play again?");
} while(end.equals("again"));
terminal.println("The game has ended.");
writePersons(first);
System.out.println(first);
}
public static void writePersons(Node first) throws Exception {
ObjectOutputStream outputStream = null;
try {
outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Daniel\\My Documents\\College\\CS1011\\testfiles\\index.txt"));
outputStream.writeObject(first);
} finally {
//Close the ObjectOutputStream
outputStream.flush();
outputStream.close();
}
}
static public void readFile(String ... args) {
Node first = null;
try {
FileInputStream flinpstr = new FileInputStream("C:\\Users\\Daniel\\My Documents\\College\\CS1011\\testfiles\\index.txt");
ObjectInputStream objinstr= new ObjectInputStream(flinpstr);
try {
first = (Node) objinstr.readObject();
} finally {
try {
objinstr.close();
} finally {
flinpstr.close();
}
}
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
if(first != null) {
System.out.println(first + " has been deserialize");
}
}
}