Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: Vuzuggu on Dec 15th, 2011  |  syntax: Java  |  size: 4.11 KB  |  hits: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7.  
  8. public class Assignment1 extends AMVsetup  implements Serializable {
  9.         private static final long serialVersionUID = 1L;
  10.  
  11. static class Node implements Serializable       {
  12.         private static final long serialVersionUID = 6265978352799255699L;
  13.                 public String question;    
  14.                 public Node True;    
  15.                 public String name;  
  16.                 public Node False;
  17.                 public int parentID;
  18.                 public int ID;
  19.                 public char fromWhichBranch;   
  20.                 public char questionAnswer;
  21.                
  22.  
  23.  
  24. public void branch()    {        
  25.                 if(question != null)            {                
  26.                         input = terminal.readString(question + "\n");  
  27.                 if (input.contains("true"))                
  28.                         True.branch();                
  29.                 else                
  30.                         False.branch();              
  31.                 }        
  32.                 else if(question == null)       {            
  33.                 input = terminal.readString("Is it a/an " + name + "?\n");                    
  34.                 if(input.contains("true"))              
  35.                 {  terminal.println("I win!"); }            
  36.                 else  {                                            
  37.                      terminal.println("I give up.\n");                
  38.                      newName = terminal.readString("What were you thinking of?\n");                
  39.                                        
  40.                      newQuestion = terminal.readString("What is a question that you " + "would answer no to " + newName + ", but " + "yes to " + name + "?\n");                              
  41.                      True = new Node();                
  42.                      True.name = name;    
  43.                      False = new Node();                
  44.                      False.name = newName;
  45.                      name = null;              
  46.                      question = newQuestion;                
  47.                      terminal.println(newName + " has been added to the game.");            
  48.                  }                    
  49.                 }          
  50.                 }      
  51. }
  52.        
  53.  
  54. public static void main(String[] args) throws Exception {
  55.        
  56.         terminal.println("Welcome to my AMV Game\n");
  57.        
  58.         readFile();
  59.          
  60.          Node first = new Node();
  61.          first.question = "Is it an animal?";
  62.          first.True = new Node();
  63.          first.True.name = "Elephant";
  64.          first.False = new Node();
  65.          first.False.question = "Is it a Vegetable?";
  66.          first.False.True = new Node();
  67.          first.False.True.name = "Carrot";
  68.          first.False.False = new Node();
  69.          first.False.False.name = "Coal";
  70.                                 do  {
  71.                                                 first.branch();
  72.                                                 end = terminal.readString("Quit or play again?");
  73.                                         }  while(end.equals("again"));
  74.                                                
  75.                
  76.                         terminal.println("The game has ended.");
  77.                         writePersons(first);
  78.                         System.out.println(first);
  79.          
  80.                        
  81.         }
  82.  
  83. public static void writePersons(Node first) throws Exception {
  84.        
  85.        ObjectOutputStream outputStream = null;
  86.        try {
  87.            
  88.            outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Daniel\\My Documents\\College\\CS1011\\testfiles\\index.txt"));      
  89.            outputStream.writeObject(first);
  90.        
  91.        } finally {
  92.            //Close the ObjectOutputStream
  93.                            outputStream.flush();
  94.                    outputStream.close();
  95.                }
  96.            
  97.        }
  98.  
  99.  
  100. static public void readFile(String ... args) {
  101.                 Node first = null;
  102.                 try {
  103.                
  104.                         FileInputStream flinpstr = new FileInputStream("C:\\Users\\Daniel\\My Documents\\College\\CS1011\\testfiles\\index.txt");
  105.                         ObjectInputStream objinstr= new ObjectInputStream(flinpstr);
  106.                         try {  
  107.                                
  108.                                 first = (Node) objinstr.readObject();
  109.                         } finally {
  110.                                
  111.                                 try {
  112.                                         objinstr.close();
  113.                                 } finally {
  114.                                         flinpstr.close();
  115.                                 }
  116.                         }
  117.                 } catch(IOException ioe) {
  118.                         ioe.printStackTrace();
  119.                 } catch(ClassNotFoundException cnfe) {
  120.                         cnfe.printStackTrace();
  121.                 }
  122.                 if(first != null) {
  123.                         System.out.println(first + " has been deserialize");
  124.                 }
  125.         }
  126. }