1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.ObjectOutputStream;
  4. import java.io.Serializable;
  5.  
  6. public class Assignment1 extends AMVsetup  implements Serializable {
  7.     private static final long serialVersionUID = 1L;
  8.  
  9. static class Node implements Serializable   {
  10.     private static final long serialVersionUID = 6265978352799255699L;
  11.         public String question;    
  12.         public Node True;    
  13.         public String name;  
  14.         public Node False;
  15.         public int parentID;
  16.         public int ID;
  17.         public char fromWhichBranch;   
  18.         public char questionAnswer;
  19.        
  20.  
  21.  
  22. public void branch()    {        
  23.         if(question != null)        {                
  24.                 input = terminal.readString(question + "\n");  
  25.                 if (input.contains("true"))                
  26.                         True.branch();                
  27.                 else                
  28.                         False.branch();              
  29.             }        
  30.             else if(question == null)   {            
  31.                 input = terminal.readString("Is it a/an " + name + "?\n");                    
  32.                 if(input.contains("true"))              
  33.                 {  terminal.println("I win!"); }            
  34.                 else  {                                            
  35.                      terminal.println("I give up.\n");                
  36.                      newName = terminal.readString("What were you thinking of?\n");                
  37.                                        
  38.                      newQuestion = terminal.readString("What is a question that you " + "would answer no to " + newName + ", but " + "yes to " + name + "?\n");                              
  39.                      True = new Node();                
  40.                      True.name = name;    
  41.                      True.ID = 10;
  42.                      True.parentID = 5;
  43.                      True.questionAnswer = 'A';
  44.                      True.fromWhichBranch = 'T';
  45.                      False = new Node();                
  46.                      False.name = newName;
  47.                      False.ID = 11;
  48.                      False.parentID = 6;
  49.                      False.questionAnswer = 'A';
  50.                      False.fromWhichBranch = 'F';
  51.                      name = null;              
  52.                      question = newQuestion;                
  53.                      terminal.println(newName + " has been added to the game.");            
  54.                  }                    
  55.             }          
  56.         }  
  57. }
  58.    
  59.  
  60. public static void main(String[] args) throws Exception {
  61.      
  62.      Node first = new Node();
  63.      first.question = "Is it an animal?";
  64.      first.parentID = -1;
  65.      first.ID = 0;
  66.      first.questionAnswer = 'Q';
  67.      first.fromWhichBranch = 'Y';
  68.      first.True = new Node();
  69.      first.True.name = "Elephant";
  70.      first.True.parentID = 0;
  71.      first.True.ID = 1;
  72.      first.True.questionAnswer = 'A';
  73.      first.True.fromWhichBranch = 'Y';
  74.      first.False = new Node();
  75.      first.False.name = "DVD";
  76.      first.False.parentID = 0;
  77.      first.False.ID = 1;
  78.      first.False.questionAnswer = 'A';
  79.      first.False.fromWhichBranch = 'N';
  80.      
  81.                 do  {
  82.                         first.branch();
  83.                         end = terminal.readString("Quit or play again?");
  84.                     }  while(end.equals("again"));
  85.                        
  86.        
  87.             terminal.println("The game has ended.");
  88.      
  89.            
  90.     }
  91.  
  92. public void writePersons(Node first) throws Exception {
  93.        
  94.        ObjectOutputStream outputStream = null;
  95.        try {
  96.            
  97.            outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Daniel\\My Documents\\College\\CS1011\\testfiles\\index.txt"));      
  98.            outputStream.writeObject(first.question);
  99.        
  100.        } finally {
  101.            //Close the ObjectOutputStream
  102.                    outputStream.flush();
  103.                    outputStream.close();
  104.                }
  105.            
  106.        }
  107.  
  108. }