Я создал программу, это игра с угадыванием, но у меня возникают проблемы с изучением этого нового метода для его сериализации. У меня все уже сделано, мне просто нужно сделать так, чтобы программа могла сохранять и загружать открытые с помощью метода сериализации. Я пытаюсь выполнить сериализацию «игровой» части или метода 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();
}
}
В чем именно проблема? – bvulaj
Я не знаю, как его реализовать, и вам нужна помощь. – Renuz