Pessoal estou criando esse jogo comum como muitos fazem na faculdade, MAIS SO PORQUE o professor quer que salve em arquivo TXT, ele deu as classes, mais nao sei como colocar no jogo para ir salvando todos os dados ate quando fechar o netbeans e continua salvo…para o jogo ir ficando inteligente…
Acho que voces entenderam o que eu quis dizer…
SEGUE AS CLASSES FEITAS…
import javax.swing.JOptionPane;
/*
-
To change this template, choose Tools | Templates
-
and open the template in the editor.
/
/* -
@author
*/
public class ArvoreMain implements Interface {public static int index = 1;
public static No raiz;@Override
public No getArvore() {return raiz;}
@Override
public void setArvore(No arvore) {raiz = arvore;}
@Override
public void inserirAnimal(No noArvore) {String animal = JOptionPane.showInputDialog(null, "Qual o animal que você pensou?"); String oqueFaz = JOptionPane.showInputDialog("Fale uma caracteristica para o " + animal); String auxiliar = noArvore.valor; noArvore.valor = oqueFaz; noArvore.noDireito = new No(++index, animal); noArvore.noEsquerdo = new No(++index, auxiliar);}
@Override
public void perguntar(No noArvore) {int pergunta = JOptionPane.showConfirmDialog(null, "O animal que você pensou " + noArvore.valor, "Confirme", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (pergunta == 0) { if (noArvore.noDireito == null) { JOptionPane.showMessageDialog(null, "Acertei!"); } else { perguntar(noArvore.noDireito); } } else { if (noArvore.noEsquerdo == null) { inserirAnimal(noArvore); } else { perguntar(noArvore.noEsquerdo); } }}
public static void main(String[] args) {
ArvoreMain main = new ArvoreMain(); JOptionPane.showMessageDialog(null, "Pense em um Animal real. Eu vou tentar advinhar quem é."); if (main.getArvore() == null) { main.setArvore(new No(index, "Tem quatro patas?")); main.getArvore().noEsquerdo = new No(++index, "É felino?"); main.getArvore().noDireito = new No(++index, "é Barata?"); } int sair = 1; do { int resposta = JOptionPane.showConfirmDialog(null, "O animal que você pensou " + main.getArvore().valor, "Confirme", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (resposta == 0) { main.perguntar(main.getArvore().noDireito); } else { main.perguntar(main.getArvore().noEsquerdo); } if (resposta == JOptionPane.CLOSED_OPTION) { sair = 0; } } while (sair == 1);}
}
public interface Interface {
public void inserirAnimal(T e);
public void perguntar(T e);
public T getArvore();
public void setArvore(T e);
}
public class No <T> implements Serializable {
public int elemento;
public No noEsquerdo;
public No noDireito;
public String valor;
/**
* Construtor que seta os valores da arvore
*/
public No(int elemento, String valor) {
System.out.println(elemento + " - " + valor);
this.elemento = elemento;
this.valor = valor;
}
public No() {
}
}
SEGUE AS CLASSE DE SALVAR ARQUIVO TXT JA PRONTAS
import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
/**
*
-
@author
*/
public class ArquivoJSON {
private String path;public ArquivoJSON(String path) {
setPath(path);
}public void salvar(Object obj) throws Exception {
Gson gson = new Gson();
String txt = gson.toJson(obj);
ArquivoTexto arqTxt = new ArquivoTexto(path);
arqTxt.salvar2(txt);
}public Object ler(Class classe) throws Exception {
ArquivoTexto arqTxt = new ArquivoTexto(path);
String txt = arqTxt.ler2();
Gson gson = new Gson();
Object obj = gson.fromJson(txt, classe);
return obj;
}/**
-
@return the path
*/
public String getPath() {
return path;
}
/**
-
@param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; -
@return the path
/**
*
-
@author
*/
public class ArquivoSerializado {private String path;
public ArquivoSerializado(String path) {
setPath(path);
}public void salvar(Object obj) throws Exception {
File file = new File(getPath());
FileOutputStream fos = new FileOutputStream(file, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.flush();
oos.close();
}public Object ler() throws Exception {
File file = new File(getPath());
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
ois.close();
return obj;} return null;}
/**
-
@return the path
*/
public String getPath() {
return path;
}
/**
-
@param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter; -
@return the path
/**
*
-
@author
*/
public class ArquivoTexto {
private String path;
public ArquivoTexto(String path) {
setPath(path);
}public void salvar1(String txt) throws Exception {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file, true);
fos.write(txt.getBytes());
fos.flush();
fos.close();
}public void salvar2(String txt) throws Exception {
File file = new File(path);
FileWriter fw = new FileWriter(file, true);
fw.write(txt);
fw.flush();
fw.close();
}public String ler1() throws Exception {
File file = new File(path);
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[10];
int controle= 0;
String str = “”;
StringBuffer sb = new StringBuffer();
while ( (controle=fis.read(buffer)) > 0) {
byte[] bufferTemp = new byte[controle];
System.arraycopy(buffer, 0, bufferTemp, 0, controle);
//str += new String(bufferTemp);
sb.append(new String(bufferTemp));
}
fis.close();
return sb.toString();
}
return null;
}public String ler2() throws Exception {
File file = new File(path);
if (file.isFile()) {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
while (br.ready()) {
sb.append(br.readLine()).append("\n\r");
}
br.close();
return sb.toString();
}
return null;
}/**
-
@return the path
*/
public String getPath() {
return path;
}
/**
-
@param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
-
@return the path
}
estao ai as classe como posso incluir para o jogo ser salvo no arquivo txt???