Jump to content

Guardar e Carregar Jogo


mcj
 Share

Recommended Posts

Basicamente queres guardar o estado do jogo.

O que tens de fazer é guardar num ficheiro todo o estado do jogo, localização dos objectos, valores das variáveis, etc..

Quando carregares o jogo lês essa informação e colocas o jogo no estado em que estava anteriormente.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Link to comment
Share on other sites

Este foi o codigo que fiz mas da-me erros na consola:

O codigo abaixo e quando escolho ou carrego na barra de menus guardar jogo ou continuar jogo, ou seja, os eventos do rato:

class menuJogo1 implements ActionListener{

public void actionPerformed(ActionEvent e){

core.autoSave(core.getTabuleiro());

}

}

class menuJogo2 implements ActionListener{

public void actionPerformed(ActionEvent e){

core.loadGame();

}

}

Aqui sao os metodos implementados, o guardar e carregar jogo:

public void autoSave(ITabuleiro gameTab)

{

try

{

FileOutputStream fileOut = new FileOutputStream("FileOutput.txt");

ObjectOutputStream out = new ObjectOutputStream(fileOut);

out.writeObject(gameTab);

out.close();

fileOut.close();

}

catch(IOException e)

{

e.printStackTrace();

}

}

public ITabuleiro loadGame() {

ITabuleiro gameTab = null;

try{

FileInputStream fileIn = new FileInputStream("FileOutput.txt");

ObjectInputStream in = new ObjectInputStream(fileIn);

gameTab = (ITabuleiro)in.readObject();

in.close();

fileIn.close();

}

catch(IOException e)

{

e.printStackTrace();

}

catch(ClassNotFoundException c) // catch para o "in.readObject();"

{

c.printStackTrace();

}

return gameTab;

}

Estes são os erros ao guardar:

java.io.NotSerializableException: Tabuleiro

at java.io.ObjectOutputStream.writeObject0(Unknown Source)

at java.io.ObjectOutputStream.writeObject(Unknown Source)

at GameCore.autoSave(GameCore.java:92)

at Frm$menuJogo1.actionPerformed(Frm.java:24)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.AbstractButton.doClick(Unknown Source)

at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)

at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Estes são os erros ao carregar:

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: Tabuleiro

at java.io.ObjectInputStream.readObject0(Unknown Source)

at java.io.ObjectInputStream.readObject(Unknown Source)

at GameCore.loadGame(GameCore.java:111)

at Frm$menuJogo2.actionPerformed(Frm.java:32)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.AbstractButton.doClick(Unknown Source)

at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)

at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Caused by: java.io.NotSerializableException: Tabuleiro

at java.io.ObjectOutputStream.writeObject0(Unknown Source)

at java.io.ObjectOutputStream.writeObject(Unknown Source)

at GameCore.autoSave(GameCore.java:92)

at Frm$menuJogo1.actionPerformed(Frm.java:24)

... 27 more

O que estarei a fazer de mal?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.