Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.io.IOException;
- 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;
- True.ID = 10;
- True.parentID = 5;
- True.questionAnswer = 'A';
- True.fromWhichBranch = 'T';
- False = new Node();
- False.name = newName;
- False.ID = 11;
- False.parentID = 6;
- False.questionAnswer = 'A';
- False.fromWhichBranch = 'F';
- name = null;
- question = newQuestion;
- terminal.println(newName + " has been added to the game.");
- }
- }
- }
- }
- public static void main(String[] args) throws Exception {
- Node first = new Node();
- first.question = "Is it an animal?";
- first.parentID = -1;
- first.ID = 0;
- first.questionAnswer = 'Q';
- first.fromWhichBranch = 'Y';
- first.True = new Node();
- first.True.name = "Elephant";
- first.True.parentID = 0;
- first.True.ID = 1;
- first.True.questionAnswer = 'A';
- first.True.fromWhichBranch = 'Y';
- first.False = new Node();
- first.False.name = "DVD";
- first.False.parentID = 0;
- first.False.ID = 1;
- first.False.questionAnswer = 'A';
- first.False.fromWhichBranch = 'N';
- do {
- first.branch();
- end = terminal.readString("Quit or play again?");
- } while(end.equals("again"));
- terminal.println("The game has ended.");
- }
- public 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.question);
- } finally {
- //Close the ObjectOutputStream
- outputStream.flush();
- outputStream.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement