简体   繁体   中英

How to exit 2D game simulation (only) without exiting the Java code?

I (new to Java) am working on a decades old Java project built using Golden T Studios Game dev jdk. I have a game in the project which runs a 2D simulation. The code structure in a nutshell is as follows:

package game;
import java.io.*;
import java.lang.reflect.Field;
import javax.swing.JFrame;

import com.golden.gamedev.GameLoader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import sometcpimports.*;

public class MainGAME extends JFrame implements Runnable {

    public static void main(String[] args) { //call new MainGAME;
    }

    public MainGAME() {//initiate game parameters; 
    //start new THREAD; 
    }

   @Override
   public void run() { //initiate new game and environment;
    game = new GameLoader();
    gameenv = new GameEnvironment(params); //This class is in another file "public class GameEnvironment extends Game {}" 
    //I don't clearly undertsand what the following lines do, so I'm mentioning them as is;

    game.setup(gameenv, dimensions);
    this.setVisible(false);
    gameenv.setVisible(false);
    game.start();
    game.setVisible(true);

    //tbd (code reaches this step)

    }
}

My goal is to run the above simulation multiple times (with different inputs each time) and extract information after each run in a new main class as follows.

public class gamedriver {
    public static void main(String[] args) {
        MainGAME.params = some params;
        MainGAME.main(); // runs the simulation;

        //tbd1 (code doesn't reach this step)
    }
}

The issue is that, on running the simulation from a different file (new main class), I am unable to exit 2D simulator after one run. The code doesn't reach //tbd1 in the new main class which has some output print statements. Essentially I want to simply exit simulator and not the whole JVM. So far I've tried:

  1. game.stop() & gameenv.finish() at //tbd which does nothing.
  2. System.exit(0) at //tbd which exits game after simulation but also exits jvm and doesnt reach the other main class.
  3. finish() at both //tbd and GameEnvironment class which behaves exactly like point 2.

Additionally, I am unable to run the above (MainGAME in gamedriver class) in a for loop. This throws Game.Exception error . I understand it has something to do with threads but I'm not sure how to run them.

Thank you for your time. Appreciate your help!

I found a method to solve the above problem. I'm posting my workaround as a reference for someone who encounters a similar issue. Please note that there might be other (better) solutions out there.

  1. I disabled all (any) threads implementation in the MainGAME (by simply commenting it out).
  2. I added System.out.println() in MainGAME to print all my outputs at the end of simulation.
  3. My second script uses Runtime processes to execute MainGAME:
public class gamedriver {
    public static void main(String[] args) {
        try {
            String separator = System.getProperty("file.separator");
            String classpath = System.getProperty("java.class.path");
            String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
            String[] command = new String[]{path, "-cp", classpath, gamedriver.GAME.class.getName(), "output.csv"};
            Process process = Runtime.getRuntime().exec(command);
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class GAME {

        public static void main(String args[]) {
            PrintStream out = null;
            try {
                out = new PrintStream(args[1]); 
            } catch (FileNotFoundException p) {
                p.printStackTrace();
            }

            System.setOut(out);// this catches all output from the game to a csv file (output.csv)
            MainGAME temp = new MainGame(some params); // initiate the simulation
            temp.run()
            out.close();
            System.exit(0); // force close the simulator
        }
    }
}

  1. I parse output.csv to get the solution.

This might be a sloppy way to solve the issue, but it worked for me, especially because none of the other methods provided by GTGE worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM