2012-03-29 31 views
2

我已经创建了一个程序,它是一个猜谜游戏,但我有问题学习这个新的方法来序列化它。我已经完成了所有工作,只需要编写它,程序就可以通过序列化方法保存并加载。我正在尝试序列化程序的“游戏”部分或play()方法,以便下次加载它时将加载旧信息。我有序列化的麻烦,我需要在java中实现它

import javax.swing.JOptionPane; 
import java.io.Serializable; 
import java.io.ObjectOutputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

//Node class 
class Node 
{ 
//instance variables 
public String questionText; 
public Node leftChild; 
public Node rightChild; 

public void displayText() 
{ 
    System.out.println(questionText); 
} 
} 


    //Tree class 
class Tree implements Serializable 
{ 
private Node root; 

//constructor 
public Tree() 
{ root = new Node(); 
    root.leftChild = new Node(); 
    root.rightChild = new Node(); 
    root.questionText = "Does it live on land?"; 
    root.leftChild.questionText ="bear"; // left side is Yes, right side is No 
    root.rightChild.questionText = "parrot"; 
} 

public void instruction() 
{ 
    JOptionPane.showMessageDialog(null, "Think of an animal, I will try to guess it, answer yes or no"); 
} 


public void play() 
{ 
Node current = root; 
Node parent = current; 
boolean isLeftChild = true; 


while(true) 
{ parent = current; 
    int response = JOptionPane.showConfirmDialog(null,current.questionText); 
    //code here for yes 
    if (response == JOptionPane.YES_OPTION) 
    { 
     current = current.leftChild; 
     isLeftChild=true; 
    } 
    //code here for no 
    else if (response == JOptionPane.NO_OPTION) 
    { 
     current = current.rightChild; 
     isLeftChild = false; 
    } 


    if (current.leftChild == null && current.rightChild == null) 
    { 
     int secondQ = JOptionPane.showConfirmDialog(null, "Is your animal a " + current.questionText + "?"); 

     if (secondQ == JOptionPane.YES_OPTION) 
     { 
      JOptionPane.showMessageDialog(null,"I Guessed your animal!"); 
      return; 
     } 
     else if (secondQ == JOptionPane.NO_OPTION) 
     { 
      Node nodeOne = new Node(); 
      Node nodeTwo = new Node(); 

       nodeOne.questionText = JOptionPane.showInputDialog("Write a question that differentiates your animal from the animal I guessed, it would be yes for your animal"); 

       nodeTwo.questionText = JOptionPane.showInputDialog("What is this animal?"); 

       nodeOne.rightChild = current; 
       nodeOne.leftChild = nodeTwo; 

       // parent.leftChild = nodeOne or parent.rightChild = nodeOne 
       if(isLeftChild == false) 
       { 
        parent.rightChild = nodeOne; 
        System.out.println("right child"); 
       } 
       else 
        { 
        parent.leftChild = nodeOne; 
        System.out.println("left Child"); 
        } 
       return; 




     } 


    } 

} 
} 


public void preOrder(Node localRoot) 
{ 
if(localRoot != null) 
    { 
    System.out.print(localRoot.questionText + " "); 
    preOrder(localRoot.leftChild); 
    preOrder(localRoot.rightChild); 
    } 
} 

public Node getRoot(){ 
    return root; 
} 
} 




public class GuessTheAnimal 
{ 
    public static void main(String[] args) 
{ 
    Tree animal = new Tree(); 
    animal.instruction(); 
    animal.play(); 
    animal.play(); 



} 

} 
+0

你的问题到底是什么? – bvulaj 2012-03-29 19:51:37

+0

我不知道如何实现它,并需要帮助。 – Renuz 2012-03-29 19:54:16

回答

3

我建议Java serialization

FileOutputStream fileOut = 
new FileOutputStream("node.ser"); 
ObjectOutputStream out = new ObjectOutputStream(fileOut); 
out.writeObject(tree); 
out.close(); 

阅读是同样简单:

FileInputStream fileIn = new FileInputStream("employee.ser"); 
ObjectInputStream in = new ObjectInputStream(fileIn); 
tree = (Tree) in.readObject(); 
in.close(); 
+0

+1不需要'fileIn.close();'和'fileOut.close();',因为'in.close();'和'out.close();'会为你关闭它们。 :) – 2012-03-29 19:54:12

+0

有没有我会把这个位置?在Tree类中,之前和之后? – Renuz 2012-03-29 19:55:08

+0

在主要方法中,IMO。尽管没有更多的要求,但很难说明问题。 – jsight 2012-03-29 20:02:15

1

我看到两个问题的代码。

首先,你的Tree实现了Serializable,但这还不够。你需要告诉程序(可能在程序启动后的早些时候),读取你的序列化文件,并从输入流初始化你的树。相反,当你准备好保存你的状态时(可能在程序退出前),你需要将你的Tree写入你的序列化文件。其次,你的树实现了Serializable,但是你的树是由Node对象构成的,它没有实现Serializable。序列化类的所有成员都必须是序列化才能工作的。

Java Serialization

+0

是的,我真的不知道从哪里开始,我会查看指南,我已经读了一些其他人,但没有完全理解他们,这就是为什么我张贴在这里。 – Renuz 2012-03-29 19:58:07

0

你的问题很好的教程是相当广阔的。 一个,它应该让你用自己的方式回答是,首先,你应该让你的树类实现java.io.Serializable:

Tree类实现Serializable { ... }

然后,确保所有的字段树都以相同的方式实现Serializable(以此类推)(其他字段)

完成此操作后,可以使用ObjectOutputStream将Tree实例写出,并使用ObjectInputStream读取它。

E.g.新的ObjectOutputStream(新的FileInputStream(“文件名”))。writeObject(树)

但是,你需要先阅读一下关于Serializable和ObjectOutputSteam,我会建议。